我制作了一张蝴蝶图片在页面上随机移动,模拟一只真正的蝴蝶。
$(document).ready(function(){
    animateIMG();
});
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 animateIMG(){
    var newq = makeNewPosition();
    var oldq = $('img').offset();
    var speed = calcSpeed([oldq.top, oldq.left], newq);
    $('img').animate({ top: newq[0], left: newq[1] }, speed, function(){
      animateIMG();        
    });
};
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;// control the speed here 
    var speed = Math.ceil(greatest/speedModifier);
    return speed;
}
但我不知道如何将方向(旋转角度)更改为移动方向。也许我应该计算旧点和新点之间的角度。然后使用这个插件 来旋转角度?
有人可以帮忙修改代码吗?
谢谢! 
