【问题标题】:getting a random animation totally random得到一个完全随机的随机动画
【发布时间】:2013-11-24 19:01:08
【问题描述】:

我已经创建了一个随机的鱼动画,但是从一开始的某个时间点(大约 10 秒),我的鱼就靠在右边,我希望它们在整个区域内保持随机移动,知道吗?

这里是小提琴链接:http://jsfiddle.net/832Fx/1/

jquery 代码:

$.fn.rotate = function (degrees) {
    var self = $(this);
    self.transition({
        rotateY: degrees + 'deg'
    }, 5000);
};

var animeVars = {
    maxPixelsPerSecond: 50,
    minPixelsPerSecond: 10,
    topMargin: 0,
    bottomMargin: 400,
    leftMargin: 0,
    rightMargin: 400
};

function topFlip(obj) {
    var speed = $(obj).data('speed') ? (1 / parseFloat($(obj).data('speed'))) * 1000000 : 300;
    $(obj).find('.top_fin, .top_fins').transition({
        rotate: '+=25deg',
        x: '+=10'
    }, speed, function () {
        $(obj).find('.top_fin, .top_fins').transition({
            rotate: '-=25deg',
            x: '-=10'
        }, speed, function () {
            topFlip(obj);
        });
    });
}

function tailFlip(obj) {
    var speed = $(obj).data('speed') ? (1 / parseFloat($(obj).data('speed'))) * 1000000 : 300;
    $(obj).find('.tail_fin, .tail_fins').transition({
        rotateX: '-=25deg'
    }, speed, function () {
        $(obj).find('.tail_fin, .tail_fins').transition({
            rotateX: '+=25deg'
        }, speed, function () {
            tailFlip(obj);
        });
    });
}

function animateFish(obj) {
    var heading = $(obj).data('heading');
    if (!heading) heading = 'left';
    var rotation = 0;
    var currentCoords = {
        top: parseInt($(obj).css('top').replace(/[^-\d\.]/g, ''), 10),
        left: parseInt($(obj).css('left').replace(/[^-\d\.]/g, ''), 10)
    };
    var newCoords = {
        top: Math.random() * (animeVars.topMargin - animeVars.bottomMargin + 1) + animeVars.bottomMargin,
        left: Math.random() * (animeVars.leftMargin - animeVars.rightMargin + 1) + animeVars.rightMargin
    };
    if (currentCoords.left < newCoords.left && heading != 'right') {
        $(obj).rotate(180);
        $(obj).data('heading', 'right');
        console.log('swimming right');
    } else if (currentCoords.left > newCoords.left && heading != 'left') {
        $(obj).rotate(0);
        $(obj).data('heading', 'left');
        console.log('swimming left');
    }

    var totalMovement = Math.sqrt(Math.pow(currentCoords.left - newCoords.left, 2) + Math.pow(currentCoords.top - newCoords.top, 2));
    console.log('Total pixels to move: ' + totalMovement);
    var pps = Math.floor(Math.random() * (animeVars.maxPixelsPerSecond - animeVars.maxPixelsPerSecond)) + animeVars.maxPixelsPerSecond;
    var speed = totalMovement / pps * 1000;

    $(obj).data('speed', speed);
    $(obj).animate({
        top: newCoords.top,
        left: newCoords.left
    }, speed, function () {
        animateFish(obj);
    });
}

$(document).ready(function () {

    $('.loop ').each(function () {
        animateFish(this);
        topFlip(this);
        tailFlip(this);
    });

});

【问题讨论】:

  • 对我来说效果很好..即使在 10 秒后也会完全随机。
  • @putvande 除了他们要去左边区域并留在那里
  • 你的鱼被困在0px400px 之间,因为你的随机算法将它们的位置设置在leftMarginrightMargin 之间(尽管这些变量的名称非常不准确,它应该只是leftright,它们不是边距而是边界)。
  • @BaliBalo 那我该怎么办?
  • @alonblack 将marginRight 值更改为窗口宽度(调整大小时)或更改算法以将鱼的位置设置在marginLeft 和窗口宽度减去marginRight 之间

标签: javascript jquery json animation random


【解决方案1】:

我将如何解决这个问题:http://jsfiddle.net/BaliBalo/832Fx/2/

我刚刚加了

var $wnd = $(window);
$wnd.resize(function() {
    animeVars.rightMargin = $wnd.width();
    animeVars.bottomMargin = $wnd.height();
}).resize();

在您的代码末尾,并稍微更改了算法以考虑鱼的大小:

var w = $(obj).width();
var h = $(obj).height();
var newCoords = {
    top: Math.random() * (animeVars.topMargin - animeVars.bottomMargin + h + 1) + animeVars.bottomMargin - h,
    left: Math.random() * (animeVars.leftMargin - animeVars.rightMargin + w + 1) + animeVars.rightMargin - w
};

但是,请注意可以进行许多优化,例如将 jQuery 对象缓存在变量中,而不是每次都调用 $ 函数。

【讨论】:

  • 几乎完美!非常感谢,正如你在这里看到的:jsfiddle.net/832Fx/3 鱼现在通过了鱼缸的底部,我没有成功解决这个问题,你上次可以帮忙吗?
  • 计算的位置是鱼的顶部。你必须考虑到鱼的高度。用var h = $(obj).height(); 做,然后用newCoordstop: Math.random() * (animeVars.topMargin - animeVars.bottomMargin + h + 1) + animeVars.bottomMargin - h(就像我的回答一样)
  • 你的意思是我应该创建另一个或使用现有的代码?
  • 这里是一个例子:jsfiddle.net/BaliBalo/832Fx/4。你应该对宽度做同样的事情,以防止鱼通过鱼缸的右边界
猜你喜欢
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-30
  • 2013-04-03
相关资源
最近更新 更多