UNPKG

canvas-size

Version:

Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.

10 lines (9 loc) 3.7 kB
/*! * canvas-size * v2.0.0 * https://github.com/jhildenbiddle/canvas-size * (c) 2015-2024 John Hildenbiddle <http://hildenbiddle.com> * MIT license */ function e(t){const s=t.sizes.shift(),n=Math.max(Math.ceil(s[0]),1),i=Math.max(Math.ceil(s[1]),1),o=[n-1,i-1,1,1],h=performance.now(),r="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope;let a,c;r?(a=new OffscreenCanvas(1,1),c=new OffscreenCanvas(n,i)):(a=document.createElement("canvas"),a.width=1,a.height=1,c=document.createElement("canvas"),c.width=n,c.height=i);const m=a.getContext("2d"),d=c.getContext("2d");d&&(d.fillRect.apply(d,o),m.drawImage(c,n-1,i-1,1,1,0,0,1,1));const l=m&&0!==m.getImageData(0,0,1,1).data[3],g=parseInt(performance.now()-h);return[a,c].forEach((e=>{e.height=0,e.width=0})),r?(postMessage({width:n,height:i,testTime:g,isTestPass:l}),!l&&t.sizes.length&&setTimeout((()=>{e(t)}),0)):l?t.onSuccess({width:n,height:i,testTime:g}):(t.onError({width:n,height:i,testTime:g}),t.sizes.length&&setTimeout((()=>{e(t)}),0)),l}const t={area:[16384,14188,11402,11180,10836,8192,4096,1],height:[8388607,65535,32767,16384,8192,4096,1],width:[4194303,65535,32767,16384,8192,4096,1]},s={max:null,min:1,sizes:[],step:1024,useWorker:!1,onError:Function.prototype,onSuccess:Function.prototype},n={};function i(e){const t=e.width===e.height,n=1===e.height,i=1===e.width,o=[];if(e.width&&e.height){const h=e.min||s.min,r=e.step||s.step;let a=Math.max(e.width,e.height);for(;a>=h;){const e=t||n?a:1,s=t||i?a:1;o.push([e,s]),a-=r}}else e.sizes.forEach((e=>{const s=t||n?e:1,h=t||i?e:1;o.push([s,h])}));return o}function o(t){const s="undefined"!=typeof window,i=s&&"Promise"in window,o=s&&"HTMLCanvasElement"in window,h=s&&"OffscreenCanvas"in window,r=URL.createObjectURL(new Blob([])).slice(-36),a=performance.now(),{onError:c,onSuccess:m,...d}=t,l=()=>parseInt(performance.now()-a);let g=null;if(!o)return!1;if(t.useWorker&&h){const t=`\n var canvasTest = ${e.toString()};\n onmessage = function(e) {\n canvasTest(e.data);\n };\n `,s=new Blob([t],{type:"application/javascript"}),i=URL.createObjectURL(s);g=new Worker(i),URL.revokeObjectURL(i),g.onmessage=function(e){const{width:t,height:s,testTime:i,isTestPass:o}=e.data,h={width:t,height:s,testTime:i,totalTime:l()};o?(n[r].onSuccess(h),delete n[r]):n[r].onError(h)}}return i?new Promise((s=>{const i={...t,onError(e){let{width:n,height:i,testTime:o}=e;const h={width:n,height:i,testTime:o,totalTime:l()};let r;if(0===t.sizes.length)r=!0;else{const[[e,s]]=t.sizes.slice(-1);r=n===e&&i===s}c(h),r&&s({...h,success:!1})},onSuccess(e){let{width:t,height:n,testTime:i}=e;const o={width:t,height:n,testTime:i,totalTime:l()};m(o),s({...o,success:!0})}};if(g){const{onError:e,onSuccess:t}=i;n[r]={onError:e,onSuccess:t},g.postMessage(d)}else e(i)})):g?(n[r]={onError:c,onSuccess:m},void g.postMessage(d)):e(t)}const h={maxArea(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};const n=i({width:e.max,height:e.max,min:e.min,step:e.step,sizes:[...t.area]});return o({...s,...e,sizes:n})},maxHeight(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};const n=i({width:1,height:e.max,min:e.min,step:e.step,sizes:[...t.height]});return o({...s,...e,sizes:n})},maxWidth(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};const n=i({width:e.max,height:1,min:e.min,step:e.step,sizes:[...t.width]});return o({...s,...e,sizes:n})},test(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};const t={...s,...e};return t.sizes=[...t.sizes],t.width&&t.height&&(t.sizes=[[t.width,t.height]]),o(t)}};export{h as default}; //# sourceMappingURL=canvas-size.esm.min.js.map