UNPKG

fabric

Version:

Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.

2 lines 3.52 kB
import{_defineProperty as e}from"../../_virtual/_@oxc-project_runtime@0.122.0/helpers/defineProperty.min.mjs";import{config as t}from"../config.min.mjs";import"../constants.min.mjs";import{classRegistry as n}from"../ClassRegistry.min.mjs";import{Point as r}from"../Point.min.mjs";import{toFixed as i}from"../util/misc/toFixed.min.mjs";import{makeBoundingBoxFromPoints as a}from"../util/misc/boundingBoxFromPoints.min.mjs";import{cacheProperties as o}from"./Object/defaultValues.min.mjs";import{FabricObject as s}from"./Object/FabricObject.min.mjs";import{SHARED_ATTRIBUTES as c}from"../parser/attributes.min.mjs";import{parseAttributes as l}from"../parser/parseAttributes.min.mjs";import{getBoundsOfCurve as u,joinPath as d,makePathSimpler as f,parsePath as p}from"../util/path/index.min.mjs";var m=class e extends s{constructor(t,{path:n,left:r,top:i,...a}={}){super(),Object.assign(this,e.ownDefaults),this.setOptions(a),this._setPath(t||[],!0),typeof r==`number`&&this.set(`left`,r),typeof i==`number`&&this.set(`top`,i)}_setPath(e,t){this.path=f(Array.isArray(e)?e:p(e)),this.setBoundingBox(t)}_findCenterFromElement(){let e=this._calcBoundsFromPath();return new r(e.left+e.width/2,e.top+e.height/2)}_renderPathCommands(e){let t=-this.pathOffset.x,n=-this.pathOffset.y;e.beginPath();for(let r of this.path)switch(r[0]){case`L`:e.lineTo(r[1]+t,r[2]+n);break;case`M`:e.moveTo(r[1]+t,r[2]+n);break;case`C`:e.bezierCurveTo(r[1]+t,r[2]+n,r[3]+t,r[4]+n,r[5]+t,r[6]+n);break;case`Q`:e.quadraticCurveTo(r[1]+t,r[2]+n,r[3]+t,r[4]+n);break;case`Z`:e.closePath()}}_render(e){this._renderPathCommands(e),this._renderPaintInOrder(e)}toString(){return`#<Path (${this.complexity()}): { "top": ${this.top}, "left": ${this.left} }>`}toObject(e=[]){return{...super.toObject(e),path:this.path.map(e=>e.slice())}}toDatalessObject(e=[]){let t=this.toObject(e);return this.sourcePath&&(delete t.path,t.sourcePath=this.sourcePath),t}_toSVG(){return[`<path `,`COMMON_PARTS`,`d="${d(this.path,t.NUM_FRACTION_DIGITS)}" stroke-linecap="round" />\n`]}_getOffsetTransform(){let e=t.NUM_FRACTION_DIGITS;return` translate(${i(-this.pathOffset.x,e)}, ${i(-this.pathOffset.y,e)})`}toClipPathSVG(e){let t=this._getOffsetTransform();return` `+this._createBaseClipPathSVGMarkup(this._toSVG(),{reviver:e,additionalTransform:t})}toSVG(e){let t=this._getOffsetTransform();return this._createBaseSVGMarkup(this._toSVG(),{reviver:e,additionalTransform:t})}complexity(){return this.path.length}setDimensions(){this.setBoundingBox()}setBoundingBox(e){let{width:t,height:n,pathOffset:r}=this._calcDimensions();this.set({width:t,height:n,pathOffset:r}),e&&this.setPositionByOrigin(r,`center`,`center`)}_calcBoundsFromPath(){let e=[],t=0,n=0,r=0,i=0;for(let a of this.path)switch(a[0]){case`L`:r=a[1],i=a[2],e.push({x:t,y:n},{x:r,y:i});break;case`M`:r=a[1],i=a[2],t=r,n=i;break;case`C`:e.push(...u(r,i,a[1],a[2],a[3],a[4],a[5],a[6])),r=a[5],i=a[6];break;case`Q`:e.push(...u(r,i,a[1],a[2],a[1],a[2],a[3],a[4])),r=a[3],i=a[4];break;case`Z`:r=t,i=n}return a(e)}_calcDimensions(){let e=this._calcBoundsFromPath();return{...e,pathOffset:new r(e.left+e.width/2,e.top+e.height/2)}}static fromObject(e){return this._fromObject(e,{extraParam:`path`})}static async fromElement(e,t,n){let{d:r,...i}=l(e,this.ATTRIBUTE_NAMES,n);return new this(r,{...i,...t,left:void 0,top:void 0})}};e(m,`type`,`Path`),e(m,`cacheProperties`,[...o,`path`,`fillRule`]),e(m,`ATTRIBUTE_NAMES`,[...c,`d`]),n.setClass(m),n.setSVGClass(m);export{m as Path}; //# sourceMappingURL=Path.min.mjs.map