@@ -614,12 +614,15 @@ def download(self, filename: str = "snapped.png") -> None:
614
614
Output:
615
615
None
616
616
"""
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 ())
621
618
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 ):
623
626
"""Draw `what` on the current element
624
627
625
628
Inputs:
@@ -634,7 +637,12 @@ def draw(self, what, width, height):
634
637
what = what ._dom_element
635
638
636
639
# 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 )
638
646
639
647
640
648
class caption (ContainerElement ):
@@ -1404,6 +1412,8 @@ class video(ContainerElement):
1404
1412
preload = DOMProperty ("preload" )
1405
1413
src = DOMProperty ("src" )
1406
1414
width = DOMProperty ("width" )
1415
+ videoHeight = DOMProperty ("videoHeight" )
1416
+ videoWidth = DOMProperty ("videoWidth" )
1407
1417
1408
1418
def snap (
1409
1419
self ,
@@ -1412,33 +1422,29 @@ def snap(
1412
1422
height : int | None = None ,
1413
1423
):
1414
1424
"""
1415
- Captures a snapshot of a video.
1425
+ Capture a snapshot (i.e. a single frame) of a video to a canvas .
1416
1426
1417
1427
Inputs:
1418
1428
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).
1422
1432
1423
1433
Output:
1424
1434
(Element) canvas element where the video frame snapshot was drawn into
1425
1435
"""
1436
+ width = width if width is not None else self .videoWidth
1437
+ height = height if height is not None else self .videoHeight
1438
+
1426
1439
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 )
1434
1441
1435
1442
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." )
1439
1445
1440
1446
elif getattr (to , "tagName" , "" ) == "CANVAS" :
1441
- to_canvas = canvas (to )
1447
+ to = canvas (dom_element = to )
1442
1448
1443
1449
# If 'to' is a string, then assume it is a query selector.
1444
1450
elif isinstance (to , str ):
@@ -1447,13 +1453,13 @@ def snap(
1447
1453
raise TypeError ("No element with selector {to} to snap to." )
1448
1454
1449
1455
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." )
1451
1457
1452
- to_canvas = canvas (nodelist [0 ])
1458
+ to = canvas (dom_element = nodelist [0 ])
1453
1459
1454
- to_canvas .draw (self , width , height )
1460
+ to .draw (self , width , height )
1455
1461
1456
- return canvas
1462
+ return to
1457
1463
1458
1464
1459
1465
class wbr (Element ):
0 commit comments