UNPKG

fabric

Version:

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

1 lines 3.1 kB
{"version":3,"file":"changeWidth.min.mjs","sources":["../../../src/controls/changeWidth.ts"],"sourcesContent":["import type { TransformActionHandler } from '../EventTypeDefs';\nimport { CENTER, LEFT, RESIZING, RIGHT } from '../constants';\nimport { resolveOrigin } from '../util/misc/resolveOrigin';\nimport { getLocalPoint, isTransformCentered } from './util';\nimport { wrapWithFireEvent } from './wrapWithFireEvent';\nimport { wrapWithFixedAnchor } from './wrapWithFixedAnchor';\n\n/**\n * Action handler to change object's width\n * Needs to be wrapped with `wrapWithFixedAnchor` to be effective\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 eventData,\n transform,\n x,\n y,\n) => {\n const localPoint = getLocalPoint(\n transform,\n transform.originX,\n transform.originY,\n x,\n y,\n );\n // make sure the control changes width ONLY from it's side of target\n if (\n resolveOrigin(transform.originX) === resolveOrigin(CENTER) ||\n (resolveOrigin(transform.originX) === resolveOrigin(RIGHT) &&\n localPoint.x < 0) ||\n (resolveOrigin(transform.originX) === resolveOrigin(LEFT) &&\n localPoint.x > 0)\n ) {\n const { target } = transform,\n strokePadding =\n target.strokeWidth / (target.strokeUniform ? target.scaleX : 1),\n multiplier = isTransformCentered(transform) ? 2 : 1,\n oldWidth = target.width,\n newWidth =\n Math.abs((localPoint.x * multiplier) / target.scaleX) - strokePadding;\n target.set('width', Math.max(newWidth, 1));\n // check against actual target width in case `newWidth` was rejected\n return oldWidth !== target.width;\n }\n return false;\n};\n\nexport const changeWidth = wrapWithFireEvent(\n RESIZING,\n wrapWithFixedAnchor(changeObjectWidth),\n);\n"],"names":["changeObjectWidth","eventData","transform","x","y","localPoint","getLocalPoint","originX","originY","resolveOrigin","CENTER","RIGHT","LEFT","target","strokePadding","strokeWidth","strokeUniform","scaleX","multiplier","isTransformCentered","oldWidth","width","newWidth","Math","abs","set","max","changeWidth","wrapWithFireEvent","RESIZING","wrapWithFixedAnchor"],"mappings":"gWAgBO,MAAMA,EAA4CA,CACvDC,EACAC,EACAC,EACAC,KAEA,MAAMC,EAAaC,EACjBJ,EACAA,EAAUK,QACVL,EAAUM,QACVL,EACAC,GAGF,GACEK,EAAcP,EAAUK,WAAaE,EAAcC,IAClDD,EAAcP,EAAUK,WAAaE,EAAcE,IAClDN,EAAWF,EAAI,GAChBM,EAAcP,EAAUK,WAAaE,EAAcG,IAClDP,EAAWF,EAAI,EACjB,CACA,MAAMU,OAAEA,GAAWX,EACjBY,EACED,EAAOE,aAAeF,EAAOG,cAAgBH,EAAOI,OAAS,GAC/DC,EAAaC,EAAoBjB,GAAa,EAAI,EAClDkB,EAAWP,EAAOQ,MAClBC,EACEC,KAAKC,IAAKnB,EAAWF,EAAIe,EAAcL,EAAOI,QAAUH,EAG5D,OAFAD,EAAOY,IAAI,QAASF,KAAKG,IAAIJ,EAAU,IAEhCF,IAAaP,EAAOQ,KAC7B,CACA,OAAO,CAAK,EAGDM,EAAcC,EACzBC,EACAC,EAAoB9B"}