lazy-widgets
Version:
Typescript retained mode GUI for the HTML canvas API
43 lines • 1.82 kB
JavaScript
import { AsyncImageBitmap } from './AsyncImageBitmap.js';
export function getBackingMediaSourceType(media) {
if (media instanceof Element) {
if (media.namespaceURI === 'http://www.w3.org/1999/xhtml') {
switch (media.tagName) {
case 'IMG': return 0 /* BackingMediaSourceType.HTMLImageElement */;
case 'VIDEO': return 1 /* BackingMediaSourceType.HTMLVideoElement */;
case 'CANVAS': return 5 /* BackingMediaSourceType.Immediate */;
}
}
else if (media.namespaceURI === 'http://www.w3.org/2000/svg') {
switch (media.tagName) {
case 'IMAGE': return 2 /* BackingMediaSourceType.SVGImageElement */;
}
}
}
else if (media instanceof AsyncImageBitmap) {
return 4 /* BackingMediaSourceType.AsyncImageBitmap */;
}
else if (media instanceof VideoFrame) {
return 3 /* BackingMediaSourceType.VideoFrame */;
}
return 5 /* BackingMediaSourceType.Immediate */;
}
const VIDEO_REGEX = /^.*\.(webm|og[gv]|m(p4|4v|ov)|avi|qt)$/i;
export function urlToBackingMediaSource(url) {
if (VIDEO_REGEX.test(url)) {
const videoElem = document.createElement('video');
videoElem.crossOrigin = 'anonymous';
videoElem.src = url;
// So that video poster shows. If you're passing your own video element
// then this won't be automatically set
videoElem.preload = 'auto';
return [videoElem, 1 /* BackingMediaSourceType.HTMLVideoElement */];
}
else {
const imgElem = document.createElement('img');
imgElem.crossOrigin = 'anonymous';
imgElem.src = url;
return [imgElem, 0 /* BackingMediaSourceType.HTMLImageElement */];
}
}
//# sourceMappingURL=BackingMediaSource.js.map