gpt4 book ai didi

javascript - Canvas 粒子、碰撞和性能

转载 作者:太空狗 更新时间:2023-10-29 15:05:05 24 4
gpt4 key购买 nike

我正在创建一个网络应用程序,该应用程序具有交互式背景,粒子会四处弹跳。在任何时候,屏幕上都有大约 200 个圆形粒子,最多大约 800 个粒子。为粒子运行的一些碰撞和效果是以下原型(prototype)。我想知道是否可以通过使用 Web Worker 进行这些计算来提高性能?

/**
* Particles
*/

Jarvis.prototype.genForegroundParticles = function(options, count){

count = count || this.logoParticlesNum;

for (var i = 0; i < count; i++) {
this.logoParticles.push(new Particle());
}

}

Jarvis.prototype.genBackgroundParticles = function(options, count){

count = count || this.backgroundParticlesNum;

for (var i = 0; i < count; i++) {
this.backgroundParticles.push(new Particle(options));
}

}

Jarvis.prototype.motion = {
linear : function(particle, pIndex, particles){
particle.x += particle.vx
particle.y += particle.vy
},
normalizeVelocity : function(particle, pIndex, particles){

if (particle.vx - particle.vxInitial > 1) {
particle.vx -= 0.05;
} else if (particle.vx - particle.vxInitial < -1) {
particle.vx += 0.05;
}

if (particle.vy - particle.vyInitial > 1) {
particle.vy -= 0.05;
} else if (particle.vx - particle.vxInitial < -1) {
particle.vy += 0.05;
}

},
explode : function(particle, pIndex, particles) {

if (particle.isBottomOut()) {
particles.splice(pIndex, 1);
} else {
particle.x += particle.vx;
particle.y += particle.vy;
particle.vy += 0.1;
}

if (particles.length === 0){
particles.motion.removeMotion("explode");
this.allowMenu = true;
}

}
}

Jarvis.prototype.collision = {
boundingBox: function(particle, pIndex, particles){

if (particle.y > (this.HEIGHT - particle.radius) || particle.y < particle.radius) {
particle.vy *= -1;
}

if(particle.x > (this.WIDTH - particle.radius) || particle.x < particle.radius) {
particle.vx *= -1;
}
},
boundingBoxGravity: function(particle, pIndex, particles){
// TODO: FIX GRAVITY TO WORK PROPERLY IN COMBINATION WITH FX AND MOTION
if (particle.y > (this.HEIGHT - particle.radius) || particle.y < particle.radius) {
particle.vy *= -1;
particle.vy += 5;
}

if(particle.x > (this.WIDTH - particle.radius) || particle.x < particle.radius) {
particle.vx *= -1;
particle.vx += 5;
}

},
infinity: function(particle, pIndex, particles){

if (particle.x > this.WIDTH){
particle.x = 0;
}

if (particle.x < 0){
particle.x = this.WIDTH;
}

if (particle.y > this.HEIGHT){
particle.y = 0;
}

if (particle.y < 0) {
particle.y = this.HEIGHT;
}

}
}

Jarvis.prototype.fx = {
link : function(particle, pIndex, particles){

for(var j = pIndex + 1; j < particles.length; j++) {

var p1 = particle;
var p2 = particles[j];
var particleDistance = getDistance(p1, p2);

if (particleDistance <= this.particleMinLinkDistance) {
this.backgroundCtx.beginPath();
this.backgroundCtx.strokeStyle = "rgba("+p1.red+", "+p1.green+", "+p1.blue+","+ (p1.opacity - particleDistance / this.particleMinLinkDistance) +")";
this.backgroundCtx.moveTo(p1.x, p1.y);
this.backgroundCtx.lineTo(p2.x, p2.y);
this.backgroundCtx.stroke();
this.backgroundCtx.closePath();
}
}
},
shake : function(particle, pIndex, particles){

if (particle.xInitial - particle.x >= this.shakeAreaThreshold){
particle.xOper = (randBtwn(this.shakeFactorMin, this.shakeFactorMax) * 2) % (this.WIDTH);
} else if (particle.xInitial - particle.x <= -this.shakeAreaThreshold) {
particle.xOper = (randBtwn(-this.shakeFactorMax, this.shakeFactorMin) * 2) % (this.WIDTH);
}

if (particle.yInitial - particle.y >= this.shakeAreaThreshold){
particle.yOper = (randBtwn(this.shakeFactorMin, this.shakeFactorMax) * 2) % (this.HEIGHT);
} else if (particle.yInitial - particle.y <= -this.shakeAreaThreshold) {
particle.yOper = (randBtwn(-this.shakeFactorMax, this.shakeFactorMin) * 2) % (this.HEIGHT);
}

particle.x += particle.xOper;
particle.y += particle.yOper;

},
radialWave : function(particle, pIndex, particles){

var distance = getDistance(particle, this.center);

if (particle.radius >= (this.dim * 0.0085)) {
particle.radiusOper = -0.02;
} else if (particle.radius <= 1) {
particle.radiusOper = 0.02;
}

particle.radius += particle.radiusOper * particle.radius;
},
responsive : function(particle, pIndex, particles){

var newPosX = (this.logoParticles.logoOffsetX + this.logoParticles.particleRadius) + (this.logoParticles.particleDistance + this.logoParticles.particleRadius) * particle.arrPos.x;
var newPosY = (this.logoParticles.logoOffsetY + this.logoParticles.particleRadius) + (this.logoParticles.particleDistance + this.logoParticles.particleRadius) * particle.arrPos.y;

if (particle.xInitial !== newPosX || particle.yInitial !== newPosY){

particle.xInitial = newPosX;
particle.yInitial = newPosY;
particle.x = particle.xInitial;
particle.y = particle.yInitial;

}

},
motionDetect : function(particle, pIndex, particles){

var isClose = false;
var distance = null;

for (var i = 0; i < this.touches.length; i++) {

var t = this.touches[i];

var point = {
x : t.clientX,
y : t.clientY
}

var d = getDistance(point, particle);

if (d <= this.blackhole) {
isClose = true;

if (d <= distance || distance === null) {
distance = d;
}

}

}

if (isClose){
if (particle.radius < (this.dim * 0.0085)) {
particle.radius += 0.25;
}
if (particle.green >= 0 && particle.blue >= 0) {
particle.green -= 10;
particle.blue -= 10;
}
} else {
if (particle.radius > particle.initialRadius) {
particle.radius -= 0.25;
}
if (particle.green <= 255 && particle.blue <= 255) {
particle.green += 10;
particle.blue += 10;
}
}

},
reverseBlackhole : function(particle, pIndex, particles){

for (var i = 0; i < this.touches.length; i++) {

var t = this.touches[i];

var point = {
x : t.clientX,
y : t.clientY
}

var distance = getDistance(point, particle);

if (distance <= this.blackhole){

var diff = getPointsDifference(point, particle);

particle.vx += -diff.x / distance;
particle.vy += -diff.y / distance;
}

}
}
}

