Javascript 画布动画(网络节点和边缘):如何提高其性能,特别是在FireFox上?

Javascript 画布动画(网络节点和边缘):如何提高其性能,特别是在FireFox上?,javascript,performance,firefox,animation,canvas,Javascript,Performance,Firefox,Animation,Canvas,必须给出一个不断变化的节点网络(每个节点都有不同的影响,可能不止一种颜色)的想法,这些节点相互连接来创造一些东西 我想给它深度的感觉,所以我最终在标题周围使用了两幅画布:一幅在前景,甚至在文字上方,另一幅在背景,元素稍大且模糊 演示,目前完整的JavaScript代码: // min and max radius, radius threshold and percentage of filled circles var radMin = 5, radMax = 125, filledC

必须给出一个不断变化的节点网络(每个节点都有不同的影响,可能不止一种颜色)的想法,这些节点相互连接来创造一些东西

我想给它深度的感觉,所以我最终在标题周围使用了两幅画布:一幅在前景,甚至在文字上方,另一幅在背景,元素稍大且模糊

演示,目前完整的JavaScript代码:

// min and max radius, radius threshold and percentage of filled circles
var radMin = 5,
  radMax = 125,
  filledCircle = 60, //percentage of filled circles
  concentricCircle = 30, //percentage of concentric circles
  radThreshold = 25; //IFF special, over this radius concentric, otherwise filled

//min and max speed to move
var speedMin = 0.3,
  speedMax = 2.5;

//max reachable opacity for every circle and blur effect
var maxOpacity = 0.6;

//default palette choice
var colors = ['52,168,83', '117,95,147', '199,108,23', '194,62,55', '0,172,212', '120,120,120'],
  bgColors = ['52,168,83', '117,95,147', '199,108,23', '194,62,55', '0,172,212', '120,120,120'],
  circleBorder = 10,
  backgroundLine = bgColors[0];
var backgroundMlt = 0.85;

//min distance for links
var linkDist = Math.min(canvas.width, canvas.height) / 2.4,
  lineBorder = 2.5;

//most importantly: number of overall circles and arrays containing them
var maxCircles = 12,
  points = [],
  pointsBack = [];

//populating the screen
for (var i = 0; i < maxCircles * 2; i++) points.push(new Circle());
for (var i = 0; i < maxCircles; i++) pointsBack.push(new Circle(true));

//experimental vars
var circleExp = 1,
  circleExpMax = 1.003,
  circleExpMin = 0.997,
  circleExpSp = 0.00004,
  circlePulse = false;

//circle class
function Circle(background) {
  //if background, it has different rules
  this.background = (background || false);
  this.x = randRange(-canvas.width / 2, canvas.width / 2);
  this.y = randRange(-canvas.height / 2, canvas.height / 2);
  this.radius = background ? hyperRange(radMin, radMax) * backgroundMlt : hyperRange(radMin, radMax);
  this.filled = this.radius < radThreshold ? (randint(0, 100) > filledCircle ? false : 'full') : (randint(0, 100) > concentricCircle ? false : 'concentric');
  this.color = background ? bgColors[randint(0, bgColors.length - 1)] : colors[randint(0, colors.length - 1)];
  this.borderColor = background ? bgColors[randint(0, bgColors.length - 1)] : colors[randint(0, colors.length - 1)];
  this.opacity = 0.05;
  this.speed = (background ? randRange(speedMin, speedMax) / backgroundMlt : randRange(speedMin, speedMax)); // * (radMin / this.radius);
  this.speedAngle = Math.random() * 2 * Math.PI;
  this.speedx = Math.cos(this.speedAngle) * this.speed;
  this.speedy = Math.sin(this.speedAngle) * this.speed;
  var spacex = Math.abs((this.x - (this.speedx < 0 ? -1 : 1) * (canvas.width / 2 + this.radius)) / this.speedx),
    spacey = Math.abs((this.y - (this.speedy < 0 ? -1 : 1) * (canvas.height / 2 + this.radius)) / this.speedy);
  this.ttl = Math.min(spacex, spacey);
};

Circle.prototype.init = function() {
  Circle.call(this, this.background);
}

//support functions
//generate random int a<=x<=b
function randint(a, b) {
    return Math.floor(Math.random() * (b - a + 1) + a);
  }
  //generate random float
function randRange(a, b) {
    return Math.random() * (b - a) + a;
  }
  //generate random float more likely to be close to a
