UNPKG

canvas-size

Version:

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

10 lines (9 loc) 6.83 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){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).canvasSize=t()}(this,(function(){"use strict";function e(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function t(t){for(var r=1;r<arguments.length;r++){var i=null!=arguments[r]?arguments[r]:{};r%2?e(Object(i),!0).forEach((function(e){n(t,e,i[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(i)):e(Object(i)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(i,e))}))}return t}function r(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}function n(e,t,n){return(t=r(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e,t){if(null==e)return{};var r,n,i=function(e,t){if(null==e)return{};var r,n,i={},o=Object.keys(e);for(n=0;n<o.length;n++)r=o[n],t.indexOf(r)>=0||(i[r]=e[r]);return i}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(n=0;n<o.length;n++)r=o[n],t.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(e,r)&&(i[r]=e[r])}return i}function o(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,i,o,s,a=[],c=!0,u=!1;try{if(o=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;c=!1}else for(;!(c=(n=o.call(r)).done)&&(a.push(n.value),a.length!==t);c=!0);}catch(e){u=!0,i=e}finally{try{if(!c&&null!=r.return&&(s=r.return(),Object(s)!==s))return}finally{if(u)throw i}}return a}}(e,t)||a(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function s(e){return function(e){if(Array.isArray(e))return c(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||a(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(e,t){if(e){if("string"==typeof e)return c(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?c(e,t):void 0}}function c(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function u(e){var t,r,n=e.sizes.shift(),i=Math.max(Math.ceil(n[0]),1),o=Math.max(Math.ceil(n[1]),1),s=[i-1,o-1,1,1],a=performance.now(),c="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope;c?(t=new OffscreenCanvas(1,1),r=new OffscreenCanvas(i,o)):((t=document.createElement("canvas")).width=1,t.height=1,(r=document.createElement("canvas")).width=i,r.height=o);var f=t.getContext("2d"),l=r.getContext("2d");l&&(l.fillRect.apply(l,s),f.drawImage(r,i-1,o-1,1,1,0,0,1,1));var h=f&&0!==f.getImageData(0,0,1,1).data[3],m=parseInt(performance.now()-a);return[t,r].forEach((function(e){e.height=0,e.width=0})),c?(postMessage({width:i,height:o,testTime:m,isTestPass:h}),!h&&e.sizes.length&&setTimeout((function(){u(e)}),0)):h?e.onSuccess({width:i,height:o,testTime:m}):(e.onError({width:i,height:o,testTime:m}),e.sizes.length&&setTimeout((function(){u(e)}),0)),h}var f=[16384,14188,11402,11180,10836,8192,4096,1],l=[8388607,65535,32767,16384,8192,4096,1],h=[4194303,65535,32767,16384,8192,4096,1],m=["onError","onSuccess"],p={max:null,min:1,sizes:[],step:1024,useWorker:!1,onError:Function.prototype,onSuccess:Function.prototype},d={};function g(e){var t=e.width===e.height,r=1===e.height,n=1===e.width,i=[];if(e.width&&e.height)for(var o=e.min||p.min,s=e.step||p.step,a=Math.max(e.width,e.height);a>=o;){var c=t||r?a:1,u=t||n?a:1;i.push([c,u]),a-=s}else e.sizes.forEach((function(e){var o=t||r?e:1,s=t||n?e:1;i.push([o,s])}));return i}function v(e){var r="undefined"!=typeof window,n=r&&"Promise"in window,s=r&&"HTMLCanvasElement"in window,a=r&&"OffscreenCanvas"in window,c=URL.createObjectURL(new Blob([])).slice(-36),f=performance.now(),l=e.onError,h=e.onSuccess,p=i(e,m),g=function(){return parseInt(performance.now()-f)},v=null;if(!s)return!1;if(e.useWorker&&a){var y="\n var canvasTest = ".concat(u.toString(),";\n onmessage = function(e) {\n canvasTest(e.data);\n };\n "),b=new Blob([y],{type:"application/javascript"}),w=URL.createObjectURL(b);v=new Worker(w),URL.revokeObjectURL(w),v.onmessage=function(e){var t=e.data,r=t.width,n=t.height,i=t.testTime,o=t.isTestPass,s={width:r,height:n,testTime:i,totalTime:g()};o?(d[c].onSuccess(s),delete d[c]):d[c].onError(s)}}return n?new Promise((function(r){var n=t(t({},e),{},{onError:function(n){var i,s=n.width,a=n.height,c={width:s,height:a,testTime:n.testTime,totalTime:g()};if(0===e.sizes.length)i=!0;else{var u=o(e.sizes.slice(-1),1),f=o(u[0],2),h=f[0],m=f[1];i=s===h&&a===m}l(c),i&&r(t(t({},c),{},{success:!1}))},onSuccess:function(e){var n={width:e.width,height:e.height,testTime:e.testTime,totalTime:g()};h(n),r(t(t({},n),{},{success:!0}))}});if(v){var i=n.onError,s=n.onSuccess;d[c]={onError:i,onSuccess:s},v.postMessage(p)}else u(n)})):v?(d[c]={onError:l,onSuccess:h},void v.postMessage(p)):u(e)}var y={maxArea:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=g({width:e.max,height:e.max,min:e.min,step:e.step,sizes:s(f)});return v(t(t(t({},p),e),{},{sizes:r}))},maxHeight:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=g({width:1,height:e.max,min:e.min,step:e.step,sizes:s(l)});return v(t(t(t({},p),e),{},{sizes:r}))},maxWidth:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=g({width:e.max,height:1,min:e.min,step:e.step,sizes:s(h)});return v(t(t(t({},p),e),{},{sizes:r}))},test:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=t(t({},p),e);return r.sizes=s(r.sizes),r.width&&r.height&&(r.sizes=[[r.width,r.height]]),v(r)}};return y})); //# sourceMappingURL=canvas-size.min.js.map