此外,如果有人想知道我有 3 个 Canvas 层,我会添加粒子渲染功能以及所有 Canvas 层的清除功能

  1. 绘制全屏径向渐变和粒子的背景

  2. 菜单 Canvas

  3. 菜单按钮覆盖选择器(显示哪个菜单处于事件状态等)


Jarvis.prototype.backgroundDraw = function() {

// particles

var that = this;

this.logoParticles.forEach(function(particle, i){

particle.draw(that.backgroundCtx);

that.logoParticles.motion.forEach(function(motionType, motionIndex){
that.motion[motionType].call(that, particle, i, that.logoParticles, "foregroundParticles");
});
that.logoParticles.fx.forEach(function(fxType, fxIndex){
that.fx[fxType].call(that, particle, i, that.logoParticles, "foregroundParticles");
});
that.logoParticles.collision.forEach(function(collisionType, collisionIndex){
that.collision[collisionType].call(that, particle, i, that.logoParticles, "foregroundParticles");
});
});

this.backgroundParticles.forEach(function(particle, i){

particle.draw(that.backgroundCtx);

that.backgroundParticles.motion.forEach(function(motionType, motionIndex){
that.motion[motionType].call(that, particle, i, that.backgroundParticles, "backgroundParticles");
});
that.backgroundParticles.fx.forEach(function(fxType, fxIndex){
that.fx[fxType].call(that, particle, i, that.backgroundParticles, "backgroundParticles");
});
that.backgroundParticles.collision.forEach(function(collisionType, collisionIndex){
that.collision[collisionType].call(that, particle, i, that.backgroundParticles, "backgroundParticles");
});
});

}

Jarvis.prototype.clearCanvas = function() {

switch(this.background.type){
case "radial_gradient":
this.setBackgroundRadialGradient(this.background.color1, this.background.color2);
break;
case "plane_color":
this.setBackgroundColor(this.background.red, this.background.green, this.background.blue, this.background.opacity);
break;
default:
this.setBackgroundColor(142, 214, 255, 1);
}

this.foregroundCtx.clearRect(this.clearStartX, this.clearStartY, this.clearDistance, this.clearDistance);
this.middlegroundCtx.clearRect(this.clearStartX, this.clearStartY, this.clearDistance, this.clearDistance);
}

Jarvis.prototype.mainLoop = function() {
this.clearCanvas();
this.backgroundDraw();
this.drawMenu();
window.requestAnimFrame(this.mainLoop.bind(this));
}

任何其他优化技巧将不胜感激。我已经阅读了几篇文章,但不确定如何进一步优化此代码。

最佳答案

您可以使用 FabricJS Canvas 图书馆。 FabricJS 默认支持交互性,当您创建一个新对象(圆形、矩形等)时,您可以通过鼠标或触摸屏对其进行操作。

var canvas = new fabric.Canvas('c');
var rect = new fabric.Rect({
width: 10, height: 20,
left: 100, top: 100,
fill: 'yellow',
angle: 30
});

canvas.add(rect);

看,我们以面向对象的方式在那里工作。

关于javascript - Canvas 粒子、碰撞和性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26689470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com