fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 5.32 kB
Source Map (JSON)
{"version":3,"file":"objectTransforms.mjs","names":[],"sources":["../../../../src/util/misc/objectTransforms.ts"],"sourcesContent":["import { Point } from '../../Point';\nimport { CENTER } from '../../constants';\nimport type { FabricObject } from '../../shapes/Object/Object';\nimport type { TMat2D } from '../../typedefs';\nimport { makeBoundingBoxFromPoints } from './boundingBoxFromPoints';\nimport {\n invertTransform,\n multiplyTransformMatrices,\n qrDecompose,\n} from './matrix';\n\n/**\n * given an object and a transform, apply the inverse transform to the object,\n * this is equivalent to remove from that object that transformation, so that\n * added in a space with the removed transform, the object will be the same as before.\n * Removing from an object a transform that scale by 2 is like scaling it by 1/2.\n * Removing from an object a transform that rotate by 30deg is like rotating by 30deg\n * in the opposite direction.\n * This util is used to add objects inside transformed groups or nested groups.\n * @param {FabricObject} object the object you want to transform\n * @param {TMat2D} transform the destination transform\n */\nexport const removeTransformFromObject = (\n object: FabricObject,\n transform: TMat2D,\n) => {\n const inverted = invertTransform(transform),\n finalTransform = multiplyTransformMatrices(\n inverted,\n object.calcOwnMatrix(),\n );\n applyTransformToObject(object, finalTransform);\n};\n\n/**\n * given an object and a transform, apply the transform to the object.\n * this is equivalent to change the space where the object is drawn.\n * Adding to an object a transform that scale by 2 is like scaling it by 2.\n * This is used when removing an object from an active selection for example.\n * @param {FabricObject} object the object you want to transform\n * @param {Array} transform the destination transform\n */\nexport const addTransformToObject = (object: FabricObject, transform: TMat2D) =>\n applyTransformToObject(\n object,\n multiplyTransformMatrices(transform, object.calcOwnMatrix()),\n );\n\n/**\n * discard an object transform state and apply the one from the matrix.\n * @param {FabricObject} object the object you want to transform\n * @param {Array} transform the destination transform\n */\nexport const applyTransformToObject = (\n object: FabricObject,\n transform: TMat2D,\n) => {\n const { translateX, translateY, scaleX, scaleY, ...otherOptions } =\n qrDecompose(transform),\n center = new Point(translateX, translateY);\n object.flipX = false;\n object.flipY = false;\n Object.assign(object, otherOptions);\n object.set({ scaleX, scaleY });\n object.setPositionByOrigin(center, CENTER, CENTER);\n};\n/**\n * reset an object transform state to neutral. Top and left are not accounted for\n * @param {FabricObject} target object to transform\n */\nexport const resetObjectTransform = (target: FabricObject) => {\n target.scaleX = 1;\n target.scaleY = 1;\n target.skewX = 0;\n target.skewY = 0;\n target.flipX = false;\n target.flipY = false;\n target.rotate(0);\n};\n\n/**\n * Extract Object transform values\n * @param {FabricObject} target object to read from\n * @return {Object} Components of transform\n */\nexport const saveObjectTransform = (target: FabricObject) => ({\n scaleX: target.scaleX,\n scaleY: target.scaleY,\n skewX: target.skewX,\n skewY: target.skewY,\n angle: target.angle,\n left: target.left,\n flipX: target.flipX,\n flipY: target.flipY,\n top: target.top,\n});\n\n/**\n * given a width and height, return the size of the bounding box\n * that can contains the box with width/height with applied transform.\n * Use to calculate the boxes around objects for controls.\n * @param {Number} width\n * @param {Number} height\n * @param {TMat2D} t\n * @returns {Point} size\n */\nexport const sizeAfterTransform = (\n width: number,\n height: number,\n t: TMat2D,\n) => {\n const dimX = width / 2,\n dimY = height / 2,\n points = [\n new Point(-dimX, -dimY),\n new Point(dimX, -dimY),\n new Point(-dimX, dimY),\n new Point(dimX, dimY),\n ].map((p) => p.transform(t)),\n bbox = makeBoundingBoxFromPoints(points);\n return new Point(bbox.width, bbox.height);\n};\n"],"mappings":";;;;;;;;;;;;;;;;AAsBA,MAAa,6BACX,QACA,cACG;AAMH,wBAAuB,QAJJ,0BADF,gBAAgB,UAAU,EAGvC,OAAO,eAAe,CACvB,CAC2C;;;;;;;;;;AAWhD,MAAa,wBAAwB,QAAsB,cACzD,uBACE,QACA,0BAA0B,WAAW,OAAO,eAAe,CAAC,CAC7D;;;;;;AAOH,MAAa,0BACX,QACA,cACG;CACH,MAAM,EAAE,YAAY,YAAY,QAAQ,QAAQ,GAAG,iBAC/C,YAAY,UAAU,EACxB,SAAS,IAAI,MAAM,YAAY,WAAW;AAC5C,QAAO,QAA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