UNPKG

fabric

Version:

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

1 lines 10.2 kB
{"version":3,"file":"skew.min.mjs","names":[],"sources":["../../../src/controls/skew.ts"],"sourcesContent":["import type {\n ControlCursorCallback,\n TPointerEvent,\n Transform,\n TransformActionHandler,\n} from '../EventTypeDefs';\nimport { resolveOrigin } from '../util/misc/resolveOrigin';\nimport { Point } from '../Point';\nimport type { TAxis, TAxisKey } from '../typedefs';\nimport {\n degreesToRadians,\n radiansToDegrees,\n} from '../util/misc/radiansDegreesConversion';\nimport {\n findCornerQuadrant,\n getLocalPoint,\n isLocked,\n NOT_ALLOWED_CURSOR,\n} from './util';\nimport { wrapWithFireEvent } from './wrapWithFireEvent';\nimport { wrapWithFixedAnchor } from './wrapWithFixedAnchor';\nimport {\n CENTER,\n SCALE_X,\n SCALE_Y,\n SKEWING,\n SKEW_X,\n SKEW_Y,\n} from '../constants';\n\nexport type SkewTransform = Transform & { skewingSide: -1 | 1 };\n\nconst AXIS_KEYS: Record<\n TAxis,\n {\n counterAxis: TAxis;\n scale: TAxisKey<'scale'>;\n skew: TAxisKey<'skew'>;\n lockSkewing: TAxisKey<'lockSkewing'>;\n origin: TAxisKey<'origin'>;\n flip: TAxisKey<'flip'>;\n }\n> = {\n x: {\n counterAxis: 'y',\n scale: SCALE_X,\n skew: SKEW_X,\n lockSkewing: 'lockSkewingX',\n origin: 'originX',\n flip: 'flipX',\n },\n y: {\n counterAxis: 'x',\n scale: SCALE_Y,\n skew: SKEW_Y,\n lockSkewing: 'lockSkewingY',\n origin: 'originY',\n flip: 'flipY',\n },\n};\n\nconst skewMap = ['ns', 'nesw', 'ew', 'nwse'];\n\n/**\n * return the correct cursor style for the skew 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 skewCursorStyleHandler: ControlCursorCallback = (\n eventData,\n control,\n fabricObject,\n coord,\n) => {\n if (control.x !== 0 && isLocked(fabricObject, 'lockSkewingY')) {\n return NOT_ALLOWED_CURSOR;\n }\n if (control.y !== 0 && isLocked(fabricObject, 'lockSkewingX')) {\n return NOT_ALLOWED_CURSOR;\n }\n const n = findCornerQuadrant(fabricObject, control, coord) % 4;\n return `${skewMap[n]}-resize`;\n};\n\n/**\n * Since skewing is applied before scaling, calculations are done in a scaleless plane\n * @see https://github.com/fabricjs/fabric.js/pull/8380\n */\nfunction skewObject(\n axis: TAxis,\n { target, ex, ey, skewingSide, ...transform }: SkewTransform,\n pointer: Point,\n) {\n const { skew: skewKey } = AXIS_KEYS[axis],\n offset = pointer\n .subtract(new Point(ex, ey))\n .divide(new Point(target.scaleX, target.scaleY))[axis],\n skewingBefore = target[skewKey],\n skewingStart = transform[skewKey],\n shearingStart = Math.tan(degreesToRadians(skewingStart)),\n // let a, b be the size of target\n // let a' be the value of a after applying skewing\n // then:\n // a' = a + b * skewA => skewA = (a' - a) / b\n // the value b is tricky since skewY is applied before skewX\n b =\n axis === 'y'\n ? target._getTransformedDimensions({\n scaleX: 1,\n scaleY: 1,\n // since skewY is applied before skewX, b (=width) is not affected by skewX\n skewX: 0,\n }).x\n : target._getTransformedDimensions({\n scaleX: 1,\n scaleY: 1,\n }).y;\n\n const shearing =\n (2 * offset * skewingSide) /\n // we max out fractions to safeguard from asymptotic behavior\n Math.max(b, 1) +\n // add starting state\n shearingStart;\n\n const skewing = radiansToDegrees(Math.atan(shearing));\n\n target.set(skewKey, skewing);\n const changed = skewingBefore !== target[skewKey];\n\n if (changed && axis === 'y') {\n // we don't want skewing to affect scaleX\n // so we factor it by the inverse skewing diff to make it seem unchanged to the viewer\n const { skewX, scaleX } = target,\n dimBefore = target._getTransformedDimensions({ skewY: skewingBefore }),\n dimAfter = target._getTransformedDimensions(),\n compensationFactor = skewX !== 0 ? dimBefore.x / dimAfter.x : 1;\n compensationFactor !== 1 &&\n target.set(SCALE_X, compensationFactor * scaleX);\n }\n\n return changed;\n}\n\n/**\n * Wrapped Action handler for skewing on a given axis, takes care of the\n * skew direction and determines the correct transform origin for the anchor point\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 */\nfunction skewHandler(\n axis: TAxis,\n eventData: TPointerEvent,\n transform: Transform,\n x: number,\n y: number,\n) {\n const { target } = transform,\n {\n counterAxis,\n origin: originKey,\n lockSkewing: lockSkewingKey,\n skew: skewKey,\n flip: flipKey,\n } = AXIS_KEYS[axis];\n if (isLocked(target, lockSkewingKey)) {\n return false;\n }\n\n const { origin: counterOriginKey, flip: counterFlipKey } =\n AXIS_KEYS[counterAxis],\n counterOriginFactor =\n resolveOrigin(transform[counterOriginKey]) *\n (target[counterFlipKey] ? -1 : 1),\n // if the counter origin is top/left (= -0.5) then we are skewing x/y values on the bottom/right side of target respectively.\n // if the counter origin is bottom/right (= 0.5) then we are skewing x/y values on the top/left side of target respectively.\n // skewing direction on the top/left side of target is OPPOSITE to the direction of the movement of the pointer,\n // so we factor skewing direction by this value.\n skewingSide = (-Math.sign(counterOriginFactor) *\n (target[flipKey] ? -1 : 1)) as 1 | -1,\n skewingDirection =\n ((target[skewKey] === 0 &&\n // in case skewing equals 0 we use the pointer offset from target center to determine the direction of skewing\n getLocalPoint(transform, CENTER, CENTER, x, y)[axis] > 0) ||\n // in case target has skewing we use that as the direction\n target[skewKey] > 0\n ? 1\n : -1) * skewingSide,\n // anchor to the opposite side of the skewing direction\n // normalize value from [-1, 1] to origin value [0, 1]\n origin = -skewingDirection * 0.5 + 0.5;\n\n const finalHandler = wrapWithFireEvent<SkewTransform>(\n SKEWING,\n wrapWithFixedAnchor((eventData, transform, x, y) =>\n skewObject(axis, transform, new Point(x, y)),\n ),\n );\n\n return finalHandler(\n eventData,\n {\n ...transform,\n [originKey]: origin,\n skewingSide,\n },\n x,\n y,\n );\n}\n\n/**\n * Wrapped Action handler for skewing on the X axis, takes care of the\n * skew direction and determines the correct transform origin for the anchor point\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 skewHandlerX: TransformActionHandler = (\n eventData,\n transform,\n x,\n y,\n) => {\n return skewHandler('x', eventData, transform, x, y);\n};\n\n/**\n * Wrapped Action handler for skewing on the Y axis, takes care of the\n * skew direction and determines the correct transform origin for the anchor point\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 skewHandlerY: TransformActionHandler = (\n eventData,\n transform,\n x,\n y,\n) => {\n return skewHandler('y', eventData, transform, x, y);\n};\n"],"mappings":"qiBAgCA,MAAM,EAUF,CACF,EAAG,CACD,YAAa,IACb,MAAO,EACP,KAAM,EACN,YAAa,eACb,OAAQ,UACR,KAAM,QAAA,CAER,EAAG,CACD,YAAa,IACb,MAAO,EACP,KAAM,EACN,YAAa,eACb,OAAQ,UACR,KAAM,QAAA,CAAA,CAIJ,EAAU,CAAC,KAAM,OAAQ,KAAM,OAAA,CASxB,GACX,EACA,EACA,EACA,IAEI,EAAQ,IAAM,GAAK,EAAS,EAAc,eAAA,EAG1C,EAAQ,IAAM,GAAK,EAAS,EAAc,eAAA,CAFrC,EAMF,GAAG,EADA,EAAmB,EAAc,EAAS,EAAA,CAAS,GAAA,SAyE/D,SAAS,EACP,EACA,EACA,EACA,EACA,EAAA,CAEA,GAAA,CAAM,OAAE,GAAW,EAAA,CACjB,YACE,EACA,OAAQ,EACR,YAAa,EACb,KAAM,EACN,KAAM,GACJ,EAAU,GAChB,GAAI,EAAS,EAAQ,EAAA,CACnB,MAAA,CAAO,EAGT,GAAA,CAAQ,OAA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