samedi 11 juin 2016

make a canvas move randomly and allow vw size


I’m working on this animated flag to place on a website.
SEE THE ANIMATION HERE
and the FULL FIDDLE

Here is a screenshot of the flag I’d like it to move randomly on my page, bouncing slowly around the window. but the animation of <canvas> doesn’t seem to work. The thing is : the flag have to move, but still allow the background link to be clickable

Here is the html part

<div class="a">
<canvas style="margin-left: -160px; margin-top: -124px;" height="248" width="320" id="flag"></canvas>
</div>

and the javascript part

function makeNewPosition(){

    // Get viewport dimensions (remove the dimension of the div)
    var h = $(window).height() - 50;
    var w = $(window).width() - 50;

    var nh = Math.floor(Math.random() * h);
    var nw = Math.floor(Math.random() * w);

    return [nh,nw];    

}

function animateDiv(){
    var newq = makeNewPosition();
    var oldq = $('.a').offset();
    var speed = calcSpeed([oldq.top, oldq.left], newq);

    $('.a').animate({ top: newq[0], left: newq[1] }, speed, function(){
      animateDiv();        
    });

};

function calcSpeed(prev, next) {

    var x = Math.abs(prev[1] - next[1]);
    var y = Math.abs(prev[0] - next[0]);

    var greatest = x > y ? x : y;

    var speedModifier = 0.1;

    var speed = Math.ceil(greatest/speedModifier);

    return speed;

}

Is it possible to make the flag (and animation) responsive in the same time ?
I’d like it to have vw size, like width : 9vw and height : 3vw


Aucun commentaire:

Enregistrer un commentaire