javascript - Canvas Spark 线

标签 javascript html css canvas html5-canvas

我想创建一条不同长度的迷你图,并通过将它们从 Canvas 的中心移动到 Canvas 的末端来制作动画,并且该过程必须在 Canvas 上循环。为了实现这一点,我从粒子系统开始。

请检查下面的代码,这是codepen链接https://codepen.io/yesvin/pen/XPKNYW

window.onload = function() {
  var canvas = document.getElementById("canvas"),
    context = canvas.getContext("2d"),
    w = canvas.width = window.innerWidth,
    h = canvas.height = window.innerHeight,
    centerX = w / 2,
    centerY = h / 2,
    speed = 0,
    time = 0,
    numObjects = 5,
    x, y, vx, vy, life, maxLife;

  var lines = {},
    lineIndex = 0;

  function Line() {
    this.x = centerX;
    this.y = centerY;
    this.vx = Math.random() * 16 - 8;
    this.vy = Math.random() * 16 - 8;
    this.life = 0;
    this.maxLife = Math.random() * 10 + 20;
    lineIndex++;
    lines[lineIndex] = this;
    this.id = lineIndex;
  }

  Line.prototype.draw = function() {
    this.x += this.vx;
    this.y += this.vy;
    this.life++;
    if (this.life >= this.maxLife) {
      delete lines[this.id];
    }
    context.fillStyle = "#000";
    context.fillRect(this.x, this.y, 3, 3)
  }

  setInterval(function() {
    context.fillStyle = 'rgba(255,255,255,.05)';
    context.fillRect(0, 0, w, h);
    new Line();
    for (var i in lines) {
      lines[i].draw();
    }
  }, 30)
};
body {
  overflow: hidden;
  margin: 0;
  padding: 0;
}

canvas {
  display: block;
}
<canvas id="canvas"></canvas>

那么,我们如何使用 lineTo()moveTo() 方法创建相同的效果?我尝试了以下代码(在 codepen 中有注释)

context.beginPath();
context.moveTo(centerX, centerY);     
context.lineTo(this.x * Math.random() * w, this.y * Math.random() * h);
context.lineWidth = 1;
context.stroke();
context.strokeStyle = "#000";

示例 GIF enter image description here

提前致谢。

最佳答案

这是一种方法...
使用线条,您将获得更连续的效果:

我对您的代码所做的更改是跟踪起点和终点。

window.onload = function() {
  var canvas = document.getElementById("canvas"),
    context = canvas.getContext("2d"),
    w = canvas.width = window.innerWidth,
    h = canvas.height = window.innerHeight,
    centerX = w / 2,
    centerY = h / 2;

  var lines = {},
    lineIndex = 0;

  function Line() {
    this.start = { x: centerX, y: centerY };
    this.end = { x: centerX, y: centerY };
    this.vx = Math.random() * 16 - 8;
    this.vy = Math.random() * 16 - 8;
    this.life = 0;
    this.maxLife = Math.random() * 10 + 20;
    lineIndex++;
    lines[lineIndex] = this;
    this.id = lineIndex;
  }

  Line.prototype.draw = function() {
    this.end.x += this.vx;
    this.end.y += this.vy;
    this.life++;
    if (this.life >= this.maxLife) {
      delete lines[this.id];
    }
    //context.fillStyle = "#000";
    //context.fillRect(this.x, this.y, 1, 1)
    context.beginPath();
    context.moveTo(this.start.x, this.start.y);
    context.lineTo(this.end.x, this.end.y);
    context.lineWidth = 1;
    context.stroke();
    context.strokeStyle = "#000";
  }

  setInterval(function() {
    context.fillStyle = 'rgba(255,255,255,.05)';
    context.fillRect(0, 0, w, h);
    new Line();
    for (var i in lines) {
      lines[i].draw();
    }
  }, 30)
};
body {
  overflow: hidden;
  margin: 0;
  padding: 0;
}

canvas {
  display: block;
}
<canvas id="canvas"></canvas>

关于javascript - Canvas Spark 线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52233169/

相关文章:

javascript - ember.js 错误 : Assertion Failed: The response from a findAll must be an Array, 未定义

javascript - 如何处理 meteor 调用内或调用后的事件?

javascript - window.load 不起作用

javascript - 我如何将其从 jQuery 转换为 vanilla JS?

javascript - 如何在父 div 中调整子 div 大小时隐藏 div

html - 将焦点 css 属性设置为 div

javascript - 在javascript中删除字符串中的空格

javascript - backbone.js 中的 View 和附加表行

css - 如何避免在 div 定位相对内绝对定位的两个 div 之间的重叠?

php - 如何在 PHP 上调用点击按钮