function hyperRange(a, b) {
  return Math.random() * Math.random() * Math.random() * (b - a) + a;
}

//rendering function
function drawCircle(ctx, circle) {
  //circle.radius *= circleExp;
  var radius = circle.background ? circle.radius *= circleExp : circle.radius /= circleExp;
  ctx.beginPath();
  ctx.arc(circle.x, circle.y, radius * circleExp, 0, 2 * Math.PI, false);
  ctx.lineWidth = Math.max(1, circleBorder * (radMin - circle.radius) / (radMin - radMax));
  ctx.strokeStyle = ['rgba(', circle.borderColor, ',', circle.opacity, ')'].join('');
  if (circle.filled == 'full') {
    ctx.fillStyle = ['rgba(', circle.borderColor, ',', circle.background ? circle.opacity * 0.8 : circle.opacity, ')'].join('');
    ctx.fill();
    ctx.lineWidth=0;
    ctx.strokeStyle = ['rgba(', circle.borderColor, ',', 0, ')'].join('');
  }
  ctx.stroke();
  if (circle.filled == 'concentric') {
    ctx.beginPath();
    ctx.arc(circle.x, circle.y, radius / 2, 0, 2 * Math.PI, false);
    ctx.lineWidth = Math.max(1, circleBorder * (radMin - circle.radius) / (radMin - radMax));
    ctx.strokeStyle = ['rgba(', circle.color, ',', circle.opacity, ')'].join('');
    ctx.stroke();
  }
  circle.x += circle.speedx;
  circle.y += circle.speedy;
  if (circle.opacity < (circle.background ? maxOpacity : 1)) circle.opacity += 0.01;
  circle.ttl--;
}

//initializing function
function init() {
  window.requestAnimationFrame(draw);
}

//rendering function
function draw() {

  if (circlePulse) {
    if (circleExp < circleExpMin || circleExp > circleExpMax) circleExpSp *= -1;
    circleExp += circleExpSp;
  }
  var ctxfr = document.getElementById('canvas').getContext('2d');
  var ctxbg = document.getElementById('canvasbg').getContext('2d');

  ctxfr.globalCompositeOperation = 'destination-over';
  ctxfr.clearRect(0, 0, canvas.width, canvas.height); // clear canvas
  ctxbg.globalCompositeOperation = 'destination-over';
  ctxbg.clearRect(0, 0, canvas.width, canvas.height); // clear canvas

  ctxfr.save();
  ctxfr.translate(canvas.width / 2, canvas.height / 2);
  ctxbg.save();
  ctxbg.translate(canvas.width / 2, canvas.height / 2);

  //function to render each single circle, its connections and to manage its out of boundaries replacement
  function renderPoints(ctx, arr) {
    for (var i = 0; i < arr.length; i++) {
      var circle = arr[i];
      //checking if out of boundaries
      if (circle.ttl<0) {}
      var xEscape = canvas.width / 2 + circle.radius,
        yEscape = canvas.height / 2 + circle.radius;
      if (circle.ttl < -20) arr[i].init(arr[i].background);
      //if (Math.abs(circle.y) > yEscape || Math.abs(circle.x) > xEscape) arr[i].init(arr[i].background);
      drawCircle(ctx, circle);
    }
    for (var i = 0; i < arr.length - 1; i++) {
      for (var j = i + 1; j < arr.length; j++) {
        var deltax = arr[i].x - arr[j].x;
        var deltay = arr[i].y - arr[j].y;
        var dist = Math.pow(Math.pow(deltax, 2) + Math.pow(deltay, 2), 0.5);
        //if the circles are overlapping, no laser connecting them
        if (dist <= arr[i].radius + arr[j].radius) continue;
        //otherwise we connect them only if the dist is < linkDist
        if (dist < linkDist) {
          var xi = (arr[i].x < arr[j].x ? 1 : -1) * Math.abs(arr[i].radius * deltax / dist);
          var yi = (arr[i].y < arr[j].y ? 1 : -1) * Math.abs(arr[i].radius * deltay / dist);
          var xj = (arr[i].x < arr[j].x ? -1 : 1) * Math.abs(arr[j].radius * deltax / dist);
          var yj = (arr[i].y < arr[j].y ? -1 : 1) * Math.abs(arr[j].radius * deltay / dist);
          ctx.beginPath();
          ctx.moveTo(arr[i].x + xi, arr[i].y + yi);
          ctx.lineTo(arr[j].x + xj, arr[j].y + yj);
          var samecolor = arr[i].color == arr[j].color;
          ctx.strokeStyle = ["rgba(", arr[i].borderColor, ",", Math.min(arr[i].opacity, arr[j].opacity) * ((linkDist - dist) / linkDist), ")"].join("");
          ctx.lineWidth = (arr[i].background ? lineBorder * backgroundMlt : lineBorder) * ((linkDist - dist) / linkDist); //*((linkDist-dist)/linkDist);
          ctx.stroke();
        }
      }
    }
  }

  var startTime = Date.now();
  renderPoints(ctxfr, points);
  renderPoints(ctxbg, pointsBack);
  deltaT = Date.now() - startTime;

  ctxfr.restore();
  ctxbg.restore();

  window.requestAnimationFrame(draw);
}

