UNPKG

fabric

Version:

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

1 lines 5.3 kB
{"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.getPositionByOrigin(originX, originY);\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","getPositionByOrigin","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;AAC3B,EAAA;EACA,OAAOH,OAAO,CAACI,WAAW;AAC5B;;AAEA;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;AAAQ,GAAC,GAAAR,IAAA;EAI3C,MAAMS,UAAU,GAAGN,MAAM,CAACO,mBAAm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