UNPKG

fabric

Version:

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

1 lines 3.71 kB
{"version":3,"file":"changeWidth.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":";;;;;;AAOA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,iBAAyC,GAAGA,CACvDC,SAAS,EACTC,SAAS,EACTC,CAAC,EACDC,CAAC,KACE;AACH,EAAA,MAAMC,UAAU,GAAGC,aAAa,CAC9BJ,SAAS,EACTA,SAAS,CAACK,OAAO,EACjBL,SAAS,CAACM,OAAO,EACjBL,CAAC,EACDC,CACF,CAAC,CAAA;AACD;EACA,IACEK,aAAa,CAACP,SAAS,CAACK,OAAO,CAAC,KAAKE,aAAa,CAACC,MAAM,CAAC,IACzDD,aAAa,CAACP,SAAS,CAACK,OAAO,CAAC,KAAKE,aAAa,CAACE,KAAK,CAAC,IACxDN,UAAU,CAACF,CAAC,GAAG,CAAE,IAClBM,aAAa,CAACP,SAAS,CAACK,OAAO,CAAC,KAAKE,aAAa,CAACG,IAAI,CAAC,IACvDP,UAAU,CAACF,CAAC,GAAG,CAAE,EACnB;IACA,MAAM;AAAEU,QAAAA,MAAAA;AAAO,OAAC,GAAGX,SAAS;AAC1BY,MAAAA,aAAa,GACXD,MAAM,CAACE,WAAW,IAAIF,MAAM,CAACG,aAAa,GAAGH,MAAM,CAACI,MAAM,GAAG,CAAC,CAAC;MACjEC,UAAU,GAAGC,mBAAmB,CAACjB,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC;MACnDkB,QAAQ,GAAGP,MAAM,CAACQ,KAAK;AACvBC,MAAAA,QAAQ,GACNC,IAAI,CAACC,GAAG,CAAEnB,UAAU,CAACF,CAAC,GAAGe,UAAU,GAAIL,MAAM,CAACI,MAAM,CAAC,GAAGH,aAAa,CAAA;AACzED,IAAAA,MAAM,CAACY,GAAG,CAAC,OAAO,EAAEF,IAAI,CAACG,GAAG,CAACJ,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAA;AAC1C;AACA,IAAA,OAAOF,QAAQ,KAAKP,MAAM,CAACQ,KAAK,CAAA;AAClC,GAAA;AACA,EAAA,OAAO,KAAK,CAAA;AACd,EAAC;AAEM,MAAMM,WAAW,GAAGC,iBAAiB,CAC1CC,QAAQ,EACRC,mBAAmB,CAAC9B,iBAAiB,CACvC;;;;"}