/*! * artplayer-tool-thumbnail.js v4.4.0 * Github: https://github.com/zhw2590582/ArtPlayer * (c) 2017-2026 Harvey Zhao * Released under the MIT License. */ !function(t,e){"object"===typeof exports&&"undefined"!==typeof module?module.exports=e():"function"==typeof define&&define.amd?(t.ArtplayerToolThumbnail=e(),define(function(){return t.ArtplayerToolThumbnail})):(t="undefined"!==typeof globalThis?globalThis:t||self).ArtplayerToolThumbnail=e()}(this,function(){"use strict";class t{on(t,e,i){const n=this.e||(this.e={});return(n[t]||(n[t]=[])).push({fn:e,ctx:i}),this}once(t,e,i){const n=this;function o(...s){n.off(t,o),e.apply(i,s)}return o._=e,this.on(t,o,i)}emit(t,...e){const i=((this.e||(this.e={}))[t]||[]).slice();for(let n=0;nsetTimeout(e,t))).then(()=>this.start());var t;const{width:i,number:n,begin:o,end:s}=this.option,r=this.video.videoHeight/this.video.videoWidth*i;this.option.height=r,this.option.begin=e(o,0,this.video.duration),this.option.end=e(s||this.video.duration,o,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=n/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 ${this.density}`);const h=this.creatScreenshotDate(),a=this.creatCanvas(),l=a.getContext("2d");this.emit("canvas",a);const c=h.map((t,e)=>()=>new Promise(o=>{this.video.oncanplay=()=>{l.drawImage(this.video,t.x,t.y,i,r),a.toBlob(t=>{this.thumbnailUrl&&URL.revokeObjectURL(this.thumbnailUrl),this.thumbnailUrl=URL.createObjectURL(t),this.emit("update",this.thumbnailUrl,(e+1)/n),this.video.oncanplay=null,o()})},this.video.currentTime=t.time}));return this.processing=!0,(d=c,d.reduce((t,e)=>t.then(e),Promise.resolve())).then(()=>{this.processing=!1,this.emit("done")}).catch(t=>{throw this.processing=!1,this.emit("error",t.message),t});var d}creatScreenshotDate(){return function(t,e){const{number:i,width:n,height:o,column:s,begin:r}=t,h=e/i,a=[r+h];for(;a.length({time:t-h/2,x:e%s*n,y:Math.floor(e/s)*o}))}(this.option,this.duration)}creatCanvas(){return function(t){const{number:e,width:i,height:n,column:o}=t,s=document.createElement("canvas"),r=s.getContext("2d");return s.width=i*o,s.height=Math.ceil(e/o)*n+30,r.fillStyle="black",r.fillRect(0,0,s.width,s.height),r.font="14px Georgia",r.fillStyle="#fff",r.fillText(`From: https://artplayer.org/, Number: ${e}, Width: ${i}, Height: ${n}, Column: ${o}`,10,s.height-11),s}(this.option)}download(){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...");const t=function(t,e){const i=document.createElement("a"),n=`${function(t){const e=t.split(".");return e.pop(),e.join(".")}(t.name)}.png`;i.download=n,i.href=e;try{document.body.appendChild(i),i.click()}finally{i.parentNode&&i.parentNode.removeChild(i)}return n}(this.file,this.thumbnailUrl);return this.emit("download",t),this}errorHandle(t,e){if(!t)throw this.emit("error",e),new Error(e)}destroy(){if(r.has(this))return;let t;r.add(this);for(const i of[()=>s(this),()=>this.video.parentNode?.removeChild(this.video),()=>this.videoUrl&&URL.revokeObjectURL(this.videoUrl),()=>this.thumbnailUrl&&URL.revokeObjectURL(this.thumbnailUrl),()=>this.emit("destroy")])try{i()}catch(e){t||(t=e)}if(t)throw t}}return h});