{"version":3,"file":"changeWidth.min.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":"+TAOA,MAAa,GAET,EACA,EACA,EACA,KAED,EAAW,EAAW,EAAG,IAAA,CAQxB,IAAM,EAPa,EACjB,EACA,EAAU,QACV,EAAU,QACV,EACA,EAAA,CAEiC,GAE7B,EAAc,EAAc,EAAU,GAAA,CAC5C,GACE,IAAgB,GACf,EAAc,GAAK,EAAkB,GACrC,EAAc,GAAK,EAAkB,EACtC,CACA,GAAA,CAAM,OAAE,GAAW,EACjB,EACE,EAAO,aAAe,EAAO,cAAgB,EAAO,GAAS,GAC/D,EAAa,EAAoB,EAAA,CAAa,EAAI,EAClD,EAAW,EAAO,GAClB,EACE,KAAK,IAAK,EAAkB,EAAc,EAAO,GAAA,CACjD,EAGJ,OAFA,EAAO,IAAI,EAAW,KAAK,IAAI,EAAU,EAAA,CAAA,CAElC,IAAa,EAAO,GAE7B,MAAA,CAAO,GAcE,EACX,EAAyB,QAAS,UAAW,IAAK,SAAA,CAavC,EACX,EAAyB,SAAU,UAAW,IAAK,SAAA,CAKxC,EAAc,EACzB,EACA,EAAoB,EAAA,CAAA,CAMT,EAAe,EAC1B,EACA,EAAoB,EAAA,CAAA,CAAA,OAAA,KAAA,aAAA,KAAA,mBAAA,KAAA,kBAAA,KAAA"}