init();
//最小和最大半径、半径阈值和填充圆的百分比
var radMin=5,
radMax=125,
filledCircle=60,//填充圆的百分比
同心圆=30,//同心圆的百分比
radThreshold=25//如果特殊,则在该半径上同心,否则填充
//移动的最小和最大速度
var speedMin=0.3,
speedMax=2.5;
//每个圆和模糊效果的最大可达不透明度
var maxOpacity=0.6;
//默认调色板选择
变量颜色=['52168,83','117,95147','199108,23','194,62,55','0172212','120120120'],
bgColors=['52168,83','117,95147','199108,23','194,62,55','0172212','120120120'],
圆圈顺序=10,
背景线=背景颜色[0];
var背景MLT=0.85;
//链接的最小距离
var linkDist=Math.min(canvas.width、canvas.height)/2.4,
lineBorder=2.5;
//最重要的是:包含圆和圆的数组的总数
var maxCircles=12,
点数=[],
pointsBack=[];
//填充屏幕
用于(var i=0;ifilledCircle?假:“满”):(randint(01100)>同心圆?假:“同心”);
this.color=background?bgColors[randint(0,bgColors.length-1)]:colors[randint(0,colors.length-1)];
this.borderColor=background?bgColors[randint(0,bgColors.length-1)]:colors[randint(0,colors.length-1)];
这是不透明度=0.05;
this.speed=(背景?randRange(speedMin,speedMax)/backgroundMlt:randRange(speedMin,speedMax));//*(radMin/this.radius);
this.speedAngle=Math.random()*2*Math.PI;
this.speedx=Math.cos(this.speedAngle)*this.speed;
this.speedy=Math.sin(this.speedAngle)*this.speed;
var spacex=Math.abs((this.x-(this.speedx<0?-1:1)*(canvas.width/2+this.radius))/this.speedx),
spacey=Math.abs((this.y-(this.speedy<0?-1:1)*(canvas.height/2+this.radius))/this.speedy);
this.ttl=Math.min(spacex,spacey);
};
Circle.prototype.init=函数(){
循环调用(这个,这个背景);
}
//支持功能
//生成随机int(xEscape)arr[i].init(arr[i].background);
drawCircle(ctx,circle);
}
对于(变量i=0;iif(dist是在调用
ctx.save()后更改的上下文的唯一属性)
是它们的translate。因此,与调用此消耗性方法(它将保存上下文的所有属性)不同,您可以在完成绘图后以相反的方向再次对其进行翻译。此外,您可能希望避免调用
getElementById()
getContext()
在每次绘制时,您都可以将其存储在变量中。最后,如果您只需要一种类型的
全局复合操作
,则只需设置一次(每个上下文)。操作,我几乎可以肯定我已经在这里回复了,很抱歉延迟:非常感谢您宝贵的提要,它极大地帮助了我修复问题并提高了性能:)调用
ctx.save()后更改的上下文的唯一属性
是它们的translate。因此,与调用此消耗性方法(它将保存上下文的所有属性)不同,您可以在完成绘图后以相反的方向再次对其进行翻译。此外,您可能希望避免调用
getElementById()
getContext()
在每次绘制时,您都可以将其存储在变量中。最后,如果您只需要一种类型的
全局复合操作
,则只需设置一次(每个上下文)。操作,我几乎可以肯定我已经在这里回复了,很抱歉延迟:非常感谢您宝贵的提要,它极大地帮助了我修复问题并提高了性能:)