dear-image
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -1,1 +0,1 @@ | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(t=t||self).DearImage=n()}(this,function(){"use strict";var t=function(t){this.context=t},n={canvas:{configurable:!0},sizeX:{configurable:!0},sizeY:{configurable:!0},size:{configurable:!0},minSize:{configurable:!0},maxSize:{configurable:!0}};function e(){return document.createElement("canvas")}function r(){return new OffscreenCanvas}function i(){try{return e()}catch(t){}try{return r()}catch(t){}try{return new(0,require("canvas").Canvas)}catch(t){}throw new Error}function a(t){return Number.isFinite(t)}function s(t){return a(t)&&t>0}n.canvas.get=function(){return this.context.canvas},n.sizeX.get=function(){return this.canvas.width},n.sizeY.get=function(){return this.canvas.height},n.size.get=function(){return{x:this.sizeX,y:this.sizeY}},n.minSize.get=function(){return Math.min(this.sizeX,this.sizeY)},n.maxSize.get=function(){return Math.max(this.sizeX,this.sizeY)},Object.defineProperties(t.prototype,n),t.blank=function(t,n){t=s(t)?Math.round(t):0,n=s(n)?Math.round(n):0;var e=i();return e.width=t,e.height=n,new this(e.getContext("2d"))},t.isDearImage=(t.is=function(t){return t instanceof this}).bind(t);var o="undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:void 0;function u(t){return function(t){var n=o.HTMLCanvasElement;return!!n&&t instanceof n}(t)||function(t){var n=o.OffscreenCanvas;return!!n&&t instanceof n}(t)||function(t){try{return t instanceof require("canvas").Canvas}catch(t){}return!1}(t)}function c(){}function f(t){return"string"==typeof t}var h=/^data:(.*?)(;base64)?,(.*)$/;function l(t){if(f(t)){var n=h.exec(t);if(n){var e=n[1],r=n[3];return Object.assign(new c,{type:e,data:r,isEmpty:function(){return!r},toString:function(){return t}})}}return!1}var v={is:function(t){return t instanceof c},isString:function(t){return!!l(t)},parse:l};function p(){return new Image}function z(){try{return p()}catch(t){}try{return new(0,require("canvas").Image)}catch(t){}throw new Error}function m(t){return function(t){var n=o.HTMLImageElement;return!!n&&t instanceof n}(t)||function(t){try{return t instanceof require("canvas").Image}catch(t){}return!1}(t)}function d(t){return new Promise(function(n){n(t())})}function g(t){var n=o.ImageData;return!!n&&t instanceof n||function(t){try{return t instanceof require("canvas").ImageData}catch(t){}return!1}(t)}function y(t){var n=o.URL;return!!n&&t instanceof n}var w=function(t){var n=this.blank(t.width,t.height);return n.sizeX&&n.sizeY&&n.context.putImageData(t,0,0),n},b=function(t,n,e){var r=this.blank(n,e);return r.sizeX&&r.sizeY&&r.context.drawImage(t,0,0),r},x=function(t){return b.call(this,t,t.width,t.height)},X=function(t){return b.call(this,t,t.naturalWidth,t.naturalHeight)},Y=function(t){return x.call(this,t.canvas)},M=function(n){if(t.is(n))return Y.call(this,n);if(g(n))return w.call(this,n);if(u(n))return x.call(this,n);if(m(n))return X.call(this,n);throw new TypeError},I=function(t){var n,e=this;return(n=t,d(function(){if(!n.complete)return new Promise(function(t,e){n.onload=t,n.onerror=e}).finally(function(){n.onload=null,n.onerror=null})})).then(function(){return X.call(e,t)})},L=function(t){var n=z();return n.src=t,I.call(this,n)},S=function(t){return t.length?L.call(this,t):this.blank()},C=function(t){var n=this;if(!t.size)return this.blank();var e=URL.createObjectURL(t);return d(function(){return L.call(n,e)}).finally(function(){URL.revokeObjectURL(e)})},E=function(t){return t.isEmpty()?this.blank():L.call(this,""+t)},O=function(t){var n=v.parse(t);return n?E.call(this,n):L.call(this,t)},U=function(t){return L.call(this,""+t)};function k(t){if(t){var n=typeof t;return"object"===n||"function"===n}return!1}function D(){}Object.assign(t,{from:M,loadFrom:function(t){var n=this;return d(function(){return f(t)?O.call(n,t):y(t)?U.call(n,t):function(t){var n=o.Buffer;return!!n&&t instanceof n}(t)?S.call(n,t):function(t){var n=o.Blob;return!!n&&t instanceof n}(t)?C.call(n,t):m(t)?I.call(n,t):M.call(n,t)})}}),t.fromExcept=function(t){return this.is(t)?t:this.from(t)},t.loadFromExcept=function(t){var n=this;return d(function(){return n.is(t)?t:n.loadFrom(t)})};var j="sans-serif",R="normal";function T(t){return f(t)&&!function(t){return!t}(t)}var B=new Set([R,"italic","oblique"]);var q=new Set([R,"small-caps"]);function F(t,n,e,r,i){return(r?t>=n:t>n)&&(i?t<=e:t<e)}function H(t){for(var n=[],e=arguments.length-1;e-- >0;)n[e]=arguments[e+1];return function(t){return"number"==typeof t&&!Number.isNaN(t)}(t)&&F.apply(void 0,[t].concat(n))}var P=new Set([R,"bold"]);function N(t){var n=j,e=R,r=R,i=R;return k(t)&&(n=function(t){return T(t)?t:j}(t.family),e=function(t){return T(t)&&(t=t.toLowerCase(),B.has(t))?t:R}(t.style),r=function(t){return T(t)&&(t=t.toLowerCase(),q.has(t))?t:R}(t.variant),i=function(t){if(T(t)){if(t=t.toLowerCase(),P.has(t))return t}else if(H(t,1,1e3,!0,!0))return t=Math.round(t);return R}(t.weight)),Object.assign(new D,{family:n,style:e,variant:r,weight:i})}var A={default:N(),from:N,fromExcept:function(t){return function(t){return t instanceof D}(t)?t:N(t)}};function W(){}var $=10;function G(t){return a(t)&&!function(t){return t<0}(t)}function J(t){var n=A.from(t),e=n.family,r=n.style,i=n.variant,a=n.weight,s=$;return k(t)&&(s=function(t){return G(t)?t=Math.round(t):$}(t.size)),Object.assign(new W,{family:e,size:s,style:r,variant:i,weight:a,toCSS:function(){return[r,i,a,s+"px",'"'+e+'"'].join(" ")}})}var K={default:J(),from:J,fromExcept:function(t){return function(t){return t instanceof W}(t)?t:J(t)}};function Q(t){return function(t){return void 0===t}(t)||function(t){return null===t}(t)}function V(t){for(var n=[],e=arguments.length-1;e-- >0;)n[e]=arguments[e+1];return function(){for(var e=[],r=arguments.length;r--;)e[r]=arguments[r];return t.call.apply(t,[this].concat(n,e))}}t.measureText=function(t,n){t=Q(t)?"":""+t;var e=this.blank().context;return e.font=K.fromExcept(n).toCSS(),e.measureText(t)},t.prototype.crop=function(t,n,e,r){if(a(t)?(t=Math.round(t))<0&&(t+=this.sizeX):t=0,a(n)?(n=Math.round(n))<0&&(n+=this.sizeY):n=0,a(e)?(e=Math.round(e))<0&&(t+=e,e*=-1):e=this.sizeX,a(r)?(r=Math.round(r))<0&&(n+=r,r*=-1):r=this.sizeY,t||n||e!==this.sizeX||r!==this.sizeY){var i=this.constructor.blank(e,r);return e&&r&&this.sizeX&&this.sizeY&&i.context.drawImage(this.canvas,-t,-n),i}return this};var Z=function(t,n){if(this.sizeX&&this.sizeY){var e=this.constructor.blank(this.sizeX,this.sizeY),r=e.context;return r.save(),r.translate(t?this.sizeX:0,n?this.sizeY:0),r.scale(t?-1:1,n?-1:1),r.drawImage(this.canvas,0,0),r.restore(),e}return this};Object.assign(t.prototype,{flipX:V(Z,!0,!1),flipY:V(Z,!1,!0),rotate180:V(Z,!0,!0)}),t.prototype.reframe=function(t,n,e,r){var i=this;t=G(t)?Math.round(t):this.sizeX,n=G(n)?Math.round(n):this.sizeY;var a=function(){switch(e){case"start":return 0;case"end":return-t}return(i.sizeX+t)/-2}(),s=function(){switch(r){case"start":return 0;case"end":return-n}return(i.sizeY+n)/-2}();return this.crop(a,s,t,n)},t.prototype.resize=function(t,n){if(t=G(t)?Math.round(t):this.sizeX,n=G(n)?Math.round(n):this.sizeY,t!==this.sizeX||n!==this.sizeY){var e=this.constructor.blank(t,n);if(t&&n&&this.sizeX&&this.sizeY){var r=t/this.sizeX,i=n/this.sizeY,a=e.context;a.save(),a.scale(r,i),a.drawImage(this.canvas,0,0),a.restore()}return e}return this},t.prototype.rescale=function(t,n){var e=this.sizeX,r=this.sizeY;return a(t)&&(e*=t),a(n)&&(r*=n),this.resize(e,r)},t.prototype.resizeX=function(t,n){var e=this,r=function(){if(n){if(!e.sizeX)return 0;if(a(t))return t*e.sizeY/e.sizeX}return e.sizeY}();return this.resize(t,r)},t.prototype.resizeY=function(t,n){var e=this,r=function(){if(n){if(!e.sizeY)return 0;if(a(t))return t*e.sizeX/e.sizeY}return e.sizeX}();return this.resize(r,t)};var _=function(t){if(this.sizeX||this.sizeY){var n=this.constructor.blank(this.sizeY,this.sizeX);if(this.sizeX&&this.sizeY){var e=n.context;e.save(),e.translate(this.sizeY/2,this.sizeX/2),e.rotate(Math.PI/(t?2:-2)),e.drawImage(this.canvas,this.sizeX/-2,this.sizeY/-2),e.restore()}return n}return this};function tt(t){return t}Object.assign(t.prototype,{rotate90:V(_,!0),rotate270:V(_,!1)}),t.prototype.toBuffer=function(){for(var t,n=[],e=arguments.length;e--;)n[e]=arguments[e];return this.sizeX&&this.sizeY?(t=this.canvas).toBuffer.apply(t,n):Buffer.alloc(0)},t.prototype.saveToFileSystem=function(t){for(var n=this,e=[],r=arguments.length-1;r-- >0;)e[r]=arguments[r+1];return new Promise(function(r,i){var a,s=require("fs"),o=require("path"),u=(a=n).toBuffer.apply(a,e);s.mkdir(o.dirname(t),{recursive:!0},function(n){n?i(n):s.writeFile(t,u,function(t){t?i(t):r()})})})},t.prototype.scale=function(t){return this.rescale(t,t)};var nt=function(t,n,e,r){if(e=G(e)?Math.round(e):this.sizeX,r=G(r)?Math.round(r):this.sizeY,this.sizeX&&this.sizeY){var i=n(t(G(e)?e/this.sizeX:1,G(r)?r/this.sizeY:1),1);return this.scale(i)}return this.resize(e,r)};function et(t){for(var n=t.length,e=new Uint8Array(n),r=0;r<n;r++)e[r]=t.charCodeAt(r);return e}function rt(t){return!!function(t){if(f(t)){var n=o.URL;if(n){var e=o.document,r=e&&e.location&&e.location.origin;try{return new n(t,r)}catch(t){}}}return!1}(t)}return Object.assign(t.prototype,{scaleIn:V(nt,Math.min,tt),scaleOut:V(nt,Math.max,tt),scaleDownIn:V(nt,Math.min,Math.min),scaleDownOut:V(nt,Math.max,Math.min),scaleUpIn:V(nt,Math.min,Math.max),scaleUpOut:V(nt,Math.max,Math.max)}),t.prototype.toDataURL=function(){for(var t,n=[],e=arguments.length;e--;)n[e]=arguments[e];return(t=this.canvas).toDataURL.apply(t,n)},t.prototype.toBlob=function(){for(var t,n=[],e=arguments.length;e--;)n[e]=arguments[e];var r=(t=this).toDataURL.apply(t,n),i=v.parse(r),a=et(atob(i.data));return new Blob([a],{type:i.type})},t.prototype.toHTMLCanvasElement=function(){var t=e();return t.width=this.sizeX,t.height=this.sizeY,t.getContext("2d").drawImage(this.canvas,0,0),t},t.prototype.toHTMLImageElement=function(){for(var t,n=[],e=arguments.length;e--;)n[e]=arguments[e];var r=p();return r.src=(t=this).toDataURL.apply(t,n),r},t.prototype.toImageData=function(){return this.context.getImageData(0,0,this.sizeX,this.sizeY)},t.prototype.toOffscreenCanvas=function(){var t=r();return t.width=sizeX,t.height=sizeY,t.context.drawImage(this.canvas,0,0),t},t.text=function(t,n){t=Q(t)?"":""+t;var e="#000",r=K.default,i=.28;k(n)&&(T(n.fill)&&(e=n.fill),r=K.fromExcept(n.font),G(n.padding)&&(i=n.padding)),i=Math.ceil(i*r.size);var a=this.blank(this.measureText(t,r).width+2*i,r.size+2*i),s=a.context;return s.save(),s.font=r.toCSS(),s.textBaseline="top",s.textAlign="left",s.fillStyle=e,s.fillText(t,i,i),s.restore(),a},(t.utils={}).isDataURL=function(t){return v.is(t)||v.isString(t)},t.utils.isURL=function(t){return y(t)||rt(t)},t}); | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(t=t||self).DearImage=n()}(this,(function(){"use strict";var t=function(t){this.context=t},n={canvas:{configurable:!0},sizeX:{configurable:!0},sizeY:{configurable:!0}};function r(){try{return document.createElement("canvas")}catch(t){}throw new Error("HTMLCanvasElement is not supported.")}function e(){try{return new OffscreenCanvas}catch(t){}throw new Error("OffscreenCanvas is not supported.")}function i(){try{return r()}catch(t){}try{return e()}catch(t){}return function(){try{return new(0,require("canvas").Canvas)}catch(t){}throw new Error("Canvas is not supported.")}()}function a(t){return Number.isFinite(t)&&t>0}function s(t){return function(t){var n=globalThis.HTMLCanvasElement;return!!n&&t instanceof n}(t)||function(t){var n=globalThis.OffscreenCanvas;return!!n&&t instanceof n}(t)||function(t){try{return t instanceof require("canvas").Canvas}catch(t){}return!1}(t)}function o(t){return"string"==typeof t}n.canvas.get=function(){return this.context.canvas},n.sizeX.get=function(){return this.canvas.width},n.sizeY.get=function(){return this.canvas.height},Object.defineProperties(t.prototype,n),t.blank=function(t,n){t=a(t)?Math.round(t):0,n=a(n)?Math.round(n):0;var r=i();return r.width=t,r.height=n,new this(r.getContext("2d"))},t.is=function(t){return t instanceof this};var u=/^data:(.*?)(;base64)?,(.*)$/;var c={is:function(t){return t instanceof this},isString:function(t){return!!this.parse(t)},parse:function(t){if(o(t)){var n=u.exec(t);if(n){var r=n[1],e=n[3];return Object.assign(new this,{type:r,data:e,toString:function(){return t}})}}return!1}};var f={isEmpty:function(){return!this.data}},h=function(){return function(){}}(),l=h.prototype;function p(){try{return new Image}catch(t){}throw new Error("HTMLImageElement is not supported.")}function v(){try{return p()}catch(t){}return function(){try{return new(0,require("canvas").Image)}catch(t){}throw new Error("Image is not supported.")}()}function m(t){return function(t){var n=globalThis.HTMLImageElement;return!!n&&t instanceof n}(t)||function(t){try{return t instanceof require("canvas").Image}catch(t){}return!1}(t)}function g(t){return new Promise((function(n){n(t())}))}function d(t){var n=globalThis.ImageData;return!!n&&t instanceof n||function(t){try{return t instanceof require("canvas").ImageData}catch(t){}return!1}(t)}function y(t){var n=globalThis.URL;return!!n&&t instanceof n}Object.assign(h,c),Object.assign(l,f);var z=function(t){var n=this.blank(t.width,t.height);return n.sizeX&&n.sizeY&&n.context.putImageData(t,0,0),n},w=function(t,n,r){var e=this.blank(n,r);return e.sizeX&&e.sizeY&&e.context.drawImage(t,0,0),e},b=function(t){return w.call(this,t,t.width,t.height)},M=function(t){return w.call(this,t,t.naturalWidth,t.naturalHeight)},x=function(t){return b.call(this,t.canvas)},X=function(n){if(t.is(n))return x.call(this,n);if(d(n))return z.call(this,n);if(s(n))return b.call(this,n);if(m(n))return M.call(this,n);throw new Error},Y=function(t){var n,r=this;return(n=t,g((function(){if(!n.complete)return new Promise((function(t,r){n.onload=t,n.onerror=r})).finally((function(){n.onload=null,n.onerror=null}))}))).then((function(){return M.call(r,t)}))},I=function(t){var n=v();return n.src=t,Y.call(this,n)},L=function(t){return t.length?I.call(this,t):this.blank()},C=function(t){var n=this;if(!t.size)return this.blank();var r=URL.createObjectURL(t);return g((function(){return I.call(n,r)})).finally((function(){URL.revokeObjectURL(r)}))},E=function(t){return t.isEmpty()?this.blank():I.call(this,""+t)},O=function(t){var n=h.parse(t);return n?E.call(this,n):I.call(this,t)},T=function(t){return I.call(this,""+t)};function j(t){if(t){var n=typeof t;return"object"===n||"function"===n}return!1}Object.assign(t,{from:X,loadFrom:function(t){var n=this;return g((function(){return o(t)?O.call(n,t):y(t)?T.call(n,t):function(t){var n=globalThis.Buffer;return!!n&&t instanceof n}(t)?L.call(n,t):function(t){var n=globalThis.Blob;return!!n&&t instanceof n}(t)?C.call(n,t):m(t)?Y.call(n,t):X.call(n,t)}))}}),t.fromExcept=function(t){return this.is(t)?t:this.from(t)},t.isDearImage=t.is.bind(t);function F(t){return o(t)&&!function(t){return!t}(t)}function U(t){return F(t)?t:"sans-serif"}var D=new Set(["normal","italic","oblique"]);var S=new Set(["normal","small-caps"]);function k(t){return"number"==typeof t&&!Number.isNaN(t)}function R(t,n,r,e,i){return(e?t>=n:t>n)&&(i?t<=r:t<r)}var q=new Set(["normal","bold"]);function B(t){if(o(t)){if(t=t.trim().toLowerCase(),q.has(t))return t}else if(function(t){for(var n=[],r=arguments.length-1;r-- >0;)n[r]=arguments[r+1];return k(t)&&R.apply(void 0,[t].concat(n))}(t,1,1e3,!0,!0))return t=Math.round(t);return"normal"}var N={from:function(t){var n="sans-serif",r="normal",e="normal",i="normal";return o(t)?n=U(t):j(t)&&(n=U(t.family),r=function(t){return o(t)&&(t=t.trim().toLowerCase(),D.has(t))?t:"normal"}(t.style),e=function(t){return o(t)&&(t=t.trim().toLowerCase(),S.has(t))?t:"normal"}(t.variant),i=B(t.weight)),Object.assign(new this,{family:n,style:r,variant:e,weight:i})},fromExcept:function(t){return this.is(t)?t:this.from(t)},is:function(t){return t instanceof this}};function H(t,n,r,e,i){return[r,e,i,n+"px",t].join(" ")}function P(){}function A(t){return function(t){return void 0===t}(t)||function(t){return null===t}(t)}var W={load:function(t){var n=this;return g((function(){var r=n,e=r.family,i=r.style,a=r.variant,s=r.weight;if(A(t))try{return document.fonts.load(H(e,10,i,a,s))}catch(t){}else{try{return new FontFace(e,t,{style:i,variant:a,weight:s}).load()}catch(t){}try{return(0,require("canvas").registerFont)(t,{family:e,style:i,variant:a,weight:s})}catch(t){}}})).then(P)}},$=function(){return function(){}}(),G=$.prototype;Object.assign($,N),$.default=$.from(),Object.assign(G,W),t.loadFontFace=function(t,n){return g((function(){return $.fromExcept(t).load(n)}))},t.loadFromExcept=function(t){var n=this;return g((function(){return n.is(t)?t:n.loadFrom(t)}))};function J(t){return Number.isFinite(t)&&!function(t){return t<0}(t)}var K={from:function(t){var n=$.default,r=10;j(t)&&(n=$.from(t),r=function(t){return J(t)?t=Math.round(t):10}(t.size));var e=n.family,i=n.style,a=n.variant,s=n.weight;return Object.assign(new this,{family:e,size:r,style:i,variant:a,weight:s})},fromExcept:function(t){return this.is(t)?t:this.from(t)},is:function(t){return t instanceof this}};var Q={toCSS:function(){return H(this.family,this.size,this.style,this.variant,this.weight)}},V=function(){return function(){}}(),Z=V.prototype;function _(t){for(var n=[],r=arguments.length-1;r-- >0;)n[r]=arguments[r+1];return function(){for(var r=[],e=arguments.length;e--;)r[e]=arguments[e];return t.call.apply(t,[this].concat(n,r))}}Object.assign(V,K),V.default=V.from(),Object.assign(Z,Q),t.measureText=function(t,n){t=A(t)?"":""+t;var r=this.blank().context;return r.font=V.fromExcept(n).toCSS(),r.measureText(t)},t.text=function(t,n){t=A(t)?"":""+t;var r="#000",e=V.default,i=.28;j(n)&&(F(n.fill)&&(r=n.fill),e=V.fromExcept(n.font),J(n.padding)&&(i=n.padding)),i=Math.ceil(i*e.size);var a=this.blank(this.measureText(t,e).width+2*i,e.size+2*i),s=a.context;return s.save(),s.font=e.toCSS(),s.textBaseline="top",s.textAlign="left",s.fillStyle=r,s.fillText(t,i,i),s.restore(),a},t.prototype.crop=function(t,n,r,e){if(Number.isFinite(t)?(t=Math.round(t))<0&&(t+=this.sizeX):t=0,Number.isFinite(n)?(n=Math.round(n))<0&&(n+=this.sizeY):n=0,Number.isFinite(r)?(r=Math.round(r))<0&&(t+=r,r*=-1):(this,r=this.sizeX),Number.isFinite(e)?(e=Math.round(e))<0&&(n+=e,e*=-1):(this,e=this.sizeY),t||n||r!==this.sizeX||e!==this.sizeY){var i=this.constructor.blank(r,e);return r&&e&&this.sizeX&&this.sizeY&&i.context.drawImage(this.canvas,-t,-n),i}return this};var tt=function(t,n){var r=this.sizeX,e=this.sizeY;if(r&&e){var i=this.constructor.blank(r,e),a=i.context;return a.save(),a.translate(t?r:0,n?e:0),a.scale(t?-1:1,n?-1:1),a.drawImage(this.canvas,0,0),a.restore(),i}return this};Object.assign(t.prototype,{flipX:_(tt,!0,!1),flipY:_(tt,!1,!0)}),t.prototype.reframe=function(t,n,r,e){var i=this;J(t)?t=Math.round(t):(this,t=this.sizeX),J(n)?n=Math.round(n):(this,n=this.sizeY);var a=function(){if(o(r))switch(r.trim().toLowerCase()){case"start":return 0;case"end":return-t}return(i.sizeX+t)/-2}(),s=function(){if(o(e))switch(e.trim().toLowerCase()){case"start":return 0;case"end":return-n}return(i.sizeY+n)/-2}();return this.crop(a,s,t,n)},t.prototype.resize=function(t,n){if(J(t)?t=Math.round(t):(this,t=this.sizeX),J(n)?n=Math.round(n):(this,n=this.sizeY),t!==this.sizeX||n!==this.sizeY){var r=this.constructor.blank(t,n);if(t&&n&&this.sizeX&&this.sizeY){var e=t/this.sizeX,i=n/this.sizeY,a=r.context;a.save(),a.scale(e,i),a.drawImage(this.canvas,0,0),a.restore()}return r}return this},t.prototype.rescale=function(t,n){var r=this.sizeX,e=this.sizeY;return J(t)&&(r*=t),J(n)&&(e*=n),this.resize(r,e)},t.prototype.scale=function(t){return this.rescale(t,t)},t.prototype.resizeX=function(t,n){return n?this.sizeX&&J(t)?(t=Math.round(t),this.scale(t/this.sizeX)):this:this.resize(t,this.sizeY)},t.prototype.resizeY=function(t,n){return n?this.sizeY&&J(t)?(t=Math.round(t),this.scale(t/this.sizeY)):this:this.resize(this.sizeX,t)},t.prototype.toBuffer=function(){for(var t,n=[],r=arguments.length;r--;)n[r]=arguments[r];return this.sizeX&&this.sizeY?(t=this.canvas).toBuffer.apply(t,n):Buffer.alloc(0)},t.prototype.saveToFileSystem=function(t){for(var n=this,r=[],e=arguments.length-1;e-- >0;)r[e]=arguments[e+1];return new Promise((function(e,i){var a,s=require("fs"),o=require("path"),u=(a=n).toBuffer.apply(a,r);s.mkdir(o.dirname(t),{recursive:!0},(function(n){n?i(n):s.writeFile(t,u,(function(t){t?i(t):e()}))}))}))};var nt=function(t,n,r){J(n)?n=Math.round(n):(this,n=this.sizeX),J(r)?r=Math.round(r):(this,r=this.sizeY);var e=[];if(n&&this.sizeX){var i=n/this.sizeX;e.push(i)}if(r&&this.sizeY){var a=r/this.sizeY;e.push(a)}if(e.length){var s=t(e);return this.scale(s)}return this};function rt(t){for(var n=t.length,r=new Uint8Array(n),e=0;e<n;e++)r[e]=t.charCodeAt(e);return r}function et(t){return!!function(t){if(o(t)){var n=globalThis.URL;if(n){var r=globalThis.document,e=r&&r.location&&r.location.origin;try{return new n(t,e)}catch(t){}}}return!1}(t)}return Object.assign(t.prototype,{scaleDownIn:_(nt,(function(t){return Math.min(Math.min.apply(Math,t),1)})),scaleDownOut:_(nt,(function(t){return Math.min(Math.max.apply(Math,t),1)})),scaleIn:_(nt,(function(t){return Math.min.apply(Math,t)})),scaleOut:_(nt,(function(t){return Math.max.apply(Math,t)})),scaleUpIn:_(nt,(function(t){return Math.max(Math.min.apply(Math,t),1)})),scaleUpOut:_(nt,(function(t){return Math.max(Math.max.apply(Math,t),1)}))}),t.prototype.toDataURL=function(){for(var t,n=[],r=arguments.length;r--;)n[r]=arguments[r];return(t=this.canvas).toDataURL.apply(t,n)},t.prototype.toBlob=function(){for(var t,n=[],r=arguments.length;r--;)n[r]=arguments[r];var e=h.parse((t=this).toDataURL.apply(t,n)),i=e.data,a=e.type,s=rt(atob(i));return new Blob([s],{type:a})},t.prototype.toHTMLCanvasElement=function(){var t=r(),n=this.sizeX,e=this.sizeY;return t.width=n,t.height=e,n&&e&&t.getContext("2d").drawImage(this.canvas,0,0),t},t.prototype.toHTMLImageElement=function(){for(var t,n=[],r=arguments.length;r--;)n[r]=arguments[r];var e=p();return e.src=(t=this).toDataURL.apply(t,n),e},t.prototype.toImageData=function(){return this.context.getImageData(0,0,this.sizeX,this.sizeY)},t.prototype.toOffscreenCanvas=function(){var t=e(),n=this.sizeX,r=this.sizeY;return t.width=n,t.height=r,n&&r&&t.getContext("2d").drawImage(this.canvas,0,0),t},(t.utils={}).isDataURL=function(t){return h.is(t)||h.isString(t)},t.utils.isURL=function(t){return y(t)||et(t)},t})); |
{ | ||
"name": "dear-image", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "A class that represents a graphical image.", | ||
@@ -21,2 +21,3 @@ "keywords": [ | ||
"flip", | ||
"font", | ||
"frame", | ||
@@ -27,2 +28,3 @@ "html", | ||
"node", | ||
"resize", | ||
"rotate", | ||
@@ -41,13 +43,12 @@ "save", | ||
"build": "rollup -c", | ||
"generateExamples": "node generateExamples", | ||
"prepublishOnly": "npm run build", | ||
"test": "node test" | ||
"test": "node test/index" | ||
}, | ||
"devDependencies": { | ||
"canvas": "^2.6.0", | ||
"rollup": "^1.20.3", | ||
"rollup-plugin-buble": "^0.19.8", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-terser": "^5.1.1" | ||
"@rollup/plugin-buble": "^0.21.1", | ||
"@rollup/plugin-node-resolve": "^7.1.1", | ||
"canvas": "^2.6.1", | ||
"rollup": "^2.0.3", | ||
"rollup-plugin-terser": "^5.3.0" | ||
} | ||
} |
227
README.md
@@ -11,7 +11,14 @@ # DearImage | ||
npm i dear-image | ||
npm i canvas # to support node | ||
``` | ||
### es6 | ||
--- | ||
Install optionally to support Node. | ||
```shell | ||
npm i canvas | ||
``` | ||
### ES module | ||
```javascript | ||
@@ -21,3 +28,3 @@ import DearImage from 'dear-image'; | ||
### node | ||
### Node | ||
@@ -46,3 +53,3 @@ ```javascript | ||
| ---: | :--- | | ||
| `value` | The value to be checked. | | ||
| `value` | The value to be checked. | | ||
@@ -59,3 +66,3 @@ Returns `true` if the passed value is an instance of `DearImage`, `false` otherwise. | ||
| ---: | :--- | | ||
| `value` | The value to create from. Supported value types are [`ImageData`](https://developer.mozilla.org/en-US/docs/Web/API/ImageData), [`HTMLCanvasElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement), [`OffscreenCanvas`](https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas), [`HTMLImageElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement) and `DearImage`. | | ||
| `value` | The value to create from. Supported value types are [`ImageData`](https://developer.mozilla.org/en-US/docs/Web/API/ImageData), [`HTMLCanvasElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement), [`OffscreenCanvas`](https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas), [`HTMLImageElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement) and `DearImage`. | | ||
@@ -78,3 +85,3 @@ Returns the created `DearImage` instance. | ||
| ---: | :--- | | ||
| `value` | The value to create from. | | ||
| `value` | The value to create from. | | ||
@@ -135,3 +142,4 @@ Returns the same or created `DearImage` instance. | ||
| ---: | :--- | | ||
| `sizeXY` | The size of the image. | | ||
| `sizeX` | A number as the width of the image. | | ||
| `sizeY` | A number as the height of the image. | | ||
@@ -143,2 +151,24 @@ Returns the created `DearImage` instance. | ||
``` | ||
.loadFontFace(fontFace = { | ||
family: 'sans-serif', | ||
style: 'normal', | ||
variant: 'normal', | ||
weight: 'normal', | ||
}, source) | ||
``` | ||
| argument | description | | ||
| ---: | :--- | | ||
| `fontFace` | Either a string as the font family or an object with the font face options. | | ||
| `fontFace.family` | A string as the font family. | | ||
| `fontFace.style` | A string as the font style. | | ||
| `fontFace.variant` | A string as the font variant. | | ||
| `fontFace.weight` | A number or a string as the font weight. | | ||
| `source` | A string as the source path to load from. | | ||
Returns a promise. | ||
--- | ||
``` | ||
.measureText(text, font = { | ||
@@ -157,8 +187,8 @@ family: 'sans-serif', | ||
| ---: | :--- | | ||
| `text` | The text. | | ||
| `font.family` | The font family. | | ||
| `font.size` | The font size. | | ||
| `font.style` | The font style. | | ||
| `font.variant` | The font variant. | | ||
| `font.weight` | The font weight. | | ||
| `text` | A string as the text. | | ||
| `font.family` | A string as the font family. | | ||
| `font.size` | A number as the font size. | | ||
| `font.style` | A string as the font style. | | ||
| `font.variant` | A string as the font variant. | | ||
| `font.weight` | A number or a string as the font weight. | | ||
@@ -179,3 +209,3 @@ Returns the created `TextMetrics` instance. | ||
}, | ||
padding: 0.28, | ||
padding: 0.3, | ||
}) | ||
@@ -188,10 +218,10 @@ ``` | ||
| ---: | :--- | | ||
| `text` | The text. | | ||
| `options.fill` | The fill color. | | ||
| `options.font.family` | The font family. | | ||
| `options.font.size` | The font size. | | ||
| `options.font.style` | The font style. | | ||
| `options.font.variant` | The font variant. | | ||
| `options.font.weight` | The font weight. | | ||
| `options.padding` | The space around the text. The value is relative to the font size. | | ||
| `text` | A string as the text. | | ||
| `options.fill` | A string as the fill color. | | ||
| `options.font.family` | A string as the font family. | | ||
| `options.font.size` | A string as the font size. | | ||
| `options.font.style` | A string as the font style. | | ||
| `options.font.variant` | A string as the font variant. | | ||
| `options.font.weight` | A number or a string as the font weight. | | ||
| `options.padding` | A number as the space around the text. The value is relative to the font size. | | ||
@@ -222,26 +252,2 @@ Returns the created `DearImage` instance. | ||
--- | ||
`.size` | ||
The size of the image. | ||
```javascript | ||
let image = DearImage.blank(300, 150); | ||
console.log(image.size); // => {x: 300, y: 150} | ||
``` | ||
--- | ||
`.minSize` | ||
The minimum size of the image. | ||
--- | ||
`.maxSize` | ||
The maximum size of the image. | ||
### methods | ||
@@ -255,8 +261,7 @@ | ||
| ---: | :--- | | ||
| `sizeXY` | The new size of the image. | | ||
| `sizeX` | A number as the new width of the image. | | ||
| `sizeY` | A number as the new height of the image. | | ||
Returns the created `DearImage` instance. | ||
![](./examples/resize.png) | ||
```javascript | ||
@@ -270,7 +275,7 @@ let image = DearImage.from(source).resize(300, 150); | ||
Changes the size of the image along the x-axis. | ||
Changes the width of the image. | ||
| argument | description | | ||
| ---: | :--- | | ||
| `size` | The new size of the image along the x-axis. | | ||
| `size` | A number as the new width of the image. | | ||
| `proportionally` | If `true`, the aspect ratio of the image is preserved. | | ||
@@ -280,4 +285,2 @@ | ||
![](./examples/resizeX.png) | ||
--- | ||
@@ -287,7 +290,7 @@ | ||
Changes the size of the image along the y-axis. | ||
Changes the height of the image. | ||
| argument | description | | ||
| ---: | :--- | | ||
| `size` | The new size of the image along the y-axis. | | ||
| `size` | A number as the new height of the image. | | ||
| `proportionally` | If `true`, the aspect ratio of the image is preserved. | | ||
@@ -297,7 +300,5 @@ | ||
![](./examples/resizeY.png) | ||
--- | ||
`.crop(originX = 0, originY = 0, sizeX = this.sizeX, sizeY = this.sizeY)` | ||
`.crop(startX = 0, startY = 0, sizeX = this.sizeX, sizeY = this.sizeY)` | ||
@@ -308,9 +309,9 @@ Selects an area from the image. | ||
| ---: | :--- | | ||
| `originXY` | The origin of the area. A negative number indicates the origin from the end of the image. | | ||
| `sizeXY` | The size of the area. A negative number reverses the direction. | | ||
| `startX` | A number as the horizontal offset of the area. A positive value indicates the offset from the left of the image. A negative value indicates the offset from the right of the image. | | ||
| `startY` | A number as the vertical offset of the area. A positive value indicates the offset from the top of the image. A negative value indicates the offset from the bottom of the image. | | ||
| `sizeX` | A number as the width of the area. A positive value selects an area from left to right. A negative value selects an area from right to left. | | ||
| `sizeY` | A number as the height of the area. A positive value selects an area from top to bottom. A negative value selects an area from bottom to top. | | ||
Returns the created `DearImage` instance. | ||
![](./examples/crop.png) | ||
```javascript | ||
@@ -328,4 +329,6 @@ let image = DearImage.from(source).crop(100, -200, -50, 150); | ||
| ---: | :--- | | ||
| `sizeXY` | The size of the area. | | ||
| `alignmentXY` | The alignment of the image. Possible values are `'start'`, `'center'` and `'end'`. | | ||
| `sizeX` | A number as the width of the area. | | ||
| `sizeY` | A number as the height of the area. | | ||
| `alignmentX` | A number as the horizontal alignment of the image. Possible values are `'start'`, `'center'` and `'end'`. | | ||
| `alignmentY` | A number as the vertical alignment of the image. Possible values are `'start'`, `'center'` and `'end'`. | | ||
@@ -346,3 +349,4 @@ Returns the created `DearImage` instance. | ||
| ---: | :--- | | ||
| `scalingXY` | The scaling factor. | | ||
| `scalingX` | A number as the scaling factor for the width. | | ||
| `scalingY` | A number as the scaling factor for the height. | | ||
@@ -363,8 +367,6 @@ Returns the created `DearImage` instance. | ||
| ---: | :--- | | ||
| `scaling` | The scaling factor. | | ||
| `scaling` | A number as the scaling factor. | | ||
Returns the created `DearImage` instance. | ||
![](./examples/scale.png) | ||
```javascript | ||
@@ -376,3 +378,3 @@ let image = DearImage.from(source).scale(3/2); | ||
`.scaleIn(sizeX, sizeY)` | ||
`.scaleIn(sizeX = this.sizeX, sizeY = this.sizeY)` | ||
@@ -383,3 +385,4 @@ Scales the image inside an area. The aspect ratio of the image is preserved. | ||
| ---: | :--- | | ||
| `sizeXY` | The size of the area. | | ||
| `sizeX` | A number as the width of the area. | | ||
| `sizeY` | A number as the height of the area. | | ||
@@ -390,3 +393,3 @@ Returns the created `DearImage` instance. | ||
`.scaleOut(sizeX, sizeY)` | ||
`.scaleOut(sizeX = this.sizeX, sizeY = this.sizeY)` | ||
@@ -397,10 +400,10 @@ Scales the image outside an area. The aspect ratio of the image is preserved. | ||
| ---: | :--- | | ||
| `sizeXY` | The size of the area. | | ||
| `sizeX` | A number as the width of the area. | | ||
| `sizeY` | A number as the height of the area. | | ||
Returns the created `DearImage` instance. | ||
--- | ||
`.scaleDownIn(sizeX, sizeY)` | ||
`.scaleDownIn(sizeX = this.sizeX, sizeY = this.sizeY)` | ||
@@ -411,3 +414,4 @@ If necessary, scales the image down inside an area. The aspect ratio of the image is preserved. | ||
| ---: | :--- | | ||
| `sizeXY` | The size of the area. | | ||
| `sizeX` | A number as the width of the area. | | ||
| `sizeY` | A number as the height of the area. | | ||
@@ -418,3 +422,3 @@ Returns the created `DearImage` instance. | ||
`.scaleDownOut(sizeX, sizeY)` | ||
`.scaleDownOut(sizeX = this.sizeX, sizeY = this.sizeY)` | ||
@@ -425,3 +429,4 @@ If necessary, scales the image down outside an area. The aspect ratio of the image is preserved. | ||
| ---: | :--- | | ||
| `sizeXY` | The size of the area. | | ||
| `sizeX` | A number as the width of the area. | | ||
| `sizeY` | A number as the height of the area. | | ||
@@ -432,3 +437,3 @@ Returns the created `DearImage` instance. | ||
`.scaleUpIn(sizeX, sizeY)` | ||
`.scaleUpIn(sizeX = this.sizeX, sizeY = this.sizeY)` | ||
@@ -439,3 +444,4 @@ If necessary, scales the image up inside an area. The aspect ratio of the image is preserved. | ||
| ---: | :--- | | ||
| `sizeXY` | The size of the area. | | ||
| `sizeX` | A number as the width of the area. | | ||
| `sizeY` | A number as the height of the area. | | ||
@@ -446,3 +452,3 @@ Returns the created `DearImage` instance. | ||
`.scaleUpOut(sizeX, sizeY)` | ||
`.scaleUpOut(sizeX = this.sizeX, sizeY = this.sizeY)` | ||
@@ -453,3 +459,4 @@ If necessary, scales the image up outside an area. The aspect ratio of the image is preserved. | ||
| ---: | :--- | | ||
| `sizeXY` | The size of the area. | | ||
| `sizeX` | A number as the width of the area. | | ||
| `sizeY` | A number as the height of the area. | | ||
@@ -462,8 +469,6 @@ Returns the created `DearImage` instance. | ||
Flips the image along the x-axis. | ||
Flips the image horizontally. | ||
Returns the created `DearImage` instance. | ||
![](./examples/flipX.png) | ||
--- | ||
@@ -473,8 +478,6 @@ | ||
Flips the image along the y-axis. | ||
Flips the image vertically. | ||
Returns the created `DearImage` instance. | ||
![](./examples/flipY.png) | ||
--- | ||
@@ -556,3 +559,3 @@ | ||
`.saveToFileSystem(file, format, quality)` | ||
`.saveToFileSystem(target, format, quality)` | ||
@@ -565,3 +568,3 @@ *node only* | ||
| ---: | :--- | | ||
| `file` | The file to save to. | | ||
| `target` | A string as the target path to save to. | | ||
@@ -574,1 +577,47 @@ Returns a promise. | ||
``` | ||
### utils | ||
`.isURL(value)` | ||
Determines whether the passed value is an instance of [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) or an URL string. | ||
| argument | description | | ||
| ---: | :--- | | ||
| `value` | The value to be checked. | | ||
Returns `true` if the passed value is an instance of `URL` or an URL string, `false` otherwise. | ||
```javascript | ||
console.log(DearImage.utils.isURL('https://github.com/SeregPie/DearImage')); | ||
// => true | ||
console.log(DearImage.utils.isURL(new URL('/SeregPie/DearImage', 'https://github.com'))); | ||
// => true | ||
console.log(DearImage.utils.isURL('/SeregPie/DearImage'))); | ||
// => true in browser and false in node | ||
``` | ||
--- | ||
`.isDataURL(value)` | ||
Determines whether the passed value is a [data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs) string. | ||
| argument | description | | ||
| ---: | :--- | | ||
| `value` | The value to be checked. | | ||
Returns `true` if the passed value is a data URL string, `false` otherwise. | ||
```javascript | ||
console.log(DearImage.utils.isDataURL('data:image/png;base64,R0lGODdh')); | ||
// => true | ||
console.log(DearImage.utils.isDataURL('data:,'))); | ||
// => true | ||
console.log(DearImage.utils.isDataURL('data:image/png;base64'))); | ||
// => false | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28290
45
588