fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 12.6 kB
Source Map (JSON)
{"version":3,"file":"scale.min.mjs","names":[],"sources":["../../../src/controls/scale.ts"],"sourcesContent":["import type {\n ControlCursorCallback,\n TPointerEvent,\n Transform,\n TransformActionHandler,\n} from '../EventTypeDefs';\nimport type { FabricObject } from '../shapes/Object/FabricObject';\nimport type { TAxis } from '../typedefs';\nimport type { Canvas } from '../canvas/Canvas';\nimport {\n findCornerQuadrant,\n getLocalPoint,\n invertOrigin,\n isLocked,\n isTransformCentered,\n NOT_ALLOWED_CURSOR,\n} from './util';\nimport { wrapWithFireEvent } from './wrapWithFireEvent';\nimport { wrapWithFixedAnchor } from './wrapWithFixedAnchor';\nimport { SCALE_X, SCALE_Y, SCALING } from '../constants';\n\ntype ScaleTransform = Transform & {\n gestureScale?: number;\n signX?: number;\n signY?: number;\n};\n\ntype ScaleBy = TAxis | 'equally' | '' | undefined;\n\n/**\n * Inspect event and fabricObject properties to understand if the scaling action\n * @param {Event} eventData from the user action\n * @param {FabricObject} fabricObject the fabric object about to scale\n * @return {Boolean} true if scale is proportional\n */\nexport function scaleIsProportional(\n eventData: TPointerEvent,\n fabricObject: FabricObject,\n): boolean {\n const canvas = fabricObject.canvas as Canvas,\n uniformIsToggled = eventData[canvas.uniScaleKey!];\n return (\n (canvas.uniformScaling && !uniformIsToggled) ||\n (!canvas.uniformScaling && uniformIsToggled)\n );\n}\n\n/**\n * Inspect fabricObject to understand if the current scaling action is allowed\n * @param {FabricObject} fabricObject the fabric object about to scale\n * @param {String} by 'x' or 'y' or ''\n * @param {Boolean} scaleProportionally true if we are trying to scale proportionally\n * @return {Boolean} true if scaling is not allowed at current conditions\n */\nexport function scalingIsForbidden(\n fabricObject: FabricObject,\n by: ScaleBy,\n scaleProportionally: boolean,\n) {\n const lockX = isLocked(fabricObject, 'lockScalingX'),\n lockY = isLocked(fabricObject, 'lockScalingY');\n if (lockX && lockY) {\n return true;\n }\n if (!by && (lockX || lockY) && scaleProportionally) {\n return true;\n }\n if (lockX && by === 'x') {\n return true;\n }\n if (lockY && by === 'y') {\n return true;\n }\n // code crashes because of a division by 0 if a 0 sized object is scaled\n // forbid to prevent scaling to happen. ISSUE-9475\n const { width, height, strokeWidth } = fabricObject;\n if (width === 0 && strokeWidth === 0 && by !== 'y') {\n return true;\n }\n if (height === 0 && strokeWidth === 0 && by !== 'x') {\n return true;\n }\n return false;\n}\n\nconst scaleMap = ['e', 'se', 's', 'sw', 'w', 'nw', 'n', 'ne', 'e'];\n\n/**\n * return the correct cursor style for the scale action\n * @param {Event} eventData the javascript event that is causing the scale\n * @param {Control} control the control that is interested in the action\n * @param {FabricObject} fabricObject the fabric object that is interested in the action\n * @return {String} a valid css string for the cursor\n */\nexport const scaleCursorStyleHandler: ControlCursorCallback = (\n eventData,\n control,\n fabricObject,\n coord,\n) => {\n const scaleProportionally = scaleIsProportional(eventData, fabricObject),\n by =\n control.x !== 0 && control.y === 0\n ? 'x'\n : control.x === 0 && control.y !== 0\n ? 'y'\n : '';\n if (scalingIsForbidden(fabricObject, by, scaleProportionally)) {\n return NOT_ALLOWED_CURSOR;\n }\n const n = findCornerQuadrant(fabricObject, control, coord);\n return `${scaleMap[n]}-resize`;\n};\n\n/**\n * Basic scaling logic, reused with different constrain for scaling X,Y, freely or equally.\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 * @param {Object} options additional information for scaling\n * @param {String} options.by 'x', 'y', 'equally' or '' to indicate type of scaling\n * @return {Boolean} true if some change happened\n * @private\n */\nfunction scaleObject(\n eventData: TPointerEvent,\n transform: ScaleTransform,\n x: number,\n y: number,\n options: { by?: ScaleBy } = {},\n) {\n const target = transform.target,\n by = options.by,\n scaleProportionally = scaleIsProportional(eventData, target),\n forbidScaling = scalingIsForbidden(target, by, scaleProportionally);\n let newPoint, scaleX, scaleY, dim, signX, signY;\n\n if (forbidScaling) {\n return false;\n }\n if (transform.gestureScale) {\n scaleX = transform.scaleX * transform.gestureScale;\n scaleY = transform.scaleY * transform.gestureScale;\n } else {\n newPoint = getLocalPoint(\n transform,\n transform.originX,\n transform.originY,\n x,\n y,\n );\n // use of sign: We use sign to detect change of direction of an action. sign usually change when\n // we cross the origin point with the mouse. So a scale flip for example. There is an issue when scaling\n // by center and scaling using one middle control ( default: mr, mt, ml, mb), the mouse movement can easily\n // cross many time the origin point and flip the object. so we need a way to filter out the noise.\n // This ternary here should be ok to filter out X scaling when we want Y only and vice versa.\n signX = by !== 'y' ? Math.sign(newPoint.x || transform.signX || 1) : 1;\n signY = by !== 'x' ? Math.sign(newPoint.y || transform.signY || 1) : 1;\n if (!transform.signX) {\n transform.signX = signX;\n }\n if (!transform.signY) {\n transform.signY = signY;\n }\n\n if (\n isLocked(target, 'lockScalingFlip') &&\n (transform.signX !== signX || transform.signY !== signY)\n ) {\n return false;\n }\n\n dim = target._getTransformedDimensions();\n // missing detection of flip and logic to switch the origin\n if (scaleProportionally && !by) {\n // uniform scaling\n const distance = Math.abs(newPoint.x) + Math.abs(newPoint.y),\n { original } = transform,\n originalDistance =\n Math.abs((dim.x * original.scaleX) / target.scaleX) +\n Math.abs((dim.y * original.scaleY) / target.scaleY),\n scale = distance / originalDistance;\n scaleX = original.scaleX * scale;\n scaleY = original.scaleY * scale;\n } else {\n scaleX = Math.abs((newPoint.x * target.scaleX) / dim.x);\n scaleY = Math.abs((newPoint.y * target.scaleY) / dim.y);\n }\n // if we are scaling by center, we need to double the scale\n if (isTransformCentered(transform)) {\n scaleX *= 2;\n scaleY *= 2;\n }\n if (transform.signX !== signX && by !== 'y') {\n transform.originX = invertOrigin(transform.originX);\n scaleX *= -1;\n transform.signX = signX;\n }\n if (transform.signY !== signY && by !== 'x') {\n transform.originY = invertOrigin(transform.originY);\n scaleY *= -1;\n transform.signY = signY;\n }\n }\n // minScale is taken care of in the setter.\n const oldScaleX = target.scaleX,\n oldScaleY = target.scaleY;\n if (!by) {\n !isLocked(target, 'lockScalingX') && target.set(SCALE_X, scaleX);\n !isLocked(target, 'lockScalingY') && target.set(SCALE_Y, scaleY);\n } else {\n // forbidden cases already handled on top here.\n by === 'x' && target.set(SCALE_X, scaleX);\n by === 'y' && target.set(SCALE_Y, scaleY);\n }\n return oldScaleX !== target.scaleX || oldScaleY !== target.scaleY;\n}\n\n/**\n * Generic scaling logic, to scale from corners either equally or freely.\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 scaleObjectFromCorner: TransformActionHandler = (\n eventData,\n transform,\n x,\n y,\n) => {\n return scaleObject(eventData, transform, x, y);\n};\n\n/**\n * Scaling logic for the X axis.\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 */\nconst scaleObjectX: TransformActionHandler = (eventData, transform, x, y) => {\n return scaleObject(eventData, transform, x, y, { by: 'x' });\n};\n\n/**\n * Scaling logic for the Y axis.\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 */\nconst scaleObjectY: TransformActionHandler = (eventData, transform, x, y) => {\n return scaleObject(eventData, transform, x, y, { by: 'y' });\n};\n\nexport const scalingEqually = wrapWithFireEvent(\n SCALING,\n wrapWithFixedAnchor(scaleObjectFromCorner),\n);\n\nexport const scalingX = wrapWithFireEvent(\n SCALING,\n wrapWithFixedAnchor(scaleObjectX),\n);\n\nexport const scalingY = wrapWithFireEvent(\n SCALING,\n wrapWithFixedAnchor(scaleObjectY),\n);\n"],"mappings":"2UAmCA,SAAgB,EACd,EACA,EAAA,CAEA,IAAM,EAAS,EAAa,OAC1B,EAAmB,EAAU,EAAO,aACtC,OACG,EAAO,gBAAA,CAAmB,GAAA,CACzB,EAAO,gBAAkB,EAW/B,SAAgB,EACd,EACA,EACA,EAAA,CAEA,IAAM,EAAQ,EAAS,EAAc,eAAA,CACnC,EAAQ,EAAS,EAAc,eAAA,CAUjC,GATI,GAAS,GAGb,CAAK,IAAO,GAAS,IAAU,GAG3B,GAAS,IAAO,KAGhB,GAAS,IAAO,IAClB,MAAA,CAAO,EAIT,GAAA,CAAM,MAAE,EAAA,OAAO,EAAA,YAAQ,GAAgB,EACvC,OAAI,IAAU,GAAK,IAAgB,GAAK,IAAO,KAG3C,IAAW,GAAK,IAAgB,GAAK,IAAO,IAMlD,MAAM,EAAW,CAAC,IAAK,KAAM,IAAK,KAAM,IAAK,KAAM,IAAK,KAAM,IAAA,CASjD,GACX,EACA,EACA,EACA,IAAA,CAEA,IAAM,EAAsB,EAAoB,EAAW,EAAA,CAO3D,OAAI,EAAmB,EALnB,EAAQ,IAAM,GAAK,EAAQ,IAAM,EAC7B,IACA,EAAQ,IAAM,GAAK,EAAQ,IAAM,EAC/B,IACA,GAC+B,EAAA,CAChC,EAGF,GAAG,EADA,EAAmB,EAAc,EAAS,EAAA,EAAA,UAgBtD,SAAS,EACP,EACA,EACA,EACA,EACA,EAA4B,EAAA,CAAA,CAE5B,IAAM,EAAS,EAAU,OACvB,EAAK,EAAQ,GACb,EAAsB,EAAoB,EAAW,EAAA,CAEnD,EAAU,EAAQ,EAAQ,EAAK,EAAO,EAE1C,GAHkB,EAAmB,EAAQ,EAAI,EAAA,CAI/C,MAAA,CAAO,EAET,GAAI,EAAU,aACZ,EAAS,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