{"version":3,"file":"changeWidth.min.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","localPointValue","getLocalPoint","originX","originY","originValue","resolveOrigin","target","strokePadding","strokeWidth","strokeUniform","multiplier","isTransformCentered","oldWidth","newWidth","Math","abs","set","max","changeObjectWidth","changeObjectHeight","changeWidth","wrapWithFireEvent","RESIZING","wrapWithFixedAnchor","changeHeight"],"mappings":"+TAOO,MAAMA,EACXA,CACEC,EACAC,EACAC,EACAC,IAEF,CAACC,EAAWC,EAAWC,EAAGC,KACxB,MAOMC,EAPaC,EACjBJ,EACAA,EAAUK,QACVL,EAAUM,QACVL,EACAC,GAEiCL,GAE7BU,EAAcC,EAAcR,EAAUJ,IAC5C,GACkB,IAAhBW,GACCA,EAAc,GAAKJ,EAAkB,GACrCI,EAAc,GAAKJ,EAAkB,EACtC,CACA,MAAMM,OAAEA,GAAWT,EACjBU,EACED,EAAOE,aAAeF,EAAOG,cAAgBH,EAAOX,GAAS,GAC/De,EAAaC,EAAoBd,GAAa,EAAI,EAClDe,EAAWN,EAAOd,GAClBqB,EACEC,KAAKC,IAAKf,EAAkBU,EAAcJ,EAAOX,IACjDY,EAGJ,OAFAD,EAAOU,IAAIxB,EAAWsB,KAAKG,IAAIJ,EAAU,IAElCD,IAAaN,EAAOd,EAC7B,CACA,OAAO,GAcE0B,EACX3B,EAAyB,QAAS,UAAW,IAAK,UAavC4B,EACX5B,EAAyB,SAAU,UAAW,IAAK,UAKxC6B,EAAcC,EACzBC,EACAC,EAAoBL,IAMTM,EAAeH,EAC1BC,EACAC,EAAoBJ"}