UNPKG

fabric

Version:

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

1 lines 4.35 kB
{"version":3,"file":"changeWidth.mjs","names":[],"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"],"mappings":";;;;;;AAOA,MAAa,4BAET,WACA,QACA,MACA,WAED,WAAW,WAAW,GAAG,MAAM;CAQ9B,MAAM,kBAPa,cACjB,WACA,UAAU,SACV,UAAU,SACV,GACA,EACD,CACkC;CAEnC,MAAM,cAAc,cAAc,UAAU,QAAQ;AACpD,KACE,gBAAgB,KACf,cAAc,KAAK,kBAAkB,KACrC,cAAc,KAAK,kBAAkB,GACtC;EACA,MAAM,EAAE,WAAW,WACjB,gBACE,OAAO,eAAe,OAAO,gBAAgB,OAAO,SAAS,IAC/D,aAAa,oBAAoB,UAAU,GAAG,IAAI,GAClD,WAAW,OAAO,YAClB,WACE,KAAK,IAAK,kBAAkB,aAAc,OAAO,OAAO,GACxD;AACJ,SAAO,IAAI,WAAW,KAAK,IAAI,UAAU,EAAE,CAAC;AAE5C,SAAO,aAAa,OAAO;;AAE7B,QAAO;;;;;;;;;;;;;AAcX,MAAa,oBACX,yBAAyB,SAAS,WAAW,KAAK,SAAS;;;;;;;;;;;;AAa7D,MAAa,qBACX,yBAAyB,UAAU,WAAW,KAAK,SAAS;;;;AAK9D,MAAa,cAAc,kBACzB,UACA,oBAAoB,kBAAkB,CACvC;;;;AAKD,MAAa,eAAe,kBAC1B,UACA,oBAAoB,mBAAmB,CACxC"}