UNPKG

fabric

Version:

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

1 lines 5.6 kB
{"version":3,"file":"changeWidth.mjs","sources":["../../../src/controls/changeWidth.ts"],"sourcesContent":["import type { TransformActionHandler } from '../EventTypeDefs';\nimport { RESIZING } from '../constants';\nimport { resolveOrigin } from '../util/misc/resolveOrigin';\nimport { getLocalPoint, isTransformCentered } from './util';\nimport { wrapWithFireEvent } from './wrapWithFireEvent';\nimport { wrapWithFixedAnchor } from './wrapWithFixedAnchor';\n\nexport const changeObjectDimensionGen =\n (\n dimension: 'width' | 'height',\n origin: 'originX' | 'originY',\n xorY: 'x' | 'y',\n scale: 'scaleX' | 'scaleY',\n ): TransformActionHandler =>\n (eventData, transform, x, y) => {\n const localPoint = getLocalPoint(\n transform,\n transform.originX,\n transform.originY,\n x,\n y,\n );\n const localPointValue = localPoint[xorY];\n // make sure the control changes width ONLY from it's side of target\n const originValue = resolveOrigin(transform[origin]);\n if (\n originValue === 0 ||\n (originValue > 0 && localPointValue < 0) ||\n (originValue < 0 && localPointValue > 0)\n ) {\n const { target } = transform,\n strokePadding =\n target.strokeWidth / (target.strokeUniform ? target[scale] : 1),\n multiplier = isTransformCentered(transform) ? 2 : 1,\n oldWidth = target[dimension],\n newWidth =\n Math.abs((localPointValue * multiplier) / target[scale]) -\n strokePadding;\n target.set(dimension, Math.max(newWidth, 1));\n // check against actual target width in case `newWidth` was rejected\n return oldWidth !== target[dimension];\n }\n return false;\n };\n\n/**\n * Action handler to change object's width\n * Needs to be wrapped with `wrapWithFixedAnchor` to be effective\n * You want to use this only if you are building a new control handler and you want\n * to reuse some logic. use \"changeWidth\" if you are looking to just use a control for width\n * @param {Event} eventData javascript event that is doing the transform\n * @param {Object} transform javascript object containing a series of information around the current transform\n * @param {number} x current mouse x position, canvas normalized\n * @param {number} y current mouse y position, canvas normalized\n * @return {Boolean} true if some change happened\n */\nexport const changeObjectWidth: TransformActionHandler =\n changeObjectDimensionGen('width', 'originX', 'x', 'scaleX');\n\n/**\n * Action handler to change object's height\n * Needs to be wrapped with `wrapWithFixedAnchor` to be effective\n * You want to use this only if you are building a new control handler and you want\n * to reuse some logic. use \"changeHeight\" if you are looking to just use a control for height\n * @param {Event} eventData javascript event that is doing the transform\n * @param {Object} transform javascript object containing a series of information around the current transform\n * @param {number} x current mouse x position, canvas normalized\n * @param {number} y current mouse y position, canvas normalized\n * @return {Boolean} true if some change happened\n */\nexport const changeObjectHeight: TransformActionHandler =\n changeObjectDimensionGen('height', 'originY', 'y', 'scaleY');\n\n/**\n * Control handler for changing width\n */\nexport const changeWidth = wrapWithFireEvent(\n RESIZING,\n wrapWithFixedAnchor(changeObjectWidth),\n);\n\n/**\n * Control handler for changing height\n */\nexport const changeHeight = wrapWithFireEvent(\n RESIZING,\n wrapWithFixedAnchor(changeObjectHeight),\n);\n"],"names":["changeObjectDimensionGen","dimension","origin","xorY","scale","eventData","transform","x","y","localPoint","getLocalPoint","originX","originY","localPointValue","originValue","resolveOrigin","target","strokePadding","strokeWidth","strokeUniform","multiplier","isTransformCentered","oldWidth","newWidth","Math","abs","set","max","changeObjectWidth","changeObjectHeight","changeWidth","wrapWithFireEvent","RESIZING","wrapWithFixedAnchor","changeHeight"],"mappings":";;;;;;AAOO,MAAMA,wBAAwB,GACnCA,CACEC,SAA6B,EAC7BC,MAA6B,EAC7BC,IAAe,EACfC,KAA0B,KAE5B,CAACC,SAAS,EAAEC,SAAS,EAAEC,CAAC,EAAEC,CAAC,KAAK;AAC9B,EAAA,MAAMC,UAAU,GAAGC,aAAa,CAC9BJ,SAAS,EACTA,SAAS,CAACK,OAAO,EACjBL,SAAS,CAACM,OAAO,EACjBL,CAAC,EACDC,CACF,CAAC;AACD,EAAA,MAAMK,eAAe,GAAGJ,UAAU,CAACN,IAAI,CAAC;AACxC;EACA,MAAMW,WAAW,GAAGC,aAAa,CAACT,SAAS,CAACJ,MAAM,CAAC,CAAC;AACpD,EAAA,IACEY,WAAW,KAAK,CAA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