|
3 | 3 | (function() {
|
4 | 4 |
|
5 | 5 | var
|
6 |
| - video = dom.screenVideo[ 0 ], |
7 |
| - canvas = document.createElement( "canvas" ), |
8 |
| - ctx = canvas.getContext( "2d" ) |
| 6 | + canvasCapture = document.createElement( "canvas" ), |
| 7 | + ctx = canvasCapture.getContext( "2d" ) |
9 | 8 | ;
|
10 | 9 |
|
11 | 10 | dom.ctrlCaptureBtn.click( function() {
|
12 | 11 | var
|
13 |
| - w = video.videoWidth, |
14 |
| - h = video.videoHeight, |
15 |
| - sec = api.video.currentTime(), |
16 |
| - hr = ~~( sec / 3600 ), |
17 |
| - mn = ~~( sec / 60 ) % 60, |
18 |
| - sc = sec % 60, |
19 |
| - wasPlaying = api.video.isPlaying |
| 12 | + w, h, |
| 13 | + dur, hr, mn, sc, |
| 14 | + file, canvas |
20 | 15 | ;
|
21 | 16 |
|
22 |
| - if ( w && h ) { |
| 17 | + if ( api.isLoaded ) { |
| 18 | + file = api.playlist.selectedFile(); |
| 19 | + |
| 20 | + if ( file.type === "audio" ) { |
| 21 | + canvas = dom.screenCanvas[ 0 ]; |
| 22 | + } else { |
| 23 | + canvas = canvasCapture; |
| 24 | + w = canvas.width = api.videoElement.videoWidth; |
| 25 | + h = canvas.height = api.videoElement.videoHeight; |
| 26 | + ctx.drawImage( api.videoElement, 0, 0, w, h ); |
| 27 | + } |
| 28 | + |
| 29 | + // Write the specific time in the screenshot's name. |
| 30 | + // Be careful by not adding any forbidden char like ":>/" etc. |
| 31 | + dur = api.video.currentTime(); |
| 32 | + hr = ~~( dur / 3600 ); |
| 33 | + mn = ~~( dur / 60 ) % 60; |
| 34 | + sc = dur % 60; |
23 | 35 | if ( mn < 10 ) {
|
24 | 36 | mn = "0" + mn;
|
25 | 37 | }
|
26 | 38 | sc = ( sc < 10 ? "0" : "" ) + sc.toFixed( 2 );
|
27 |
| - canvas.width = w; |
28 |
| - canvas.height = h; |
29 | 39 |
|
30 |
| - // Pause the video to avoid any conflict and accelerate the screenshot. |
31 |
| - api.video.pause(); |
32 |
| - ctx.drawImage( video, 0, 0, w, h ); |
33 | 40 | dom.ctrlCaptureBtn.attr( {
|
34 | 41 | href: canvas.toDataURL(),
|
35 |
| - download: api.playlist.selectedFile().name.replace( /\s/g, "_" ) + |
| 42 | + download: file.name.replace( /\s/g, "_" ) + |
36 | 43 | "__at_" + hr + "h" + mn + "m" + sc + "s.png"
|
37 | 44 | });
|
38 |
| - api.video.playToggle( wasPlaying ); |
39 | 45 | }
|
40 | 46 | });
|
41 | 47 |
|
|
0 commit comments