fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 4.47 kB
Source Map (JSON)
{"version":3,"file":"rotate.min.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","rotationWithSnapping","wrapWithFireEvent","ROTATING","wrapWithFixedAnchor","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"],"mappings":"uUAkBO,MAAMA,EAA8CA,CACzDC,EACAC,EACAC,IAEIA,EAAaC,aACRC,EAEFH,EAAQI,YA0DJC,EAAuBC,EAClCC,EACAC,GA/CuDC,CACvDV,EAASW,EAETC,EACAC,KACG,IAHHC,OAAEA,EAAMC,GAAEA,EAAEC,GAAEA,EAAEC,MAAEA,EAAKC,QAAEA,EAAOC,QAAEA,GAASR,EAI3C,MAAMS,EAAaN,EAAOO,uBACxBP,EAAOQ,yBACPJ,EACAC,GAGF,GAAII,EAAST,EAAQ,gBACnB,OAAO,EAGT,MAAMU,EAAYC,KAAKC,MAAMV,EAAKI,EAAWP,EAAGE,EAAKK,EAAWR,GAC9De,EAAWF,KAAKC,MAAMb,EAAIO,EAAWP,EAAGD,EAAIQ,EAAWR,GACzD,IAAIgB,EAAQC,EAAiBF,EAAWH,EAAYP,GAEpD,GAAIH,EAAOgB,WAAahB,EAAOgB,UAAY,EAAG,CAC5C,MAAMA,EAAYhB,EAAOgB,UACvBC,EAAgBjB,EAAOiB,eAAiBD,EACxCE,EAAmBP,KAAKQ,KAAKL,EAAQE,GAAaA,EAClDI,EAAkBT,KAAKU,MAAMP,EAAQE,GAAaA,EAEhDL,KAAKW,IAAIR,EAAQM,GAAmBH,EACtCH,EAAQM,EACCT,KAAKW,IAAIR,EAAQI,GAAoBD,IAC9CH,EAAQI,EAEZ,CAGIJ,EAAQ,IACVA,EAAQ,IAAMA,GAEhBA,GAAS,IAET,MAAMS,EAAavB,EAAOc,QAAUA,EAGpC,OADAd,EAAOc,MAAQA,EACRS,CAAU"}