A bespoke [HTML canvas element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas) with Pythonic drawing and download capabilities.
| 1180 | |
| 1181 | |
| 1182 | class canvas(ContainerElement): |
| 1183 | """ |
| 1184 | A bespoke |
| 1185 | [HTML canvas element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas) |
| 1186 | with Pythonic drawing and download capabilities. |
| 1187 | """ |
| 1188 | |
| 1189 | def download(self, filename="snapped.png"): |
| 1190 | """ |
| 1191 | Download the canvas content as an image file. |
| 1192 | |
| 1193 | Creates a temporary download link and triggers it. |
| 1194 | """ |
| 1195 | # The `a` element class is dynamically created below. |
| 1196 | download_link = a( # noqa: F821 |
| 1197 | download=filename, href=self._dom_element.toDataURL() |
| 1198 | ) |
| 1199 | self.append(download_link) |
| 1200 | download_link._dom_element.click() |
| 1201 | |
| 1202 | def draw(self, what, width=None, height=None): |
| 1203 | """ |
| 1204 | Draw a 2d image source (`what`) onto the canvas. Optionally scale to |
| 1205 | `width` and `height`. |
| 1206 | |
| 1207 | Accepts canvas image sources: `HTMLImageElement`, `SVGImageElement`, |
| 1208 | `HTMLVideoElement`, `HTMLCanvasElement`, `ImageBitmap`, |
| 1209 | `OffscreenCanvas`, or `VideoFrame`. |
| 1210 | """ |
| 1211 | if isinstance(what, Element): |
| 1212 | what = what._dom_element |
| 1213 | |
| 1214 | ctx = self._dom_element.getContext("2d") |
| 1215 | if width or height: |
| 1216 | ctx.drawImage(what, 0, 0, width, height) |
| 1217 | else: |
| 1218 | ctx.drawImage(what, 0, 0) |
| 1219 | |
| 1220 | |
| 1221 | class video(ContainerElement): |