|
| 1 | +import { Polygon } from "./polygon.js"; |
| 2 | + |
| 3 | +class App { |
| 4 | + constructor() { |
| 5 | + this.canvas = document.createElement("canvas"); |
| 6 | + document.body.appendChild(this.canvas); |
| 7 | + this.ctx = this.canvas.getContext("2d"); |
| 8 | + |
| 9 | + this.pixelRatio = window.devicePixelRatio > 1 ? 2 : 1; |
| 10 | + |
| 11 | + window.addEventListener("resize", this.resize.bind(this), false); |
| 12 | + this.resize(); |
| 13 | + |
| 14 | + this.isDown = false; |
| 15 | + this.moveX = 0; |
| 16 | + this.offsetX = 0; |
| 17 | + |
| 18 | + document.addEventListener("pointerdown", this.onDown.bind(this), false); |
| 19 | + document.addEventListener("pointermove", this.onMove.bind(this), false); |
| 20 | + document.addEventListener("pointerup", this.onUp.bind(this), false); |
| 21 | + |
| 22 | + window.requestAnimationFrame(this.animate.bind(this)); |
| 23 | + } |
| 24 | + |
| 25 | + resize() { |
| 26 | + this.stageWidth = document.body.clientWidth; |
| 27 | + this.stageHeight = document.body.clientHeight; |
| 28 | + |
| 29 | + this.canvas.width = this.stageWidth * this.pixelRatio; |
| 30 | + this.canvas.height = this.stageHeight * this.pixelRatio; |
| 31 | + this.ctx.scale(this.pixelRatio, this.pixelRatio); |
| 32 | + |
| 33 | + this.polygon = new Polygon( |
| 34 | + this.stageWidth / 2, |
| 35 | + this.stageHeight / 2, |
| 36 | + this.stageHeight / 5.5, |
| 37 | + 5 |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + animate() { |
| 42 | + window.requestAnimationFrame(this.animate.bind(this)); |
| 43 | + |
| 44 | + this.ctx.clearRect(0, 0, this.stageWidth, this.stageHeight); |
| 45 | + |
| 46 | + this.moveX *= 0.92; |
| 47 | + |
| 48 | + this.polygon.animate(this.ctx, this.moveX); |
| 49 | + } |
| 50 | + |
| 51 | + onDown(e) { |
| 52 | + this.isDown = true; |
| 53 | + this.moveX = 0; |
| 54 | + this.offsetX = e.clientX; |
| 55 | + } |
| 56 | + |
| 57 | + onMove(e) { |
| 58 | + if (this.isDown) { |
| 59 | + this.moveX = e.clientX - this.offsetX; |
| 60 | + this.offsetX = e.clientX; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + onUp(e) { |
| 65 | + this.isDown = false; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +window.onload = () => { |
| 70 | + new App(); |
| 71 | +}; |
0 commit comments