/*! * artplayer-tool-thumbnail.js v4.0.0 * Github: https://github.com/zhw2590582/ArtPlayer#readme * (c) 2017-2022 Harvey Zack * Released under the MIT License. */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("tiny-emitter")):"function"==typeof define&&define.amd?define(["tiny-emitter"],e):(t="undefined"!=typeof globalThis?globalThis:t||self).ArtplayerToolThumbnail=e(t.Emitter)}(this,(function(t){"use strict";function e(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:{};return n(this,h),(t=s.call(this)).processing=!1,t.option={},t.setup(Object.assign({},h.DEFAULTS,e)),t.video=h.creatVideo(),t.duration=0,t.inputChange=t.inputChange.bind(o(t)),t.ondrop=t.ondrop.bind(o(t)),t.option.fileInput.addEventListener("change",t.inputChange),t.option.fileInput.addEventListener("dragover",h.ondragover),t.option.fileInput.addEventListener("drop",h.ondrop),t}return e=h,a=[{key:"ondrop",value:function(t){t.preventDefault();var e=t.dataTransfer.files[0];this.loadVideo(e)}},{key:"setup",value:function(){var t=this,e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.option=Object.assign({},this.option,e);var n=this.option,i=n.fileInput,o=n.number,r=n.width,a=n.column;if(this.errorHandle(i instanceof Element,"The 'fileInput' is not a Element"),"INPUT"!==i.tagName||"file"!==i.type){i.style.position="relative";var u=document.createElement("input");u.type="file",u.style.position="absolute",u.style.width="100%",u.style.height="100%",u.style.left="0",u.style.top="0",u.style.right="0",u.style.bottom="0",u.style.opacity="0",i.appendChild(u),this.option.fileInput=u}return["number","width","column","begin","end"].forEach((function(e){t.errorHandle("number"==typeof t.option[e],"The '".concat(e,"' is not a number"))})),this.option.number=l(o,10,1e3),this.option.width=l(r,10,1e3),this.option.column=l(a,1,1e3),this}},{key:"inputChange",value:function(t){var e=this.option.fileInput.files[0];this.loadVideo(e),t.target.value=""}},{key:"loadVideo",value:function(t){if(t){var e=this.video.canPlayType(t.type);this.errorHandle("maybe"===e||"probably"===e,"Playback of this file format is not supported: ".concat(t.type));var n=URL.createObjectURL(t);this.videoUrl=n,this.file=t,this.emit("file",this.file),this.video.src=n,this.emit("video",this.video)}}},{key:"start",value:function(){var t,e=this;if(!this.video.duration)return(t=1e3,new Promise((function(e){return setTimeout(e,t)}))).then((function(){return e.start()}));var n=this.option,i=n.width,o=n.number,r=n.begin,a=n.end,u=this.video.videoHeight/this.video.videoWidth*i;this.option.height=u,this.option.begin=l(r,0,this.video.duration),this.option.end=l(a||this.video.duration,r,this.video.duration),this.errorHandle(this.option.end>this.option.begin,"End time must be greater than the start time"),this.duration=this.option.end-this.option.begin,this.density=o/this.duration,this.errorHandle(this.file&&this.video,"Please select the video file first"),this.errorHandle(!this.processing,"There is currently a task in progress, please wait a moment..."),this.errorHandle(this.density<=1,"The preview density cannot be greater than 1, but got ".concat(this.density));var s=this.creatScreenshotDate(),c=this.creatCanvas(),h=c.getContext("2d");this.emit("canvas",c);var d,f=s.map((function(t,n){return function(){return new Promise((function(r){e.video.oncanplay=function(){h.drawImage(e.video,t.x,t.y,i,u),c.toBlob((function(t){e.thumbnailUrl&&URL.revokeObjectURL(e.thumbnailUrl),e.thumbnailUrl=URL.createObjectURL(t),e.emit("update",e.thumbnailUrl,(n+1)/o),e.video.oncanplay=null,r()}))},e.video.currentTime=t.time}))}}));return this.processing=!0,(d=f,d.reduce((function(t,e){return t.then(e)}),Promise.resolve())).then((function(){e.processing=!1,e.emit("done")})).catch((function(t){throw e.processing=!1,e.emit("error",t.message),t}))}},{key:"creatScreenshotDate",value:function(){for(var t=this.option,e=t.number,n=t.width,i=t.height,o=t.column,r=t.begin,a=this.duration/e,u=[r+a];u.length