diff --git a/build/CanvasLite.js b/build/CanvasLite.js index 0af3de5..af2bfb2 100644 --- a/build/CanvasLite.js +++ b/build/CanvasLite.js @@ -2,14 +2,14 @@ * CanvasLite * an html canvas implementation in pure JavaScript * -* @version 0.9.92r2 (2023-08-02 12:05:01) +* @version 0.9.92r2 (2023-08-02 12:49:06) * https://github.com/foo123/CanvasLite * **//** * CanvasLite * an html canvas implementation in pure JavaScript * -* @version 0.9.92r2 (2023-08-02 12:05:01) +* @version 0.9.92r2 (2023-08-02 12:49:06) * https://github.com/foo123/CanvasLite * **/ @@ -277,7 +277,7 @@ function RenderingContext2D(width, height, set_rgba_at, get_rgba_from) set: function(c) { if (c instanceof Gradient || c instanceof Pattern) { - get_stroke_at = Rasterizer.getRGBAFrom(c.getColorAt.bind(c)); + get_stroke_at = c.getColorAt.bind(c); } else { @@ -292,7 +292,7 @@ function RenderingContext2D(width, height, set_rgba_at, get_rgba_from) set: function(c) { if (c instanceof Gradient || c instanceof Pattern) { - get_fill_at = Rasterizer.getRGBAFrom(c.getColorAt.bind(c)); + get_fill_at = c.getColorAt.bind(c); } else { @@ -3314,37 +3314,16 @@ function Gradient(grad_color_at) self.getColorAt = function(x, y) { var im = transform.imatrix(true), p = im ? im.transform(x, y) : null, - rgba = new ImArray(4); + rgba = [0, 0, 0, 0]; return p ? grad_color_at(p[0], p[1], colorStops(), rgba, 0) : rgba; }; - self.getBitmap = function(width, height) { - width = stdMath.round(width); - height = stdMath.round(height); - var imatrix = transform.imatrix(true), - color_stops, - i, x, y, p, - size = (width*height) << 2, - bmp = new ImArray(size); - if (imatrix) - { - color_stops = colorStops(); - for (x=0,y=0,i=0; i= width) {x=0; ++y;} - p = imatrix.transform(x, y); - grad_color_at(p[0], p[1], color_stops, bmp, i); - } - } - return bmp; - }; } Gradient.VERSION = "1.2.3"; Gradient[PROTO] = { constructor: Gradient, transform: null, addColorStop: null, - getColorAt: null, - getBitmap: null + getColorAt: null }; Gradient.createLinearGradient = function(x1, y1, x2, y2) { x1 = x1 || 0; @@ -3487,38 +3466,20 @@ function Pattern(pat_color_at) configurable: false }); self.getColorAt = function(x, y) { - var im = transform.imatrix(true), p, rgba = new ImArray(4); + var im = transform.imatrix(true), p, rgba = [0, 0, 0, 0]; if (im) { p = im.transform(x, y); pat_color_at(p[0], p[1], rgba, 0); } + rgba[3] /= 255; return rgba; }; - self.getBitmap = function(width, height) { - width = stdMath.round(width); - height = stdMath.round(height); - var imatrix = transform.imatrix(true), - i, x, y, p, - size = (width*height) << 2, - bmp = new ImArray(size); - if (imatrix) - { - for (x=0,y=0,i=0; i= width) {x=0; ++y;} - p = imatrix.transform(x, y); - pat_color_at(p[0], p[1], bmp, i); - } - } - return bmp; - }; } Pattern[PROTO] = { constructor: Pattern, transform: null, - getColorAt: null, - getBitmap: null + getColorAt: null }; Pattern.createPattern = function(imageData, repetition) { if (imageData && ('function' === typeof imageData.getImageData)) imageData = imageData.getImageData(); @@ -3733,7 +3694,7 @@ function interpolatePixel(pixel, index, rgba0, rgba1, t) pixel[index + 0] = clamp(stdMath.round(rgba0[0] + t*(rgba1[0] - rgba0[0])), 0, 255); pixel[index + 1] = clamp(stdMath.round(rgba0[1] + t*(rgba1[1] - rgba0[1])), 0, 255); pixel[index + 2] = clamp(stdMath.round(rgba0[2] + t*(rgba1[2] - rgba0[2])), 0, 255); - pixel[index + 3] = 3 < rgba0.length ? clamp(stdMath.round(255*(rgba0[3] + t*(rgba1[3] - rgba0[3]))), 0, 255) : 255; + pixel[index + 3] = 3 < rgba0.length ? clamp(rgba0[3] + t*(rgba1[3] - rgba0[3]), 0, 1) : 1; return pixel; } var hexRE = /^#([0-9a-fA-F]{3,6})\b/, diff --git a/build/CanvasLite.min.js b/build/CanvasLite.min.js index 1247aeb..e5dfd80 100644 --- a/build/CanvasLite.min.js +++ b/build/CanvasLite.min.js @@ -2,7 +2,7 @@ * CanvasLite * an html canvas implementation in pure JavaScript * -* @version 0.9.92r2 (2023-08-02 12:05:01) +* @version 0.9.92r2 (2023-08-02 12:49:06) * https://github.com/foo123/CanvasLite * -**/!function(e,n,t){"use strict";"object"==typeof module&&module.exports?module.exports=t.call(e):"function"==typeof define&&define.amd&&"function"==typeof require&&"function"==typeof require.specified&&require.specified(n)?define(n,["module"],function(n){return t.call(e)}):n in e||(e[n]=t.call(e)||1)&&"function"==typeof define&&define.amd&&define(function(){return e[n]})}("undefined"!=typeof self?self:this,"CanvasLite",function(e){"use strict";var n="prototype",P=Object.defineProperty,c=Object[n].hasOwnProperty,t=Object[n].toString,f="undefined"!=typeof global&&"[object global]"===t.call(global),o="undefined"!=typeof window&&"[object Window]"===t.call(window),Q=Math,q=1/0,r=Q.sqrt(2),A=isNaN,B=isFinite,d=Q.PI,x=2*d,l=6,p=r,m=.5,E="undefined"!=typeof Uint8ClampedArray?Uint8ClampedArray:"undefined"!=typeof Uint8Array?Uint8Array:Array,z=[0,0,0,0],a=/[MLHVCSQTAZ]/gi,S=/-?(?:(?:\d+\.\d+)|(?:\.\d+)|(?:\d+))/g;function F(n,e,t,r){var a,o=this;if(!(o instanceof F))return new F(n,e,t,r);r=r||(t&&t.$target?F.getRGBAFrom(t.$target):function(n,e){return z}),a=new G(n,e,t,r),P(o,"width",{get:function(){return n},set:function(n){}}),P(o,"height",{get:function(){return e},set:function(n){}}),o.getContext=function(n){if("2d"===n)return a;V('Unsupported context "'+n+'"')},o.dispose=function(){a&&a.dispose(),a=null}}function G(D,S,m,d){var o,i,e,t,g,v,r,a,l,u,s,c,f,h,p,C,y=this,b=null,w=null,x=function(){b={}},k=function(n){for(var e in b){var t=b[e],e=e.split(",");n(+e[0],+e[1],t)}b=null},L=function(n,e,t){var r;0<=n&&nI)I=Q.floor(f[1]);else{for(y=f[0],b=f[2],d=f[1],p=f[3],v=J(d,p)?(w=!1,g=Q.min(y,b),Q.max(y,b)):g=w=(b-y)*(I-d)/(p-d)+y,f[8]=w,f[9]=0,T[0]=f,s=A+(c=1);s=I&&(y=f[0],b=f[2],d=f[1],p=f[3],v=J(d,p)?(w=!1,g=Q.min(g,y,b),Q.max(v,y,b)):(w=(b-y)*(I-d)/(p-d)+y,g=Q.min(g,w),Q.max(v,w)),f[8]=w,f[9]=0,T[c++]=f);if(h=sn(T,c),!(ci.ymax)){for(var l,u,s,c,f,h,d,p,m,g,v,y,b,w=i.length,x=new Array(w),k=0,t="evenodd"===e;ko)){for(m=s[0],g=s[2],f=s[1],h=s[3],p=J(f,h)?(v=!1,d=Q.min(m,g),Q.max(m,g)):d=v=(g-m)*(o-f)/(h-f)+m,s[8]=v,s[9]=0,x[0]=s,l=k+(u=1);l=o&&(m=s[0],g=s[2],f=s[1],h=s[3],p=J(f,h)?(v=!1,d=Q.min(d,m,g),Q.max(p,m,g)):(v=(g-m)*(o-f)/(h-f)+m,d=Q.min(d,v),Q.max(p,v)),s[8]=v,s[9]=0,x[u++]=s);if(c=sn(x,u),us||Q.max(t,a,i)c)){if(ah)h=Q.floor(A[1]);else{for(m=-(p=q),t<=h&&h<=a&&(m=R?(p=Q.min(p,e,r),Q.max(m,e,r)):(p=Q.min(p,g=k*(h-t)/C+e),Q.max(m,g))),I=T;I=h&&(v=A[0],y=A[2],m=J(b=A[1],w=A[3])?(p=Q.min(p,v,y),Q.max(m,v,y)):(p=Q.min(p,g=(y-v)*(h-b)/(w-b)+v),Q.max(m,g)));for(p=Q.round(p+.5),m=Q.round(m-.5),U&&(p=Q.max(p,u),m=Q.min(m,c));p<=m;++p)n(p,h,1)}}}function X(n,e,t,r,a,o,i,l,u,s,c,f,h,d){var p=Q.min(e,r),m=Q.max(e,r),g=Q.min(t,a),v=Q.max(t,a);if(!(ma[t+3]?o.push([+a[t+2],a[t+3],+a[t],a[t+1],-1,s,c,c,0,0]):o.push([+a[t],a[t+1],+a[t+2],a[t+3],1,s,c,c,0,0]),++c;l=Q.min(l,a[1+e]),u=Q.max(u,a[1+e])}return(o=o.sort(y)).ymin=l,o.ymax=u,o}function sn(n,e){for(var t,r,a,o=0,i=0;i=n)return a;e[r=a+o>>>1][0]t[o][0]?(n-t[o][0])/(t[i][0]-t[o][0]):n)})},$.createRadialGradient=function(d,p,m,g,v,n){var y=(m=m||0)*m-2*m*(n=n||0)+n*n-(d=d||0)*d+2*d*(g=g||0)-g*g-(p=p||0)*p+2*p*(v=v||0)-v*v,b=-2*m*m+2*m*n+2*d*d-2*d*g+2*p*p-2*p*v,w=-d*d-p*p+m*m;return new $(function(n,e,t,r,a){var o,i,l,u,s,c,f,h=t.length;return u=b-2*n*d+2*n*g-2*e*p+2*e*v,s=w-n*n+2*n*d-e*e+2*e*p,(u=(s=J(l=y,0)?(f=s,!J(c=u,0)&&[-f/c]):Y(f=u*u-4*l*s,c=0,1e-6)?[-u/(2*l)]:!(f<0)&&(c=Q.sqrt(f),[(-u-c)/(2*l),(-u+c)/(2*l)]))?1t[o][0]?(u-t[o][0])/(t[i][0]-t[o][0]):u)})},$.createConicGradient=function(i,l,u){return i=i||0,l=l||0,u=u||0,new $(function(n,e,t,r,a){var o=t.length,e=Q.atan2(e-u,n-l)-i;return e<0&&(e+=x),xt[r][0]?(e-t[r][0])/(t[n][0]-t[r][0]):e)})},Z[n]={constructor:Z,transform:null,getColorAt:null,getBitmap:null},Z.createPattern=function(a,n){if(!((a=a&&"function"==typeof a.getImageData?a.getImageData():a)&&a.data&&a.width&&a.height&&a.data.length===4*a.width*a.height))throw new Error("Pattern: invalid image data");var o=a.width,i=a.height;switch(n){case"no-repeat":return new Z(function(n,e,t,r){return n=Q.round(n),e=Q.round(e),0<=n&&n>3)&1<<(7&a)&&(e|=1<=this.data.length)throw new Error("Attempted to read past end of stream.");return n instanceof Uint8Array?n[this.pos++]:255&n.charCodeAt(this.pos++)},e.readBytes=function(n){for(var e=[],t=0;t>--f&1;if(255==(l=e[t++])){var n=e[t++];if(n)throw"unexpected marker: "+(l<<8|n).toString(16)}return l>>>(f=7)}function d(n){for(var e,t=n;null!=(e=h());){if("number"==typeof(t=t[e]))return t;if("object"!=typeof t)throw"invalid huffman sequence"}return null}function p(n){for(var e=0;0>4;if(0==o){if(a<15){g=p(a)+(1<>4;if(0==l)y=a<15?(g=p(a)+(1<>4;if(0==o){if(a<15)break;r+=16}else e[Ln[r+=a]]=m(o),r++}},F=0,G=1==z?r[0].blocksPerLine*r[0].blocksPerColumn:i*n.mcusPerColumn;for(a=a||G;F>10,T[0+P]=b,T[1+P]=b,T[2+P]=b,T[3+P]=b,T[4+P]=b,T[5+P]=b,T[6+P]=b,T[7+P]=b):(f=K*T[0+P]+128>>8,h=K*T[4+P]+128>>8,d=T[2+P],p=T[6+P],m=2896*(T[1+P]-T[7+P])+128>>8,y=2896*(T[1+P]+T[7+P])+128>>8,g=T[3+P]<<4,b=f-h+1>>1,f=f+h+1>>1,h=b,b=3784*d+1567*p+128>>8,d=1567*d-3784*p+128>>8,p=b,b=m-(v=T[5+P]<<4)+1>>1,m=m+v+1>>1,v=b,b=y+g+1>>1,g=y-g+1>>1,y=b,b=f-p+1>>1,f=f+p+1>>1,p=b,b=h-d+1>>1,h=h+d+1>>1,d=b,b=2276*m+3406*y+2048>>12,m=3406*m-2276*y+2048>>12,y=b,b=799*g+4017*v+2048>>12,g=4017*g-799*v+2048>>12,v=b,T[0+P]=f+y,T[7+P]=f-y,T[1+P]=h+v,T[6+P]=h-v,T[2+P]=d+g,T[5+P]=d-g,T[3+P]=p+m,T[4+P]=p-m)}for(I=0;I<8;++I){var A=I;0==T[8+A]&&0==T[16+A]&&0==T[24+A]&&0==T[32+A]&&0==T[40+A]&&0==T[48+A]&&0==T[56+A]?(b=K*k[I+0]+8192>>14,T[0+A]=b,T[8+A]=b,T[16+A]=b,T[24+A]=b,T[32+A]=b,T[40+A]=b,T[48+A]=b,T[56+A]=b):(f=K*T[0+A]+2048>>12,h=K*T[32+A]+2048>>12,d=T[16+A],p=T[48+A],m=2896*(T[8+A]-T[56+A])+2048>>12,y=2896*(T[8+A]+T[56+A])+2048>>12,g=T[24+A],b=f-h+1>>1,f=f+h+1>>1,h=b,b=3784*d+1567*p+2048>>12,d=1567*d-3784*p+2048>>12,p=b,b=m-(v=T[40+A])+1>>1,m=m+v+1>>1,v=b,b=y+g+1>>1,g=y-g+1>>1,y=b,b=f-p+1>>1,f=f+p+1>>1,p=b,b=h-d+1>>1,h=h+d+1>>1,d=b,b=2276*m+3406*y+2048>>12,m=3406*m-2276*y+2048>>12,y=b,b=799*g+4017*v+2048>>12,g=4017*g-799*v+2048>>12,v=b,T[0+A]=f+y,T[56+A]=f-y,T[8+A]=h+v,T[48+A]=h-v,T[16+A]=d+g,T[40+A]=d-g,T[24+A]=p+m,T[32+A]=p-m)}for(I=0;I<64;++I){var B=128+(T[I]+8>>4);x[I]=B<0?0:255>4==0)for(z=0;z<64;++z)d[Ln[z]]=e[t++];else{if(h>>4!=1)throw"DQT: invalid table spec";for(z=0;z<64;++z)d[Ln[z]]=n()}l[15&h]=d}break;case 65472:case 65473:case 65474:n(),(r={}).extended=65473===f,r.progressive=65474===f,r.precision=e[t++],r.scanLines=n(),r.samplesPerLine=n(),r.components={},r.componentsOrder=[];var U=e[t++];for(E=0;E>4,O=15&e[t+1],_=e[t+2];r.componentsOrder.push(p),r.components[p]={h:q,v:O,quantizationIdx:_},t+=3}D=B=A=P=I=T=C=k=w=b=m=x=y=v=g=void 0;var m,g=r,v=0,y=0;for(m in g.components)g.components.hasOwnProperty(m)&&(v<(x=g.components[m]).h&&(v=x.h),y>4==0?c:s)[15&X]=function(n,e){for(var t,r=0,a=[],o=16;0>4],G.huffmanTableAC=s[15&H],j.push(G)}var L=e[t++],V=e[t++],W=e[t++],V=zn(e,t,r,j,a,L,V,W>>4,15&W);t+=V;break;default:if(255==e[t-3]&&192<=e[t-2]&&e[t-2]<=254){t-=3;break}throw"unknown JPEG marker "+f.toString(16)}f=n()}if(1!=u.length)throw"only single frame JPEGs supported";for(var E=0;Ef.data.length)throw new Error("Incomplete or corrupt PNG file")}},decodePixels:async function(n){var e,t,r,a,o,i,l,u,s,c,f,h,d,p,m,g,v,y,b,w,x,k,C,T;if(0===(n=null==n?this.imgData:n).length)return new Uint8Array(0);for(k=n,T={chunkSize:null==C?16384:C},n=await new Promise(function(t){require("zlib").inflate(k instanceof Buffer?k:Buffer.from(k),T,function(n,e){t(n?null:e)})}),p=(c=this.pixelBitlength/8)*this.width,f=new Uint8Array(p*this.height),i=n.length,t=h=d=0;h>1);r[a+i]=255&l}}function Qn(n,e,t,r){for(var a=0,o=0;o>1);a+=Q.abs(i)}return a}function $n(n,e,t,r,a,o){for(var i=0;i>>1:t>>>1;Kn[n]=t}return Kn}(),t=-1,r=0,a=n.length;r>>8;return-1^t}function ee(n,e=null,t=0){(e=null==e?Buffer.alloc(1):e)[t=null==t?0:t]=255&n}function te(n,e=null,t=0){return(e=null==e?Buffer.alloc(4):e).writeUInt32BE(4294967295&n,t=null==t?0:t),e}function re(n){if((n=n||{}).deflateChunkSize=Q.max(1024,parseInt(n.deflateChunkSize||32768)),n.deflateLevel=Q.min(9,Q.max(0,parseInt(null!=n.deflateLevel?n.deflateLevel:9))),n.deflateStrategy=Q.min(3,Q.max(0,parseInt(null!=n.deflateStrategy?n.deflateStrategy:3))),n.inputHasAlpha=!(null!=n.inputHasAlpha&&!n.inputHasAlpha),n.bitDepth=8,n.colorType=Q.min(6,Q.max(0,parseInt("number"==typeof n.colorType?n.colorType:_n))),n.colorType!==On&&n.colorType!==_n)throw new Error("option color type:"+n.colorType+" is not supported at present");this._options=n}async function ae(n,e,t,r,a){return a=a||{},"base64"!==n?new re(a).toPNG(e,t,r):f?"data:image/png;base64,"+(await new re(a).toPNG(e,t,r)).toString("base64"):o?(a=(n=document.createElement("canvas")).getContext("2d"),n.width=t,n.height=r,a.createImageData(t,r),(t=a.getImageData(0,0,t,r)).data.set(e,0),a.putImageData(t,0,0),n.toDataURL("image/png")):""}function u(){var t=this,e="",r=0,a=0,o=null,i=function(n){if(!n instanceof Error&&(n=new Error(String(n))),!t.onerror)throw n;t.onerror(n)},l=function(n){var e;if(n)return(e=u.Reader[u.detectImageType(n)])?void e(n).then(function(n){r=(o=n).width,a=n.height,t.onload&&t.onload()}).catch(i):i("Image file type is not supported!")};P(t,"width",{get:function(){return r},set:function(n){}}),P(t,"height",{get:function(){return a},set:function(n){}}),P(t,"naturalWidth",{get:function(){return r},set:function(n){}}),P(t,"naturalHeight",{get:function(){return a},set:function(n){}}),P(t,"src",{get:function(){return e},set:function(n){"undefined"!=typeof ArrayBuffer&&n instanceof ArrayBuffer||"undefined"!=typeof Buffer&&n instanceof Buffer?l(e=n):"undefined"!=typeof Blob&&n instanceof Blob||"undefined"!=typeof Buffer&&Buffer.Blob&&n instanceof Buffer.Blob?(e=n).arrayBuffer().then(l).catch(i):("string"==typeof n||n instanceof String)&&(/^data:image\/[a-z]+;base64,/.test(n)?l(function(n){if("undefined"!=typeof Buffer)return Buffer.from(n,"base64");if("function"==typeof atob){for(var e=atob(n),t=e.length,r=new Uint8Array(t),a=0;aI)I=Q.floor(f[1]);else{for(y=f[0],b=f[2],d=f[1],p=f[3],v=J(d,p)?(w=!1,g=Q.min(y,b),Q.max(y,b)):g=w=(b-y)*(I-d)/(p-d)+y,f[8]=w,f[9]=0,T[0]=f,s=A+(c=1);s=I&&(y=f[0],b=f[2],d=f[1],p=f[3],v=J(d,p)?(w=!1,g=Q.min(g,y,b),Q.max(v,y,b)):(w=(b-y)*(I-d)/(p-d)+y,g=Q.min(g,w),Q.max(v,w)),f[8]=w,f[9]=0,T[c++]=f);if(h=sn(T,c),!(ci.ymax)){for(var l,u,s,c,f,h,d,p,m,g,v,y,b,w=i.length,x=new Array(w),k=0,t="evenodd"===e;ko)){for(m=s[0],g=s[2],f=s[1],h=s[3],p=J(f,h)?(v=!1,d=Q.min(m,g),Q.max(m,g)):d=v=(g-m)*(o-f)/(h-f)+m,s[8]=v,s[9]=0,x[0]=s,l=k+(u=1);l=o&&(m=s[0],g=s[2],f=s[1],h=s[3],p=J(f,h)?(v=!1,d=Q.min(d,m,g),Q.max(p,m,g)):(v=(g-m)*(o-f)/(h-f)+m,d=Q.min(d,v),Q.max(p,v)),s[8]=v,s[9]=0,x[u++]=s);if(c=sn(x,u),us||Q.max(t,a,i)c)){if(ah)h=Q.floor(A[1]);else{for(m=-(p=q),t<=h&&h<=a&&(m=R?(p=Q.min(p,e,r),Q.max(m,e,r)):(p=Q.min(p,g=k*(h-t)/C+e),Q.max(m,g))),I=T;I=h&&(v=A[0],y=A[2],m=J(b=A[1],w=A[3])?(p=Q.min(p,v,y),Q.max(m,v,y)):(p=Q.min(p,g=(y-v)*(h-b)/(w-b)+v),Q.max(m,g)));for(p=Q.round(p+.5),m=Q.round(m-.5),U&&(p=Q.max(p,u),m=Q.min(m,c));p<=m;++p)n(p,h,1)}}}function X(n,e,t,r,a,o,i,l,u,s,c,f,h,d){var p=Q.min(e,r),m=Q.max(e,r),g=Q.min(t,a),v=Q.max(t,a);if(!(ma[t+3]?o.push([+a[t+2],a[t+3],+a[t],a[t+1],-1,s,c,c,0,0]):o.push([+a[t],a[t+1],+a[t+2],a[t+3],1,s,c,c,0,0]),++c;l=Q.min(l,a[1+e]),u=Q.max(u,a[1+e])}return(o=o.sort(y)).ymin=l,o.ymax=u,o}function sn(n,e){for(var t,r,a,o=0,i=0;i=n)return a;e[r=a+o>>>1][0]t[o][0]?(n-t[o][0])/(t[i][0]-t[o][0]):n)})},$.createRadialGradient=function(d,p,m,g,v,n){var y=(m=m||0)*m-2*m*(n=n||0)+n*n-(d=d||0)*d+2*d*(g=g||0)-g*g-(p=p||0)*p+2*p*(v=v||0)-v*v,b=-2*m*m+2*m*n+2*d*d-2*d*g+2*p*p-2*p*v,w=-d*d-p*p+m*m;return new $(function(n,e,t,r,a){var o,i,l,u,s,c,f,h=t.length;return u=b-2*n*d+2*n*g-2*e*p+2*e*v,s=w-n*n+2*n*d-e*e+2*e*p,(u=(s=J(l=y,0)?(f=s,!J(c=u,0)&&[-f/c]):Y(f=u*u-4*l*s,c=0,1e-6)?[-u/(2*l)]:!(f<0)&&(c=Q.sqrt(f),[(-u-c)/(2*l),(-u+c)/(2*l)]))?1t[o][0]?(u-t[o][0])/(t[i][0]-t[o][0]):u)})},$.createConicGradient=function(i,l,u){return i=i||0,l=l||0,u=u||0,new $(function(n,e,t,r,a){var o=t.length,e=Q.atan2(e-u,n-l)-i;return e<0&&(e+=x),xt[r][0]?(e-t[r][0])/(t[n][0]-t[r][0]):e)})},Z[n]={constructor:Z,transform:null,getColorAt:null},Z.createPattern=function(a,n){if(!((a=a&&"function"==typeof a.getImageData?a.getImageData():a)&&a.data&&a.width&&a.height&&a.data.length===4*a.width*a.height))throw new Error("Pattern: invalid image data");var o=a.width,i=a.height;switch(n){case"no-repeat":return new Z(function(n,e,t,r){return n=Q.round(n),e=Q.round(e),0<=n&&n>3)&1<<(7&a)&&(e|=1<=this.data.length)throw new Error("Attempted to read past end of stream.");return n instanceof Uint8Array?n[this.pos++]:255&n.charCodeAt(this.pos++)},e.readBytes=function(n){for(var e=[],t=0;t>--f&1;if(255==(l=e[t++])){var n=e[t++];if(n)throw"unexpected marker: "+(l<<8|n).toString(16)}return l>>>(f=7)}function d(n){for(var e,t=n;null!=(e=h());){if("number"==typeof(t=t[e]))return t;if("object"!=typeof t)throw"invalid huffman sequence"}return null}function p(n){for(var e=0;0>4;if(0==o){if(a<15){g=p(a)+(1<>4;if(0==l)y=a<15?(g=p(a)+(1<>4;if(0==o){if(a<15)break;r+=16}else e[Ln[r+=a]]=m(o),r++}},F=0,G=1==z?r[0].blocksPerLine*r[0].blocksPerColumn:i*n.mcusPerColumn;for(a=a||G;F>10,T[0+P]=b,T[1+P]=b,T[2+P]=b,T[3+P]=b,T[4+P]=b,T[5+P]=b,T[6+P]=b,T[7+P]=b):(f=K*T[0+P]+128>>8,h=K*T[4+P]+128>>8,d=T[2+P],p=T[6+P],m=2896*(T[1+P]-T[7+P])+128>>8,y=2896*(T[1+P]+T[7+P])+128>>8,g=T[3+P]<<4,b=f-h+1>>1,f=f+h+1>>1,h=b,b=3784*d+1567*p+128>>8,d=1567*d-3784*p+128>>8,p=b,b=m-(v=T[5+P]<<4)+1>>1,m=m+v+1>>1,v=b,b=y+g+1>>1,g=y-g+1>>1,y=b,b=f-p+1>>1,f=f+p+1>>1,p=b,b=h-d+1>>1,h=h+d+1>>1,d=b,b=2276*m+3406*y+2048>>12,m=3406*m-2276*y+2048>>12,y=b,b=799*g+4017*v+2048>>12,g=4017*g-799*v+2048>>12,v=b,T[0+P]=f+y,T[7+P]=f-y,T[1+P]=h+v,T[6+P]=h-v,T[2+P]=d+g,T[5+P]=d-g,T[3+P]=p+m,T[4+P]=p-m)}for(I=0;I<8;++I){var A=I;0==T[8+A]&&0==T[16+A]&&0==T[24+A]&&0==T[32+A]&&0==T[40+A]&&0==T[48+A]&&0==T[56+A]?(b=K*k[I+0]+8192>>14,T[0+A]=b,T[8+A]=b,T[16+A]=b,T[24+A]=b,T[32+A]=b,T[40+A]=b,T[48+A]=b,T[56+A]=b):(f=K*T[0+A]+2048>>12,h=K*T[32+A]+2048>>12,d=T[16+A],p=T[48+A],m=2896*(T[8+A]-T[56+A])+2048>>12,y=2896*(T[8+A]+T[56+A])+2048>>12,g=T[24+A],b=f-h+1>>1,f=f+h+1>>1,h=b,b=3784*d+1567*p+2048>>12,d=1567*d-3784*p+2048>>12,p=b,b=m-(v=T[40+A])+1>>1,m=m+v+1>>1,v=b,b=y+g+1>>1,g=y-g+1>>1,y=b,b=f-p+1>>1,f=f+p+1>>1,p=b,b=h-d+1>>1,h=h+d+1>>1,d=b,b=2276*m+3406*y+2048>>12,m=3406*m-2276*y+2048>>12,y=b,b=799*g+4017*v+2048>>12,g=4017*g-799*v+2048>>12,v=b,T[0+A]=f+y,T[56+A]=f-y,T[8+A]=h+v,T[48+A]=h-v,T[16+A]=d+g,T[40+A]=d-g,T[24+A]=p+m,T[32+A]=p-m)}for(I=0;I<64;++I){var D=128+(T[I]+8>>4);x[I]=D<0?0:255>4==0)for(z=0;z<64;++z)d[Ln[z]]=e[t++];else{if(h>>4!=1)throw"DQT: invalid table spec";for(z=0;z<64;++z)d[Ln[z]]=n()}l[15&h]=d}break;case 65472:case 65473:case 65474:n(),(r={}).extended=65473===f,r.progressive=65474===f,r.precision=e[t++],r.scanLines=n(),r.samplesPerLine=n(),r.components={},r.componentsOrder=[];var U=e[t++];for(E=0;E>4,O=15&e[t+1],_=e[t+2];r.componentsOrder.push(p),r.components[p]={h:q,v:O,quantizationIdx:_},t+=3}B=D=A=P=I=T=C=k=w=b=m=x=y=v=g=void 0;var m,g=r,v=0,y=0;for(m in g.components)g.components.hasOwnProperty(m)&&(v<(x=g.components[m]).h&&(v=x.h),y>4==0?c:s)[15&X]=function(n,e){for(var t,r=0,a=[],o=16;0>4],G.huffmanTableAC=s[15&H],j.push(G)}var L=e[t++],V=e[t++],W=e[t++],V=zn(e,t,r,j,a,L,V,W>>4,15&W);t+=V;break;default:if(255==e[t-3]&&192<=e[t-2]&&e[t-2]<=254){t-=3;break}throw"unknown JPEG marker "+f.toString(16)}f=n()}if(1!=u.length)throw"only single frame JPEGs supported";for(var E=0;Ef.data.length)throw new Error("Incomplete or corrupt PNG file")}},decodePixels:async function(n){var e,t,r,a,o,i,l,u,s,c,f,h,d,p,m,g,v,y,b,w,x,k,C,T;if(0===(n=null==n?this.imgData:n).length)return new Uint8Array(0);for(k=n,T={chunkSize:null==C?16384:C},n=await new Promise(function(t){require("zlib").inflate(k instanceof Buffer?k:Buffer.from(k),T,function(n,e){t(n?null:e)})}),p=(c=this.pixelBitlength/8)*this.width,f=new Uint8Array(p*this.height),i=n.length,t=h=d=0;h>1);r[a+i]=255&l}}function Qn(n,e,t,r){for(var a=0,o=0;o>1);a+=Q.abs(i)}return a}function $n(n,e,t,r,a,o){for(var i=0;i>>1:t>>>1;Kn[n]=t}return Kn}(),t=-1,r=0,a=n.length;r>>8;return-1^t}function ee(n,e=null,t=0){(e=null==e?Buffer.alloc(1):e)[t=null==t?0:t]=255&n}function te(n,e=null,t=0){return(e=null==e?Buffer.alloc(4):e).writeUInt32BE(4294967295&n,t=null==t?0:t),e}function re(n){if((n=n||{}).deflateChunkSize=Q.max(1024,parseInt(n.deflateChunkSize||32768)),n.deflateLevel=Q.min(9,Q.max(0,parseInt(null!=n.deflateLevel?n.deflateLevel:9))),n.deflateStrategy=Q.min(3,Q.max(0,parseInt(null!=n.deflateStrategy?n.deflateStrategy:3))),n.inputHasAlpha=!(null!=n.inputHasAlpha&&!n.inputHasAlpha),n.bitDepth=8,n.colorType=Q.min(6,Q.max(0,parseInt("number"==typeof n.colorType?n.colorType:_n))),n.colorType!==On&&n.colorType!==_n)throw new Error("option color type:"+n.colorType+" is not supported at present");this._options=n}async function ae(n,e,t,r,a){return a=a||{},"base64"!==n?new re(a).toPNG(e,t,r):f?"data:image/png;base64,"+(await new re(a).toPNG(e,t,r)).toString("base64"):o?(a=(n=document.createElement("canvas")).getContext("2d"),n.width=t,n.height=r,a.createImageData(t,r),(t=a.getImageData(0,0,t,r)).data.set(e,0),a.putImageData(t,0,0),n.toDataURL("image/png")):""}function u(){var t=this,e="",r=0,a=0,o=null,i=function(n){if(!n instanceof Error&&(n=new Error(String(n))),!t.onerror)throw n;t.onerror(n)},l=function(n){var e;if(n)return(e=u.Reader[u.detectImageType(n)])?void e(n).then(function(n){r=(o=n).width,a=n.height,t.onload&&t.onload()}).catch(i):i("Image file type is not supported!")};P(t,"width",{get:function(){return r},set:function(n){}}),P(t,"height",{get:function(){return a},set:function(n){}}),P(t,"naturalWidth",{get:function(){return r},set:function(n){}}),P(t,"naturalHeight",{get:function(){return a},set:function(n){}}),P(t,"src",{get:function(){return e},set:function(n){"undefined"!=typeof ArrayBuffer&&n instanceof ArrayBuffer||"undefined"!=typeof Buffer&&n instanceof Buffer?l(e=n):"undefined"!=typeof Blob&&n instanceof Blob||"undefined"!=typeof Buffer&&Buffer.Blob&&n instanceof Buffer.Blob?(e=n).arrayBuffer().then(l).catch(i):("string"==typeof n||n instanceof String)&&(/^data:image\/[a-z]+;base64,/.test(n)?l(function(n){if("undefined"!=typeof Buffer)return Buffer.from(n,"base64");if("function"==typeof atob){for(var e=atob(n),t=e.length,r=new Uint8Array(t),a=0;a= width) {x=0; ++y;} - p = imatrix.transform(x, y); - grad_color_at(p[0], p[1], color_stops, bmp, i); - } - } - return bmp; - }; } Gradient.VERSION = "1.2.3"; Gradient[PROTO] = { constructor: Gradient, transform: null, addColorStop: null, - getColorAt: null, - getBitmap: null + getColorAt: null }; Gradient.createLinearGradient = function(x1, y1, x2, y2) { x1 = x1 || 0; @@ -221,38 +200,20 @@ function Pattern(pat_color_at) configurable: false }); self.getColorAt = function(x, y) { - var im = transform.imatrix(true), p, rgba = new ImArray(4); + var im = transform.imatrix(true), p, rgba = [0, 0, 0, 0]; if (im) { p = im.transform(x, y); pat_color_at(p[0], p[1], rgba, 0); } + rgba[3] /= 255; return rgba; }; - self.getBitmap = function(width, height) { - width = stdMath.round(width); - height = stdMath.round(height); - var imatrix = transform.imatrix(true), - i, x, y, p, - size = (width*height) << 2, - bmp = new ImArray(size); - if (imatrix) - { - for (x=0,y=0,i=0; i= width) {x=0; ++y;} - p = imatrix.transform(x, y); - pat_color_at(p[0], p[1], bmp, i); - } - } - return bmp; - }; } Pattern[PROTO] = { constructor: Pattern, transform: null, - getColorAt: null, - getBitmap: null + getColorAt: null }; Pattern.createPattern = function(imageData, repetition) { if (imageData && ('function' === typeof imageData.getImageData)) imageData = imageData.getImageData(); @@ -467,7 +428,7 @@ function interpolatePixel(pixel, index, rgba0, rgba1, t) pixel[index + 0] = clamp(stdMath.round(rgba0[0] + t*(rgba1[0] - rgba0[0])), 0, 255); pixel[index + 1] = clamp(stdMath.round(rgba0[1] + t*(rgba1[1] - rgba0[1])), 0, 255); pixel[index + 2] = clamp(stdMath.round(rgba0[2] + t*(rgba1[2] - rgba0[2])), 0, 255); - pixel[index + 3] = 3 < rgba0.length ? clamp(stdMath.round(255*(rgba0[3] + t*(rgba1[3] - rgba0[3]))), 0, 255) : 255; + pixel[index + 3] = 3 < rgba0.length ? clamp(rgba0[3] + t*(rgba1[3] - rgba0[3]), 0, 1) : 1; return pixel; } var hexRE = /^#([0-9a-fA-F]{3,6})\b/, diff --git a/src/Rasterizer.js b/src/Rasterizer.js index b62916b..83f7d7d 100644 --- a/src/Rasterizer.js +++ b/src/Rasterizer.js @@ -233,7 +233,7 @@ function RenderingContext2D(width, height, set_rgba_at, get_rgba_from) set: function(c) { if (c instanceof Gradient || c instanceof Pattern) { - get_stroke_at = Rasterizer.getRGBAFrom(c.getColorAt.bind(c)); + get_stroke_at = c.getColorAt.bind(c); } else { @@ -248,7 +248,7 @@ function RenderingContext2D(width, height, set_rgba_at, get_rgba_from) set: function(c) { if (c instanceof Gradient || c instanceof Pattern) { - get_fill_at = Rasterizer.getRGBAFrom(c.getColorAt.bind(c)); + get_fill_at = c.getColorAt.bind(c); } else {