fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 5.51 kB
Source Map (JSON)
{"version":3,"file":"rotate.mjs","sources":["../../../src/controls/rotate.ts"],"sourcesContent":["import type {\n ControlCursorCallback,\n TransformActionHandler,\n} from '../EventTypeDefs';\nimport { ROTATING } from '../constants';\nimport { radiansToDegrees } from '../util/misc/radiansDegreesConversion';\nimport { isLocked, NOT_ALLOWED_CURSOR } from './util';\nimport { wrapWithFireEvent } from './wrapWithFireEvent';\nimport { wrapWithFixedAnchor } from './wrapWithFixedAnchor';\n\n/**\n * Find the correct style for the control that is used for rotation.\n * this function is very simple and it just take care of not-allowed or standard cursor\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 rotationStyleHandler: ControlCursorCallback = (\n eventData,\n control,\n fabricObject,\n) => {\n if (fabricObject.lockRotation) {\n return NOT_ALLOWED_CURSOR;\n }\n return control.cursorStyle;\n};\n\n/**\n * Action handler for rotation and snapping, without anchor point.\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 * @private\n */\nconst rotateObjectWithSnapping: TransformActionHandler = (\n eventData,\n { target, ex, ey, theta, originX, originY },\n x,\n y,\n) => {\n const pivotPoint = target.translateToOriginPoint(\n target.getRelativeCenterPoint(),\n originX,\n originY,\n );\n\n if (isLocked(target, 'lockRotation')) {\n return false;\n }\n\n const lastAngle = Math.atan2(ey - pivotPoint.y, ex - pivotPoint.x),\n curAngle = Math.atan2(y - pivotPoint.y, x - pivotPoint.x);\n let angle = radiansToDegrees(curAngle - lastAngle + theta);\n\n if (target.snapAngle && target.snapAngle > 0) {\n const snapAngle = target.snapAngle,\n snapThreshold = target.snapThreshold || snapAngle,\n rightAngleLocked = Math.ceil(angle / snapAngle) * snapAngle,\n leftAngleLocked = Math.floor(angle / snapAngle) * snapAngle;\n\n if (Math.abs(angle - leftAngleLocked) < snapThreshold) {\n angle = leftAngleLocked;\n } else if (Math.abs(angle - rightAngleLocked) < snapThreshold) {\n angle = rightAngleLocked;\n }\n }\n\n // normalize angle to positive value\n if (angle < 0) {\n angle = 360 + angle;\n }\n angle %= 360;\n\n const hasRotated = target.angle !== angle;\n // TODO: why aren't we using set?\n target.angle = angle;\n return hasRotated;\n};\n\nexport const rotationWithSnapping = wrapWithFireEvent(\n ROTATING,\n wrapWithFixedAnchor(rotateObjectWithSnapping),\n);\n"],"names":["rotationStyleHandler","eventData","control","fabricObject","lockRotation","NOT_ALLOWED_CURSOR","cursorStyle","rotateObjectWithSnapping","_ref","x","y","target","ex","ey","theta","originX","originY","pivotPoint","translateToOriginPoint","getRelativeCenterPoint","isLocked","lastAngle","Math","atan2","curAngle","angle","radiansToDegrees","snapAngle","snapThreshold","rightAngleLocked","ceil","leftAngleLocked","floor","abs","hasRotated","rotationWithSnapping","wrapWithFireEvent","ROTATING","wrapWithFixedAnchor"],"mappings":";;;;;;AAUA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,oBAA2C,GAAGA,CACzDC,SAAS,EACTC,OAAO,EACPC,YAAY,KACT;EACH,IAAIA,YAAY,CAACC,YAAY,EAAE;AAC7B,IAAA,OAAOC,kBAAkB,CAAA;AAC3B,GAAA;EACA,OAAOH,OAAO,CAACI,WAAW,CAAA;AAC5B,EAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,wBAAgD,GAAGA,CACvDN,SAAS,EAAAO,IAAA,EAETC,CAAC,EACDC,CAAC,KACE;EAAA,IAHH;IAAEC,MAAM;IAAEC,EAAE;IAAEC,EAAE;IAAEC,KAAK;IAAEC,OAAO;AAAEC,IAAAA,OAAAA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