Skip to content

Instantly share code, notes, and snippets.

@schmidtsonian
Created April 12, 2019 20:48
Show Gist options
  • Save schmidtsonian/ffe04a8dc21f3d8c2fe51746266228c6 to your computer and use it in GitHub Desktop.
Save schmidtsonian/ffe04a8dc21f3d8c2fe51746266228c6 to your computer and use it in GitHub Desktop.
Creates video and load it
/**
* Creates video and load it
* @param {Object} videoAttrs - Video element attributes
* @param {Object} sourceAttrs - Source element attributes
* @returns {Promise} {elm: video}
* @example
* loadVideo({autoPlay: false, controls: true}, {src: 'foo.webm', type: 'video/mp4'})
* .then(obj => console.log(obj.elem));
*/
export function loadVideo(videoAttrs, sourceAttrs) {
const p = new Promise((resolve) => {
const video = document.createElement('video');
const source = document.createElement('source');
// Resolve promise when data is loaded and pass the video elem
video.onloadeddata = resolve({
elem: video
});
// Set video sourceAttrs
for (const attr in videoAttrs) {
if (attr) {
video.setAttribute(attr, videoAttrs[attr]);
}
}
// Set video sourceAttrs
for (const attr in sourceAttrs) {
if (attr) {
source.setAttribute(attr, sourceAttrs[attr]);
}
}
video.appendChild(source);
});
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment