Skip to content

Commit f20a000

Browse files
authored
fix: broken methods video.snap and canvas.download (#2126)
* fix: broken methods video.snap and canvas.download * Allow canvas.draw to use actual image width/height.
1 parent 6c938df commit f20a000

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

pyscript.core/src/stdlib/pyscript/web/elements.py

+31-25
Original file line numberDiff line numberDiff line change
@@ -614,12 +614,15 @@ def download(self, filename: str = "snapped.png") -> None:
614614
Output:
615615
None
616616
"""
617-
link = self.create("a")
618-
link._dom_element.download = filename
619-
link._dom_element.href = self._dom_element.toDataURL()
620-
link._dom_element.click()
617+
download_link = a(download=filename, href=self._dom_element.toDataURL())
621618

622-
def draw(self, what, width, height):
619+
# Adding the link to the DOM is recommended for browser compatibility to make
620+
# sure that the click works.
621+
self.append(download_link)
622+
623+
download_link._dom_element.click()
624+
625+
def draw(self, what, width=None, height=None):
623626
"""Draw `what` on the current element
624627
625628
Inputs:
@@ -634,7 +637,12 @@ def draw(self, what, width, height):
634637
what = what._dom_element
635638

636639
# https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage
637-
self._dom_element.getContext("2d").drawImage(what, 0, 0, width, height)
640+
ctx = self._dom_element.getContext("2d")
641+
if width or height:
642+
ctx.drawImage(what, 0, 0, width, height)
643+
644+
else:
645+
ctx.drawImage(what, 0, 0)
638646

639647

640648
class caption(ContainerElement):
@@ -1404,6 +1412,8 @@ class video(ContainerElement):
14041412
preload = DOMProperty("preload")
14051413
src = DOMProperty("src")
14061414
width = DOMProperty("width")
1415+
videoHeight = DOMProperty("videoHeight")
1416+
videoWidth = DOMProperty("videoWidth")
14071417

14081418
def snap(
14091419
self,
@@ -1412,33 +1422,29 @@ def snap(
14121422
height: int | None = None,
14131423
):
14141424
"""
1415-
Captures a snapshot of a video.
1425+
Capture a snapshot (i.e. a single frame) of a video to a canvas.
14161426
14171427
Inputs:
14181428
1419-
* to: element where to save the snapshot of the video frame to
1420-
* width: width of the image
1421-
* height: height of the image
1429+
* to: the canvas to save the video frame to (if None, one is created).
1430+
* width: width of the snapshot (defaults to the video width).
1431+
* height: height of the snapshot (defaults to the video height).
14221432
14231433
Output:
14241434
(Element) canvas element where the video frame snapshot was drawn into
14251435
"""
1436+
width = width if width is not None else self.videoWidth
1437+
height = height if height is not None else self.videoHeight
1438+
14261439
if to is None:
1427-
to_canvas = self.create("canvas")
1428-
if width is None:
1429-
width = self._dom_element.width
1430-
if height is None:
1431-
height = self._dom_element.height
1432-
to_canvas._dom_element.width = width
1433-
to_canvas._dom_element.height = height
1440+
to = canvas(width=width, height=height)
14341441

14351442
elif isinstance(to, Element):
1436-
if to._dom_element.tagName != "CANVAS":
1437-
raise TypeError("Element to snap to must a canvas.")
1438-
to_canvas = to
1443+
if to.tag != "canvas":
1444+
raise TypeError("Element to snap to must be a canvas.")
14391445

14401446
elif getattr(to, "tagName", "") == "CANVAS":
1441-
to_canvas = canvas(to)
1447+
to = canvas(dom_element=to)
14421448

14431449
# If 'to' is a string, then assume it is a query selector.
14441450
elif isinstance(to, str):
@@ -1447,13 +1453,13 @@ def snap(
14471453
raise TypeError("No element with selector {to} to snap to.")
14481454

14491455
if nodelist[0].tagName != "CANVAS":
1450-
raise TypeError("Element to snap to must a be canvas.")
1456+
raise TypeError("Element to snap to must be a canvas.")
14511457

1452-
to_canvas = canvas(nodelist[0])
1458+
to = canvas(dom_element=nodelist[0])
14531459

1454-
to_canvas.draw(self, width, height)
1460+
to.draw(self, width, height)
14551461

1456-
return canvas
1462+
return to
14571463

14581464

14591465
class wbr(Element):

0 commit comments

Comments
 (0)