1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
| <template> <div class="layered-canvas-demo"> <div class="controls"> <button @click="toggleAnimation"> {{ isAnimating ? '暂停动画' : '开始动画' }} </button> <button @click="addParticles">添加粒子</button> <button @click="clearParticles">清除粒子</button> <div class="stats"> <span>FPS: {{ fps }}</span> <span>粒子数: {{ particleCount }}</span> </div> </div> <div ref="canvasContainer" class="canvas-container"></div> </div> </template>
<script setup> import { ref, onMounted, onUnmounted } from 'vue'
// 粒子类 class Particle { constructor(x, y) { this.x = x this.y = y this.vx = (Math.random() - 0.5) * 4 this.vy = (Math.random() - 0.5) * 4 this.radius = Math.random() * 3 + 1 this.color = `hsl(${Math.random() * 60 + 180}, 100%, 50%)` } update(width, height) { this.x += this.vx this.y += this.vy if (this.x < 0 || this.x > width) this.vx *= -1 if (this.y < 0 || this.y > height) this.vy *= -1 } draw(ctx) { ctx.beginPath() ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2) ctx.fillStyle = this.color ctx.fill() } }
// 分层Canvas管理器 class LayeredCanvasManager { constructor(container) { this.container = container this.layers = {} this.particles = [] this.animationId = null this.isAnimating = false this.lastTime = 0 this.fps = 60 this.frameCount = 0 this.fpsUpdateTime = 0 this.init() } init() { const width = this.container.offsetWidth const height = this.container.offsetHeight // 创建图层 const layerNames = ['background', 'grid', 'animation', 'overlay'] layerNames.forEach((name, index) => { const canvas = document.createElement('canvas') canvas.width = width canvas.height = height canvas.style.position = 'absolute' canvas.style.left = '0' canvas.style.top = '0' canvas.style.zIndex = index this.container.appendChild(canvas) this.layers[name] = { canvas, ctx: canvas.getContext('2d'), dirty: true } }) this.renderStatic() } // 渲染静态图层(只绘制一次) renderStatic() { this.renderBackground() this.renderGrid() } renderBackground() { const { ctx, canvas } = this.layers.background // 渐变背景 const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height) gradient.addColorStop(0, '#0a0e27') gradient.addColorStop(1, '#1a1f3a') ctx.fillStyle = gradient ctx.fillRect(0, 0, canvas.width, canvas.height) this.layers.background.dirty = false } renderGrid() { const { ctx, canvas } = this.layers.grid ctx.strokeStyle = 'rgba(0, 246, 255, 0.1)' ctx.lineWidth = 1 // 绘制网格 const gridSize = 50 for (let x = 0; x < canvas.width; x += gridSize) { ctx.beginPath() ctx.moveTo(x, 0) ctx.lineTo(x, canvas.height) ctx.stroke() } for (let y = 0; y < canvas.height; y += gridSize) { ctx.beginPath() ctx.moveTo(0, y) ctx.lineTo(canvas.width, y) ctx.stroke() } this.layers.grid.dirty = false } // 渲染动画图层(每帧绘制) renderAnimation() { const { ctx, canvas } = this.layers.animation // 清空画布 ctx.clearRect(0, 0, canvas.width, canvas.height) // 更新和绘制粒子 this.particles.forEach(particle => { particle.update(canvas.width, canvas.height) particle.draw(ctx) }) } // 渲染覆盖层(显示信息) renderOverlay() { const { ctx, canvas } = this.layers.overlay ctx.clearRect(0, 0, canvas.width, canvas.height) // 显示图层信息 ctx.fillStyle = '#00f6ff' ctx.font = '14px monospace' ctx.fillText('分层渲染演示', 10, 20) ctx.fillText(`图层: ${Object.keys(this.layers).length}`, 10, 40) ctx.fillText(`粒子: ${this.particles.length}`, 10, 60) } addParticles(count = 50) { const { canvas } = this.layers.animation for (let i = 0; i < count; i++) { const x = Math.random() * canvas.width const y = Math.random() * canvas.height this.particles.push(new Particle(x, y)) } } clearParticles() { this.particles = [] } start() { if (this.isAnimating) return this.isAnimating = true this.lastTime = performance.now() this.animate() } stop() { this.isAnimating = false if (this.animationId) { cancelAnimationFrame(this.animationId) this.animationId = null } } animate(currentTime = 0) { if (!this.isAnimating) return // 计算FPS this.frameCount++ if (currentTime - this.fpsUpdateTime > 1000) { this.fps = this.frameCount this.frameCount = 0 this.fpsUpdateTime = currentTime } // 渲染 this.renderAnimation() this.renderOverlay() this.animationId = requestAnimationFrame((time) => this.animate(time)) } destroy() { this.stop() Object.values(this.layers).forEach(layer => { layer.canvas.remove() }) } }
// 组件状态 const canvasContainer = ref(null) let manager = null const isAnimating = ref(false) const fps = ref(60) const particleCount = ref(0)
// FPS更新 setInterval(() => { if (manager) { fps.value = manager.fps particleCount.value = manager.particles.length } }, 100)
const toggleAnimation = () => { if (!manager) return if (isAnimating.value) { manager.stop() } else { manager.start() } isAnimating.value = !isAnimating.value }
const addParticles = () => { if (manager) { manager.addParticles(50) } }
const clearParticles = () => { if (manager) { manager.clearParticles() } }
onMounted(() => { manager = new LayeredCanvasManager(canvasContainer.value) manager.addParticles(100) manager.start() isAnimating.value = true })
onUnmounted(() => { if (manager) { manager.destroy() } }) </script>
<style scoped> .layered-canvas-demo { padding: 20px; background: #0a0e27; border-radius: 10px; color: #fff; }
.controls { display: flex; gap: 10px; margin-bottom: 15px; align-items: center; }
.controls button { padding: 10px 20px; background: #00f6ff; border: none; border-radius: 5px; color: #000; font-weight: bold; cursor: pointer; transition: all 0.3s ease; }
.controls button:hover { background: #00d9e6; transform: translateY(-2px); }
.stats { margin-left: auto; display: flex; gap: 20px; color: #00f6ff; font-family: monospace; }
.canvas-container { position: relative; width: 100%; height: 500px; border: 2px solid #00f6ff; border-radius: 8px; overflow: hidden; } </style>
|