canvas 流动波点
时间: 2023-10-16 15:08:48 浏览: 33
要在Canvas上创建流动波点效果,你可以使用JavaScript来实现。下面是一个简单的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>流动波点</title>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById("myCanvas");
const context = canvas.getContext("2d");
const numDots = 100; // 波点数量
const radius = 2; // 波点半径
const speed = 0.05; // 波点移动速度
let phase = 0;
function draw() {
context.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < numDots; i++) {
const angle = (i / numDots) * Math.PI * 2; // 计算角度
const x =
(canvas.width / 2) *
(1 +
Math.cos(angle * 5 + phase) *
Math.sin(angle * 3 + phase * 2)) +
Math.cos(phase * 2) * (i * 2); // 计算x坐标
const y =
(canvas.height / 2) *
(1 +
Math.sin(angle * 5 + phase) *
Math.cos(angle * 3 + phase * 2)) +
Math.sin(phase * 2) * (i * 2); // 计算y坐标
context.beginPath();
context.arc(x, y, radius, 0, Math.PI * 2);
context.fillStyle = "blue";
context.fill();
}
phase += speed;
requestAnimationFrame(draw);
}
draw();
</script>
</body>
</html>
```
这段代码会在一个400x400的Canvas上创建100个流动的蓝色波点效果。你可以根据需要调整波点的数量、半径和移动速度,以及Canvas的大小。通过在`draw`函数中计算每个波点的坐标,并使用`context.arc`方法绘制圆形,实现了流动波点效果。整个动画使用了requestAnimationFrame进行循环绘制。你可以将上述代码复制到HTML文件中运行,查看效果。