gpt4 book ai didi

javascript - Canvas 画线动画

转载 作者:行者123 更新时间:2023-11-28 13:10:31 24 4
gpt4 key购买 nike

我是使用 HTML5 Canvas 制作动画的新学员。我正在努力在 Canvas 上创建具有所需线条长度的线条绘制动画。

这是代码

var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"),
width = canvas.width = window.innerWidth,
height = canvas.height = window.innerHeight;
var x = 200;
var y = 200;
draw();
update();

function draw() {
context.beginPath();
context.moveTo(100, 100);
context.lineTo(x, y);
context.stroke();
}

function update() {
context.clearRect(0, 0, width, height);
x = x + 1;
y = y + 1;
draw();
requestAnimationFrame(update);
}
html,
body {
margin: 0px;
}

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

在上面的代码中,线条在 Canvas 上不断增长。但是如何实现 200px 宽的线并为 x 和 y 方向上的运动设置动画。相同的动画具有多条线,使用 for 循环并将它们向不同的方向移动。

检查引用图像....
enter image description here

需要向不同方向移动每条线

提前致谢

enter image description here

找到我想要实现的新引用图像

最佳答案

您需要使用变换或一些三 Angular 函数。

变换

对于每一帧:

  • 重置变换并平移到中心
  • 透明 Canvas
  • 从中心向右画线
  • 旋转 x Angular
  • 重复第 2 步,直到绘制完所有线条

var ctx = c.getContext("2d");
var centerX = c.width>>1;
var centerY = c.height>>1;
var maxLength = Math.min(centerX, centerY); // use the shortest direction for demo
var currentLength = 0; // current length, for animation
var lenStep = 1; // "speed" of animation

function render() {
ctx.setTransform(1,0,0,1, centerX, centerY);
ctx.clearRect(-centerX, -centerY, c.width, c.height);
ctx.beginPath();

for(var angle = 0, step = 0.1; angle < Math.PI * 2; angle += step) {
ctx.moveTo(0, 0);
ctx.lineTo(currentLength, 0);
ctx.rotate(step);
}
ctx.stroke(); // stroke all at once
}

(function loop() {
render();
currentLength += lenStep;
if (currentLength < maxLength) requestAnimationFrame(loop);
})();
<canvas id=c></canvas>

您可以以不同的方式使用转换,但由于您正在学习,所以我在上面的代码中保持简单。

三 Angular 函数

您还可以使用三 Angular 学手动计算线 Angular 。同样在这里您可以使用不同的方法,即。如果您想使用增量值、向量或使用隐式数学的强力方法。

对于每一帧:

  • 重置变换并平移到中心
  • 透明 Canvas
  • 计算每条线的 Angular 和方向
  • 画线

var ctx = c.getContext("2d");
var centerX = c.width>>1;
var centerY = c.height>>1;
var maxLength = Math.min(centerX, centerY); // use the shortest direction for demo
var currentLength = 0; // current length, for animation
var lenStep = 1; // "speed" of animation

ctx.setTransform(1,0,0,1, centerX, centerY);

function render() {
ctx.clearRect(-centerX, -centerY, c.width, c.height);
ctx.beginPath();

for(var angle = 0, step = 0.1; angle < Math.PI * 2; angle += step) {
ctx.moveTo(0, 0);
ctx.lineTo(currentLength * Math.cos(angle), currentLength * Math.sin(angle));
}
ctx.stroke(); // stroke all at once
}

(function loop() {
render();
currentLength += lenStep;
if (currentLength < maxLength) requestAnimationFrame(loop);
})();
<canvas id=c></canvas>

可以使用的奖励动画(使用与上面相同的基础):

var ctx = c.getContext("2d", {alpha: false});
var centerX = c.width>>1;
var centerY = c.height>>1;

ctx.setTransform(1,0,0,1, centerX, centerY);
ctx.lineWidth = 2;
ctx.strokeStyle = "rgba(0,0,0,0.8)";
ctx.shadowBlur = 16;

function render(time) {
ctx.globalAlpha=0.77;
ctx.fillRect(-500, -500, 1000, 1000);
ctx.globalAlpha=1;
ctx.beginPath();
ctx.rotate(0.025);
ctx.shadowColor = "hsl(" + time*0.1 + ",100%,75%)";
ctx.shadowBlur = 16;

for(var angle = 0, step = Math.PI / ((time % 200) + 50); angle < Math.PI * 2; angle += step) {
ctx.moveTo(0, 0);
var len = 150 + 150 * Math.cos(time*0.0001618*angle*Math.tan(time*0.00025)) * Math.sin(time*0.01);
ctx.lineTo(len * Math.cos(angle), len * Math.sin(angle));
}
ctx.stroke();
ctx.globalCompositeOperation = "lighter";
ctx.shadowBlur = 0;
ctx.drawImage(ctx.canvas, -centerX, -centerY);
ctx.drawImage(ctx.canvas, -centerX, -centerY);
ctx.globalCompositeOperation = "source-over";
}

function loop(time) {
render(time);
requestAnimationFrame(loop);
};
requestAnimationFrame(loop);
body {margin:0;background:#222}
<canvas id=c width=640 height=640></canvas>

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

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