Created
April 12, 2019 20:48
-
-
Save schmidtsonian/ffe04a8dc21f3d8c2fe51746266228c6 to your computer and use it in GitHub Desktop.
Creates video and load it
This file contains hidden or 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
/** | |
* 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