|
1 | 1 | <!DOCTYPE html> |
2 | | -<html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <title>Simulation Comparison</title> |
| 6 | + <style> |
| 7 | + body { |
| 8 | + margin: 0; |
| 9 | + overflow: hidden; |
| 10 | + font-family: sans-serif; |
| 11 | + background: #111; |
| 12 | + color: #fff; |
| 13 | + } |
| 14 | + #label { |
| 15 | + position: absolute; |
| 16 | + top: 10px; |
| 17 | + left: 50%; |
| 18 | + transform: translateX(-50%); |
| 19 | + z-index: 1; |
| 20 | + text-align: center; |
| 21 | + } |
| 22 | + canvas { |
| 23 | + display: inline-block; |
| 24 | + vertical-align: top; |
| 25 | + } |
| 26 | + </style> |
| 27 | +</head> |
3 | 28 | <body> |
4 | | -<h1>Hello World</h1> |
5 | | -<p>I'm hosted with GitHub Pages.</p> |
| 29 | + <div id="label"> |
| 30 | + <h2>Code Optimization Showcase</h2> |
| 31 | + <p>Left: Naive Simulation | Right: Optimized Simulation</p> |
| 32 | + </div> |
| 33 | + <canvas id="canvas1" width="600" height="400"></canvas> |
| 34 | + <canvas id="canvas2" width="600" height="400"></canvas> |
| 35 | + |
| 36 | + <script> |
| 37 | + class BouncingBox { |
| 38 | + constructor(width, height) { |
| 39 | + this.w = width; |
| 40 | + this.h = height; |
| 41 | + this.reset(); |
| 42 | + } |
| 43 | + |
| 44 | + reset() { |
| 45 | + this.x = Math.random() * (this.w - 50); |
| 46 | + this.y = Math.random() * (this.h - 50); |
| 47 | + this.vx = (Math.random() * 2 + 1) * (Math.random() < 0.5 ? 1 : -1); |
| 48 | + this.vy = (Math.random() * 2 + 1) * (Math.random() < 0.5 ? 1 : -1); |
| 49 | + this.size = 40; |
| 50 | + } |
| 51 | + |
| 52 | + update() { |
| 53 | + this.x += this.vx; |
| 54 | + this.y += this.vy; |
| 55 | + if (this.x <= 0 || this.x + this.size >= this.w) this.vx *= -1; |
| 56 | + if (this.y <= 0 || this.y + this.size >= this.h) this.vy *= -1; |
| 57 | + } |
| 58 | + |
| 59 | + draw(ctx) { |
| 60 | + ctx.fillStyle = "#00ffcc"; |
| 61 | + ctx.fillRect(this.x, this.y, this.size, this.size); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + class Simulation { |
| 66 | + constructor(canvasId) { |
| 67 | + const canvas = document.getElementById(canvasId); |
| 68 | + this.canvas = canvas; |
| 69 | + this.ctx = canvas.getContext("2d"); |
| 70 | + this.box = new BouncingBox(canvas.width, canvas.height); |
| 71 | + } |
| 72 | + |
| 73 | + update() { |
| 74 | + this.box.update(); |
| 75 | + } |
| 76 | + |
| 77 | + draw() { |
| 78 | + this.ctx.fillStyle = "#222"; |
| 79 | + this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height); |
| 80 | + this.box.draw(this.ctx); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + const sim1 = new Simulation("canvas1"); // Naive |
| 85 | + const sim2 = new Simulation("canvas2"); // Optimized |
| 86 | + |
| 87 | + function loop() { |
| 88 | + sim1.update(); |
| 89 | + sim2.update(); |
| 90 | + sim1.draw(); |
| 91 | + sim2.draw(); |
| 92 | + requestAnimationFrame(loop); |
| 93 | + } |
| 94 | + |
| 95 | + loop(); |
| 96 | + </script> |
6 | 97 | </body> |
7 | 98 | </html> |
0 commit comments