<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>星空</title> <style> body { margin: 0; padding: 0; overflow: hidden; background: #000; } canvas { display: block; background: #000; } </style> </head> <body> <canvas id="canvas"></canvas> <script> function initializeCanvas(elemid) { let canvas = document.getElementById(elemid); let context = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; createStars(context, canvas.width, canvas.height); return { context: context, canvas: canvas }; } function createStars(context, width, height) { const stars = 500; for (let i = 0; i < stars; i++) { const x = Math.random() * width; const y = Math.random() * height; const radius = Math.random() * 2; context.beginPath(); context.arc(x, y, radius, 0, Math.PI * 2, false); context.fillStyle = 'white'; context.fill(); } } class Segment { constructor(parent, length, angle, isFirst) { this.position = isFirst ? { x: parent.x, y: parent.y } : { x: parent.nextPosition.x, y: parent.nextPosition.y }; this.length = length; this.angle = angle; this.nextPosition = { x: this.position.x + length * Math.cos(this.angle), y: this.position.y + length * Math.sin(this.angle) }; } // ... 其他方法 } class Tentacle { constructor(x, y, length, segments, angle) { this.x = x; this.y = y; this.length = length; this.segments = segments; // ... 初始化触手的每一段 } updatePosition(lastTarget, target) { // ... 更新触手的位置 } drawSegments(target) { // ... 绘制触手的每一段 } drawBase(target) { // ... 绘制触手的基部 } } canvas.addEventListener("mousemove", function(e) { mouse.x = e.pageX - this.offsetLeft; m
时间: 2025-04-01 19:04:55 浏览: 89
### 动态星空效果的实现
HTML5 Canvas 提供了一个灵活且功能丰富的绘图接口,可以用来创建各种视觉效果。以下是通过 HTML5 Canvas 绘制动态星空效果的一个简单示例:
```javascript
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// 设置画布大小
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
function drawStar(x, y, radius, spikes) {
let rot = Math.PI / 2 * 3;
const step = Math.PI / spikes;
ctx.beginPath();
ctx.moveTo(x, y - radius);
for (let i = 0; i < spikes; i++) {
x += Math.cos(rot) * radius;
y += Math.sin(rot) * radius;
ctx.lineTo(x, y);
rot += step;
x += Math.cos(rot) * radius * 0.5;
y += Math.sin(rot) * radius * 0.5;
ctx.lineTo(x, y);
rot += step;
}
ctx.closePath();
}
function animateStars() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 随机生成星星
for (let i = 0; i < 100; i++) {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const size = Math.random() * 2 + 1;
const spikes = Math.floor(Math.random() * 5) + 4;
ctx.fillStyle = 'white';
drawStar(x, y, size, spikes);
}
requestAnimationFrame(animateStars);
}
animateStars();
```
上述代码展示了如何使用 `CanvasRenderingContext2D` 的方法来绘制随机分布的星星,并通过 `requestAnimationFrame()` 实现动画效果[^1]。
---
### 触手动画的实现
触手动画可以通过贝塞尔曲线(Bezier Curves)和路径操作来模拟柔韧的效果。以下是一个简单的触手动画示例:
```javascript
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// 设置画布大小
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
class Tentacle {
constructor(startX, startY, length, segments) {
this.startX = startX;
this.startY = startY;
this.length = length;
this.segments = [];
for (let i = 0; i < segments; i++) {
this.segments.push({
angle: Math.random() * Math.PI,
distance: Math.random() * length / segments
});
}
}
update() {
for (let segment of this.segments) {
segment.angle += (Math.random() - 0.5) * 0.1;
}
}
draw() {
ctx.strokeStyle = '#ff7f50'; // 橙红色
ctx.lineWidth = 8;
ctx.lineCap = 'round';
ctx.beginPath();
ctx.moveTo(this.startX, this.startY);
let currentX = this.startX;
let currentY = this.startY;
for (let segment of this.segments) {
const dx = Math.cos(segment.angle) * segment.distance;
const dy = Math.sin(segment.angle) * segment.distance;
currentX += dx;
currentY += dy;
ctx.quadraticCurveTo(currentX, currentY, currentX, currentY);
}
ctx.stroke();
}
}
const tentacles = [];
for (let i = 0; i < 5; i++) {
tentacles.push(new Tentacle(canvas.width / 2, canvas.height, 200, 10));
}
function animateTentacles() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let tentacle of tentacles) {
tentacle.update();
tentacle.draw();
}
requestAnimationFrame(animateTentacles);
}
animateTentacles();
```
此代码片段定义了一种基于分段角度变化的触手类,并通过更新每一段的角度实现了波浪式的运动效果。
---
### 总结
以上两个例子分别展示了如何利用 HTML5 Canvas 创建动态星空和触手动画。这些技术的核心在于理解 Canvas API 中的路径绘制、变换矩阵以及帧刷新机制。对于更复杂的应用场景,还可以结合 NanoVG-RS 进行高性能矢量图形渲染。
阅读全文
相关推荐


















