mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-08 19:06:15 -08:00
9 lines
8.4 KiB
JavaScript
9 lines
8.4 KiB
JavaScript
/*!
|
|
* artplayer-tool-thumbnail.js v4.0.0
|
|
* Github: https://github.com/zhw2590582/ArtPlayer#readme
|
|
* (c) 2017-2021 Harvey Zack
|
|
* Released under the MIT License.
|
|
*/
|
|
|
|
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).ArtplayerToolThumbnail=e()}(this,(function(){"use strict";function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function e(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}function n(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function i(t,e){return i=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},i(t,e)}function o(t){return o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o(t)}function r(t,e){if(e&&("object"===o(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return n(t)}function a(t){return a=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},a(t)}var s={exports:{}};function u(){}function h(t){return new Promise((function(e){return setTimeout(e,t)}))}function l(t,e,n){return Math.max(Math.min(t,Math.max(e,n)),Math.min(e,n))}function c(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}();return function(){var n,i=a(t);if(e){var o=a(this).constructor;n=Reflect.construct(i,arguments,o)}else n=i.apply(this,arguments);return r(this,n)}}u.prototype={on:function(t,e,n){var i=this.e||(this.e={});return(i[t]||(i[t]=[])).push({fn:e,ctx:n}),this},once:function(t,e,n){var i=this;function o(){i.off(t,o),e.apply(n,arguments)}return o._=e,this.on(t,o,n)},emit:function(t){for(var e=[].slice.call(arguments,1),n=((this.e||(this.e={}))[t]||[]).slice(),i=0,o=n.length;i<o;i++)n[i].fn.apply(n[i].ctx,e);return this},off:function(t,e){var n=this.e||(this.e={}),i=n[t],o=[];if(i&&e)for(var r=0,a=i.length;r<a;r++)i[r].fn!==e&&i[r].fn._!==e&&o.push(i[r]);return o.length?n[t]=o:delete n[t],this}},s.exports=u,s.exports.TinyEmitter=u;var f=function(o){!function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&i(t,e)}(f,o);var r,a,s,u=c(f);function f(){var e,i=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};return t(this,f),(e=u.call(this)).processing=!1,e.option={},e.setup(Object.assign({},f.DEFAULTS,i)),e.video=f.creatVideo(),e.duration=0,e.inputChange=e.inputChange.bind(n(e)),e.ondrop=e.ondrop.bind(n(e)),e.option.fileInput.addEventListener("change",e.inputChange),e.option.fileInput.addEventListener("dragover",f.ondragover),e.option.fileInput.addEventListener("drop",f.ondrop),e}return r=f,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.delay,r=n.number,a=n.width,s=n.height,u=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 h=document.createElement("input");h.type="file",h.style.position="absolute",h.style.width="100%",h.style.height="100%",h.style.left="0",h.style.top="0",h.style.right="0",h.style.bottom="0",h.style.opacity="0",i.appendChild(h),this.option.fileInput=h}return["delay","number","width","height","column","begin","end"].forEach((function(e){t.errorHandle("number"==typeof t.option[e],"The '".concat(e,"' is not a number"))})),this.option.delay=l(o,10,1e3),this.option.number=l(r,10,1e3),this.option.width=l(a,10,1e3),this.option.height=l(s,10,1e3),this.option.column=l(u,1,1e3),this}},{key:"inputChange",value:function(){var t=this.option.fileInput.files[0];this.loadVideo(t)}},{key:"loadVideo",value:function(t){var e=this,n=this.option.delay;if(t){var i=this.video.canPlayType(t.type);this.errorHandle("maybe"===i||"probably"===i,"Playback of this file format is not supported: ".concat(t.type));var o=URL.createObjectURL(t);this.videoUrl=o,this.file=t,this.emit("file",this.file),this.video.src=o,h(n).then((function(){e.emit("video",e.video)})).catch((function(t){throw e.emit("error",t.message),t}))}}},{key:"start",value:function(){var t=this;if(!this.video.duration)return h(1e3).then(this.start);var e=this.option,n=e.width,i=e.height,o=e.number,r=e.delay,a=e.begin,s=e.end;this.option.begin=l(a,0,this.video.duration),this.option.end=l(s||this.video.duration,a,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 u=this.creatScreenshotDate(),c=this.creatCanvas(),f=c.getContext("2d");this.emit("canvas",c);var p,d=u.map((function(e,a){return function(){return t.video.currentTime=e.time,new Promise((function(s){h(r).then((function(){f.drawImage(t.video,e.x,e.y,n,i),c.toBlob((function(e){t.thumbnailUrl&&URL.revokeObjectURL(t.thumbnailUrl),t.thumbnailUrl=URL.createObjectURL(e),t.emit("update",t.thumbnailUrl,(a+1)/o),s()}))})).catch((function(t){throw t}))}))}}));return this.processing=!0,(p=d,p.reduce((function(t,e){return t.then(e)}),Promise.resolve())).then((function(){return h(2*r).then((function(){t.processing=!1,t.emit("done")})).catch((function(e){throw t.processing=!1,t.emit("error",e.message),e}))})).catch((function(e){throw t.processing=!1,t.emit("error",e.message),e}))}},{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,s=[r+a];s.length<e;){var u=s[s.length-1];s.push(u+a)}return s.map((function(t,e){return{time:t-a/2,x:e%o*n,y:Math.floor(e/o)*i}}))}},{key:"creatCanvas",value:function(){var t=this.option,e=t.number,n=t.width,i=t.height,o=t.column,r=document.createElement("canvas"),a=r.getContext("2d");return r.width=n*o,r.height=Math.ceil(e/o)*i+30,a.fillStyle="black",a.fillRect(0,0,r.width,r.height),a.font="14px Georgia",a.fillStyle="#fff",a.fillText("From: https://artplayer.org/, Number: ".concat(e,", Width: ").concat(n,", Height: ").concat(i,", Column: ").concat(o),10,r.height-11),r}},{key:"download",value:function(){this.errorHandle(this.file&&this.thumbnailUrl,"Download does not seem to be ready, please create preview first"),this.errorHandle(!this.processing,"There is currently a task in progress, please wait a moment...");var t=document.createElement("a"),e="".concat(function(t){var e=t.split(".");return e.pop(),e.join(".")}(this.file.name),".png");return t.download=e,t.href=this.thumbnailUrl,document.body.appendChild(t),t.click(),document.body.removeChild(t),this.emit("download",e),this}},{key:"errorHandle",value:function(t,e){if(!t)throw this.emit("error",e),new Error(e)}},{key:"destroy",value:function(){this.option.fileInput.removeEventListener("change",this.inputChange),this.option.fileInput.removeEventListener("dragover",f.ondragover),this.option.fileInput.removeEventListener("drop",f.ondrop),document.body.removeChild(this.video),this.videoUrl&&URL.revokeObjectURL(this.videoUrl),this.thumbnailUrl&&URL.revokeObjectURL(this.thumbnailUrl),this.emit("destroy")}}],s=[{key:"DEFAULTS",get:function(){return{delay:300,number:60,width:160,height:90,column:10,begin:0,end:NaN}}},{key:"ondragover",value:function(t){t.preventDefault()}},{key:"creatVideo",value:function(){var t=document.createElement("video");return t.style.position="absolute",t.style.top="-9999px",t.style.left="-9999px",t.muted=!0,t.controls=!0,document.body.appendChild(t),t}}],a&&e(r.prototype,a),s&&e(r,s),f}(s.exports);return f}));
|