|
20 | 20 | type Animation, |
21 | 21 | ZERO_VEC2, |
22 | 22 | copy, |
23 | | - copyObject |
| 23 | + copyObject, |
| 24 | + type unsubscribe |
24 | 25 | } from 'aninest'; |
25 | 26 | import { getUpdateLayer } from '@aninest/extensions'; |
26 | 27 | import { onMount } from 'svelte'; |
|
90 | 91 | addLocalListener(anim.children.shape.children.p2, 'start', () => |
91 | 92 | onPointChange(anim.children.shape.children.p2) |
92 | 93 | ); |
93 | | -
|
94 | | - return { |
| 94 | + let cancelRandomize: unsubscribe | undefined = undefined; |
| 95 | + const out = { |
95 | 96 | setP1(p1: Vec2) { |
96 | 97 | modifyTo(anim.children.shape, { p1 }); |
97 | 98 | }, |
|
104 | 105 | setColor(color: Color) { |
105 | 106 | return modifyTo(anim, { color }); |
106 | 107 | }, |
| 108 | + randomize(withTimeout: boolean) { |
| 109 | + if (cancelRandomize) cancelRandomize(); |
| 110 | + cancelRandomize = randomizeLine(out, withTimeout); |
| 111 | + }, |
107 | 112 | update: animLoop, |
108 | | - draw: draw |
| 113 | + draw: draw, |
| 114 | + symbol: Symbol() |
109 | 115 | }; |
| 116 | + return out; |
110 | 117 | }; |
111 | 118 | const canvas: HTMLCanvasElement = document.querySelector('#porky-canvas')!; |
112 | 119 | const increasinglySlower = (x: number) => { |
|
127 | 134 | return Math.floor(brightness + randomness); |
128 | 135 | }; |
129 | 136 |
|
130 | | - const randomizeLine = async (line: DrawableLine, withTimeout = true) => { |
| 137 | + const randomizeLine = (line: DrawableLine, withTimeout = true) => { |
131 | 138 | // wait for a random amount of time |
132 | 139 | const canvasMag = mag(newVec2(canvas.width, canvas.height)); |
133 | 140 | const p1 = newVec2(canvas.width * Math.random(), canvas.height * Math.random()); |
|
152 | 159 | }); |
153 | 160 | if (!withTimeout) return; |
154 | 161 | const sinceLastClick = (performance.now() - lastClicked) / 1000; |
155 | | - setTimeout( |
156 | | - () => randomizeLine(line), |
| 162 | + const cancel = setTimeout( |
| 163 | + () => line.randomize(), |
157 | 164 | increasinglySlower(1000 * Math.random() + 1000) * sinceLastClick + 1000 * Math.random() |
158 | 165 | ); |
| 166 | + return () => clearTimeout(cancel); |
159 | 167 | }; |
160 | 168 | const randomizeLines = (withTimeout = true) => { |
161 | 169 | const sinceLastClick = (performance.now() - lastClicked) / 1000; |
162 | 170 | for (let line of lines) { |
163 | 171 | setTimeout( |
164 | | - () => randomizeLine(line, withTimeout), |
| 172 | + () => line.randomize(withTimeout), |
165 | 173 | increasinglySlower(10000 * Math.random() * sinceLastClick + 1000 * sinceLastClick + 50) |
166 | 174 | ); |
167 | 175 | } |
168 | 176 | }; |
169 | 177 | let lastClicked = performance.now(); |
170 | 178 |
|
171 | 179 | const onUp = async (e: PointerEvent) => { |
172 | | - if (downCt != 0) randomizeLines(false); |
| 180 | + if (downCt != 0) randomizeLines(); |
173 | 181 | downCt = Math.max(0, downCt - 1); |
174 | 182 | }; |
175 | 183 |
|
|
210 | 218 | setTimeout(() => { |
211 | 219 | onResize(); |
212 | 220 | const canvasMag = mag(newVec2(canvas.width, canvas.height)); |
213 | | - for (let i = 0; i < canvasMag; i++) { |
| 221 | + for (let i = 0; i < canvasMag * 2; i++) { |
214 | 222 | const canvasCenter = newVec2(canvas.width / 2, canvas.height / 2); |
215 | 223 | lines.push(createLine(canvasCenter, canvasCenter)); |
216 | 224 | } |
|
0 commit comments