A categorical programming language
修訂 | ae1a5286be825e690fa13ca4fa35676d11109f4d (tree) |
---|---|
時間 | 2022-11-01 05:00:12 |
作者 | Corbin <cds@corb...> |
Commiter | Corbin |
Allow pausing and unpausing videos.
@@ -176,23 +176,31 @@ function CammyImage({ drawer }) { | ||
176 | 176 | |
177 | 177 | class CammyAnimation extends Component { |
178 | 178 | #frameRequest; |
179 | - state = null; | |
179 | + state = { frames: 0, isPlaying: false }; | |
180 | 180 | |
181 | - componentDidMount() { | |
182 | - // Reset frame count and timestamp to keep FPS accurate. | |
183 | - this.setState({ frames: 0 }); | |
181 | + componentDidMount() { this.play(); } | |
182 | + componentWillUnmount() { this.pause(); } | |
183 | + | |
184 | + play() { | |
185 | + this.setState({ isPlaying: true }); | |
184 | 186 | this.#frameRequest = window.requestAnimationFrame(this.stepFrame.bind(this)); |
185 | 187 | } |
186 | 188 | |
187 | - componentWillUnmount() { | |
189 | + pause() { | |
190 | + // Reset frame count and timestamp to keep FPS accurate. | |
191 | + this.setState({ start: null, frames: 0, isPlaying: false }); | |
188 | 192 | window.cancelAnimationFrame(this.#frameRequest); |
189 | 193 | this.#frameRequest = null; |
190 | 194 | } |
191 | 195 | |
196 | + playPause() { | |
197 | + if (this.state.isPlaying) { this.pause(); } else { this.play(); } | |
198 | + } | |
199 | + | |
192 | 200 | stepFrame(timestamp) { |
193 | 201 | const start = this.state.start || timestamp; |
194 | 202 | this.setState({ start, timestamp, frames: this.state.frames + 1 }); |
195 | - setTimeout(() => window.requestAnimationFrame(this.stepFrame.bind(this)), 50); | |
203 | + this.#frameRequest = window.requestAnimationFrame(this.stepFrame.bind(this)); | |
196 | 204 | } |
197 | 205 | |
198 | 206 | render({ drawer }) { |
@@ -207,6 +215,9 @@ class CammyAnimation extends Component { | ||
207 | 215 | |
208 | 216 | return h("div", {}, |
209 | 217 | h("canvas", { ref: canvas, width: 100, height: 100 }), |
218 | + h("label", {}, | |
219 | + h("input", { type: "checkbox", checked: this.state.isPlaying, onClick: this.playPause.bind(this) }), | |
220 | + "Play/Pause"), | |
210 | 221 | h("p", {}, `${fps} frames/s`), |
211 | 222 | ); |
212 | 223 | } |