改善画布中粒子性能

5
我一直在尝试用canvas和Javascript重新创建这个项目。我无法解密原始代码,所以我从头开始做。不同之处在于,我的项目在大约2500个粒子时开始出现延迟,而上面的项目可以使用30,000个粒子。
我将整个代码都粘贴在下面,但这些是相关的部分:
var particleContainer = []
var distance = 10


for(let i = 0; i< square.height/distance; i++){
    for(let j = 0; j< square.height/distance; j++){
    particleContainer.push( new Particle(square.x + i*distance,square.y + j*distance) )
}  
}

if(  c < 90  ){
            i.xVelocity = a/c * -20
            i.yVelocity = b/c * -20
        }else if(90 < c && c < 95){
            i.xVelocity = a/c * -1
            i.yVelocity = b/c * -1
        }else if(c2 !== 0){
            i.xVelocity =( a2/c2 )
            i.yVelocity = (b2/c2 )
        }
  • (c -> 鼠标与粒子之间的距离)

我在正方形每'距离'像素创建一个新的粒子,并将它们全部推入数组中。当鼠标靠近它们之一时,粒子将开始远离鼠标移动,直到距离鼠标90-95像素。

从这行代码判断,30000像素似乎也是以类似的方式工作。

  for ( i = 0; i < NUM_PARTICLES; i++ ) {

    p = Object.create( particle );
    p.x = p.ox = MARGIN + SPACING * ( i % COLS );
    p.y = p.oy = MARGIN + SPACING * Math.floor( i / COLS );

    list[i] = p;
  }

但是那个项目并没有遇到和我一样的性能问题。

以下是我的完整代码供参考(HTML只是画布):

var canvas = document.querySelector("canvas")
var c = canvas.getContext('2d')




function getMousePos(canvas, evt) {
    // var rect = canvas.getBoundingClientRect();
    return {
      x: evt.clientX,
      y: evt.clientY
    };
  }

  document.addEventListener('mousemove', function(evt) {
    var mousePos = getMousePos(canvas, evt);
    mouse.x= mousePos.x; 
    mouse.y= mousePos.y;
  }, false);

  var mouse = {
    x:0,
    y:0
  }

function Particle(x,y){
    this.x = x;
    this.y = y;
    this.xFixed = x;
    this.yFixed = y;
    this.radius = 1
    this.xVelocity = 0
    this.yVelocity = 0
    this.color = 'white'
}

Particle.prototype.draw = function(){
    c.save()
    c.beginPath()
    c.arc(this.x, this.y, this.radius,0,Math.PI*2,false)
    c.fillStyle = this.color
    c.fill()
}

Particle.prototype.update = function(){
    this.draw()
    this.x += this.xVelocity
    this.y += this.yVelocity
}

var square = {
    x: 500,
    y: 150,
    height: 500,
    width: 500,
    color: 'white'
}

var particleContainer = []
var distance = 10


for(let i = 0; i< square.height/distance; i++){
    for(let j = 0; j< square.height/distance; j++){
    particleContainer.push( new Particle(square.x + i*distance,square.y + j*distance) )
}

}





function animate(){
    requestAnimationFrame(animate);
    c.clearRect(0,0,window.innerWidth,window.innerHeight)

  canvas.width = window.innerWidth
canvas.height = window.innerHeight

    for(i of particleContainer){
        let a = mouse.x - i.x
        let b = mouse.y - i.y
        let c = Math.sqrt(Math.pow(b,2) + Math.pow(a,2))

        let a2 = i.xFixed - i.x
        let b2 = i.yFixed - i.y
        let c2 = Math.sqrt(Math.pow(b2,2) + Math.pow(a2,2))

        if(  c < 90  ){
            i.xVelocity = a/c * -20
            i.yVelocity = b/c * -20
        }else if(90 < c && c < 95){
            i.xVelocity = a/c * -1
            i.yVelocity = b/c * -1
        }else if(c2 !== 0){
            i.xVelocity =( a2/c2 )
            i.yVelocity = (b2/c2 )
        }


    }

   for(i of particleContainer){
       i.update()
   }
}

animate()
2个回答

3
为了获得更好的渲染效果,您需要将渲染对象添加到同一路径中。一旦路径创建完成,您就可以在一个调用ctx.fill中绘制它们。 尽量限制访问innerWidthinnerHeight,因为它们是非常慢的DOM对象,仅通过访问它们就可能导致回流。 使用对象池和预分配可以进一步改进,但这超出了单个答案的范围。 请对您的动画函数进行以下更改。
var W = 1, H = 1;
function animate() {
    requestAnimationFrame(animate);
    c.clearRect(0 ,0, W, H)
    if (H !== innerHeight || W !== innerWidth) {
        W = canvas.width = innerWidth;
        H = canvas.height = innerHeight;
    }
    c.beginPath(); // start a new path
    c.fillStyle = "white";
    for (i of particleContainer) {  // update and draw all particles in one pass
        const a = mouse.x - i.x, b = mouse.y - i.y
        const dist = (b * b + a * a) ** 0.5;
        const a2 = i.xFixed - i.x, b2 = i.yFixed - i.y
        const dist2 = (b2 * b2 + a2 * a2) ** 0.5; 
        if (dist < 90  ){
            i.x += a / dist * -20
            i.y += b / dist * -20
        } else if (90 < dist && dist < 95){
            i.x += a / dist * -1
            i.y += b / dist * -1
        } else if (dist2 !== 0){
            i.x += (a2 / dist2 )
            i.y += (b2 / dist2 )
        }
        c.rect(i.x, i.y, 1, 1);
    }
    c.fill();  // path complete render it.
   //for(i of particleContainer){  // no longer needed
   //    i.update()
   //}
}

在上面的代码中,c 既用于上下文,也用于 for 循环内部的任何条件。这会导致问题。在原始帖子中,它被赋值为 let,并且循环内没有调用上下文。我知道这不是重点,但这引起了我的注意。 - i--
@i-- 感谢您的提醒。我会进行修复。 - Blindman67

2
学习使用开发工具中的性能选项卡,您可以看到哪些函数需要最长时间。在这种情况下,我认为您会看到它是ctx.fill。您所发布的示例正在将像素写入ImageData缓冲区,这比绘制和填充弧要快得多。该示例中还有许多其他小型优化,但这将是最重要的一个,绘图通常比更新更慢。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接