index.umd.min.js 15 KB

1
  1. !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@antv/g-lite")):"function"==typeof define&&define.amd?define(["exports","@antv/g-lite"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).G=e.G||{},e.G.CanvasRenderer={}),e.window.G)}(this,(function(e,t){"use strict";function r(){return r=Object.assign?Object.assign.bind():function(e){for(var t=1;arguments.length>t;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},r.apply(this,arguments)}function n(e,t){e.prototype=Object.create(t.prototype),e.prototype.constructor=e,i(e,t)}function i(e,t){return i=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},i(e,t)}var a="undefined"!=typeof Float32Array?Float32Array:Array;function o(){var e=new a(16);return a!=Float32Array&&(e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[6]=0,e[7]=0,e[8]=0,e[9]=0,e[11]=0,e[12]=0,e[13]=0,e[14]=0),e[0]=1,e[5]=1,e[10]=1,e[15]=1,e}function s(e,t){return e[0]=t[0],e[1]=t[1],e[2]=t[2],e[3]=t[3],e[4]=t[4],e[5]=t[5],e[6]=t[6],e[7]=t[7],e[8]=t[8],e[9]=t[9],e[10]=t[10],e[11]=t[11],e[12]=t[12],e[13]=t[13],e[14]=t[14],e[15]=t[15],e}function l(e,t,r){var n=t[0],i=t[1],a=t[2],o=t[3],s=t[4],l=t[5],h=t[6],c=t[7],d=t[8],u=t[9],f=t[10],p=t[11],v=t[12],g=t[13],y=t[14],m=t[15],A=r[0],x=r[1],b=r[2],M=r[3];return e[0]=A*n+x*s+b*d+M*v,e[1]=A*i+x*l+b*u+M*g,e[2]=A*a+x*h+b*f+M*y,e[3]=A*o+x*c+b*p+M*m,e[4]=(A=r[4])*n+(x=r[5])*s+(b=r[6])*d+(M=r[7])*v,e[5]=A*i+x*l+b*u+M*g,e[6]=A*a+x*h+b*f+M*y,e[7]=A*o+x*c+b*p+M*m,e[8]=(A=r[8])*n+(x=r[9])*s+(b=r[10])*d+(M=r[11])*v,e[9]=A*i+x*l+b*u+M*g,e[10]=A*a+x*h+b*f+M*y,e[11]=A*o+x*c+b*p+M*m,e[12]=(A=r[12])*n+(x=r[13])*s+(b=r[14])*d+(M=r[15])*v,e[13]=A*i+x*l+b*u+M*g,e[14]=A*a+x*h+b*f+M*y,e[15]=A*o+x*c+b*p+M*m,e}function h(e,t,r){var n,i,a,o,s,l,h,c,d,u,f,p,v=r[0],g=r[1],y=r[2];return t===e?(e[12]=t[0]*v+t[4]*g+t[8]*y+t[12],e[13]=t[1]*v+t[5]*g+t[9]*y+t[13],e[14]=t[2]*v+t[6]*g+t[10]*y+t[14],e[15]=t[3]*v+t[7]*g+t[11]*y+t[15]):(i=t[1],a=t[2],o=t[3],s=t[4],l=t[5],h=t[6],c=t[7],d=t[8],u=t[9],f=t[10],p=t[11],e[0]=n=t[0],e[1]=i,e[2]=a,e[3]=o,e[4]=s,e[5]=l,e[6]=h,e[7]=c,e[8]=d,e[9]=u,e[10]=f,e[11]=p,e[12]=n*v+s*g+d*y+t[12],e[13]=i*v+l*g+u*y+t[13],e[14]=a*v+h*g+f*y+t[14],e[15]=o*v+c*g+p*y+t[15]),e}function c(){var e=new a(3);return a!=Float32Array&&(e[0]=0,e[1]=0,e[2]=0),e}function d(e,t,r){var n=t[0],i=t[1],a=t[2],o=r[3]*n+r[7]*i+r[11]*a+r[15];return e[0]=(r[0]*n+r[4]*i+r[8]*a+r[12])/(o=o||1),e[1]=(r[1]*n+r[5]*i+r[9]*a+r[13])/o,e[2]=(r[2]*n+r[6]*i+r[10]*a+r[14])/o,e}Math.hypot||(Math.hypot=function(){for(var e=0,t=arguments.length;t--;)e+=arguments[t]*arguments[t];return Math.sqrt(e)});u=c();var u,f=function(e){return null==e},p={}.toString,v=function(e){return t="String",p.call(e)==="[object "+t+"]";var t},g=function(){function e(e){this.canvasRendererPluginOptions=void 0,this.context=void 0,this.pathGeneratorFactory=void 0,this.rBush=void 0,this.removedRBushNodeAABBs=[],this.renderQueue=[],this.restoreStack=[],this.clearFullScreen=!1,this.vpMatrix=o(),this.dprMatrix=o(),this.tmpMat4=o(),this.vec3a=c(),this.vec3b=c(),this.vec3c=c(),this.vec3d=c(),this.canvasRendererPluginOptions=e}var r=e.prototype;return r.apply=function(r,n){var i=this;this.context=r;var a=r.config,o=r.camera,s=r.renderingService,h=r.renderingContext,c=r.pathGeneratorFactory;this.rBush=r.rBushRoot,this.pathGeneratorFactory=c;var u=r.contextService,f=h.root.ownerDocument.defaultView,p=function(e){var t=e.target.rBushNode;t.aabb&&i.removedRBushNodeAABBs.push(t.aabb)},v=function(e){var t=e.target.rBushNode;t.aabb&&i.removedRBushNodeAABBs.push(t.aabb)};s.hooks.init.tap(e.tag,(function(){f.addEventListener(t.ElementEvent.UNMOUNTED,p),f.addEventListener(t.ElementEvent.CULLED,v);var e=u.getDPR(),r=a.width,n=a.height,o=u.getContext();i.clearRect(o,0,0,r*e,n*e,a.background)})),s.hooks.destroy.tap(e.tag,(function(){f.removeEventListener(t.ElementEvent.UNMOUNTED,p),f.removeEventListener(t.ElementEvent.CULLED,v)})),s.hooks.beginFrame.tap(e.tag,(function(){var e=u.getContext(),t=u.getDPR(),r=a.width,n=a.height,o=i.canvasRendererPluginOptions,l=o.dirtyObjectNumThreshold,h=o.dirtyObjectRatioThreshold,c=s.getStats(),d=c.rendered,f=d/c.total;i.clearFullScreen=s.disableDirtyRectangleRendering()||d>l&&f>h,e&&(e.resetTransform?e.resetTransform():e.setTransform(1,0,0,1,0,0),i.clearFullScreen&&i.clearRect(e,0,0,r*t,n*t,a.background))}));var g=function e(t,r){t.isVisible()&&!t.isCulled()&&(i.renderDisplayObject(t,r,i.context,i.restoreStack,n),i.saveDirtyAABB(t)),(t.sortable.sorted||t.childNodes).forEach((function(t){e(t,r)}))};s.hooks.endFrame.tap(e.tag,(function(){var e,r,s=u.getContext(),c=u.getDPR();if((e=i.dprMatrix)[0]=(r=[c,c,1])[0],e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[5]=r[1],e[6]=0,e[7]=0,e[8]=0,e[9]=0,e[10]=r[2],e[11]=0,e[12]=0,e[13]=0,e[14]=0,e[15]=1,l(i.vpMatrix,i.dprMatrix,o.getOrthoMatrix()),i.clearFullScreen)g(h.root,s);else{var p=i.safeMergeAABB.apply(i,[i.mergeDirtyAABBs(i.renderQueue)].concat(i.removedRBushNodeAABBs.map((function(e){var r=e.minX,n=e.minY,i=e.maxX,a=e.maxY,o=new t.AABB;return o.setMinMax([r,n,0],[i,a,0]),o}))));if(i.removedRBushNodeAABBs=[],t.AABB.isEmpty(p))return void(i.renderQueue=[]);var v=i.convertAABB2Rect(p),y=v.x,m=v.y,A=v.width,x=v.height,b=d(i.vec3a,[y,m,0],i.vpMatrix),M=d(i.vec3b,[y+A,m,0],i.vpMatrix),S=d(i.vec3c,[y,m+x,0],i.vpMatrix),B=d(i.vec3d,[y+A,m+x,0],i.vpMatrix),R=Math.min(b[0],M[0],B[0],S[0]),w=Math.min(b[1],M[1],B[1],S[1]),O=Math.max(b[0],M[0],B[0],S[0]),E=Math.max(b[1],M[1],B[1],S[1]),P=Math.floor(R),T=Math.floor(w),C=Math.ceil(O-R),N=Math.ceil(E-w);s.save(),i.clearRect(s,P,T,C,N,a.background),s.beginPath(),s.rect(P,T,C,N),s.clip(),s.setTransform(i.vpMatrix[0],i.vpMatrix[1],i.vpMatrix[4],i.vpMatrix[5],i.vpMatrix[12],i.vpMatrix[13]),a.renderer.getConfig().enableDirtyRectangleRenderingDebug&&f.dispatchEvent(new t.CustomEvent(t.CanvasEvent.DIRTY_RECTANGLE,{dirtyRect:{x:P,y:T,width:C,height:N}})),i.searchDirtyObjects(p).sort((function(e,t){return e.sortable.renderOrder-t.sortable.renderOrder})).forEach((function(e){e&&e.isVisible()&&!e.isCulled()&&i.renderDisplayObject(e,s,i.context,i.restoreStack,n)})),s.restore(),i.renderQueue.forEach((function(e){i.saveDirtyAABB(e)})),i.renderQueue=[]}i.restoreStack.forEach((function(){s.restore()})),i.restoreStack=[]})),s.hooks.render.tap(e.tag,(function(e){i.clearFullScreen||i.renderQueue.push(e)}))},r.clearRect=function(e,t,r,n,i,a){e.clearRect(t,r,n,i),a&&(e.fillStyle=a,e.fillRect(t,r,n,i))},r.renderDisplayObject=function(e,r,n,i,a){var o=e.nodeName,s=i[i.length-1];!s||e.compareDocumentPosition(s)&Node.DOCUMENT_POSITION_CONTAINS||(r.restore(),i.pop());var l=this.context.styleRendererFactory[o],h=this.pathGeneratorFactory[o],c=e.parsedStyle.clipPath;if(c){this.applyWorldTransform(r,c);var d=this.pathGeneratorFactory[c.nodeName];d&&(r.save(),i.push(e),r.beginPath(),d(r,c.parsedStyle),r.closePath(),r.clip())}l&&(this.applyWorldTransform(r,e),r.save(),this.applyAttributesToContext(r,e)),h&&(r.beginPath(),h(r,e.parsedStyle),e.nodeName!==t.Shape.LINE&&e.nodeName!==t.Shape.PATH&&e.nodeName!==t.Shape.POLYLINE&&r.closePath()),l&&(l.render(r,e.parsedStyle,e,n,this,a),r.restore()),e.renderable.dirty=!1},r.convertAABB2Rect=function(e){var t=e.getMin(),r=e.getMax(),n=Math.floor(t[0]),i=Math.floor(t[1]);return{x:n,y:i,width:Math.ceil(r[0])-n,height:Math.ceil(r[1])-i}},r.mergeDirtyAABBs=function(e){var r=new t.AABB;return e.forEach((function(e){var t=e.getRenderBounds();r.add(t);var n=e.renderable.dirtyRenderBounds;n&&r.add(n)})),r},r.searchDirtyObjects=function(e){var t=e.getMin(),r=t[0],n=t[1],i=e.getMax();return this.rBush.search({minX:r,minY:n,maxX:i[0],maxY:i[1]}).map((function(e){return e.displayObject}))},r.saveDirtyAABB=function(e){var r=e.renderable;r.dirtyRenderBounds||(r.dirtyRenderBounds=new t.AABB);var n=e.getRenderBounds();n&&r.dirtyRenderBounds.update(n.center,n.halfExtents)},r.applyAttributesToContext=function(e,t){var r=t.parsedStyle,n=r.stroke,i=r.fill,a=r.opacity,o=r.lineDash,s=r.lineDashOffset;o&&e.setLineDash(o),f(s)||(e.lineDashOffset=s),f(a)||(e.globalAlpha*=a),f(n)||Array.isArray(n)||n.isNone||(e.strokeStyle=t.attributes.stroke),f(i)||Array.isArray(i)||i.isNone||(e.fillStyle=t.attributes.fill)},r.applyWorldTransform=function(e,t,r){var n=0,i=0,a=(t.parsedStyle||{}).anchor,o=a&&a[0]||0,c=a&&a[1]||0;if(0!==o||0!==c){var d=t.geometry.contentBounds;n=-o*(d&&2*d.halfExtents[0]||0),i=-c*(d&&2*d.halfExtents[1]||0)}r?(s(this.tmpMat4,t.getLocalTransform()),this.vec3a[0]=n,this.vec3a[1]=i,this.vec3a[2]=0,h(this.tmpMat4,this.tmpMat4,this.vec3a),l(this.tmpMat4,r,this.tmpMat4),l(this.tmpMat4,this.vpMatrix,this.tmpMat4)):(s(this.tmpMat4,t.getWorldTransform()),this.vec3a[0]=n,this.vec3a[1]=i,this.vec3a[2]=0,h(this.tmpMat4,this.tmpMat4,this.vec3a),l(this.tmpMat4,this.vpMatrix,this.tmpMat4)),e.setTransform(this.tmpMat4[0],this.tmpMat4[1],this.tmpMat4[4],this.tmpMat4[5],this.tmpMat4[12],this.tmpMat4[13])},r.safeMergeAABB=function(){for(var e=new t.AABB,r=arguments.length,n=Array(r),i=0;r>i;i++)n[i]=arguments[i];return n.forEach((function(t){e.add(t)})),e},e}();g.tag="CanvasRenderer";var y=function(){function e(e){this.imagePool=void 0,this.imagePool=e}var n=e.prototype;return n.render=function(e,r,n,i,a,o){var s=r.fill,l=r.fillRule,h=r.opacity,c=r.fillOpacity,d=r.stroke,u=r.strokeOpacity,p=r.lineWidth,v=r.lineCap,g=r.lineJoin,y=r.shadowType,A=r.shadowColor,x=r.shadowBlur,b=r.filter,M=r.miterLimit,S=!f(s)&&!s.isNone,B=!f(d)&&!d.isNone&&p>0,R=0===s.alpha,w=!(!b||!b.length),O=!f(A)&&x>0,E=n.nodeName,P="inner"===y,T=B&&O&&(E===t.Shape.PATH||E===t.Shape.LINE||E===t.Shape.POLYLINE||R||P);S&&(e.globalAlpha=h*c,T||m(n,e,O),this.fill(e,n,s,l,i,a,o),T||this.clearShadowAndFilter(e,w,O)),B&&(e.globalAlpha=h*u,e.lineWidth=p,f(M)||(e.miterLimit=M),f(v)||(e.lineCap=v),f(g)||(e.lineJoin=g),T&&(P&&(e.globalCompositeOperation="source-atop"),m(n,e,!0),P&&(this.stroke(e,n,d,i,a,o),e.globalCompositeOperation="source-over",this.clearShadowAndFilter(e,w,!0))),this.stroke(e,n,d,i,a,o))},n.clearShadowAndFilter=function(e,t,r){if(r&&(e.shadowColor="transparent",e.shadowBlur=0),t){var n=e.filter;!f(n)&&n.indexOf("drop-shadow")>-1&&(e.filter=n.replace(/drop-shadow\([^)]*\)/,"").trim()||"none")}},n.fill=function(e,r,n,i,a,o,s){var l=this;Array.isArray(n)?n.forEach((function(t){e.fillStyle=l.getColor(t,r,e),i?e.fill(i):e.fill()})):(t.isPattern(n)&&(e.fillStyle=this.getPattern(n,r,e,a,o,s)),i?e.fill(i):e.fill())},n.stroke=function(e,r,n,i,a,o){var s=this;Array.isArray(n)?n.forEach((function(t){e.strokeStyle=s.getColor(t,r,e),e.stroke()})):(t.isPattern(n)&&(e.strokeStyle=this.getPattern(n,r,e,i,a,o)),e.stroke())},n.getPattern=function(e,t,r,n,i,a){var o,s;if("rect"===e.image.nodeName){var l=e.image.parsedStyle,h=l.width,c=l.height;s=n.contextService.getDPR();var d=n.config.offscreenCanvas;(o=a.offscreenCanvas.getOrCreateCanvas(d)).width=h*s,o.height=c*s;var u=a.offscreenCanvas.getOrCreateContext(d),f=[];e.image.forEach((function(e){i.renderDisplayObject(e,u,n,f,a)})),f.forEach((function(){u.restore()}))}return this.imagePool.getOrCreatePatternSync(e,r,o,s,(function(){t.renderable.dirty=!0,n.renderingService.dirtify()}))},n.getColor=function(e,n,i){var a;if(e.type===t.GradientType.LinearGradient||e.type===t.GradientType.RadialGradient){var o=n.getGeometryBounds();a=this.imagePool.getOrCreateGradient(r({type:e.type},e.value,{width:o&&2*o.halfExtents[0]||1,height:o&&2*o.halfExtents[1]||1}),i)}return a},e}();function m(e,t,r){var n=e.parsedStyle,i=n.filter,a=n.shadowColor,o=n.shadowBlur,s=n.shadowOffsetX,l=n.shadowOffsetY;i&&i.length&&(t.filter=e.style.filter),r&&(t.shadowColor=""+a,t.shadowBlur=o||0,t.shadowOffsetX=s||0,t.shadowOffsetY=l||0)}var A=function(){function e(e){this.imagePool=void 0,this.imagePool=e}return e.prototype.render=function(e,t,r){var n,i=t.img,a=t.shadowColor,o=t.shadowBlur,s=t.width,l=t.height;if(v(i)?n=this.imagePool.getImageSync(i):(s||(s=i.width),l||(l=i.height),n=i),n){m(r,e,!f(a)&&o>0);try{e.drawImage(n,0,0,s,l)}catch(e){}}},e}(),x=function(){function e(){}var r=e.prototype;return r.render=function(e,r,n){var i=r.lineWidth,a=r.textAlign,o=r.textBaseline,s=r.lineJoin,l=r.miterLimit,h=r.letterSpacing,c=r.stroke,d=r.fill,u=r.fillOpacity,p=r.strokeOpacity,v=r.opacity,g=r.metrics,y=r.dx,A=r.dy,x=r.shadowColor,b=r.shadowBlur,M=g.lines,S=g.height,B=g.lineHeight,R=g.lineMetrics;e.font=g.font,e.lineWidth=i,e.textAlign="middle"===a?"center":a;var w=o;t.runtime.enableCSSParsing||"alphabetic"!==w||(w="bottom"),e.lineJoin=s,f(l)||(e.miterLimit=l);var O=0;"middle"===o?O=-S/2-B/2:"bottom"===o||"alphabetic"===o||"ideographic"===o?O=-S:"top"!==o&&"hanging"!==o||(O=-B);var E=y||0;O+=A||0,1===M.length&&("bottom"===w?(w="middle",O-=.5*S):"top"===w&&(w="middle",O+=.5*S)),e.textBaseline=w,m(n,e,!f(x)&&b>0);for(var P=0;M.length>P;P++){var T=i/2+E;O+=B,f(c)||c.isNone||!i||this.drawLetterSpacing(e,M[P],R[P],a,T,O,h,u,p,v,!0),f(d)||this.drawLetterSpacing(e,M[P],R[P],a,T,O,h,u,p,v)}},r.drawLetterSpacing=function(e,t,r,n,i,a,o,s,l,h,c){if(void 0===c&&(c=!1),0!==o){var d=e.textAlign;e.textAlign="left";var u=i;"center"===n||"middle"===n?u=i-r.width/2:"right"!==n&&"end"!==n||(u=i-r.width);for(var f=Array.from(t),p=e.measureText(t).width,v=0,g=0;f.length>g;++g){var y=f[g];c?this.strokeText(e,y,u,a,l):this.fillText(e,y,u,a,s,h),u+=p-(v=e.measureText(t.substring(g+1)).width)+o,p=v}e.textAlign=d}else c?this.strokeText(e,t,i,a,l):this.fillText(e,t,i,a,s,h)},r.fillText=function(e,t,r,n,i,a){var o,s=!f(i)&&1!==i;s&&(o=e.globalAlpha,e.globalAlpha=i*a),e.fillText(t,r,n),s&&(e.globalAlpha=o)},r.strokeText=function(e,t,r,n,i){var a,o=!f(i)&&1!==i;o&&(a=e.globalAlpha,e.globalAlpha=i),e.strokeText(t,r,n),o&&(e.globalAlpha=a)},e}(),b=function(e){function t(){return e.apply(this,arguments)||this}return n(t,e),t}(y),M=function(e){function t(){return e.apply(this,arguments)||this}return n(t,e),t}(y),S=function(e){function t(){return e.apply(this,arguments)||this}return n(t,e),t}(y),B=function(e){function t(){return e.apply(this,arguments)||this}return n(t,e),t}(y),R=function(e){function t(){return e.apply(this,arguments)||this}return n(t,e),t}(y),w=function(e){function t(){return e.apply(this,arguments)||this}return n(t,e),t}(y),O=function(e){function t(){return e.apply(this,arguments)||this}return n(t,e),t}(y),E=function(e){function i(t){var r;return void 0===t&&(t={}),(r=e.call(this)||this).options=void 0,r.name="canvas-renderer",r.options=t,r}n(i,e);var a=i.prototype;return a.init=function(){var e,n=r({dirtyObjectNumThreshold:500,dirtyObjectRatioThreshold:.8},this.options),i=this.context.imagePool,a=new y(i),o=((e={})[t.Shape.CIRCLE]=a,e[t.Shape.ELLIPSE]=a,e[t.Shape.RECT]=a,e[t.Shape.IMAGE]=new A(i),e[t.Shape.TEXT]=new x,e[t.Shape.LINE]=a,e[t.Shape.POLYLINE]=a,e[t.Shape.POLYGON]=a,e[t.Shape.PATH]=a,e[t.Shape.GROUP]=void 0,e[t.Shape.HTML]=void 0,e[t.Shape.MESH]=void 0,e);this.context.defaultStyleRendererFactory=o,this.context.styleRendererFactory=o,this.addRenderingPlugin(new g(n))},a.destroy=function(){this.removeAllRenderingPlugins(),delete this.context.defaultStyleRendererFactory,delete this.context.styleRendererFactory},i}(t.AbstractRendererPlugin);e.CircleRenderer=M,e.EllipseRenderer=S,e.ImageRenderer=A,e.LineRenderer=B,e.PathRenderer=O,e.Plugin=E,e.PolygonRenderer=w,e.PolylineRenderer=R,e.RectRenderer=b,e.TextRenderer=x,Object.defineProperty(e,"__esModule",{value:!0})}));