Skip to content

Commit f5ab13e

Browse files
authored
docs: cleanup after requestAnimationFrame example (#536)
* docs: cleanup after requestAnimationFrame example * requestAnimationFrame code example without extra class * format
1 parent bcca377 commit f5ab13e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/src/pages/blog/anywidget-02.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,31 @@ export default { render };
215215
> Note: The above front-end code requires transformation with tool like
216216
> `esbuild` to allow for the special JSX syntax (e.g., `<App />`).
217217
218+
Another common scenario requiring proper cleanup is when using
219+
`requestAnimationFrame` to continually redraw into a canvas:
220+
221+
```javascript
222+
export default {
223+
render({ model, el }) {
224+
const canvasEl = document.createElement("canvas");
225+
let requestId = requestAnimationFrame(step);
226+
227+
function step() {
228+
/* ...custom rendering goes here... */
229+
requestId = requestAnimationFrame(step);
230+
}
231+
el.appendChild(canvasEl);
232+
233+
return () => {
234+
cancelAnimationFrame(requestId);
235+
};
236+
},
237+
};
238+
```
239+
240+
This prevents the accumulation of several animation loops executing at the same
241+
time after re-running the cell a bunch of times.
242+
218243
## Getting Started
219244
220245
To start using **anywidget** v0.2, upgrade your package using pip:

0 commit comments

Comments
 (0)