UNPKG

fabric

Version:

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

3 lines (2 loc) 6.84 kB
import{SCALE_X as t,SCALE_Y as i,iMatrix as e,CENTER as s,LEFT as n,TOP as r}from"../../constants.min.mjs";import{Intersection as o}from"../../Intersection.min.mjs";import{Point as h}from"../../Point.min.mjs";import{makeBoundingBoxFromPoints as a}from"../../util/misc/boundingBoxFromPoints.min.mjs";import{transformPoint as l,invertTransform as g,calcPlaneRotation as c,createRotateMatrix as m,multiplyTransformMatrices as u,composeMatrix as d,calcDimensionsMatrix as f,createTranslateMatrix as p}from"../../util/misc/matrix.min.mjs";import{radiansToDegrees as v,degreesToRadians as y}from"../../util/misc/radiansDegreesConversion.min.mjs";import{resolveOrigin as C}from"../../util/misc/resolveOrigin.min.mjs";import{sizeAfterTransform as x}from"../../util/misc/objectTransforms.min.mjs";import{CommonMethods as X}from"../../CommonMethods.min.mjs";class Y extends X{getX(){return this.getXY().x}setX(t){this.setXY(this.getXY().setX(t))}getY(){return this.getXY().y}setY(t){this.setXY(this.getXY().setY(t))}getRelativeX(){return this.left}setRelativeX(t){this.left=t}getRelativeY(){return this.top}setRelativeY(t){this.top=t}getXY(){const t=this.getRelativeXY();return this.group?l(t,this.group.calcTransformMatrix()):t}setXY(t,i,e){this.group&&(t=l(t,g(this.group.calcTransformMatrix()))),this.setRelativeXY(t,i,e)}getRelativeXY(){return new h(this.left,this.top)}setRelativeXY(t){let i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.originX,e=arguments.length>2&&void 0!==arguments[2]?arguments[2]:this.originY;this.setPositionByOrigin(t,i,e)}isStrokeAccountedForInDimensions(){return!1}getCoords(){const{tl:t,tr:i,br:e,bl:s}=this.aCoords||(this.aCoords=this.calcACoords()),n=[t,i,e,s];if(this.group){const t=this.group.calcTransformMatrix();return n.map(i=>l(i,t))}return n}intersectsWithRect(t,i){return"Intersection"===o.intersectPolygonRectangle(this.getCoords(),t,i).status}intersectsWithObject(t){const i=o.intersectPolygonPolygon(this.getCoords(),t.getCoords());return"Intersection"===i.status||"Coincident"===i.status||t.isContainedWithinObject(this)||this.isContainedWithinObject(t)}isContainedWithinObject(t){return this.getCoords().every(i=>t.containsPoint(i))}isContainedWithinRect(t,i){const{left:e,top:s,width:n,height:r}=this.getBoundingRect();return e>=t.x&&e+n<=i.x&&s>=t.y&&s+r<=i.y}isOverlapping(t){return this.intersectsWithObject(t)||this.isContainedWithinObject(t)||t.isContainedWithinObject(this)}containsPoint(t){return o.isPointInPolygon(t,this.getCoords())}isOnScreen(){if(!this.canvas)return!1;const{tl:t,br:i}=this.canvas.vptCoords;return!!this.getCoords().some(e=>e.x<=i.x&&e.x>=t.x&&e.y<=i.y&&e.y>=t.y)||(!!this.intersectsWithRect(t,i)||this.containsPoint(t.midPointFrom(i)))}isPartiallyOnScreen(){if(!this.canvas)return!1;const{tl:t,br:i}=this.canvas.vptCoords;if(this.intersectsWithRect(t,i))return!0;return this.getCoords().every(e=>(e.x>=i.x||e.x<=t.x)&&(e.y>=i.y||e.y<=t.y))&&this.containsPoint(t.midPointFrom(i))}getBoundingRect(){return a(this.getCoords())}getScaledWidth(){return this._getTransformedDimensions().x}getScaledHeight(){return this._getTransformedDimensions().y}scale(e){this._set(t,e),this._set(i,e),this.setCoords()}scaleToWidth(t){const i=this.getBoundingRect().width/this.getScaledWidth();return this.scale(t/this.width/i)}scaleToHeight(t){const i=this.getBoundingRect().height/this.getScaledHeight();return this.scale(t/this.height/i)}getCanvasRetinaScaling(){var t;return(null===(t=this.canvas)||void 0===t?void 0:t.getRetinaScaling())||1}getTotalAngle(){return this.group?v(c(this.calcTransformMatrix())):this.angle}getViewportTransform(){var t;return(null===(t=this.canvas)||void 0===t?void 0:t.viewportTransform)||e.concat()}calcACoords(){const t=m({angle:this.angle}),{x:i,y:e}=this.getRelativeCenterPoint(),s=p(i,e),n=u(s,t),r=this._getTransformedDimensions(),o=r.x/2,h=r.y/2;return{tl:l({x:-o,y:-h},n),tr:l({x:o,y:-h},n),bl:l({x:-o,y:h},n),br:l({x:o,y:h},n)}}setCoords(){this.aCoords=this.calcACoords()}transformMatrixKey(){let t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],i=[];return!t&&this.group&&(i=this.group.transformMatrixKey(t)),i.push(this.top,this.left,this.width,this.height,this.scaleX,this.scaleY,this.angle,this.strokeWidth,this.skewX,this.skewY,+this.flipX,+this.flipY,C(this.originX),C(this.originY)),i}calcTransformMatrix(){let t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],i=this.calcOwnMatrix();if(t||!this.group)return i;const e=this.transformMatrixKey(t),s=this.matrixCache;return s&&s.key.every((t,i)=>t===e[i])?s.value:(this.group&&(i=u(this.group.calcTransformMatrix(!1),i)),this.matrixCache={key:e,value:i},i)}calcOwnMatrix(){const t=this.transformMatrixKey(!0),i=this.ownMatrixCache;if(i&&i.key.every((i,e)=>i===t[e]))return i.value;const e=this.getRelativeCenterPoint(),s={angle:this.angle,translateX:e.x,translateY:e.y,scaleX:this.scaleX,scaleY:this.scaleY,skewX:this.skewX,skewY:this.skewY,flipX:this.flipX,flipY:this.flipY},n=d(s);return this.ownMatrixCache={key:t,value:n},n}_getNonTransformedDimensions(){return new h(this.width,this.height).scalarAdd(this.strokeWidth)}_calculateCurrentDimensions(t){return this._getTransformedDimensions(t).transform(this.getViewportTransform(),!0).scalarAdd(2*this.padding)}_getTransformedDimensions(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};const i={scaleX:this.scaleX,scaleY:this.scaleY,skewX:this.skewX,skewY:this.skewY,width:this.width,height:this.height,strokeWidth:this.strokeWidth,...t},e=i.strokeWidth;let s=e,n=0;this.strokeUniform&&(s=0,n=e);const r=i.width+s,o=i.height+s;let a;return a=0===i.skewX&&0===i.skewY?new h(r*i.scaleX,o*i.scaleY):x(r,o,f(i)),a.scalarAdd(n)}translateToGivenOrigin(t,i,e,s,n){let r=t.x,o=t.y;const a=C(s)-C(i),l=C(n)-C(e);if(a||l){const t=this._getTransformedDimensions();r+=a*t.x,o+=l*t.y}return new h(r,o)}translateToCenterPoint(t,i,e){if(i===s&&e===s)return t;const n=this.translateToGivenOrigin(t,i,e,s,s);return this.angle?n.rotate(y(this.angle),t):n}translateToOriginPoint(t,i,e){const n=this.translateToGivenOrigin(t,s,s,i,e);return this.angle?n.rotate(y(this.angle),t):n}getCenterPoint(){const t=this.getRelativeCenterPoint();return this.group?l(t,this.group.calcTransformMatrix()):t}getRelativeCenterPoint(){return this.translateToCenterPoint(new h(this.left,this.top),this.originX,this.originY)}getPointByOrigin(t,i){return this.getPositionByOrigin(t,i)}getPositionByOrigin(t,i){return this.translateToOriginPoint(this.getRelativeCenterPoint(),t,i)}setPositionByOrigin(t,i,e){const s=this.translateToCenterPoint(t,i,e),n=this.translateToOriginPoint(s,this.originX,this.originY);this.set({left:n.x,top:n.y})}_getLeftTopCoords(){return this.getPositionByOrigin(n,r)}positionByLeftTop(t){return this.setPositionByOrigin(t,n,r)}}export{Y as ObjectGeometry}; //# sourceMappingURL=ObjectGeometry.min.mjs.map