-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
73 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,44 @@ | ||
import { cloneCanvas } from './clone-canvas' | ||
import { consoleWarn, createImage } from './utils' | ||
import { consoleWarn, createImage, loadMedia } from './utils' | ||
|
||
export function cloneVideo<T extends HTMLVideoElement>( | ||
export async function cloneVideo<T extends HTMLVideoElement>( | ||
video: T, | ||
): HTMLCanvasElement | HTMLImageElement | HTMLVideoElement { | ||
if (video.ownerDocument) { | ||
const ownerDocument = video.ownerDocument | ||
): Promise<HTMLCanvasElement | HTMLImageElement | HTMLVideoElement> { | ||
if ( | ||
video.ownerDocument | ||
&& !video.currentSrc | ||
&& video.poster | ||
) { | ||
return createImage(video.poster, video.ownerDocument) | ||
} | ||
|
||
if (video.currentSrc && video.currentTime) { | ||
const canvas = ownerDocument.createElement('canvas') | ||
canvas.width = video.offsetWidth | ||
canvas.height = video.offsetHeight | ||
try { | ||
const ctx = canvas.getContext('2d') | ||
if (ctx) ctx.drawImage(video, 0, 0, canvas.width, canvas.height) | ||
} catch (error) { | ||
consoleWarn('Failed to clone video', error) | ||
} | ||
return cloneCanvas(canvas) | ||
} | ||
video.currentTime = 3 | ||
|
||
const clone = video.cloneNode(false) as T | ||
clone.crossOrigin = 'anonymous' | ||
if (video.currentSrc && video.currentSrc !== video.src) { | ||
clone.src = video.currentSrc | ||
} | ||
|
||
if (video.poster) { | ||
return createImage(video.poster, ownerDocument, true) | ||
// video to canvas | ||
const ownerDocument = clone.ownerDocument | ||
if (ownerDocument) { | ||
await loadMedia(clone) | ||
clone.currentTime = video.currentTime | ||
await new Promise(resolve => { | ||
clone.addEventListener('seeked', resolve, { once: true }) | ||
}) | ||
const canvas = ownerDocument.createElement('canvas') | ||
canvas.width = video.offsetWidth | ||
canvas.height = video.offsetHeight | ||
try { | ||
const ctx = canvas.getContext('2d') | ||
if (ctx) ctx.drawImage(clone, 0, 0, canvas.width, canvas.height) | ||
} catch (error) { | ||
consoleWarn('Failed to clone video', error) | ||
} | ||
return cloneCanvas(canvas) | ||
} | ||
|
||
return video.cloneNode(false) as T | ||
return clone | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters