如何通过大量粒子获得更好的性能?画布

时间:2015-05-28 14:41:30

标签: javascript jquery performance animation canvas

好吧,我真的很感兴趣i-remember.fr如何在他们的主页上用粒子制作他们的圆圈。不是他们制作了一个圆圈的事实,而是看起来似乎有成千上万的粒子,而且没有任何性能问题。所以我开始尝试复制这个圈子,试图获得相同的表现 - 到目前为止没有任何运气。

在一切开始变得不稳定之前,我似乎可以得到 3000 粒子,但是我没有得到任何数量的粒子。我正在使用RequestAnimationFrame帮助提高性能,以及我能想到的所有其他内容......我可以获得一些帮助以获得更多粒子吗?

引擎

的Javascript

$('.blackhole').click(function() {
    $(this).toggleClass('open_blackhole');
    $(this).toggleClass('close_blackhole');
});


// Define Apparatus Variables.
var cw = window.innerWidth,
    ch = window.innerHeight,
    blackhole_entities = {},
    blackhole_entitiesIndex = 0,
    blackhole_entitieAmount = 3000, //6000
    canvas = $('<canvas/>').attr({
        width: cw,
        height: ch,
        id: "apparatus"
    }).appendTo('body'),
    context = canvas.get(0).getContext("2d");

var requestframe = window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.msRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    // IE Fallback, you can even fallback to onscroll
    function(callback) {
        window.setTimeout(callback, 1000 / 60)
    };

// Default Entity "Class"
apparatus.blackhole = function(orbit) {
    blackhole_entitiesIndex++;
    this.id = blackhole_entitiesIndex;
    blackhole_entities[blackhole_entitiesIndex] = this;

    this.width = 1;
    this.height = 1;
    this.orbit = orbit;

    this.velocity = Math.floor((Math.random() * 3200) + 3000);

    this.angle = (Math.PI * 2 / this.width) * Math.floor((Math.random() * cw*4) + 10);;
    var choice = Math.random() * 5;

    var rands = [];
    rands.push(Math.random() * 100 + 1);
    rands.push(Math.random() * 10 + 241);

    var choice2 = Math.random() * 4;

    var rands2 = [];
    rands2.push(Math.random() * 100 + 1);
    rands2.push(Math.random() * 180 + 211);

    this.distance = (rands.reduce(function(p, c) {
        return p + c;
    }, 0) / rands.length);

    this.distance2 = (rands2.reduce(function(p, c) {
        return p + c;
    }, 0) / rands2.length);
    this.increase = Math.PI * 2 / this.width;

    this.distancefix = this.distance;
    this.distance2fix = this.distance2;

    this.color = "255,255,255";
    this.alpha = 0.6

    this.bx = Math.random() * 20 + 1;
    this.by = Math.random() * 20 + 1;
    this.inplace = true;
}

apparatus.blackhole.prototype.draw = function() {
    if (this.orbit >= 2) {
        this.x = this.bx + this.distance * Math.cos(this.angle / this.velocity) + cw / 2;
        this.y = this.by + this.distance * Math.sin(this.angle / this.velocity) + ch / 2;
        this.alpha = 0.6;
    } else {
        this.x = this.bx + this.distance2 * Math.cos(this.angle / this.velocity) + cw / 2;
        this.y = this.by + this.distance2 * Math.sin(this.angle / this.velocity) + ch / 2;
        this.alpha = 0.4;
    }
    if ($('.blackhole').hasClass('open_blackhole')) {
      $('.blackhole').removeClass('close_blackhole');

        if (this.distance >= 171) {
            this.distance = this.distance - 4;
        } else if (this.distance <= 161) {
            this.distance= this.distance + 8;
        }

        if (this.distance2 >= 201) {
            this.distance2 = this.distance2 - 6;
        } else if (this.distance2 <= 161) {
            this.distance2 = this.distance2 + 6;
        }
    }
    if($('.blackhole').hasClass('close_blackhole')){
        if (this.distance >= this.distancefix + 4) {
            this.distance = this.distance - 4;
        } else if (this.distance <= this.distancefix - 5) {
            this.distance= this.distance + 5;
        }

        if (this.distance2 >= this.distance2fix + 10) {
            this.distance2 = this.distance2 - 4;
        } else if (this.distance2 <= this.distance2fix - 10) {
            this.distance2 = this.distance2 + 4;
        }

    }

    this.angle += this.increase;

    context.fillStyle = "rgba(" + this.color + "," + this.alpha + ")";
    context.fillRect(this.x, this.y, this.width, this.height);
}

apparatus.start = function() {
    apparatus('true');
}

apparatus.stop = function() {
    apparatus('false');
}

for (var i = 0; i < blackhole_entitieAmount; i++) {
    new apparatus.blackhole((Math.random() * 4));
}

var mode;
apparatus.spawn_blackhole = function(){
  for (i in blackhole_entities) {
    blackhole_entities[i].draw();
  }
}
function apparatus(mode) {
    if (mode == 'true') {
        var i;
        requestframe(function() {
            context.clearRect(0, 0, cw, ch);

            apparatus.spawn_blackhole(); 

            apparatus('true');
        });
    }
}

apparatus.start();

HTML

<div class="blackhole button closed_blackhole">
  BLACKHOLE
</div>

希望这不是重复,我已经搜索了很长一段时间。

这是我当前引擎的CodePen link。 (如果你将blackhole_entitieamount变量改为,比方说6000,你会发现它变得非常迟钝。)

1 个答案:

答案 0 :(得分:1)

我认为最大的缺点之一是在draw函数中使用jQuery。类似$('.blackhole').hasClass('close_blackhole')这样的事情在你的情况下非常慢,当你每帧数千次这样做时。你整整做了三次,这意味着每帧9000次。要么缓存$('.blackhole')以便你把它降到blackhole_entitieAmount或最好 - 删除它并想一些内在的存储它的方法。

首先尝试将var blackhole = $('.blackhole');放在代码的最开头,然后替换以后的用法,然后再次测试:)