fabric 7.1.0 → 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.husky/pre-commit +1 -0
- package/CHANGELOG.md +13 -0
- package/dist/extensions/cropping_controls/croppingControls.d.ts +12 -8
- package/dist/extensions/cropping_controls/croppingControls.d.ts.map +1 -1
- package/dist/extensions/cropping_controls/croppingHandlers.d.ts +19 -1
- package/dist/extensions/cropping_controls/croppingHandlers.d.ts.map +1 -1
- package/dist/extensions/cropping_controls/enterCropMode.d.ts.map +1 -1
- package/dist/index.js +189 -160
- package/dist/index.js.map +1 -1
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +1 -1
- package/dist/index.min.mjs +1 -1
- package/dist/index.min.mjs.map +1 -1
- package/dist/index.mjs +189 -160
- package/dist/index.mjs.map +1 -1
- package/dist/index.node.cjs +189 -160
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.mjs +189 -160
- package/dist/index.node.mjs.map +1 -1
- package/dist/package.json.min.mjs +1 -1
- package/dist/package.json.mjs +1 -1
- package/dist/src/EventTypeDefs.d.ts +3 -0
- package/dist/src/EventTypeDefs.d.ts.map +1 -1
- package/dist/src/Pattern/Pattern.d.ts.map +1 -1
- package/dist/src/Pattern/Pattern.min.mjs +1 -1
- package/dist/src/Pattern/Pattern.min.mjs.map +1 -1
- package/dist/src/Pattern/Pattern.mjs +2 -1
- package/dist/src/Pattern/Pattern.mjs.map +1 -1
- package/dist/src/Shadow.d.ts +1 -1
- package/dist/src/Shadow.d.ts.map +1 -1
- package/dist/src/Shadow.min.mjs +1 -1
- package/dist/src/Shadow.min.mjs.map +1 -1
- package/dist/src/Shadow.mjs +5 -4
- package/dist/src/Shadow.mjs.map +1 -1
- package/dist/src/canvas/CanvasOptions.d.ts.map +1 -1
- package/dist/src/canvas/CanvasOptions.min.mjs.map +1 -1
- package/dist/src/canvas/CanvasOptions.mjs.map +1 -1
- package/dist/src/canvas/SelectableCanvas.d.ts +2 -0
- package/dist/src/canvas/SelectableCanvas.d.ts.map +1 -1
- package/dist/src/canvas/SelectableCanvas.min.mjs +1 -1
- package/dist/src/canvas/SelectableCanvas.min.mjs.map +1 -1
- package/dist/src/canvas/SelectableCanvas.mjs +6 -1
- package/dist/src/canvas/SelectableCanvas.mjs.map +1 -1
- package/dist/src/canvas/StaticCanvas.d.ts.map +1 -1
- package/dist/src/canvas/StaticCanvas.min.mjs +1 -1
- package/dist/src/canvas/StaticCanvas.min.mjs.map +1 -1
- package/dist/src/canvas/StaticCanvas.mjs +3 -1
- package/dist/src/canvas/StaticCanvas.mjs.map +1 -1
- package/dist/src/canvas/StaticCanvasOptions.d.ts.map +1 -1
- package/dist/src/canvas/StaticCanvasOptions.min.mjs.map +1 -1
- package/dist/src/canvas/StaticCanvasOptions.mjs.map +1 -1
- package/dist/src/controls/Control.d.ts +9 -1
- package/dist/src/controls/Control.d.ts.map +1 -1
- package/dist/src/controls/Control.min.mjs +1 -1
- package/dist/src/controls/Control.min.mjs.map +1 -1
- package/dist/src/controls/Control.mjs +8 -0
- package/dist/src/controls/Control.mjs.map +1 -1
- package/dist/src/gradient/Gradient.d.ts.map +1 -1
- package/dist/src/gradient/Gradient.min.mjs +1 -1
- package/dist/src/gradient/Gradient.min.mjs.map +1 -1
- package/dist/src/gradient/Gradient.mjs +19 -6
- package/dist/src/gradient/Gradient.mjs.map +1 -1
- package/dist/src/shapes/Circle.d.ts.map +1 -1
- package/dist/src/shapes/Circle.min.mjs +1 -1
- package/dist/src/shapes/Circle.min.mjs.map +1 -1
- package/dist/src/shapes/Circle.mjs +10 -7
- package/dist/src/shapes/Circle.mjs.map +1 -1
- package/dist/src/shapes/Ellipse.d.ts.map +1 -1
- package/dist/src/shapes/Ellipse.min.mjs +1 -1
- package/dist/src/shapes/Ellipse.min.mjs.map +1 -1
- package/dist/src/shapes/Ellipse.mjs +2 -1
- package/dist/src/shapes/Ellipse.mjs.map +1 -1
- package/dist/src/shapes/Group.d.ts.map +1 -1
- package/dist/src/shapes/Group.min.mjs +1 -1
- package/dist/src/shapes/Group.min.mjs.map +1 -1
- package/dist/src/shapes/Group.mjs +2 -1
- package/dist/src/shapes/Group.mjs.map +1 -1
- package/dist/src/shapes/IText/IText.d.ts.map +1 -1
- package/dist/src/shapes/IText/IText.min.mjs.map +1 -1
- package/dist/src/shapes/IText/IText.mjs.map +1 -1
- package/dist/src/shapes/Image.d.ts +1 -1
- package/dist/src/shapes/Image.d.ts.map +1 -1
- package/dist/src/shapes/Image.min.mjs +1 -1
- package/dist/src/shapes/Image.min.mjs.map +1 -1
- package/dist/src/shapes/Image.mjs +4 -3
- package/dist/src/shapes/Image.mjs.map +1 -1
- package/dist/src/shapes/Line.d.ts.map +1 -1
- package/dist/src/shapes/Line.min.mjs +1 -1
- package/dist/src/shapes/Line.min.mjs.map +1 -1
- package/dist/src/shapes/Line.mjs +6 -10
- package/dist/src/shapes/Line.mjs.map +1 -1
- package/dist/src/shapes/Object/FabricObjectSVGExportMixin.d.ts.map +1 -1
- package/dist/src/shapes/Object/FabricObjectSVGExportMixin.min.mjs +1 -1
- package/dist/src/shapes/Object/FabricObjectSVGExportMixin.min.mjs.map +1 -1
- package/dist/src/shapes/Object/FabricObjectSVGExportMixin.mjs +5 -4
- package/dist/src/shapes/Object/FabricObjectSVGExportMixin.mjs.map +1 -1
- package/dist/src/shapes/Object/InteractiveObject.d.ts.map +1 -1
- package/dist/src/shapes/Object/InteractiveObject.min.mjs.map +1 -1
- package/dist/src/shapes/Object/InteractiveObject.mjs.map +1 -1
- package/dist/src/shapes/Object/Object.d.ts.map +1 -1
- package/dist/src/shapes/Object/Object.min.mjs +1 -1
- package/dist/src/shapes/Object/Object.min.mjs.map +1 -1
- package/dist/src/shapes/Object/Object.mjs +3 -0
- package/dist/src/shapes/Object/Object.mjs.map +1 -1
- package/dist/src/shapes/Object/types/FabricObjectProps.d.ts.map +1 -1
- package/dist/src/shapes/Object/types/ObjectProps.d.ts.map +1 -1
- package/dist/src/shapes/Path.d.ts.map +1 -1
- package/dist/src/shapes/Path.min.mjs.map +1 -1
- package/dist/src/shapes/Path.mjs +1 -2
- package/dist/src/shapes/Path.mjs.map +1 -1
- package/dist/src/shapes/Polyline.d.ts.map +1 -1
- package/dist/src/shapes/Polyline.min.mjs +1 -1
- package/dist/src/shapes/Polyline.min.mjs.map +1 -1
- package/dist/src/shapes/Polyline.mjs +10 -6
- package/dist/src/shapes/Polyline.mjs.map +1 -1
- package/dist/src/shapes/Rect.d.ts.map +1 -1
- package/dist/src/shapes/Rect.min.mjs +1 -1
- package/dist/src/shapes/Rect.min.mjs.map +1 -1
- package/dist/src/shapes/Rect.mjs +2 -1
- package/dist/src/shapes/Rect.mjs.map +1 -1
- package/dist/src/shapes/Text/Text.d.ts.map +1 -1
- package/dist/src/shapes/Text/Text.min.mjs.map +1 -1
- package/dist/src/shapes/Text/Text.mjs.map +1 -1
- package/dist/src/shapes/Text/TextSVGExportMixin.min.mjs +1 -1
- package/dist/src/shapes/Text/TextSVGExportMixin.min.mjs.map +1 -1
- package/dist/src/shapes/Text/TextSVGExportMixin.mjs +5 -5
- package/dist/src/shapes/Text/TextSVGExportMixin.mjs.map +1 -1
- package/dist/src/shapes/Textbox.d.ts.map +1 -1
- package/dist/src/shapes/Textbox.min.mjs.map +1 -1
- package/dist/src/shapes/Textbox.mjs.map +1 -1
- package/dist/src/shapes/Triangle.d.ts.map +1 -1
- package/dist/src/shapes/Triangle.min.mjs.map +1 -1
- package/dist/src/shapes/Triangle.mjs.map +1 -1
- package/dist/src/util/lang_string.d.ts +1 -1
- package/dist/src/util/lang_string.d.ts.map +1 -1
- package/dist/src/util/lang_string.min.mjs +1 -1
- package/dist/src/util/lang_string.min.mjs.map +1 -1
- package/dist/src/util/lang_string.mjs +1 -1
- package/dist/src/util/lang_string.mjs.map +1 -1
- package/dist/src/util/misc/svgParsing.d.ts.map +1 -1
- package/dist/src/util/misc/svgParsing.min.mjs +1 -1
- package/dist/src/util/misc/svgParsing.min.mjs.map +1 -1
- package/dist/src/util/misc/svgParsing.mjs +2 -1
- package/dist/src/util/misc/svgParsing.mjs.map +1 -1
- package/dist-extensions/cropping_controls/croppingControls.mjs +39 -9
- package/dist-extensions/cropping_controls/croppingControls.mjs.map +1 -1
- package/dist-extensions/cropping_controls/croppingHandlers.mjs +84 -2
- package/dist-extensions/cropping_controls/croppingHandlers.mjs.map +1 -1
- package/dist-extensions/cropping_controls/enterCropMode.mjs +7 -2
- package/dist-extensions/cropping_controls/enterCropMode.mjs.map +1 -1
- package/dist-extensions/extensions/cropping_controls/croppingControls.d.ts +12 -8
- package/dist-extensions/extensions/cropping_controls/croppingControls.d.ts.map +1 -1
- package/dist-extensions/extensions/cropping_controls/croppingHandlers.d.ts +19 -1
- package/dist-extensions/extensions/cropping_controls/croppingHandlers.d.ts.map +1 -1
- package/dist-extensions/extensions/cropping_controls/enterCropMode.d.ts.map +1 -1
- package/dist-extensions/fabric-extensions.min.js +1 -1
- package/dist-extensions/fabric-extensions.min.js.map +1 -1
- package/dist-extensions/src/EventTypeDefs.d.ts +3 -0
- package/dist-extensions/src/EventTypeDefs.d.ts.map +1 -1
- package/dist-extensions/src/Pattern/Pattern.d.ts.map +1 -1
- package/dist-extensions/src/Shadow.d.ts +1 -1
- package/dist-extensions/src/Shadow.d.ts.map +1 -1
- package/dist-extensions/src/canvas/CanvasOptions.d.ts.map +1 -1
- package/dist-extensions/src/canvas/SelectableCanvas.d.ts +2 -0
- package/dist-extensions/src/canvas/SelectableCanvas.d.ts.map +1 -1
- package/dist-extensions/src/canvas/StaticCanvas.d.ts.map +1 -1
- package/dist-extensions/src/canvas/StaticCanvasOptions.d.ts.map +1 -1
- package/dist-extensions/src/controls/Control.d.ts +9 -1
- package/dist-extensions/src/controls/Control.d.ts.map +1 -1
- package/dist-extensions/src/gradient/Gradient.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Circle.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Ellipse.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Group.d.ts.map +1 -1
- package/dist-extensions/src/shapes/IText/IText.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Image.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Line.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Object/FabricObjectSVGExportMixin.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Object/InteractiveObject.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Object/Object.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Object/types/FabricObjectProps.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Object/types/ObjectProps.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Path.d.ts +1 -1
- package/dist-extensions/src/shapes/Path.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Polyline.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Rect.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Text/Text.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Textbox.d.ts.map +1 -1
- package/dist-extensions/src/shapes/Triangle.d.ts.map +1 -1
- package/dist-extensions/src/util/lang_string.d.ts +1 -1
- package/dist-extensions/src/util/lang_string.d.ts.map +1 -1
- package/dist-extensions/src/util/misc/svgParsing.d.ts.map +1 -1
- package/eslint.config.mjs +2 -0
- package/extensions/cropping_controls/croppingControls.spec.ts +65 -27
- package/extensions/cropping_controls/croppingControls.ts +40 -8
- package/extensions/cropping_controls/croppingHandlers.spec.ts +355 -46
- package/extensions/cropping_controls/croppingHandlers.ts +123 -0
- package/extensions/cropping_controls/enterCropMode.ts +6 -2
- package/package.json +17 -8
- package/src/ClassRegistry.spec.ts +18 -19
- package/src/EventTypeDefs.ts +13 -11
- package/src/Pattern/Pattern.spec.ts +12 -0
- package/src/Pattern/Pattern.ts +3 -2
- package/src/Shadow.ts +9 -8
- package/src/brushes/PencilBrush.spec.ts +11 -11
- package/src/canvas/Canvas-dispose.spec.ts +8 -7
- package/src/canvas/Canvas.spec.ts +27 -29
- package/src/canvas/CanvasOptions.ts +2 -1
- package/src/canvas/SelectableCanvas.ts +11 -4
- package/src/canvas/StaticCanvas.spec.ts +20 -0
- package/src/canvas/StaticCanvas.ts +7 -4
- package/src/canvas/StaticCanvasOptions.ts +1 -3
- package/src/controls/Control.ts +24 -1
- package/src/gradient/Gradient.spec.ts +101 -46
- package/src/gradient/Gradient.ts +27 -14
- package/src/shapes/Circle.spec.ts +10 -39
- package/src/shapes/Circle.ts +11 -11
- package/src/shapes/Ellipse.spec.ts +8 -37
- package/src/shapes/Ellipse.ts +7 -7
- package/src/shapes/Group.ts +3 -3
- package/src/shapes/IText/IText-click-behavior.spec.ts +36 -49
- package/src/shapes/IText/IText.ts +5 -6
- package/src/shapes/IText/__snapshots__/ITextBehavior.test.ts.snap +6 -6
- package/src/shapes/Image.spec.ts +17 -33
- package/src/shapes/Image.ts +15 -11
- package/src/shapes/Line.spec.ts +4 -30
- package/src/shapes/Line.ts +11 -16
- package/src/shapes/Object/FabricObjectSVGExportMixin.ts +11 -4
- package/src/shapes/Object/InteractiveObject.ts +4 -4
- package/src/shapes/Object/Object.ts +6 -5
- package/src/shapes/Object/objectSvgExport.spec.ts +112 -0
- package/src/shapes/Object/types/FabricObjectProps.ts +1 -4
- package/src/shapes/Object/types/ObjectProps.ts +1 -3
- package/src/shapes/Path.spec.ts +4 -27
- package/src/shapes/Path.ts +2 -4
- package/src/shapes/Polygon.spec.ts +4 -31
- package/src/shapes/Polyline.spec.ts +4 -31
- package/src/shapes/Polyline.ts +11 -12
- package/src/shapes/Rect.spec.ts +25 -33
- package/src/shapes/Rect.ts +7 -7
- package/src/shapes/Text/Text.spec.ts +3 -32
- package/src/shapes/Text/Text.ts +5 -6
- package/src/shapes/Text/TextSVGExportMixin.ts +14 -14
- package/src/shapes/Text/__snapshots__/Text.spec.ts.snap +1 -1
- package/src/shapes/Textbox.spec.ts +5 -5
- package/src/shapes/Textbox.ts +6 -5
- package/src/shapes/Triangle.ts +4 -4
- package/src/shapes/__snapshots__/Image.spec.ts.snap +4 -4
- package/src/shapes/__snapshots__/Textbox.spec.ts.snap +5 -5
- package/src/util/lang_string.ts +3 -2
- package/src/util/misc/svgParsing.ts +2 -1
- package/tsconfig.spec.json +1 -0
- package/vitest.config.ts +12 -2
- package/vitest.extend.ts +6 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Control.min.mjs","sources":["../../../src/controls/Control.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-unused-vars */\nimport type {\n ControlActionHandler,\n TPointerEvent,\n TransformActionHandler,\n} from '../EventTypeDefs';\nimport { Intersection } from '../Intersection';\nimport { Point } from '../Point';\nimport { FILL, SCALE, STROKE } from '../constants';\nimport type {\n InteractiveFabricObject,\n TOCoord,\n} from '../shapes/Object/InteractiveObject';\nimport type { TCornerPoint, TDegree, TMat2D } from '../typedefs';\nimport {\n createRotateMatrix,\n createScaleMatrix,\n createTranslateMatrix,\n multiplyTransformMatrixArray,\n} from '../util/misc/matrix';\nimport { degreesToRadians } from '../util/misc/radiansDegreesConversion';\nimport type { ControlRenderingStyleOverride } from './controlRendering';\nimport { renderCircleControl, renderSquareControl } from './controlRendering';\n\nexport class Control {\n /**\n * keep track of control visibility.\n * mainly for backward compatibility.\n * if you do not want to see a control, you can remove it\n * from the control set.\n * @type {Boolean}\n * @default true\n */\n visible = true;\n\n /**\n * Name of the action that the control will likely execute.\n * This is optional. FabricJS uses to identify what the user is doing for some\n * extra optimizations. If you are writing a custom control and you want to know\n * somewhere else in the code what is going on, you can use this string here.\n * you can also provide a custom getActionName if your control run multiple actions\n * depending on some external state.\n * default to scale since is the most common, used on 4 corners by default\n * @type {String}\n * @default 'scale'\n */\n actionName = SCALE;\n\n /**\n * Drawing angle of the control.\n * NOT used for now, but name marked as needed for internal logic\n * example: to reuse the same drawing function for different rotated controls\n * @type {Number}\n * @default 0\n */\n angle = 0;\n\n /**\n * Relative position of the control. X\n * 0,0 is the center of the Object, while -0.5 (left) or 0.5 (right) are the extremities\n * of the bounding box.\n * @type {Number}\n * @default 0\n */\n x = 0;\n\n /**\n * Relative position of the control. Y\n * 0,0 is the center of the Object, while -0.5 (top) or 0.5 (bottom) are the extremities\n * of the bounding box.\n * @type {Number}\n * @default 0\n */\n y = 0;\n\n /**\n * Horizontal offset of the control from the defined position. In pixels\n * Positive offset moves the control to the right, negative to the left.\n * It used when you want to have position of control that does not scale with\n * the bounding box. Example: rotation control is placed at x:0, y: 0.5 on\n * the boundind box, with an offset of 30 pixels vertically. Those 30 pixels will\n * stay 30 pixels no matter how the object is big. Another example is having 2\n * controls in the corner, that stay in the same position when the object scale.\n * of the bounding box.\n * @type {Number}\n * @default 0\n */\n offsetX = 0;\n\n /**\n * Vertical offset of the control from the defined position. In pixels\n * Positive offset moves the control to the bottom, negative to the top.\n * @type {Number}\n * @default 0\n */\n offsetY = 0;\n\n /**\n * Sets the length of the control. If null, defaults to object's cornerSize.\n * Expects both sizeX and sizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n sizeX = 0;\n\n /**\n * Sets the height of the control. If null, defaults to object's cornerSize.\n * Expects both sizeX and sizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n sizeY = 0;\n\n /**\n * Sets the length of the touch area of the control. If null, defaults to object's touchCornerSize.\n * Expects both touchSizeX and touchSizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n touchSizeX = 0;\n\n /**\n * Sets the height of the touch area of the control. If null, defaults to object's touchCornerSize.\n * Expects both touchSizeX and touchSizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n touchSizeY = 0;\n\n /**\n * Css cursor style to display when the control is hovered.\n * if the method `cursorStyleHandler` is provided, this property is ignored.\n * @type {String}\n * @default 'crosshair'\n */\n cursorStyle = 'crosshair';\n\n /**\n * If controls has an offsetY or offsetX, draw a line that connects\n * the control to the bounding box\n * @type {Boolean}\n * @default false\n */\n withConnection = false;\n\n constructor(options?: Partial<Control>) {\n Object.assign(this, options);\n }\n\n /**\n * The control actionHandler, provide one to handle action ( control being moved )\n * @param {Event} eventData the native mouse event\n * @param {Transform} transformData properties of the current transform\n * @param {Number} x x position of the cursor\n * @param {Number} y y position of the cursor\n * @return {Boolean} true if the action/event modified the object\n */\n declare actionHandler: TransformActionHandler;\n\n /**\n * The control handler for mouse down, provide one to handle mouse down on control\n * @param {Event} eventData the native mouse event\n * @param {Transform} transformData properties of the current transform\n * @param {Number} x x position of the cursor\n * @param {Number} y y position of the cursor\n * @return {Boolean} true if the action/event modified the object\n */\n declare mouseDownHandler?: ControlActionHandler;\n\n /**\n * The control mouseUpHandler, provide one to handle an effect on mouse up.\n * @param {Event} eventData the native mouse event\n * @param {Transform} transformData properties of the current transform\n * @param {Number} x x position of the cursor\n * @param {Number} y y position of the cursor\n * @return {Boolean} true if the action/event modified the object\n */\n declare mouseUpHandler?: ControlActionHandler;\n\n shouldActivate(\n controlKey: string,\n fabricObject: InteractiveFabricObject,\n pointer: Point,\n { tl, tr, br, bl }: TCornerPoint,\n ) {\n // TODO: locking logic can be handled here instead of in the control handler logic\n return (\n fabricObject.canvas?.getActiveObject() === fabricObject &&\n fabricObject.isControlVisible(controlKey) &&\n Intersection.isPointInPolygon(pointer, [tl, tr, br, bl])\n );\n }\n\n /**\n * Returns control actionHandler\n * @param {Event} eventData the native mouse event\n * @param {FabricObject} fabricObject on which the control is displayed\n * @param {Control} control control for which the action handler is being asked\n * @return {Function} the action handler\n */\n getActionHandler(\n eventData: TPointerEvent,\n fabricObject: InteractiveFabricObject,\n control: Control,\n ): TransformActionHandler | undefined {\n return this.actionHandler;\n }\n\n /**\n * Returns control mouseDown handler\n * @param {Event} eventData the native mouse event\n * @param {FabricObject} fabricObject on which the control is displayed\n * @param {Control} control control for which the action handler is being asked\n * @return {Function} the action handler\n */\n getMouseDownHandler(\n eventData: TPointerEvent,\n fabricObject: InteractiveFabricObject,\n control: Control,\n ): ControlActionHandler | undefined {\n return this.mouseDownHandler;\n }\n\n /**\n * Returns control mouseUp handler.\n * During actions the fabricObject or the control can be of different obj\n * @param {Event} eventData the native mouse event\n * @param {FabricObject} fabricObject on which the control is displayed\n * @param {Control} control control for which the action handler is being asked\n * @return {Function} the action handler\n */\n getMouseUpHandler(\n eventData: TPointerEvent,\n fabricObject: InteractiveFabricObject,\n control: Control,\n ): ControlActionHandler | undefined {\n return this.mouseUpHandler;\n }\n\n /**\n * Returns control cursorStyle for css using cursorStyle. If you need a more elaborate\n * function you can pass one in the constructor\n * the cursorStyle property\n * @param {Event} eventData the native mouse event\n * @param {Control} control the current control ( likely this)\n * @param {FabricObject} object on which the control is displayed\n * @return {String}\n */\n cursorStyleHandler(\n eventData: TPointerEvent,\n control: Control,\n fabricObject: InteractiveFabricObject,\n coord: TOCoord,\n ) {\n return control.cursorStyle;\n }\n\n /**\n * Returns the action name. The basic implementation just return the actionName property.\n * @param {Event} eventData the native mouse event\n * @param {Control} control the current control ( likely this)\n * @param {FabricObject} object on which the control is displayed\n * @return {String}\n */\n getActionName(\n eventData: TPointerEvent,\n control: Control,\n fabricObject: InteractiveFabricObject,\n ) {\n return control.actionName;\n }\n\n /**\n * Returns controls visibility\n * @param {FabricObject} object on which the control is displayed\n * @param {String} controlKey key where the control is memorized on the\n * @return {Boolean}\n */\n getVisibility(fabricObject: InteractiveFabricObject, controlKey: string) {\n return fabricObject._controlsVisibility?.[controlKey] ?? this.visible;\n }\n\n /**\n * Sets controls visibility\n * @param {Boolean} visibility for the object\n * @return {Void}\n */\n setVisibility(\n visibility: boolean,\n name?: string,\n fabricObject?: InteractiveFabricObject,\n ) {\n this.visible = visibility;\n }\n\n positionHandler(\n dim: Point,\n finalMatrix: TMat2D,\n fabricObject: InteractiveFabricObject,\n currentControl: Control,\n ) {\n return new Point(\n this.x * dim.x + this.offsetX,\n this.y * dim.y + this.offsetY,\n ).transform(finalMatrix);\n }\n\n /**\n * Returns the coords for this control based on object values.\n * @param {Number} objectAngle angle from the fabric object holding the control\n * @param {Number} objectCornerSize cornerSize from the fabric object holding the control (or touchCornerSize if\n * isTouch is true)\n * @param {Number} centerX x coordinate where the control center should be\n * @param {Number} centerY y coordinate where the control center should be\n * @param {boolean} isTouch true if touch corner, false if normal corner\n */\n calcCornerCoords(\n angle: TDegree,\n objectCornerSize: number,\n centerX: number,\n centerY: number,\n isTouch: boolean,\n fabricObject: InteractiveFabricObject,\n ) {\n const t = multiplyTransformMatrixArray([\n createTranslateMatrix(centerX, centerY),\n createRotateMatrix({ angle }),\n createScaleMatrix(\n (isTouch ? this.touchSizeX : this.sizeX) || objectCornerSize,\n (isTouch ? this.touchSizeY : this.sizeY) || objectCornerSize,\n ),\n ]);\n return {\n tl: new Point(-0.5, -0.5).transform(t),\n tr: new Point(0.5, -0.5).transform(t),\n br: new Point(0.5, 0.5).transform(t),\n bl: new Point(-0.5, 0.5).transform(t),\n };\n }\n\n /**\n * This is an helper method to prepare the canvas to render a control\n * It detectes common control properties and sets the correct fill and\n * stroke styles on the context. It does not execute translations or\n * rotations since different controls need differnt combination of these.\n */\n commonRenderProps(\n ctx: CanvasRenderingContext2D,\n left: number,\n top: number,\n fabricObject: InteractiveFabricObject,\n styleOverride: ControlRenderingStyleOverride = {},\n ): {\n stroke: boolean;\n xSize: number;\n ySize: number;\n transparentCorners: boolean;\n opName: 'stroke' | 'fill';\n } {\n const { cornerSize, cornerColor, transparentCorners, cornerStrokeColor } =\n styleOverride,\n sizeFromProps = cornerSize || fabricObject.cornerSize,\n xSize = this.sizeX || sizeFromProps,\n ySize = this.sizeY || sizeFromProps,\n transparent =\n typeof transparentCorners !== 'undefined'\n ? transparentCorners\n : fabricObject.transparentCorners,\n opName = transparent ? STROKE : FILL,\n strokeColor = cornerStrokeColor || fabricObject.cornerStrokeColor,\n stroke = !transparent && !!strokeColor;\n ctx.fillStyle = cornerColor || fabricObject.cornerColor || '';\n ctx.strokeStyle = strokeColor || '';\n ctx.translate(left, top);\n // angle is relative to canvas plane\n ctx.rotate(degreesToRadians(fabricObject.getTotalAngle()));\n return {\n stroke,\n xSize,\n ySize,\n transparentCorners: transparent,\n opName,\n };\n }\n\n /**\n * Render function for the control.\n * When this function runs the context is unscaled. unrotate. Just retina scaled.\n * all the functions will have to translate to the point left,top before starting Drawing\n * if they want to draw a control where the position is detected.\n * left and top are the result of the positionHandler function\n * @param {RenderingContext2D} ctx the context where the control will be drawn\n * @param {Number} left position of the canvas where we are about to render the control.\n * @param {Number} top position of the canvas where we are about to render the control.\n * @param {Object} styleOverride\n * @param {FabricObject} fabricObject the object where the control is about to be rendered\n */\n render(\n ctx: CanvasRenderingContext2D,\n left: number,\n top: number,\n styleOverride: ControlRenderingStyleOverride | undefined,\n fabricObject: InteractiveFabricObject,\n ) {\n styleOverride = styleOverride || {};\n switch (styleOverride.cornerStyle || fabricObject.cornerStyle) {\n case 'circle':\n renderCircleControl.call(\n this,\n ctx,\n left,\n top,\n styleOverride,\n fabricObject,\n );\n break;\n default:\n renderSquareControl.call(\n this,\n ctx,\n left,\n top,\n styleOverride,\n fabricObject,\n );\n }\n }\n}\n"],"names":["Control","constructor","options","_defineProperty","SCALE","Object","assign","this","shouldActivate","controlKey","fabricObject","pointer","_ref","_fabricObject$canvas","tl","tr","br","bl","canvas","getActiveObject","isControlVisible","Intersection","isPointInPolygon","getActionHandler","eventData","control","actionHandler","getMouseDownHandler","mouseDownHandler","getMouseUpHandler","mouseUpHandler","cursorStyleHandler","coord","cursorStyle","getActionName","actionName","getVisibility","_fabricObject$_contro","_fabricObject$_contro2","_controlsVisibility","visible","setVisibility","visibility","name","positionHandler","dim","finalMatrix","currentControl","Point","x","offsetX","y","offsetY","transform","calcCornerCoords","angle","objectCornerSize","centerX","centerY","isTouch","t","multiplyTransformMatrixArray","createTranslateMatrix","createRotateMatrix","createScaleMatrix","touchSizeX","sizeX","touchSizeY","sizeY","commonRenderProps","ctx","left","top","styleOverride","arguments","length","undefined","cornerSize","cornerColor","transparentCorners","cornerStrokeColor","sizeFromProps","xSize","ySize","transparent","opName","STROKE","FILL","strokeColor","stroke","fillStyle","strokeStyle","translate","rotate","degreesToRadians","getTotalAngle","render","cornerStyle","renderCircleControl","call","renderSquareControl"],"mappings":"qjBAwBO,MAAMA,EAyHXC,WAAAA,CAAYC,GAxHZC,kBAQU,GAEVA,oBAWaC,GAEbD,eAOQ,GAERA,WAOI,GAEJA,WAOI,GAEJA,iBAYU,GAEVA,iBAMU,GAEVA,eAMQ,GAERA,eAMQ,GAERA,oBAMa,GAEbA,oBAMa,GAEbA,qBAMc,aAEdA,yBAMiB,GAGfE,OAAOC,OAAOC,KAAML,EACtB,CAgCAM,cAAAA,CACEC,EACAC,EACAC,EAAcC,GAEd,IAAAC,EAAA,IADAC,GAAEA,EAAEC,GAAEA,EAAEC,GAAEA,EAAEC,GAAEA,GAAkBL,EAGhC,OACqB,QAAnBC,EAAAH,EAAaQ,cAAM,IAAAL,OAAA,EAAnBA,EAAqBM,qBAAsBT,GAC3CA,EAAaU,iBAAiBX,IAC9BY,EAAaC,iBAAiBX,EAAS,CAACG,EAAIC,EAAIC,EAAIC,GAExD,CASAM,gBAAAA,CACEC,EACAd,EACAe,GAEA,OAAOlB,KAAKmB,aACd,CASAC,mBAAAA,CACEH,EACAd,EACAe,GAEA,OAAOlB,KAAKqB,gBACd,CAUAC,iBAAAA,CACEL,EACAd,EACAe,GAEA,OAAOlB,KAAKuB,cACd,CAWAC,kBAAAA,CACEP,EACAC,EACAf,EACAsB,GAEA,OAAOP,EAAQQ,WACjB,CASAC,aAAAA,CACEV,EACAC,EACAf,GAEA,OAAOe,EAAQU,UACjB,CAQAC,aAAAA,CAAc1B,EAAuCD,GAAoB,IAAA4B,EAAAC,EACvE,OAAqD,QAArDD,UAAAC,EAAO5B,EAAa6B,2BAAmB,IAAAD,OAAA,EAAhCA,EAAmC7B,cAAW4B,EAAAA,EAAI9B,KAAKiC,OAChE,CAOAC,aAAAA,CACEC,EACAC,EACAjC,GAEAH,KAAKiC,QAAUE,CACjB,CAEAE,eAAAA,CACEC,EACAC,EACApC,EACAqC,GAEA,OAAO,IAAIC,EACTzC,K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D,GAGA,GACO,aAFP+D,EAAgBA,GAAiB,CAAA,GACXwB,aAAevF,EAAauF,aAE9CC,EAAoBC,KAClB5F,KACA+D,EACAC,EACAC,EACAC,EACA/D,QAIF0F,EAAoBD,KAClB5F,KACA+D,EACAC,EACAC,EACAC,EACA/D,EAGR"}
|
|
1
|
+
{"version":3,"file":"Control.min.mjs","sources":["../../../src/controls/Control.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-unused-vars */\nimport type {\n ControlActionHandler,\n TPointerEvent,\n TransformActionHandler,\n} from '../EventTypeDefs';\nimport { Intersection } from '../Intersection';\nimport { Point } from '../Point';\nimport { FILL, SCALE, STROKE } from '../constants';\nimport type {\n InteractiveFabricObject,\n TOCoord,\n} from '../shapes/Object/InteractiveObject';\nimport type {\n TCornerPoint,\n TDegree,\n TMat2D,\n TOriginX,\n TOriginY,\n} from '../typedefs';\nimport {\n createRotateMatrix,\n createScaleMatrix,\n createTranslateMatrix,\n multiplyTransformMatrixArray,\n} from '../util/misc/matrix';\nimport { degreesToRadians } from '../util/misc/radiansDegreesConversion';\nimport type { ControlRenderingStyleOverride } from './controlRendering';\nimport { renderCircleControl, renderSquareControl } from './controlRendering';\n\nexport class Control {\n /**\n * keep track of control visibility.\n * mainly for backward compatibility.\n * if you do not want to see a control, you can remove it\n * from the control set.\n * @type {Boolean}\n * @default true\n */\n visible = true;\n\n /**\n * Name of the action that the control will likely execute.\n * This is optional. FabricJS uses to identify what the user is doing for some\n * extra optimizations. If you are writing a custom control and you want to know\n * somewhere else in the code what is going on, you can use this string here.\n * you can also provide a custom getActionName if your control run multiple actions\n * depending on some external state.\n * default to scale since is the most common, used on 4 corners by default\n * @type {String}\n * @default 'scale'\n */\n actionName = SCALE;\n\n /**\n * Drawing angle of the control.\n * NOT used for now, but name marked as needed for internal logic\n * example: to reuse the same drawing function for different rotated controls\n * @type {Number}\n * @default 0\n */\n angle = 0;\n\n /**\n * Relative position of the control. X\n * 0,0 is the center of the Object, while -0.5 (left) or 0.5 (right) are the extremities\n * of the bounding box.\n * @type {Number}\n * @default 0\n */\n x = 0;\n\n /**\n * Relative position of the control. Y\n * 0,0 is the center of the Object, while -0.5 (top) or 0.5 (bottom) are the extremities\n * of the bounding box.\n * @type {Number}\n * @default 0\n */\n y = 0;\n\n /**\n * Horizontal offset of the control from the defined position. In pixels\n * Positive offset moves the control to the right, negative to the left.\n * It used when you want to have position of control that does not scale with\n * the bounding box. Example: rotation control is placed at x:0, y: 0.5 on\n * the boundind box, with an offset of 30 pixels vertically. Those 30 pixels will\n * stay 30 pixels no matter how the object is big. Another example is having 2\n * controls in the corner, that stay in the same position when the object scale.\n * of the bounding box.\n * @type {Number}\n * @default 0\n */\n offsetX = 0;\n\n /**\n * Vertical offset of the control from the defined position. In pixels\n * Positive offset moves the control to the bottom, negative to the top.\n * @type {Number}\n * @default 0\n */\n offsetY = 0;\n\n /**\n * Sets the length of the control. If null, defaults to object's cornerSize.\n * Expects both sizeX and sizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n sizeX = 0;\n\n /**\n * Sets the height of the control. If null, defaults to object's cornerSize.\n * Expects both sizeX and sizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n sizeY = 0;\n\n /**\n * Sets the length of the touch area of the control. If null, defaults to object's touchCornerSize.\n * Expects both touchSizeX and touchSizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n touchSizeX = 0;\n\n /**\n * Sets the height of the touch area of the control. If null, defaults to object's touchCornerSize.\n * Expects both touchSizeX and touchSizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n touchSizeY = 0;\n\n /**\n * Css cursor style to display when the control is hovered.\n * if the method `cursorStyleHandler` is provided, this property is ignored.\n * @type {String}\n * @default 'crosshair'\n */\n cursorStyle = 'crosshair';\n\n /**\n * If controls has an offsetY or offsetX, draw a line that connects\n * the control to the bounding box\n * @type {Boolean}\n * @default false\n */\n withConnection = false;\n\n declare transformAnchorPoint?: {\n x: TOriginX;\n y: TOriginY;\n };\n\n constructor(options?: Partial<Control>) {\n Object.assign(this, options);\n }\n\n getTransformAnchorPoint(): {\n x: TOriginX;\n y: TOriginY;\n } {\n return (\n // return the control transformAnchorPoint\n this.transformAnchorPoint ??\n // otherwise will return the opposite origin of where the control is located.\n new Point(-this.x + 0.5, -this.y + 0.5)\n );\n }\n\n /**\n * The control actionHandler, provide one to handle action ( control being moved )\n * @param {Event} eventData the native mouse event\n * @param {Transform} transformData properties of the current transform\n * @param {Number} x x position of the cursor\n * @param {Number} y y position of the cursor\n * @return {Boolean} true if the action/event modified the object\n */\n declare actionHandler: TransformActionHandler;\n\n /**\n * The control handler for mouse down, provide one to handle mouse down on control\n * @param {Event} eventData the native mouse event\n * @param {Transform} transformData properties of the current transform\n * @param {Number} x x position of the cursor\n * @param {Number} y y position of the cursor\n * @return {Boolean} true if the action/event modified the object\n */\n declare mouseDownHandler?: ControlActionHandler;\n\n /**\n * The control mouseUpHandler, provide one to handle an effect on mouse up.\n * @param {Event} eventData the native mouse event\n * @param {Transform} transformData properties of the current transform\n * @param {Number} x x position of the cursor\n * @param {Number} y y position of the cursor\n * @return {Boolean} true if the action/event modified the object\n */\n declare mouseUpHandler?: ControlActionHandler;\n\n shouldActivate(\n controlKey: string,\n fabricObject: InteractiveFabricObject,\n pointer: Point,\n { tl, tr, br, bl }: TCornerPoint,\n ) {\n // TODO: locking logic can be handled here instead of in the control handler logic\n return (\n fabricObject.canvas?.getActiveObject() === fabricObject &&\n fabricObject.isControlVisible(controlKey) &&\n Intersection.isPointInPolygon(pointer, [tl, tr, br, bl])\n );\n }\n\n /**\n * Returns control actionHandler\n * @param {Event} eventData the native mouse event\n * @param {FabricObject} fabricObject on which the control is displayed\n * @param {Control} control control for which the action handler is being asked\n * @return {Function} the action handler\n */\n getActionHandler(\n eventData: TPointerEvent,\n fabricObject: InteractiveFabricObject,\n control: Control,\n ): TransformActionHandler | undefined {\n return this.actionHandler;\n }\n\n /**\n * Returns control mouseDown handler\n * @param {Event} eventData the native mouse event\n * @param {FabricObject} fabricObject on which the control is displayed\n * @param {Control} control control for which the action handler is being asked\n * @return {Function} the action handler\n */\n getMouseDownHandler(\n eventData: TPointerEvent,\n fabricObject: InteractiveFabricObject,\n control: Control,\n ): ControlActionHandler | undefined {\n return this.mouseDownHandler;\n }\n\n /**\n * Returns control mouseUp handler.\n * During actions the fabricObject or the control can be of different obj\n * @param {Event} eventData the native mouse event\n * @param {FabricObject} fabricObject on which the control is displayed\n * @param {Control} control control for which the action handler is being asked\n * @return {Function} the action handler\n */\n getMouseUpHandler(\n eventData: TPointerEvent,\n fabricObject: InteractiveFabricObject,\n control: Control,\n ): ControlActionHandler | undefined {\n return this.mouseUpHandler;\n }\n\n /**\n * Returns control cursorStyle for css using cursorStyle. If you need a more elaborate\n * function you can pass one in the constructor\n * the cursorStyle property\n * @param {Event} eventData the native mouse event\n * @param {Control} control the current control ( likely this)\n * @param {FabricObject} object on which the control is displayed\n * @return {String}\n */\n cursorStyleHandler(\n eventData: TPointerEvent,\n control: Control,\n fabricObject: InteractiveFabricObject,\n coord: TOCoord,\n ) {\n return control.cursorStyle;\n }\n\n /**\n * Returns the action name. The basic implementation just return the actionName property.\n * @param {Event} eventData the native mouse event\n * @param {Control} control the current control ( likely this)\n * @param {FabricObject} object on which the control is displayed\n * @return {String}\n */\n getActionName(\n eventData: TPointerEvent,\n control: Control,\n fabricObject: InteractiveFabricObject,\n ) {\n return control.actionName;\n }\n\n /**\n * Returns controls visibility\n * @param {FabricObject} object on which the control is displayed\n * @param {String} controlKey key where the control is memorized on the\n * @return {Boolean}\n */\n getVisibility(fabricObject: InteractiveFabricObject, controlKey: string) {\n return fabricObject._controlsVisibility?.[controlKey] ?? this.visible;\n }\n\n /**\n * Sets controls visibility\n * @param {Boolean} visibility for the object\n * @return {Void}\n */\n setVisibility(\n visibility: boolean,\n name?: string,\n fabricObject?: InteractiveFabricObject,\n ) {\n this.visible = visibility;\n }\n\n positionHandler(\n dim: Point,\n finalMatrix: TMat2D,\n fabricObject: InteractiveFabricObject,\n currentControl: Control,\n ) {\n return new Point(\n this.x * dim.x + this.offsetX,\n this.y * dim.y + this.offsetY,\n ).transform(finalMatrix);\n }\n\n /**\n * Returns the coords for this control based on object values.\n * @param {Number} objectAngle angle from the fabric object holding the control\n * @param {Number} objectCornerSize cornerSize from the fabric object holding the control (or touchCornerSize if\n * isTouch is true)\n * @param {Number} centerX x coordinate where the control center should be\n * @param {Number} centerY y coordinate where the control center should be\n * @param {boolean} isTouch true if touch corner, false if normal corner\n */\n calcCornerCoords(\n angle: TDegree,\n objectCornerSize: number,\n centerX: number,\n centerY: number,\n isTouch: boolean,\n fabricObject: InteractiveFabricObject,\n ) {\n const t = multiplyTransformMatrixArray([\n createTranslateMatrix(centerX, centerY),\n createRotateMatrix({ angle }),\n createScaleMatrix(\n (isTouch ? this.touchSizeX : this.sizeX) || objectCornerSize,\n (isTouch ? this.touchSizeY : this.sizeY) || objectCornerSize,\n ),\n ]);\n return {\n tl: new Point(-0.5, -0.5).transform(t),\n tr: new Point(0.5, -0.5).transform(t),\n br: new Point(0.5, 0.5).transform(t),\n bl: new Point(-0.5, 0.5).transform(t),\n };\n }\n\n /**\n * This is an helper method to prepare the canvas to render a control\n * It detectes common control properties and sets the correct fill and\n * stroke styles on the context. It does not execute translations or\n * rotations since different controls need differnt combination of these.\n */\n commonRenderProps(\n ctx: CanvasRenderingContext2D,\n left: number,\n top: number,\n fabricObject: InteractiveFabricObject,\n styleOverride: ControlRenderingStyleOverride = {},\n ): {\n stroke: boolean;\n xSize: number;\n ySize: number;\n transparentCorners: boolean;\n opName: 'stroke' | 'fill';\n } {\n const { cornerSize, cornerColor, transparentCorners, cornerStrokeColor } =\n styleOverride,\n sizeFromProps = cornerSize || fabricObject.cornerSize,\n xSize = this.sizeX || sizeFromProps,\n ySize = this.sizeY || sizeFromProps,\n transparent =\n typeof transparentCorners !== 'undefined'\n ? transparentCorners\n : fabricObject.transparentCorners,\n opName = transparent ? STROKE : FILL,\n strokeColor = cornerStrokeColor || fabricObject.cornerStrokeColor,\n stroke = !transparent && !!strokeColor;\n ctx.fillStyle = cornerColor || fabricObject.cornerColor || '';\n ctx.strokeStyle = strokeColor || '';\n ctx.translate(left, top);\n // angle is relative to canvas plane\n ctx.rotate(degreesToRadians(fabricObject.getTotalAngle()));\n return {\n stroke,\n xSize,\n ySize,\n transparentCorners: transparent,\n opName,\n };\n }\n\n /**\n * Render function for the control.\n * When this function runs the context is unscaled. unrotate. Just retina scaled.\n * all the functions will have to translate to the point left,top before starting Drawing\n * if they want to draw a control where the position is detected.\n * left and top are the result of the positionHandler function\n * @param {RenderingContext2D} ctx the context where the control will be drawn\n * @param {Number} left position of the canvas where we are about to render the control.\n * @param {Number} top position of the canvas where we are about to render the control.\n * @param {Object} styleOverride\n * @param {FabricObject} fabricObject the object where the control is about to be rendered\n */\n render(\n ctx: CanvasRenderingContext2D,\n left: number,\n top: number,\n styleOverride: ControlRenderingStyleOverride | undefined,\n fabricObject: InteractiveFabricObject,\n ) {\n styleOverride = styleOverride || {};\n switch (styleOverride.cornerStyle || fabricObject.cornerStyle) {\n case 'circle':\n renderCircleControl.call(\n this,\n ctx,\n left,\n top,\n styleOverride,\n fabricObject,\n );\n break;\n default:\n renderSquareControl.call(\n this,\n ctx,\n left,\n top,\n styleOverride,\n fabricObject,\n );\n }\n }\n}\n"],"names":["Control","constructor","options","_defineProperty","SCALE","Object","assign","this","getTransformAnchorPoint","_this$transformAnchor","transformAnchorPoint","Point","x","y","shouldActivate","controlKey","fabricObject","pointer","_ref","_fabricObject$canvas","tl","tr","br","bl","canvas","getActiveObject","isControlVisible","Intersection","isPointInPolygon","getActionHandler","eventData","control","actionHandler","getMouseDownHandler","mouseDownHandler","getMouseUpHandler","mouseUpHandler","cursorStyleHandler","coord","cursorStyle","getActionName","actionName","getVisibility","_fabricObject$_contro","_fabricObject$_contro2","_controlsVisibility","visible","setVisibility","visibility","name","positionHandler","dim","finalMatrix","currentControl","offsetX","offsetY","transform","calcCornerCoords","angle","objectCornerSize","centerX","centerY","isTouch","t","multiplyTransformMatrixArray","createTranslateMatrix","createRotateMatrix","createScaleMatrix","touchSizeX","sizeX","touchSizeY","sizeY","commonRenderProps","ctx","left","top","styleOverride","arguments","length","undefined","cornerSize","cornerColor","transparentCorners","cornerStrokeColor","sizeFromProps","xSize","ySize","transparent","opName","STROKE","FILL","strokeColor","stroke","fillStyle","strokeStyle","translate","rotate","degreesToRadians","getTotalAngle","render","cornerStyle","renderCircleControl","call","renderSquareControl"],"mappings":"qjBA8BO,MAAMA,EA8HXC,WAAAA,CAAYC,GA7HZC,kBAQU,GAEVA,oBAWaC,GAEbD,eAOQ,GAERA,WAOI,GAEJA,WAOI,GAEJA,iBAYU,GAEVA,iBAMU,GAEVA,eAMQ,GAERA,eAMQ,GAERA,oBAMa,GAEbA,oBAMa,GAEbA,qBAMc,aAEdA,yBAMiB,GAQfE,OAAOC,OAAOC,KAAML,EACtB,CAEAM,uBAAAA,GAGE,IAAAC,EACA,OAE2B,QADzBA,EACAF,KAAKG,gCAAoBD,EAAAA,EAEzB,IAAIE,EAAgB,GAATJ,KAAKK,EAAmB,GAATL,KAAKM,EAEnC,CAgCAC,cAAAA,CACEC,EACAC,EACAC,EAAcC,GAEd,IAAAC,EAAA,IADAC,GAAEA,EAAEC,GAAEA,EAAEC,GAAEA,EAAEC,GAAEA,GAAkBL,EAGhC,OACqB,QAAnBC,EAAAH,EAAaQ,cAAM,IAAAL,OAAA,EAAnBA,EAAqBM,qBAAsBT,GAC3CA,EAAaU,iBAAiBX,IAC9BY,EAAaC,iBAAiBX,EAAS,CAACG,EAAIC,EAAIC,EAAIC,GAExD,CASAM,gBAAAA,CACEC,EACAd,EACAe,GAEA,OAAOxB,KAAKyB,aACd,CASAC,mBAAAA,CACEH,EACAd,EACAe,GAEA,OAAOxB,KAAK2B,gBACd,CAUAC,iBAAAA,CACEL,EACAd,EACAe,GAEA,OAAOxB,KAAK6B,cACd,CAWAC,kBAAAA,CACEP,EACAC,EACAf,EAC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KAAKgE,OAASa,EACtBG,OACgC,IAAvBL,EACHA,EACAlE,EAAakE,mBACnBM,EAASD,EAAcE,EAASC,EAChCC,EAAcR,GAAqBnE,EAAamE,kBAChDS,GAAUL,KAAiBI,EAM7B,OALAlB,EAAIoB,UAAYZ,GAAejE,EAAaiE,aAAe,GAC3DR,EAAIqB,YAAcH,GAAe,GACjClB,EAAIsB,UAAUrB,EAAMC,GAEpBF,EAAIuB,OAAOC,EAAiBjF,EAAakF,kBAClC,CACLN,SACAP,QACAC,QACAJ,mBAAoBK,EACpBC,SAEJ,CAcAW,MAAAA,CACE1B,EACAC,EACAC,EACAC,EACA5D,GAGA,GACO,aAFP4D,EAAgBA,GAAiB,CAAA,GACXwB,aAAepF,EAAaoF,aAE9CC,EAAoBC,KAClB/F,KACAkE,EACAC,EACAC,EACAC,EACA5D,QAIFuF,EAAoBD,KAClB/F,KACAkE,EACAC,EACAC,EACAC,EACA5D,EAGR"}
|
|
@@ -117,6 +117,14 @@ class Control {
|
|
|
117
117
|
_defineProperty(this, "withConnection", false);
|
|
118
118
|
Object.assign(this, options);
|
|
119
119
|
}
|
|
120
|
+
getTransformAnchorPoint() {
|
|
121
|
+
var _this$transformAnchor;
|
|
122
|
+
return (// return the control transformAnchorPoint
|
|
123
|
+
(_this$transformAnchor = this.transformAnchorPoint) !== null && _this$transformAnchor !== void 0 ? _this$transformAnchor :
|
|
124
|
+
// otherwise will return the opposite origin of where the control is located.
|
|
125
|
+
new Point(-this.x + 0.5, -this.y + 0.5)
|
|
126
|
+
);
|
|
127
|
+
}
|
|
120
128
|
|
|
121
129
|
/**
|
|
122
130
|
* The control actionHandler, provide one to handle action ( control being moved )
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Control.mjs","sources":["../../../src/controls/Control.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-unused-vars */\nimport type {\n ControlActionHandler,\n TPointerEvent,\n TransformActionHandler,\n} from '../EventTypeDefs';\nimport { Intersection } from '../Intersection';\nimport { Point } from '../Point';\nimport { FILL, SCALE, STROKE } from '../constants';\nimport type {\n InteractiveFabricObject,\n TOCoord,\n} from '../shapes/Object/InteractiveObject';\nimport type { TCornerPoint, TDegree, TMat2D } from '../typedefs';\nimport {\n createRotateMatrix,\n createScaleMatrix,\n createTranslateMatrix,\n multiplyTransformMatrixArray,\n} from '../util/misc/matrix';\nimport { degreesToRadians } from '../util/misc/radiansDegreesConversion';\nimport type { ControlRenderingStyleOverride } from './controlRendering';\nimport { renderCircleControl, renderSquareControl } from './controlRendering';\n\nexport class Control {\n /**\n * keep track of control visibility.\n * mainly for backward compatibility.\n * if you do not want to see a control, you can remove it\n * from the control set.\n * @type {Boolean}\n * @default true\n */\n visible = true;\n\n /**\n * Name of the action that the control will likely execute.\n * This is optional. FabricJS uses to identify what the user is doing for some\n * extra optimizations. If you are writing a custom control and you want to know\n * somewhere else in the code what is going on, you can use this string here.\n * you can also provide a custom getActionName if your control run multiple actions\n * depending on some external state.\n * default to scale since is the most common, used on 4 corners by default\n * @type {String}\n * @default 'scale'\n */\n actionName = SCALE;\n\n /**\n * Drawing angle of the control.\n * NOT used for now, but name marked as needed for internal logic\n * example: to reuse the same drawing function for different rotated controls\n * @type {Number}\n * @default 0\n */\n angle = 0;\n\n /**\n * Relative position of the control. X\n * 0,0 is the center of the Object, while -0.5 (left) or 0.5 (right) are the extremities\n * of the bounding box.\n * @type {Number}\n * @default 0\n */\n x = 0;\n\n /**\n * Relative position of the control. Y\n * 0,0 is the center of the Object, while -0.5 (top) or 0.5 (bottom) are the extremities\n * of the bounding box.\n * @type {Number}\n * @default 0\n */\n y = 0;\n\n /**\n * Horizontal offset of the control from the defined position. In pixels\n * Positive offset moves the control to the right, negative to the left.\n * It used when you want to have position of control that does not scale with\n * the bounding box. Example: rotation control is placed at x:0, y: 0.5 on\n * the boundind box, with an offset of 30 pixels vertically. Those 30 pixels will\n * stay 30 pixels no matter how the object is big. Another example is having 2\n * controls in the corner, that stay in the same position when the object scale.\n * of the bounding box.\n * @type {Number}\n * @default 0\n */\n offsetX = 0;\n\n /**\n * Vertical offset of the control from the defined position. In pixels\n * Positive offset moves the control to the bottom, negative to the top.\n * @type {Number}\n * @default 0\n */\n offsetY = 0;\n\n /**\n * Sets the length of the control. If null, defaults to object's cornerSize.\n * Expects both sizeX and sizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n sizeX = 0;\n\n /**\n * Sets the height of the control. If null, defaults to object's cornerSize.\n * Expects both sizeX and sizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n sizeY = 0;\n\n /**\n * Sets the length of the touch area of the control. If null, defaults to object's touchCornerSize.\n * Expects both touchSizeX and touchSizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n touchSizeX = 0;\n\n /**\n * Sets the height of the touch area of the control. If null, defaults to object's touchCornerSize.\n * Expects both touchSizeX and touchSizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n touchSizeY = 0;\n\n /**\n * Css cursor style to display when the control is hovered.\n * if the method `cursorStyleHandler` is provided, this property is ignored.\n * @type {String}\n * @default 'crosshair'\n */\n cursorStyle = 'crosshair';\n\n /**\n * If controls has an offsetY or offsetX, draw a line that connects\n * the control to the bounding box\n * @type {Boolean}\n * @default false\n */\n withConnection = false;\n\n constructor(options?: Partial<Control>) {\n Object.assign(this, options);\n }\n\n /**\n * The control actionHandler, provide one to handle action ( control being moved )\n * @param {Event} eventData the native mouse event\n * @param {Transform} transformData properties of the current transform\n * @param {Number} x x position of the cursor\n * @param {Number} y y position of the cursor\n * @return {Boolean} true if the action/event modified the object\n */\n declare actionHandler: TransformActionHandler;\n\n /**\n * The control handler for mouse down, provide one to handle mouse down on control\n * @param {Event} eventData the native mouse event\n * @param {Transform} transformData properties of the current transform\n * @param {Number} x x position of the cursor\n * @param {Number} y y position of the cursor\n * @return {Boolean} true if the action/event modified the object\n */\n declare mouseDownHandler?: ControlActionHandler;\n\n /**\n * The control mouseUpHandler, provide one to handle an effect on mouse up.\n * @param {Event} eventData the native mouse event\n * @param {Transform} transformData properties of the current transform\n * @param {Number} x x position of the cursor\n * @param {Number} y y position of the cursor\n * @return {Boolean} true if the action/event modified the object\n */\n declare mouseUpHandler?: ControlActionHandler;\n\n shouldActivate(\n controlKey: string,\n fabricObject: InteractiveFabricObject,\n pointer: Point,\n { tl, tr, br, bl }: TCornerPoint,\n ) {\n // TODO: locking logic can be handled here instead of in the control handler logic\n return (\n fabricObject.canvas?.getActiveObject() === fabricObject &&\n fabricObject.isControlVisible(controlKey) &&\n Intersection.isPointInPolygon(pointer, [tl, tr, br, bl])\n );\n }\n\n /**\n * Returns control actionHandler\n * @param {Event} eventData the native mouse event\n * @param {FabricObject} fabricObject on which the control is displayed\n * @param {Control} control control for which the action handler is being asked\n * @return {Function} the action handler\n */\n getActionHandler(\n eventData: TPointerEvent,\n fabricObject: InteractiveFabricObject,\n control: Control,\n ): TransformActionHandler | undefined {\n return this.actionHandler;\n }\n\n /**\n * Returns control mouseDown handler\n * @param {Event} eventData the native mouse event\n * @param {FabricObject} fabricObject on which the control is displayed\n * @param {Control} control control for which the action handler is being asked\n * @return {Function} the action handler\n */\n getMouseDownHandler(\n eventData: TPointerEvent,\n fabricObject: InteractiveFabricObject,\n control: Control,\n ): ControlActionHandler | undefined {\n return this.mouseDownHandler;\n }\n\n /**\n * Returns control mouseUp handler.\n * During actions the fabricObject or the control can be of different obj\n * @param {Event} eventData the native mouse event\n * @param {FabricObject} fabricObject on which the control is displayed\n * @param {Control} control control for which the action handler is being asked\n * @return {Function} the action handler\n */\n getMouseUpHandler(\n eventData: TPointerEvent,\n fabricObject: InteractiveFabricObject,\n control: Control,\n ): ControlActionHandler | undefined {\n return this.mouseUpHandler;\n }\n\n /**\n * Returns control cursorStyle for css using cursorStyle. If you need a more elaborate\n * function you can pass one in the constructor\n * the cursorStyle property\n * @param {Event} eventData the native mouse event\n * @param {Control} control the current control ( likely this)\n * @param {FabricObject} object on which the control is displayed\n * @return {String}\n */\n cursorStyleHandler(\n eventData: TPointerEvent,\n control: Control,\n fabricObject: InteractiveFabricObject,\n coord: TOCoord,\n ) {\n return control.cursorStyle;\n }\n\n /**\n * Returns the action name. The basic implementation just return the actionName property.\n * @param {Event} eventData the native mouse event\n * @param {Control} control the current control ( likely this)\n * @param {FabricObject} object on which the control is displayed\n * @return {String}\n */\n getActionName(\n eventData: TPointerEvent,\n control: Control,\n fabricObject: InteractiveFabricObject,\n ) {\n return control.actionName;\n }\n\n /**\n * Returns controls visibility\n * @param {FabricObject} object on which the control is displayed\n * @param {String} controlKey key where the control is memorized on the\n * @return {Boolean}\n */\n getVisibility(fabricObject: InteractiveFabricObject, controlKey: string) {\n return fabricObject._controlsVisibility?.[controlKey] ?? this.visible;\n }\n\n /**\n * Sets controls visibility\n * @param {Boolean} visibility for the object\n * @return {Void}\n */\n setVisibility(\n visibility: boolean,\n name?: string,\n fabricObject?: InteractiveFabricObject,\n ) {\n this.visible = visibility;\n }\n\n positionHandler(\n dim: Point,\n finalMatrix: TMat2D,\n fabricObject: InteractiveFabricObject,\n currentControl: Control,\n ) {\n return new Point(\n this.x * dim.x + this.offsetX,\n this.y * dim.y + this.offsetY,\n ).transform(finalMatrix);\n }\n\n /**\n * Returns the coords for this control based on object values.\n * @param {Number} objectAngle angle from the fabric object holding the control\n * @param {Number} objectCornerSize cornerSize from the fabric object holding the control (or touchCornerSize if\n * isTouch is true)\n * @param {Number} centerX x coordinate where the control center should be\n * @param {Number} centerY y coordinate where the control center should be\n * @param {boolean} isTouch true if touch corner, false if normal corner\n */\n calcCornerCoords(\n angle: TDegree,\n objectCornerSize: number,\n centerX: number,\n centerY: number,\n isTouch: boolean,\n fabricObject: InteractiveFabricObject,\n ) {\n const t = multiplyTransformMatrixArray([\n createTranslateMatrix(centerX, centerY),\n createRotateMatrix({ angle }),\n createScaleMatrix(\n (isTouch ? this.touchSizeX : this.sizeX) || objectCornerSize,\n (isTouch ? this.touchSizeY : this.sizeY) || objectCornerSize,\n ),\n ]);\n return {\n tl: new Point(-0.5, -0.5).transform(t),\n tr: new Point(0.5, -0.5).transform(t),\n br: new Point(0.5, 0.5).transform(t),\n bl: new Point(-0.5, 0.5).transform(t),\n };\n }\n\n /**\n * This is an helper method to prepare the canvas to render a control\n * It detectes common control properties and sets the correct fill and\n * stroke styles on the context. It does not execute translations or\n * rotations since different controls need differnt combination of these.\n */\n commonRenderProps(\n ctx: CanvasRenderingContext2D,\n left: number,\n top: number,\n fabricObject: InteractiveFabricObject,\n styleOverride: ControlRenderingStyleOverride = {},\n ): {\n stroke: boolean;\n xSize: number;\n ySize: number;\n transparentCorners: boolean;\n opName: 'stroke' | 'fill';\n } {\n const { cornerSize, cornerColor, transparentCorners, cornerStrokeColor } =\n styleOverride,\n sizeFromProps = cornerSize || fabricObject.cornerSize,\n xSize = this.sizeX || sizeFromProps,\n ySize = this.sizeY || sizeFromProps,\n transparent =\n typeof transparentCorners !== 'undefined'\n ? transparentCorners\n : fabricObject.transparentCorners,\n opName = transparent ? STROKE : FILL,\n strokeColor = cornerStrokeColor || fabricObject.cornerStrokeColor,\n stroke = !transparent && !!strokeColor;\n ctx.fillStyle = cornerColor || fabricObject.cornerColor || '';\n ctx.strokeStyle = strokeColor || '';\n ctx.translate(left, top);\n // angle is relative to canvas plane\n ctx.rotate(degreesToRadians(fabricObject.getTotalAngle()));\n return {\n stroke,\n xSize,\n ySize,\n transparentCorners: transparent,\n opName,\n };\n }\n\n /**\n * Render function for the control.\n * When this function runs the context is unscaled. unrotate. Just retina scaled.\n * all the functions will have to translate to the point left,top before starting Drawing\n * if they want to draw a control where the position is detected.\n * left and top are the result of the positionHandler function\n * @param {RenderingContext2D} ctx the context where the control will be drawn\n * @param {Number} left position of the canvas where we are about to render the control.\n * @param {Number} top position of the canvas where we are about to render the control.\n * @param {Object} styleOverride\n * @param {FabricObject} fabricObject the object where the control is about to be rendered\n */\n render(\n ctx: CanvasRenderingContext2D,\n left: number,\n top: number,\n styleOverride: ControlRenderingStyleOverride | undefined,\n fabricObject: InteractiveFabricObject,\n ) {\n styleOverride = styleOverride || {};\n switch (styleOverride.cornerStyle || fabricObject.cornerStyle) {\n case 'circle':\n renderCircleControl.call(\n this,\n ctx,\n left,\n top,\n styleOverride,\n fabricObject,\n );\n break;\n default:\n renderSquareControl.call(\n this,\n ctx,\n left,\n top,\n styleOverride,\n fabricObject,\n );\n }\n }\n}\n"],"names":["Control","constructor","options","_defineProperty","SCALE","Object","assign","shouldActivate","controlKey","fabricObject","pointer","_ref","_fabricObject$canvas","tl","tr","br","bl","canvas","getActiveObject","isControlVisible","Intersection","isPointInPolygon","getActionHandler","eventData","control","actionHandler","getMouseDownHandler","mouseDownHandler","getMouseUpHandler","mouseUpHandler","cursorStyleHandler","coord","cursorStyle","getActionName","actionName","getVisibility","_fabricObject$_contro","_fabricObject$_contro2","_controlsVisibility","visible","setVisibility","visibility","name","positionHandler","dim","finalMatrix","currentControl","Point","x","offsetX","y","offsetY","transform","calcCornerCoords","angle","objectCornerSize","centerX","centerY","isTouch","t","multiplyTransformMatrixArray","createTranslateMatrix","createRotateMatrix","createScaleMatrix","touchSizeX","sizeX","touchSizeY","sizeY","commonRenderProps","ctx","left","top","styleOverride","arguments","length","undefined","cornerSize","cornerColor","transparentCorners","cornerStrokeColor","sizeFromProps","xSize","ySize","transparent","opName","STROKE","FILL","strokeColor","stroke","fillStyle","strokeStyle","translate","rotate","degreesToRadians","getTotalAngle","render","cornerStyle","renderCircleControl","call","renderSquareControl"],"mappings":";;;;;;;;AAwBO,MAAMA,OAAO,CAAC;EAyHnBC,WAAWA,CAACC,OAA0B,EAAE;AAxHxC;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AAPEC,IAAAA,eAAA,kBAQU,IAAI,CAAA;AAEd;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAVEA,IAAAA,eAAA,qBAWaC,KAAK,CAAA;AAElB;AACF;AACA;AACA;AACA;AACA;AACA;AANED,IAAAA,eAAA,gBAOQ,CAAC,CAAA;AAET;AACF;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sB,SAAS,CAACrB,IAAI,EAAEC,GAAG,CAAC;AACxB;IACAF,GAAG,CAACuB,MAAM,CAACC,gBAAgB,CAACpF,YAAY,CAACqF,aAAa,EAAE,CAAC,CAAC;IAC1D,OAAO;MACLN,MAAM;MACNP,KAAK;MACLC,KAAK;AACLJ,MAAAA,kBAAkB,EAAEK,WAAW;AAC/BC,MAAAA;KACD;AACH,EAAA;;AAEA;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEW,MAAMA,CACJ1B,GAA6B,EAC7BC,IAAY,EACZC,GAAW,EACXC,aAAwD,EACxD/D,YAAqC,EACrC;AACA+D,IAAAA,aAAa,GAAGA,aAAa,IAAI,EAAE;AACnC,IAAA,QAAQA,aAAa,CAACwB,WAAW,IAAIvF,YAAY,CAACuF,WAAW;AAC3D,MAAA,KAAK,QAAQ;AACXC,QAAAA,mBAAmB,CAACC,IAAI,CACtB,IAAI,EACJ7B,GAAG,EACHC,IAAI,EACJC,GAAG,EACHC,aAAa,EACb/D,YACF,CAAC;AACD,QAAA;AACF,MAAA;AACE0F,QAAAA,mBAAmB,CAACD,IAAI,CACtB,IAAI,EACJ7B,GAAG,EACHC,IAAI,EACJC,GAAG,EACHC,aAAa,EACb/D,YACF,CAAC;AACL;AACF,EAAA;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"Control.mjs","sources":["../../../src/controls/Control.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-unused-vars */\nimport type {\n ControlActionHandler,\n TPointerEvent,\n TransformActionHandler,\n} from '../EventTypeDefs';\nimport { Intersection } from '../Intersection';\nimport { Point } from '../Point';\nimport { FILL, SCALE, STROKE } from '../constants';\nimport type {\n InteractiveFabricObject,\n TOCoord,\n} from '../shapes/Object/InteractiveObject';\nimport type {\n TCornerPoint,\n TDegree,\n TMat2D,\n TOriginX,\n TOriginY,\n} from '../typedefs';\nimport {\n createRotateMatrix,\n createScaleMatrix,\n createTranslateMatrix,\n multiplyTransformMatrixArray,\n} from '../util/misc/matrix';\nimport { degreesToRadians } from '../util/misc/radiansDegreesConversion';\nimport type { ControlRenderingStyleOverride } from './controlRendering';\nimport { renderCircleControl, renderSquareControl } from './controlRendering';\n\nexport class Control {\n /**\n * keep track of control visibility.\n * mainly for backward compatibility.\n * if you do not want to see a control, you can remove it\n * from the control set.\n * @type {Boolean}\n * @default true\n */\n visible = true;\n\n /**\n * Name of the action that the control will likely execute.\n * This is optional. FabricJS uses to identify what the user is doing for some\n * extra optimizations. If you are writing a custom control and you want to know\n * somewhere else in the code what is going on, you can use this string here.\n * you can also provide a custom getActionName if your control run multiple actions\n * depending on some external state.\n * default to scale since is the most common, used on 4 corners by default\n * @type {String}\n * @default 'scale'\n */\n actionName = SCALE;\n\n /**\n * Drawing angle of the control.\n * NOT used for now, but name marked as needed for internal logic\n * example: to reuse the same drawing function for different rotated controls\n * @type {Number}\n * @default 0\n */\n angle = 0;\n\n /**\n * Relative position of the control. X\n * 0,0 is the center of the Object, while -0.5 (left) or 0.5 (right) are the extremities\n * of the bounding box.\n * @type {Number}\n * @default 0\n */\n x = 0;\n\n /**\n * Relative position of the control. Y\n * 0,0 is the center of the Object, while -0.5 (top) or 0.5 (bottom) are the extremities\n * of the bounding box.\n * @type {Number}\n * @default 0\n */\n y = 0;\n\n /**\n * Horizontal offset of the control from the defined position. In pixels\n * Positive offset moves the control to the right, negative to the left.\n * It used when you want to have position of control that does not scale with\n * the bounding box. Example: rotation control is placed at x:0, y: 0.5 on\n * the boundind box, with an offset of 30 pixels vertically. Those 30 pixels will\n * stay 30 pixels no matter how the object is big. Another example is having 2\n * controls in the corner, that stay in the same position when the object scale.\n * of the bounding box.\n * @type {Number}\n * @default 0\n */\n offsetX = 0;\n\n /**\n * Vertical offset of the control from the defined position. In pixels\n * Positive offset moves the control to the bottom, negative to the top.\n * @type {Number}\n * @default 0\n */\n offsetY = 0;\n\n /**\n * Sets the length of the control. If null, defaults to object's cornerSize.\n * Expects both sizeX and sizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n sizeX = 0;\n\n /**\n * Sets the height of the control. If null, defaults to object's cornerSize.\n * Expects both sizeX and sizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n sizeY = 0;\n\n /**\n * Sets the length of the touch area of the control. If null, defaults to object's touchCornerSize.\n * Expects both touchSizeX and touchSizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n touchSizeX = 0;\n\n /**\n * Sets the height of the touch area of the control. If null, defaults to object's touchCornerSize.\n * Expects both touchSizeX and touchSizeY to be set when set.\n * @type {?Number}\n * @default null\n */\n touchSizeY = 0;\n\n /**\n * Css cursor style to display when the control is hovered.\n * if the method `cursorStyleHandler` is provided, this property is ignored.\n * @type {String}\n * @default 'crosshair'\n */\n cursorStyle = 'crosshair';\n\n /**\n * If controls has an offsetY or offsetX, draw a line that connects\n * the control to the bounding box\n * @type {Boolean}\n * @default false\n */\n withConnection = false;\n\n declare transformAnchorPoint?: {\n x: TOriginX;\n y: TOriginY;\n };\n\n constructor(options?: Partial<Control>) {\n Object.assign(this, options);\n }\n\n getTransformAnchorPoint(): {\n x: TOriginX;\n y: TOriginY;\n } {\n return (\n // return the control transformAnchorPoint\n this.transformAnchorPoint ??\n // otherwise will return the opposite origin of where the control is located.\n new Point(-this.x + 0.5, -this.y + 0.5)\n );\n }\n\n /**\n * The control actionHandler, provide one to handle action ( control being moved )\n * @param {Event} eventData the native mouse event\n * @param {Transform} transformData properties of the current transform\n * @param {Number} x x position of the cursor\n * @param {Number} y y position of the cursor\n * @return {Boolean} true if the action/event modified the object\n */\n declare actionHandler: TransformActionHandler;\n\n /**\n * The control handler for mouse down, provide one to handle mouse down on control\n * @param {Event} eventData the native mouse event\n * @param {Transform} transformData properties of the current transform\n * @param {Number} x x position of the cursor\n * @param {Number} y y position of the cursor\n * @return {Boolean} true if the action/event modified the object\n */\n declare mouseDownHandler?: ControlActionHandler;\n\n /**\n * The control mouseUpHandler, provide one to handle an effect on mouse up.\n * @param {Event} eventData the native mouse event\n * @param {Transform} transformData properties of the current transform\n * @param {Number} x x position of the cursor\n * @param {Number} y y position of the cursor\n * @return {Boolean} true if the action/event modified the object\n */\n declare mouseUpHandler?: ControlActionHandler;\n\n shouldActivate(\n controlKey: string,\n fabricObject: InteractiveFabricObject,\n pointer: Point,\n { tl, tr, br, bl }: TCornerPoint,\n ) {\n // TODO: locking logic can be handled here instead of in the control handler logic\n return (\n fabricObject.canvas?.getActiveObject() === fabricObject &&\n fabricObject.isControlVisible(controlKey) &&\n Intersection.isPointInPolygon(pointer, [tl, tr, br, bl])\n );\n }\n\n /**\n * Returns control actionHandler\n * @param {Event} eventData the native mouse event\n * @param {FabricObject} fabricObject on which the control is displayed\n * @param {Control} control control for which the action handler is being asked\n * @return {Function} the action handler\n */\n getActionHandler(\n eventData: TPointerEvent,\n fabricObject: InteractiveFabricObject,\n control: Control,\n ): TransformActionHandler | undefined {\n return this.actionHandler;\n }\n\n /**\n * Returns control mouseDown handler\n * @param {Event} eventData the native mouse event\n * @param {FabricObject} fabricObject on which the control is displayed\n * @param {Control} control control for which the action handler is being asked\n * @return {Function} the action handler\n */\n getMouseDownHandler(\n eventData: TPointerEvent,\n fabricObject: InteractiveFabricObject,\n control: Control,\n ): ControlActionHandler | undefined {\n return this.mouseDownHandler;\n }\n\n /**\n * Returns control mouseUp handler.\n * During actions the fabricObject or the control can be of different obj\n * @param {Event} eventData the native mouse event\n * @param {FabricObject} fabricObject on which the control is displayed\n * @param {Control} control control for which the action handler is being asked\n * @return {Function} the action handler\n */\n getMouseUpHandler(\n eventData: TPointerEvent,\n fabricObject: InteractiveFabricObject,\n control: Control,\n ): ControlActionHandler | undefined {\n return this.mouseUpHandler;\n }\n\n /**\n * Returns control cursorStyle for css using cursorStyle. If you need a more elaborate\n * function you can pass one in the constructor\n * the cursorStyle property\n * @param {Event} eventData the native mouse event\n * @param {Control} control the current control ( likely this)\n * @param {FabricObject} object on which the control is displayed\n * @return {String}\n */\n cursorStyleHandler(\n eventData: TPointerEvent,\n control: Control,\n fabricObject: InteractiveFabricObject,\n coord: TOCoord,\n ) {\n return control.cursorStyle;\n }\n\n /**\n * Returns the action name. The basic implementation just return the actionName property.\n * @param {Event} eventData the native mouse event\n * @param {Control} control the current control ( likely this)\n * @param {FabricObject} object on which the control is displayed\n * @return {String}\n */\n getActionName(\n eventData: TPointerEvent,\n control: Control,\n fabricObject: InteractiveFabricObject,\n ) {\n return control.actionName;\n }\n\n /**\n * Returns controls visibility\n * @param {FabricObject} object on which the control is displayed\n * @param {String} controlKey key where the control is memorized on the\n * @return {Boolean}\n */\n getVisibility(fabricObject: InteractiveFabricObject, controlKey: string) {\n return fabricObject._controlsVisibility?.[controlKey] ?? this.visible;\n }\n\n /**\n * Sets controls visibility\n * @param {Boolean} visibility for the object\n * @return {Void}\n */\n setVisibility(\n visibility: boolean,\n name?: string,\n fabricObject?: InteractiveFabricObject,\n ) {\n this.visible = visibility;\n }\n\n positionHandler(\n dim: Point,\n finalMatrix: TMat2D,\n fabricObject: InteractiveFabricObject,\n currentControl: Control,\n ) {\n return new Point(\n this.x * dim.x + this.offsetX,\n this.y * dim.y + this.offsetY,\n ).transform(finalMatrix);\n }\n\n /**\n * Returns the coords for this control based on object values.\n * @param {Number} objectAngle angle from the fabric object holding the control\n * @param {Number} objectCornerSize cornerSize from the fabric object holding the control (or touchCornerSize if\n * isTouch is true)\n * @param {Number} centerX x coordinate where the control center should be\n * @param {Number} centerY y coordinate where the control center should be\n * @param {boolean} isTouch true if touch corner, false if normal corner\n */\n calcCornerCoords(\n angle: TDegree,\n objectCornerSize: number,\n centerX: number,\n centerY: number,\n isTouch: boolean,\n fabricObject: InteractiveFabricObject,\n ) {\n const t = multiplyTransformMatrixArray([\n createTranslateMatrix(centerX, centerY),\n createRotateMatrix({ angle }),\n createScaleMatrix(\n (isTouch ? this.touchSizeX : this.sizeX) || objectCornerSize,\n (isTouch ? this.touchSizeY : this.sizeY) || objectCornerSize,\n ),\n ]);\n return {\n tl: new Point(-0.5, -0.5).transform(t),\n tr: new Point(0.5, -0.5).transform(t),\n br: new Point(0.5, 0.5).transform(t),\n bl: new Point(-0.5, 0.5).transform(t),\n };\n }\n\n /**\n * This is an helper method to prepare the canvas to render a control\n * It detectes common control properties and sets the correct fill and\n * stroke styles on the context. It does not execute translations or\n * rotations since different controls need differnt combination of these.\n */\n commonRenderProps(\n ctx: CanvasRenderingContext2D,\n left: number,\n top: number,\n fabricObject: InteractiveFabricObject,\n styleOverride: ControlRenderingStyleOverride = {},\n ): {\n stroke: boolean;\n xSize: number;\n ySize: number;\n transparentCorners: boolean;\n opName: 'stroke' | 'fill';\n } {\n const { cornerSize, cornerColor, transparentCorners, cornerStrokeColor } =\n styleOverride,\n sizeFromProps = cornerSize || fabricObject.cornerSize,\n xSize = this.sizeX || sizeFromProps,\n ySize = this.sizeY || sizeFromProps,\n transparent =\n typeof transparentCorners !== 'undefined'\n ? transparentCorners\n : fabricObject.transparentCorners,\n opName = transparent ? STROKE : FILL,\n strokeColor = cornerStrokeColor || fabricObject.cornerStrokeColor,\n stroke = !transparent && !!strokeColor;\n ctx.fillStyle = cornerColor || fabricObject.cornerColor || '';\n ctx.strokeStyle = strokeColor || '';\n ctx.translate(left, top);\n // angle is relative to canvas plane\n ctx.rotate(degreesToRadians(fabricObject.getTotalAngle()));\n return {\n stroke,\n xSize,\n ySize,\n transparentCorners: transparent,\n opName,\n };\n }\n\n /**\n * Render function for the control.\n * When this function runs the context is unscaled. unrotate. Just retina scaled.\n * all the functions will have to translate to the point left,top before starting Drawing\n * if they want to draw a control where the position is detected.\n * left and top are the result of the positionHandler function\n * @param {RenderingContext2D} ctx the context where the control will be drawn\n * @param {Number} left position of the canvas where we are about to render the control.\n * @param {Number} top position of the canvas where we are about to render the control.\n * @param {Object} styleOverride\n * @param {FabricObject} fabricObject the object where the control is about to be rendered\n */\n render(\n ctx: CanvasRenderingContext2D,\n left: number,\n top: number,\n styleOverride: ControlRenderingStyleOverride | undefined,\n fabricObject: InteractiveFabricObject,\n ) {\n styleOverride = styleOverride || {};\n switch (styleOverride.cornerStyle || fabricObject.cornerStyle) {\n case 'circle':\n renderCircleControl.call(\n this,\n ctx,\n left,\n top,\n styleOverride,\n fabricObject,\n );\n break;\n default:\n renderSquareControl.call(\n this,\n ctx,\n left,\n top,\n styleOverride,\n fabricObject,\n );\n }\n }\n}\n"],"names":["Control","constructor","options","_defineProperty","SCALE","Object","assign","getTransformAnchorPoint","_this$transformAnchor","transformAnchorPoint","Point","x","y","shouldActivate","controlKey","fabricObject","pointer","_ref","_fabricObject$canvas","tl","tr","br","bl","canvas","getActiveObject","isControlVisible","Intersection","isPointInPolygon","getActionHandler","eventData","control","actionHandler","getMouseDownHandler","mouseDownHandler","getMouseUpHandler","mouseUpHandler","cursorStyleHandler","coord","cursorStyle","getActionName","actionName","getVisibility","_fabricObject$_contro","_fabricObject$_contro2","_controlsVisibility","visible","setVisibility","visibility","name","positionHandler","dim","finalMatrix","currentControl","offsetX","offsetY","transform","calcCornerCoords","angle","objectCornerSize","centerX","centerY","isTouch","t","multiplyTransformMatrixArray","createTranslateMatrix","createRotateMatrix","createScaleMatrix","touchSizeX","sizeX","touchSizeY","sizeY","commonRenderProps","ctx","left","top","styleOverride","arguments","length","undefined","cornerSize","cornerColor","transparentCorners","cornerStrokeColor","sizeFromProps","xSize","ySize","transparent","opName","STROKE","FILL","strokeColor","stroke","fillStyle","strokeStyle","translate","rotate","degreesToRadians","getTotalAngle","render","cornerStyle","renderCircleControl","call","renderSquareControl"],"mappings":";;;;;;;;AA8BO,MAAMA,OAAO,CAAC;EA8HnBC,WAAWA,CAACC,OAA0B,EAAE;AA7HxC;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AAPEC,IAAAA,eAAA,kBAQU,IAAI,CAAA;AAEd;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAVEA,IAAAA,eAAA,qBAWaC,KAAK,CAAA;AAElB;AACF;AACA;AACA;AACA;AACA;AACA;AANED,IAAAA,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AAM,GAAGD,WAAW,GAAGE,MAAM,GAAGC,IAAI;AACpCC,MAAAA,WAAW,GAAGR,iBAAiB,IAAInE,YAAY,CAACmE,iBAAiB;AACjES,MAAAA,MAAM,GAAG,CAACL,WAAW,IAAI,CAAC,CAACI,WAAW;IACxClB,GAAG,CAACoB,SAAS,GAAGZ,WAAW,IAAIjE,YAAY,CAACiE,WAAW,IAAI,EAAE;AAC7DR,IAAAA,GAAG,CAACqB,WAAW,GAAGH,WAAW,IAAI,EAAE;AACnClB,IAAAA,GAAG,CAACsB,SAAS,CAACrB,IAAI,EAAEC,GAAG,CAAC;AACxB;IACAF,GAAG,CAACuB,MAAM,CAACC,gBAAgB,CAACjF,YAAY,CAACkF,aAAa,EAAE,CAAC,CAAC;IAC1D,OAAO;MACLN,MAAM;MACNP,KAAK;MACLC,KAAK;AACLJ,MAAAA,kBAAkB,EAAEK,WAAW;AAC/BC,MAAAA;KACD;AACH,EAAA;;AAEA;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACEW,MAAMA,CACJ1B,GAA6B,EAC7BC,IAAY,EACZC,GAAW,EACXC,aAAwD,EACxD5D,YAAqC,EACrC;AACA4D,IAAAA,aAAa,GAAGA,aAAa,IAAI,EAAE;AACnC,IAAA,QAAQA,aAAa,CAACwB,WAAW,IAAIpF,YAAY,CAACoF,WAAW;AAC3D,MAAA,KAAK,QAAQ;AACXC,QAAAA,mBAAmB,CAACC,IAAI,CACtB,IAAI,EACJ7B,GAAG,EACHC,IAAI,EACJC,GAAG,EACHC,aAAa,EACb5D,YACF,CAAC;AACD,QAAA;AACF,MAAA;AACEuF,QAAAA,mBAAmB,CAACD,IAAI,CACtB,IAAI,EACJ7B,GAAG,EACHC,IAAI,EACJC,GAAG,EACHC,aAAa,EACb5D,YACF,CAAC;AACL;AACF,EAAA;AACF;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Gradient.d.ts","sourceRoot":"","sources":["../../../src/gradient/Gradient.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAQ1C,OAAO,KAAK,EACV,SAAS,EACT,cAAc,EACd,eAAe,EACf,YAAY,EACZ,aAAa,EACb,UAAU,EACV,uBAAuB,EACxB,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"Gradient.d.ts","sourceRoot":"","sources":["../../../src/gradient/Gradient.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAQ1C,OAAO,KAAK,EACV,SAAS,EACT,cAAc,EACd,eAAe,EACf,YAAY,EACZ,aAAa,EACb,UAAU,EACV,uBAAuB,EACxB,MAAM,YAAY,CAAC;AAKpB;;;;GAIG;AACH,qBAAa,QAAQ,CACnB,CAAC,EACD,CAAC,SAAS,YAAY,GAAG,CAAC,SAAS,YAAY,GAAG,CAAC,GAAG,QAAQ;IAE9D;;;;OAIG;IACK,OAAO,EAAE,MAAM,CAAC;IAExB;;;;OAIG;IACK,OAAO,EAAE,MAAM,CAAC;IAExB;;;;;;;OAOG;IACK,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAEnC;;;;;;;;OAQG;IACK,aAAa,EAAE,aAAa,CAAC;IAErC;;;;OAIG;IACK,IAAI,EAAE,CAAC,CAAC;IAEhB;;;OAGG;IACK,MAAM,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC;IAElC;;;;OAIG;IACK,UAAU,EAAE,SAAS,EAAE,CAAC;IAEhC;;;OAGG;IACK,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAEpC;;;OAGG;IACH,SAAiB,EAAE,EAAE,MAAM,GAAG,MAAM,CAAC;IAErC,MAAM,CAAC,IAAI,SAAc;gBAEb,OAAO,EAAE,eAAe,CAAC,CAAC,CAAC;IA0BvC;;;;OAIG;IACH,YAAY,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAU/C;;;;OAIG;IACH,QAAQ,CACN,mBAAmB,CAAC,EAAE,CAAC,MAAM,IAAI,GAAG,MAAM,CAAC,EAAE,GAC5C,uBAAuB,CAAC,CAAC,CAAC;IAgB7B;;;;OAIG;IACH,KAAK,CACH,MAAM,EAAE,YAAY,EACpB,EACE,mBAAmB,EAAE,YAAY,GAClC,GAAE;QAAE,mBAAmB,CAAC,EAAE,MAAM,CAAA;KAAO;IA6H1C;;;;OAIG;IACH,MAAM,CAAC,GAAG,EAAE,wBAAwB,GAAG,cAAc;WAcxC,UAAU,CACrB,OAAO,EAAE,eAAe,CAAC,QAAQ,CAAC,GACjC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;WACjB,UAAU,CACrB,OAAO,EAAE,eAAe,CAAC,QAAQ,CAAC,GACjC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAe9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAyCG;IACH,MAAM,CAAC,WAAW,CAChB,EAAE,EAAE,kBAAkB,EACtB,QAAQ,EAAE,YAAY,EACtB,UAAU,EAAE,UAAU,GACrB,QAAQ,CAAC,YAAY,CAAC;CA2B1B"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{defineProperty as t}from"../../_virtual/_rollupPluginBabelHelpers.min.mjs";import{iMatrix as
|
|
1
|
+
import{defineProperty as t}from"../../_virtual/_rollupPluginBabelHelpers.min.mjs";import{iMatrix as r}from"../constants.min.mjs";import{parseTransformAttribute as o}from"../parser/parseTransformAttribute.min.mjs";import{uid as s}from"../util/internals/uid.min.mjs";import{pick as i}from"../util/misc/pick.min.mjs";import{matrixToSVG as e}from"../util/misc/svgExport.min.mjs";import{radialDefaultCoords as n,linearDefaultCoords as a}from"./constants.min.mjs";import{parseColorStops as f}from"./parser/parseColorStops.min.mjs";import{parseCoords as m}from"./parser/parseCoords.min.mjs";import{parseGradientUnits as d,parseType as p}from"./parser/misc.min.mjs";import{classRegistry as c}from"../ClassRegistry.min.mjs";import{isPath as l}from"../util/typeAssertions.min.mjs";import{escapeXml as h}from"../util/lang_string.min.mjs";class g{constructor(t){const{type:r="linear",gradientUnits:o="pixels",coords:i={},colorStops:e=[],offsetX:f=0,offsetY:m=0,gradientTransform:d,id:p}=t||{};Object.assign(this,{type:r,gradientUnits:o,coords:{..."radial"===r?n:a,...i},colorStops:e,offsetX:f,offsetY:m,gradientTransform:d,id:p?`${p}_${s()}`:s()})}addColorStop(t){for(const r in t)this.colorStops.push({offset:parseFloat(r),color:t[r]});return this}toObject(t){return{...i(this,t),type:this.type,coords:{...this.coords},colorStops:this.colorStops.map(t=>({...t})),offsetX:this.offsetX,offsetY:this.offsetY,gradientUnits:this.gradientUnits,gradientTransform:this.gradientTransform?[...this.gradientTransform]:void 0}}toSVG(t){let{additionalTransform:o}=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};const s=[],i=this.gradientTransform?this.gradientTransform.concat():r.concat(),n="pixels"===this.gradientUnits?"userSpaceOnUse":"objectBoundingBox",a=this.colorStops.map(t=>({...t})).sort((t,r)=>t.offset-r.offset);let f=-this.offsetX,m=-this.offsetY;"objectBoundingBox"===n?(f/=t.width,m/=t.height):(f+=t.width/2,m+=t.height/2),l(t)&&"percentage"!==this.gradientUnits&&(f-=t.pathOffset.x,m-=t.pathOffset.y),i[4]-=f,i[5]-=m;const d=[`id="SVGID_${h(String(this.id))}"`,`gradientUnits="${n}"`,`gradientTransform="${o?o+" ":""}${e(i)}"`,""].join(" "),p=t=>parseFloat(String(t));if("linear"===this.type){const{x1:t,y1:r,x2:o,y2:i}=this.coords,e=p(t),n=p(r),a=p(o),f=p(i);s.push("<linearGradient ",d,' x1="',e,'" y1="',n,'" x2="',a,'" y2="',f,'">\n')}else if("radial"===this.type){const{x1:t,y1:r,x2:o,y2:i,r1:e,r2:n}=this.coords,f=p(t),m=p(r),c=p(o),l=p(i),h=p(e),g=p(n),y=h>g;s.push("<radialGradient ",d,' cx="',y?f:c,'" cy="',y?m:l,'" r="',y?h:g,'" fx="',y?c:f,'" fy="',y?l:m,'">\n'),y&&(a.reverse(),a.forEach(t=>{t.offset=1-t.offset}));const u=Math.min(h,g);if(u>0){const t=u/Math.max(h,g);a.forEach(r=>{r.offset+=t*(1-r.offset)})}}return a.forEach(t=>{let{color:r,offset:o}=t;s.push(`<stop offset="${100*o}%" style="stop-color:${r};"/>\n`)}),s.push("linear"===this.type?"</linearGradient>":"</radialGradient>","\n"),s.join("")}toLive(t){const{x1:r,y1:o,x2:s,y2:i,r1:e,r2:n}=this.coords,a="linear"===this.type?t.createLinearGradient(r,o,s,i):t.createRadialGradient(r,o,e,s,i,n);return this.colorStops.forEach(t=>{let{color:r,offset:o}=t;a.addColorStop(o,r)}),a}static async fromObject(t){const{colorStops:r,gradientTransform:o}=t;return new this({...t,colorStops:r?r.map(t=>({...t})):void 0,gradientTransform:o?[...o]:void 0})}static fromElement(t,r,s){const i=d(t),e=r._findCenterFromElement();return new this({id:t.getAttribute("id")||void 0,type:p(t),coords:m(t,{width:s.viewBoxWidth||s.width,height:s.viewBoxHeight||s.height}),colorStops:f(t,s.opacity),gradientUnits:i,gradientTransform:o(t.getAttribute("gradientTransform")||""),..."pixels"===i?{offsetX:r.width/2-e.x,offsetY:r.height/2-e.y}:{offsetX:0,offsetY:0}})}}t(g,"type","Gradient"),c.setClass(g,"gradient"),c.setClass(g,"linear"),c.setClass(g,"radial");export{g as Gradient};
|
|
2
2
|
//# sourceMappingURL=Gradient.min.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Gradient.min.mjs","sources":["../../../src/gradient/Gradient.ts"],"sourcesContent":["import { iMatrix } from '../constants';\nimport { parseTransformAttribute } from '../parser/parseTransformAttribute';\nimport type { FabricObject } from '../shapes/Object/FabricObject';\nimport type { TMat2D } from '../typedefs';\nimport { uid } from '../util/internals/uid';\nimport { pick } from '../util/misc/pick';\nimport { matrixToSVG } from '../util/misc/svgExport';\nimport { linearDefaultCoords, radialDefaultCoords } from './constants';\nimport { parseColorStops } from './parser/parseColorStops';\nimport { parseCoords } from './parser/parseCoords';\nimport { parseType, parseGradientUnits } from './parser/misc';\nimport type {\n ColorStop,\n GradientCoords,\n GradientOptions,\n GradientType,\n GradientUnits,\n SVGOptions,\n SerializedGradientProps,\n} from './typedefs';\nimport { classRegistry } from '../ClassRegistry';\nimport { isPath } from '../util/typeAssertions';\n\n/**\n * Gradient class\n * @class Gradient\n * @see {@link http://fabric5.fabricjs.com/fabric-intro-part-2#gradients}\n */\nexport class Gradient<\n S,\n T extends GradientType = S extends GradientType ? S : 'linear',\n> {\n /**\n * Horizontal offset for aligning gradients coming from SVG when outside pathgroups\n * @type Number\n * @default 0\n */\n declare offsetX: number;\n\n /**\n * Vertical offset for aligning gradients coming from SVG when outside pathgroups\n * @type Number\n * @default 0\n */\n declare offsetY: number;\n\n /**\n * A transform matrix to apply to the gradient before painting.\n * Imported from svg gradients, is not applied with the current transform in the center.\n * Before this transform is applied, the origin point is at the top left corner of the object\n * plus the addition of offsetY and offsetX.\n * @type Number[]\n * @default null\n */\n declare gradientTransform?: TMat2D;\n\n /**\n * coordinates units for coords.\n * If `pixels`, the number of coords are in the same unit of width / height.\n * If set as `percentage` the coords are still a number, but 1 means 100% of width\n * for the X and 100% of the height for the y. It can be bigger than 1 and negative.\n * allowed values pixels or percentage.\n * @type GradientUnits\n * @default 'pixels'\n */\n declare gradientUnits: GradientUnits;\n\n /**\n * Gradient type linear or radial\n * @type GradientType\n * @default 'linear'\n */\n declare type: T;\n\n /**\n * Defines how the gradient is located in space and spread\n * @type GradientCoords\n */\n declare coords: GradientCoords<T>;\n\n /**\n * Defines how many colors a gradient has and how they are located on the axis\n * defined by coords\n * @type GradientCoords\n */\n declare colorStops: ColorStop[];\n\n /**\n * If true, this object will not be exported during the serialization of a canvas\n * @type boolean\n */\n declare excludeFromExport?: boolean;\n\n /**\n * ID used for SVG export functionalities\n * @type number | string\n */\n declare readonly id: string | number;\n\n static type = 'Gradient';\n\n constructor(options: GradientOptions<T>) {\n const {\n type = 'linear' as T,\n gradientUnits = 'pixels',\n coords = {},\n colorStops = [],\n offsetX = 0,\n offsetY = 0,\n gradientTransform,\n id,\n } = options || {};\n Object.assign(this, {\n type,\n gradientUnits,\n coords: {\n ...(type === 'radial' ? radialDefaultCoords : linearDefaultCoords),\n ...coords,\n },\n colorStops,\n offsetX,\n offsetY,\n gradientTransform,\n id: id ? `${id}_${uid()}` : uid(),\n });\n }\n\n /**\n * Adds another colorStop\n * @param {Record<string, string>} colorStop Object with offset and color\n * @return {Gradient} thisArg\n */\n addColorStop(colorStops: Record<string, string>) {\n for (const position in colorStops) {\n this.colorStops.push({\n offset: parseFloat(position),\n color: colorStops[position],\n });\n }\n return this;\n }\n\n /**\n * Returns object representation of a gradient\n * @param {string[]} [propertiesToInclude] Any properties that you might want to additionally include in the output\n * @return {object}\n */\n toObject(\n propertiesToInclude?: (keyof this | string)[],\n ): SerializedGradientProps<T> {\n return {\n ...pick(this, propertiesToInclude as (keyof this)[]),\n type: this.type,\n coords: { ...this.coords },\n colorStops: this.colorStops.map((colorStop) => ({ ...colorStop })),\n offsetX: this.offsetX,\n offsetY: this.offsetY,\n gradientUnits: this.gradientUnits,\n gradientTransform: this.gradientTransform\n ? [...this.gradientTransform]\n : undefined,\n };\n }\n\n /* _TO_SVG_START_ */\n /**\n * Returns SVG representation of an gradient\n * @param {FabricObject} object Object to create a gradient for\n * @return {String} SVG representation of an gradient (linear/radial)\n */\n toSVG(\n object: FabricObject,\n {\n additionalTransform: preTransform,\n }: { additionalTransform?: string } = {},\n ) {\n const markup = [],\n transform = (\n this.gradientTransform\n ? this.gradientTransform.concat()\n : iMatrix.concat()\n ) as TMat2D,\n gradientUnits =\n this.gradientUnits === 'pixels'\n ? 'userSpaceOnUse'\n : 'objectBoundingBox';\n // colorStops must be sorted ascending, and guarded against deep mutations\n const colorStops = this.colorStops\n .map((colorStop) => ({ ...colorStop }))\n .sort((a, b) => {\n return a.offset - b.offset;\n });\n\n let offsetX = -this.offsetX,\n offsetY = -this.offsetY;\n if (gradientUnits === 'objectBoundingBox') {\n offsetX /= object.width;\n offsetY /= object.height;\n } else {\n offsetX += object.width / 2;\n offsetY += object.height / 2;\n }\n // todo what about polygon/polyline?\n if (isPath(object) && this.gradientUnits !== 'percentage') {\n offsetX -= object.pathOffset.x;\n offsetY -= object.pathOffset.y;\n }\n transform[4] -= offsetX;\n transform[5] -= offsetY;\n\n const commonAttributes = [\n `id=\"SVGID_${this.id}\"`,\n `gradientUnits=\"${gradientUnits}\"`,\n `gradientTransform=\"${\n preTransform ? preTransform + ' ' : ''\n }${matrixToSVG(transform)}\"`,\n '',\n ].join(' ');\n\n if (this.type === 'linear') {\n const { x1, y1, x2, y2 } = this.coords;\n markup.push(\n '<linearGradient ',\n commonAttributes,\n ' x1=\"',\n x1,\n '\" y1=\"',\n y1,\n '\" x2=\"',\n x2,\n '\" y2=\"',\n y2,\n '\">\\n',\n );\n } else if (this.type === 'radial') {\n const { x1, y1, x2, y2, r1, r2 } = this\n .coords as GradientCoords<'radial'>;\n const needsSwap = r1 > r2;\n // svg radial gradient has just 1 radius. the biggest.\n markup.push(\n '<radialGradient ',\n commonAttributes,\n ' cx=\"',\n needsSwap ? x1 : x2,\n '\" cy=\"',\n needsSwap ? y1 : y2,\n '\" r=\"',\n needsSwap ? r1 : r2,\n '\" fx=\"',\n needsSwap ? x2 : x1,\n '\" fy=\"',\n needsSwap ? y2 : y1,\n '\">\\n',\n );\n if (needsSwap) {\n // svg goes from internal to external radius. if radius are inverted, swap color stops.\n colorStops.reverse(); // mutates array\n colorStops.forEach((colorStop) => {\n colorStop.offset = 1 - colorStop.offset;\n });\n }\n const minRadius = Math.min(r1, r2);\n if (minRadius > 0) {\n // i have to shift all colorStops and add new one in 0.\n const maxRadius = Math.max(r1, r2),\n percentageShift = minRadius / maxRadius;\n colorStops.forEach((colorStop) => {\n colorStop.offset += percentageShift * (1 - colorStop.offset);\n });\n }\n }\n\n colorStops.forEach(({ color, offset }) => {\n markup.push(\n `<stop offset=\"${offset * 100}%\" style=\"stop-color:${color};\"/>\\n`,\n );\n });\n\n markup.push(\n this.type === 'linear' ? '</linearGradient>' : '</radialGradient>',\n '\\n',\n );\n\n return markup.join('');\n }\n /* _TO_SVG_END_ */\n\n /**\n * Returns an instance of CanvasGradient\n * @param {CanvasRenderingContext2D} ctx Context to render on\n * @return {CanvasGradient}\n */\n toLive(ctx: CanvasRenderingContext2D): CanvasGradient {\n const { x1, y1, x2, y2, r1, r2 } = this.coords as GradientCoords<'radial'>;\n const gradient =\n this.type === 'linear'\n ? ctx.createLinearGradient(x1, y1, x2, y2)\n : ctx.createRadialGradient(x1, y1, r1, x2, y2, r2);\n\n this.colorStops.forEach(({ color, offset }) => {\n gradient.addColorStop(offset, color);\n });\n\n return gradient;\n }\n\n static async fromObject(\n options: GradientOptions<'linear'>,\n ): Promise<Gradient<'linear'>>;\n static async fromObject(\n options: GradientOptions<'radial'>,\n ): Promise<Gradient<'radial'>>;\n static async fromObject(\n options: GradientOptions<'linear'> | GradientOptions<'radial'>,\n ) {\n const { colorStops, gradientTransform } = options;\n return new this({\n ...options,\n colorStops: colorStops\n ? colorStops.map((colorStop) => ({ ...colorStop }))\n : undefined,\n gradientTransform: gradientTransform ? [...gradientTransform] : undefined,\n });\n }\n\n /* _FROM_SVG_START_ */\n /**\n * Returns {@link Gradient} instance from an SVG element\n * @param {SVGGradientElement} el SVG gradient element\n * @param {FabricObject} instance\n * @param {String} opacity A fill-opacity or stroke-opacity attribute to multiply to each stop's opacity.\n * @param {SVGOptions} svgOptions an object containing the size of the SVG in order to parse correctly gradients\n * that uses gradientUnits as 'userSpaceOnUse' and percentages.\n * @return {Gradient} Gradient instance\n * @see http://www.w3.org/TR/SVG/pservers.html#LinearGradientElement\n * @see http://www.w3.org/TR/SVG/pservers.html#RadialGradientElement\n *\n * @example\n *\n * <linearGradient id=\"linearGrad1\">\n * <stop offset=\"0%\" stop-color=\"white\"/>\n * <stop offset=\"100%\" stop-color=\"black\"/>\n * </linearGradient>\n *\n * OR\n *\n * <linearGradient id=\"linearGrad2\">\n * <stop offset=\"0\" style=\"stop-color:rgb(255,255,255)\"/>\n * <stop offset=\"1\" style=\"stop-color:rgb(0,0,0)\"/>\n * </linearGradient>\n *\n * OR\n *\n * <radialGradient id=\"radialGrad1\">\n * <stop offset=\"0%\" stop-color=\"white\" stop-opacity=\"1\" />\n * <stop offset=\"50%\" stop-color=\"black\" stop-opacity=\"0.5\" />\n * <stop offset=\"100%\" stop-color=\"white\" stop-opacity=\"1\" />\n * </radialGradient>\n *\n * OR\n *\n * <radialGradient id=\"radialGrad2\">\n * <stop offset=\"0\" stop-color=\"rgb(255,255,255)\" />\n * <stop offset=\"0.5\" stop-color=\"rgb(0,0,0)\" />\n * <stop offset=\"1\" stop-color=\"rgb(255,255,255)\" />\n * </radialGradient>\n *\n */\n static fromElement(\n el: SVGGradientElement,\n instance: FabricObject,\n svgOptions: SVGOptions,\n ): Gradient<GradientType> {\n const gradientUnits = parseGradientUnits(el);\n const center = instance._findCenterFromElement();\n return new this({\n id: el.getAttribute('id') || undefined,\n type: parseType(el),\n coords: parseCoords(el, {\n width: svgOptions.viewBoxWidth || svgOptions.width,\n height: svgOptions.viewBoxHeight || svgOptions.height,\n }),\n colorStops: parseColorStops(el, svgOptions.opacity),\n gradientUnits,\n gradientTransform: parseTransformAttribute(\n el.getAttribute('gradientTransform') || '',\n ),\n ...(gradientUnits === 'pixels'\n ? {\n offsetX: instance.width / 2 - center.x,\n offsetY: instance.height / 2 - center.y,\n }\n : {\n offsetX: 0,\n offsetY: 0,\n }),\n });\n }\n /* _FROM_SVG_END_ */\n}\n\nclassRegistry.setClass(Gradient, 'gradient');\nclassRegistry.setClass(Gradient, 'linear');\nclassRegistry.setClass(Gradient, 'radial');\n"],"names":["Gradient","constructor","options","type","gradientUnits","coords","colorStops","offsetX","offsetY","gradientTransform","id","Object","assign","this","radialDefaultCoords","linearDefaultCoords","uid","addColorStop","position","push","offset","parseFloat","color","toObject","propertiesToInclude","pick","map","colorStop","undefined","toSVG","object","additionalTransform","preTransform","arguments","length","markup","transform","concat","iMatrix","sort","a","b","width","height","isPath","pathOffset","x","y","commonAttributes","matrixToSVG","join","x1","y1","x2","y2","r1","r2","needsSwap","reverse","forEach","minRadius","Math","min","percentageShift","max","_ref","toLive","ctx","gradient","createLinearGradient","createRadialGradient","_ref2","fromObject","fromElement","el","instance","svgOptions","parseGradientUnits","center","_findCenterFromElement","getAttribute","parseType","parseCoords","viewBoxWidth","viewBoxHeight","parseColorStops","opacity","parseTransformAttribute","_defineProperty","classRegistry","setClass"],"mappings":"mwBA4BO,MAAMA,EAyEXC,WAAAA,CAAYC,GACV,MAAMC,KACJA,EAAO,SAAaC,cACpBA,EAAgB,SAAQC,OACxBA,EAAS,CAAA,EAAEC,WACXA,EAAa,GAAEC,QACfA,EAAU,EAACC,QACXA,EAAU,EAACC,kBACXA,EAAiBC,GACjBA,GACER,GAAW,CAAA,EACfS,OAAOC,OAAOC,KAAM,CAClBV,OACAC,gBACAC,OAAQ,IACO,WAATF,EAAoBW,EAAsBC,KAC3CV,GAELC,aACAC,UACAC,UACAC,oBACAC,GAAIA,EAAK,GAAGA,KAAMM,MAAUA,KAEhC,CAOAC,YAAAA,CAAaX,GACX,IAAK,MAAMY,KAAYZ,EACrBO,KAAKP,WAAWa,KAAK,CACnBC,OAAQC,WAAWH,GACnBI,MAAOhB,EAAWY,KAGtB,OAAOL,IACT,CAOAU,QAAAA,CACEC,GAEA,MAAO,IACFC,EAAKZ,KAAMW,GACdrB,KAAMU,KAAKV,KACXE,OAAQ,IAAKQ,KAAKR,QAClBC,WAAYO,KAAKP,WAAWoB,IAAKC,IAAS,IAAWA,KACrDpB,QAASM,KAAKN,QACdC,QAASK,K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nB,kBAAmBA,EAAoB,IAAIA,QAAqBmB,GAEpE,CA6CA,kBAAO6C,CACLC,EACAC,EACAC,GAEA,MAAMxE,EAAgByE,EAAmBH,GACnCI,EAASH,EAASI,yBACxB,OAAO,IAAIlE,KAAK,CACdH,GAAIgE,EAAGM,aAAa,YAASpD,EAC7BzB,KAAM8E,EAAUP,GAChBrE,OAAQ6E,EAAYR,EAAI,CACtBhC,MAAOkC,EAAWO,cAAgBP,EAAWlC,MAC7CC,OAAQiC,EAAWQ,eAAiBR,EAAWjC,SAEjDrC,WAAY+E,EAAgBX,EAAIE,EAAWU,SAC3ClF,gBACAK,kBAAmB8E,EACjBb,EAAGM,aAAa,sBAAwB,OAEpB,WAAlB5E,EACA,CACEG,QAASoE,EAASjC,MAAQ,EAAIoC,EAAOhC,EACrCtC,QAASmE,EAAShC,OAAS,EAAImC,EAAO/B,GAExC,CACExC,QAAS,EACTC,QAAS,IAGnB,EAhTAgF,EAjEWxF,EAAQ,OAuEL,YA8ShByF,EAAcC,SAAS1F,EAAU,YACjCyF,EAAcC,SAAS1F,EAAU,UACjCyF,EAAcC,SAAS1F,EAAU"}
|
|
1
|
+
{"version":3,"file":"Gradient.min.mjs","sources":["../../../src/gradient/Gradient.ts"],"sourcesContent":["import { iMatrix } from '../constants';\nimport { parseTransformAttribute } from '../parser/parseTransformAttribute';\nimport type { FabricObject } from '../shapes/Object/FabricObject';\nimport type { TMat2D } from '../typedefs';\nimport { uid } from '../util/internals/uid';\nimport { pick } from '../util/misc/pick';\nimport { matrixToSVG } from '../util/misc/svgExport';\nimport { linearDefaultCoords, radialDefaultCoords } from './constants';\nimport { parseColorStops } from './parser/parseColorStops';\nimport { parseCoords } from './parser/parseCoords';\nimport { parseType, parseGradientUnits } from './parser/misc';\nimport type {\n ColorStop,\n GradientCoords,\n GradientOptions,\n GradientType,\n GradientUnits,\n SVGOptions,\n SerializedGradientProps,\n} from './typedefs';\nimport { classRegistry } from '../ClassRegistry';\nimport { isPath } from '../util/typeAssertions';\nimport { escapeXml } from '../util/lang_string';\n\n/**\n * Gradient class\n * @class Gradient\n * @see {@link http://fabric5.fabricjs.com/fabric-intro-part-2#gradients}\n */\nexport class Gradient<\n S,\n T extends GradientType = S extends GradientType ? S : 'linear',\n> {\n /**\n * Horizontal offset for aligning gradients coming from SVG when outside pathgroups\n * @type Number\n * @default 0\n */\n declare offsetX: number;\n\n /**\n * Vertical offset for aligning gradients coming from SVG when outside pathgroups\n * @type Number\n * @default 0\n */\n declare offsetY: number;\n\n /**\n * A transform matrix to apply to the gradient before painting.\n * Imported from svg gradients, is not applied with the current transform in the center.\n * Before this transform is applied, the origin point is at the top left corner of the object\n * plus the addition of offsetY and offsetX.\n * @type Number[]\n * @default null\n */\n declare gradientTransform?: TMat2D;\n\n /**\n * coordinates units for coords.\n * If `pixels`, the number of coords are in the same unit of width / height.\n * If set as `percentage` the coords are still a number, but 1 means 100% of width\n * for the X and 100% of the height for the y. It can be bigger than 1 and negative.\n * allowed values pixels or percentage.\n * @type GradientUnits\n * @default 'pixels'\n */\n declare gradientUnits: GradientUnits;\n\n /**\n * Gradient type linear or radial\n * @type GradientType\n * @default 'linear'\n */\n declare type: T;\n\n /**\n * Defines how the gradient is located in space and spread\n * @type GradientCoords\n */\n declare coords: GradientCoords<T>;\n\n /**\n * Defines how many colors a gradient has and how they are located on the axis\n * defined by coords\n * @type GradientCoords\n */\n declare colorStops: ColorStop[];\n\n /**\n * If true, this object will not be exported during the serialization of a canvas\n * @type boolean\n */\n declare excludeFromExport?: boolean;\n\n /**\n * ID used for SVG export functionalities\n * @type number | string\n */\n declare readonly id: string | number;\n\n static type = 'Gradient';\n\n constructor(options: GradientOptions<T>) {\n const {\n type = 'linear' as T,\n gradientUnits = 'pixels',\n coords = {},\n colorStops = [],\n offsetX = 0,\n offsetY = 0,\n gradientTransform,\n id,\n } = options || {};\n Object.assign(this, {\n type,\n gradientUnits,\n coords: {\n ...(type === 'radial' ? radialDefaultCoords : linearDefaultCoords),\n ...coords,\n },\n colorStops,\n offsetX,\n offsetY,\n gradientTransform,\n id: id ? `${id}_${uid()}` : uid(),\n });\n }\n\n /**\n * Adds another colorStop\n * @param {Record<string, string>} colorStop Object with offset and color\n * @return {Gradient} thisArg\n */\n addColorStop(colorStops: Record<string, string>) {\n for (const position in colorStops) {\n this.colorStops.push({\n offset: parseFloat(position),\n color: colorStops[position],\n });\n }\n return this;\n }\n\n /**\n * Returns object representation of a gradient\n * @param {string[]} [propertiesToInclude] Any properties that you might want to additionally include in the output\n * @return {object}\n */\n toObject(\n propertiesToInclude?: (keyof this | string)[],\n ): SerializedGradientProps<T> {\n return {\n ...pick(this, propertiesToInclude as (keyof this)[]),\n type: this.type,\n coords: { ...this.coords },\n colorStops: this.colorStops.map((colorStop) => ({ ...colorStop })),\n offsetX: this.offsetX,\n offsetY: this.offsetY,\n gradientUnits: this.gradientUnits,\n gradientTransform: this.gradientTransform\n ? [...this.gradientTransform]\n : undefined,\n };\n }\n\n /* _TO_SVG_START_ */\n /**\n * Returns SVG representation of an gradient\n * @param {FabricObject} object Object to create a gradient for\n * @return {String} SVG representation of an gradient (linear/radial)\n */\n toSVG(\n object: FabricObject,\n {\n additionalTransform: preTransform,\n }: { additionalTransform?: string } = {},\n ) {\n const markup = [],\n transform = (\n this.gradientTransform\n ? this.gradientTransform.concat()\n : iMatrix.concat()\n ) as TMat2D,\n gradientUnits =\n this.gradientUnits === 'pixels'\n ? 'userSpaceOnUse'\n : 'objectBoundingBox';\n // colorStops must be sorted ascending, and guarded against deep mutations\n const colorStops = this.colorStops\n .map((colorStop) => ({ ...colorStop }))\n .sort((a, b) => {\n return a.offset - b.offset;\n });\n\n let offsetX = -this.offsetX,\n offsetY = -this.offsetY;\n if (gradientUnits === 'objectBoundingBox') {\n offsetX /= object.width;\n offsetY /= object.height;\n } else {\n offsetX += object.width / 2;\n offsetY += object.height / 2;\n }\n // todo what about polygon/polyline?\n if (isPath(object) && this.gradientUnits !== 'percentage') {\n offsetX -= object.pathOffset.x;\n offsetY -= object.pathOffset.y;\n }\n transform[4] -= offsetX;\n transform[5] -= offsetY;\n\n const commonAttributes = [\n `id=\"SVGID_${escapeXml(String(this.id))}\"`,\n `gradientUnits=\"${gradientUnits}\"`,\n `gradientTransform=\"${\n preTransform ? preTransform + ' ' : ''\n }${matrixToSVG(transform)}\"`,\n '',\n ].join(' ');\n\n const sanitizeCoord = (value: unknown) => parseFloat(String(value));\n\n if (this.type === 'linear') {\n const { x1, y1, x2, y2 } = this.coords;\n const sx1 = sanitizeCoord(x1);\n const sy1 = sanitizeCoord(y1);\n const sx2 = sanitizeCoord(x2);\n const sy2 = sanitizeCoord(y2);\n markup.push(\n '<linearGradient ',\n commonAttributes,\n ' x1=\"',\n sx1,\n '\" y1=\"',\n sy1,\n '\" x2=\"',\n sx2,\n '\" y2=\"',\n sy2,\n '\">\\n',\n );\n } else if (this.type === 'radial') {\n const { x1, y1, x2, y2, r1, r2 } = this\n .coords as GradientCoords<'radial'>;\n const sx1 = sanitizeCoord(x1);\n const sy1 = sanitizeCoord(y1);\n const sx2 = sanitizeCoord(x2);\n const sy2 = sanitizeCoord(y2);\n const sr1 = sanitizeCoord(r1);\n const sr2 = sanitizeCoord(r2);\n const needsSwap = sr1 > sr2;\n // svg radial gradient has just 1 radius. the biggest.\n markup.push(\n '<radialGradient ',\n commonAttributes,\n ' cx=\"',\n needsSwap ? sx1 : sx2,\n '\" cy=\"',\n needsSwap ? sy1 : sy2,\n '\" r=\"',\n needsSwap ? sr1 : sr2,\n '\" fx=\"',\n needsSwap ? sx2 : sx1,\n '\" fy=\"',\n needsSwap ? sy2 : sy1,\n '\">\\n',\n );\n if (needsSwap) {\n // svg goes from internal to external radius. if radius are inverted, swap color stops.\n colorStops.reverse(); // mutates array\n colorStops.forEach((colorStop) => {\n colorStop.offset = 1 - colorStop.offset;\n });\n }\n const minRadius = Math.min(sr1, sr2);\n if (minRadius > 0) {\n // i have to shift all colorStops and add new one in 0.\n const maxRadius = Math.max(sr1, sr2),\n percentageShift = minRadius / maxRadius;\n colorStops.forEach((colorStop) => {\n colorStop.offset += percentageShift * (1 - colorStop.offset);\n });\n }\n }\n // todo make a malicious script tag injection test with color and also apply a fix with escapeXml\n colorStops.forEach(({ color, offset }) => {\n markup.push(\n `<stop offset=\"${offset * 100}%\" style=\"stop-color:${color};\"/>\\n`,\n );\n });\n\n markup.push(\n this.type === 'linear' ? '</linearGradient>' : '</radialGradient>',\n '\\n',\n );\n\n return markup.join('');\n }\n /* _TO_SVG_END_ */\n\n /**\n * Returns an instance of CanvasGradient\n * @param {CanvasRenderingContext2D} ctx Context to render on\n * @return {CanvasGradient}\n */\n toLive(ctx: CanvasRenderingContext2D): CanvasGradient {\n const { x1, y1, x2, y2, r1, r2 } = this.coords as GradientCoords<'radial'>;\n const gradient =\n this.type === 'linear'\n ? ctx.createLinearGradient(x1, y1, x2, y2)\n : ctx.createRadialGradient(x1, y1, r1, x2, y2, r2);\n\n this.colorStops.forEach(({ color, offset }) => {\n gradient.addColorStop(offset, color);\n });\n\n return gradient;\n }\n\n static async fromObject(\n options: GradientOptions<'linear'>,\n ): Promise<Gradient<'linear'>>;\n static async fromObject(\n options: GradientOptions<'radial'>,\n ): Promise<Gradient<'radial'>>;\n static async fromObject(\n options: GradientOptions<'linear'> | GradientOptions<'radial'>,\n ) {\n const { colorStops, gradientTransform } = options;\n return new this({\n ...options,\n colorStops: colorStops\n ? colorStops.map((colorStop) => ({ ...colorStop }))\n : undefined,\n gradientTransform: gradientTransform ? [...gradientTransform] : undefined,\n });\n }\n\n /* _FROM_SVG_START_ */\n /**\n * Returns {@link Gradient} instance from an SVG element\n * @param {SVGGradientElement} el SVG gradient element\n * @param {FabricObject} instance\n * @param {String} opacity A fill-opacity or stroke-opacity attribute to multiply to each stop's opacity.\n * @param {SVGOptions} svgOptions an object containing the size of the SVG in order to parse correctly gradients\n * that uses gradientUnits as 'userSpaceOnUse' and percentages.\n * @return {Gradient} Gradient instance\n * @see http://www.w3.org/TR/SVG/pservers.html#LinearGradientElement\n * @see http://www.w3.org/TR/SVG/pservers.html#RadialGradientElement\n *\n * @example\n *\n * <linearGradient id=\"linearGrad1\">\n * <stop offset=\"0%\" stop-color=\"white\"/>\n * <stop offset=\"100%\" stop-color=\"black\"/>\n * </linearGradient>\n *\n * OR\n *\n * <linearGradient id=\"linearGrad2\">\n * <stop offset=\"0\" style=\"stop-color:rgb(255,255,255)\"/>\n * <stop offset=\"1\" style=\"stop-color:rgb(0,0,0)\"/>\n * </linearGradient>\n *\n * OR\n *\n * <radialGradient id=\"radialGrad1\">\n * <stop offset=\"0%\" stop-color=\"white\" stop-opacity=\"1\" />\n * <stop offset=\"50%\" stop-color=\"black\" stop-opacity=\"0.5\" />\n * <stop offset=\"100%\" stop-color=\"white\" stop-opacity=\"1\" />\n * </radialGradient>\n *\n * OR\n *\n * <radialGradient id=\"radialGrad2\">\n * <stop offset=\"0\" stop-color=\"rgb(255,255,255)\" />\n * <stop offset=\"0.5\" stop-color=\"rgb(0,0,0)\" />\n * <stop offset=\"1\" stop-color=\"rgb(255,255,255)\" />\n * </radialGradient>\n *\n */\n static fromElement(\n el: SVGGradientElement,\n instance: FabricObject,\n svgOptions: SVGOptions,\n ): Gradient<GradientType> {\n const gradientUnits = parseGradientUnits(el);\n const center = instance._findCenterFromElement();\n return new this({\n id: el.getAttribute('id') || undefined,\n type: parseType(el),\n coords: parseCoords(el, {\n width: svgOptions.viewBoxWidth || svgOptions.width,\n height: svgOptions.viewBoxHeight || svgOptions.height,\n }),\n colorStops: parseColorStops(el, svgOptions.opacity),\n gradientUnits,\n gradientTransform: parseTransformAttribute(\n el.getAttribute('gradientTransform') || '',\n ),\n ...(gradientUnits === 'pixels'\n ? {\n offsetX: instance.width / 2 - center.x,\n offsetY: instance.height / 2 - center.y,\n }\n : {\n offsetX: 0,\n offsetY: 0,\n }),\n });\n }\n /* _FROM_SVG_END_ */\n}\n\nclassRegistry.setClass(Gradient, 'gradient');\nclassRegistry.setClass(Gradient, 'linear');\nclassRegistry.setClass(Gradient, 'radial');\n"],"names":["Gradient","constructor","options","type","gradientUnits","coords","colorStops","offsetX","offsetY","gradientTransform","id","Object","assign","this","radialDefaultCoords","linearDefaultCoords","uid","addColorStop","position","push","offset","parseFloat","color","toObject","propertiesToInclude","pick","map","colorStop","undefined","toSVG","object","additionalTransform","preTransform","arguments","length","markup","transform","concat","iMatrix","sort","a","b","width","height","isPath","pathOffset","x","y","commonAttributes","escapeXml","String","matrixToSVG","join","sanitizeCoord","value","x1","y1","x2","y2","sx1","sy1","sx2","sy2","r1","r2","sr1","sr2","needsSwap","reverse","forEach","minRadius","Math","min","percentageShift","max","_ref","toLive","ctx","gradient","createLinearGradient","createRadialGradient","_ref2","fromObject","fromElement","el","instance","svgOptions","parseGradientUnits","center","_findCenterFromElement","getAttribute","parseType","parseCoords","viewBoxWidth","viewBoxHeight","parseColorStops","opacity","parseTransformAttribute","_defineProperty","classRegistry","setClass"],"mappings":"2zBA6BO,MAAMA,EAyEXC,WAAAA,CAAYC,GACV,MAAMC,KACJA,EAAO,SAAaC,cACpBA,EAAgB,SAAQC,OACxBA,EAAS,CAAA,EAAEC,WACXA,EAAa,GAAEC,QACfA,EAAU,EAACC,QACXA,EAAU,EAACC,kBACXA,EAAiBC,GACjBA,GACER,GAAW,CAAA,EACfS,OAAOC,OAAOC,KAAM,CAClBV,OACAC,gBACAC,OAAQ,IACO,WAATF,EAAoBW,EAAsBC,KAC3CV,GAELC,aACAC,UACAC,UACAC,oBACAC,GAAIA,EAAK,GAAGA,KAAMM,MAAUA,KAEhC,CAOAC,YAAAA,CAAaX,GACX,IAAK,MAAMY,KAAYZ,EACrBO,KAAKP,WAAWa,KAAK,CACnBC,OAAQC,WAAWH,GACnBI,MAAOhB,EAAWY,KAGtB,OAAOL,IACT,CAOAU,QAAAA,CACEC,GAEA,MAAO,IACFC,EAAKZ,KAAMW,GACdrB,KAAMU,KAAKV,KACXE,OAAQ,IAAKQ,KAAKR,QAClBC,WAAYO,KAAKP,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mB,EAAIG,qBAAqBzB,EAAIC,EAAIO,EAAIN,EAAIC,EAAIM,GAMnD,OAJAnD,KAAKP,WAAW+D,QAAQY,IAAuB,IAAtB3D,MAAEA,EAAKF,OAAEA,GAAQ6D,EACxCH,EAAS7D,aAAaG,EAAQE,KAGzBwD,CACT,CAQA,uBAAaI,CACXhF,GAEA,MAAMI,WAAEA,EAAUG,kBAAEA,GAAsBP,EAC1C,OAAO,IAAIW,KAAK,IACXX,EACHI,WAAYA,EACRA,EAAWoB,IAAKC,IAAS,IAAWA,UACpCC,EACJnB,kBAAmBA,EAAoB,IAAIA,QAAqBmB,GAEpE,CA6CA,kBAAOuD,CACLC,EACAC,EACAC,GAEA,MAAMlF,EAAgBmF,EAAmBH,GACnCI,EAASH,EAASI,yBACxB,OAAO,IAAI5E,KAAK,CACdH,GAAI0E,EAAGM,aAAa,YAAS9D,EAC7BzB,KAAMwF,EAAUP,GAChB/E,OAAQuF,EAAYR,EAAI,CACtB1C,MAAO4C,EAAWO,cAAgBP,EAAW5C,MAC7CC,OAAQ2C,EAAWQ,eAAiBR,EAAW3C,SAEjDrC,WAAYyF,EAAgBX,EAAIE,EAAWU,SAC3C5F,gBACAK,kBAAmBwF,EACjBb,EAAGM,aAAa,sBAAwB,OAEpB,WAAlBtF,EACA,CACEG,QAAS8E,EAAS3C,MAAQ,EAAI8C,EAAO1C,EACrCtC,QAAS6E,EAAS1C,OAAS,EAAI6C,EAAOzC,GAExC,CACExC,QAAS,EACTC,QAAS,IAGnB,EA5TA0F,EAjEWlG,EAAQ,OAuEL,YA0ThBmG,EAAcC,SAASpG,EAAU,YACjCmG,EAAcC,SAASpG,EAAU,UACjCmG,EAAcC,SAASpG,EAAU"}
|
|
@@ -10,6 +10,7 @@ import { parseCoords } from './parser/parseCoords.mjs';
|
|
|
10
10
|
import { parseGradientUnits, parseType } from './parser/misc.mjs';
|
|
11
11
|
import { classRegistry } from '../ClassRegistry.mjs';
|
|
12
12
|
import { isPath } from '../util/typeAssertions.mjs';
|
|
13
|
+
import { escapeXml } from '../util/lang_string.mjs';
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
16
|
* Gradient class
|
|
@@ -115,7 +116,8 @@ class Gradient {
|
|
|
115
116
|
}
|
|
116
117
|
transform[4] -= offsetX;
|
|
117
118
|
transform[5] -= offsetY;
|
|
118
|
-
const commonAttributes = [`id="SVGID_${this.id}"`, `gradientUnits="${gradientUnits}"`, `gradientTransform="${preTransform ? preTransform + ' ' : ''}${matrixToSVG(transform)}"`, ''].join(' ');
|
|
119
|
+
const commonAttributes = [`id="SVGID_${escapeXml(String(this.id))}"`, `gradientUnits="${gradientUnits}"`, `gradientTransform="${preTransform ? preTransform + ' ' : ''}${matrixToSVG(transform)}"`, ''].join(' ');
|
|
120
|
+
const sanitizeCoord = value => parseFloat(String(value));
|
|
119
121
|
if (this.type === 'linear') {
|
|
120
122
|
const {
|
|
121
123
|
x1,
|
|
@@ -123,7 +125,11 @@ class Gradient {
|
|
|
123
125
|
x2,
|
|
124
126
|
y2
|
|
125
127
|
} = this.coords;
|
|
126
|
-
|
|
128
|
+
const sx1 = sanitizeCoord(x1);
|
|
129
|
+
const sy1 = sanitizeCoord(y1);
|
|
130
|
+
const sx2 = sanitizeCoord(x2);
|
|
131
|
+
const sy2 = sanitizeCoord(y2);
|
|
132
|
+
markup.push('<linearGradient ', commonAttributes, ' x1="', sx1, '" y1="', sy1, '" x2="', sx2, '" y2="', sy2, '">\n');
|
|
127
133
|
} else if (this.type === 'radial') {
|
|
128
134
|
const {
|
|
129
135
|
x1,
|
|
@@ -133,9 +139,15 @@ class Gradient {
|
|
|
133
139
|
r1,
|
|
134
140
|
r2
|
|
135
141
|
} = this.coords;
|
|
136
|
-
const
|
|
142
|
+
const sx1 = sanitizeCoord(x1);
|
|
143
|
+
const sy1 = sanitizeCoord(y1);
|
|
144
|
+
const sx2 = sanitizeCoord(x2);
|
|
145
|
+
const sy2 = sanitizeCoord(y2);
|
|
146
|
+
const sr1 = sanitizeCoord(r1);
|
|
147
|
+
const sr2 = sanitizeCoord(r2);
|
|
148
|
+
const needsSwap = sr1 > sr2;
|
|
137
149
|
// svg radial gradient has just 1 radius. the biggest.
|
|
138
|
-
markup.push('<radialGradient ', commonAttributes, ' cx="', needsSwap ?
|
|
150
|
+
markup.push('<radialGradient ', commonAttributes, ' cx="', needsSwap ? sx1 : sx2, '" cy="', needsSwap ? sy1 : sy2, '" r="', needsSwap ? sr1 : sr2, '" fx="', needsSwap ? sx2 : sx1, '" fy="', needsSwap ? sy2 : sy1, '">\n');
|
|
139
151
|
if (needsSwap) {
|
|
140
152
|
// svg goes from internal to external radius. if radius are inverted, swap color stops.
|
|
141
153
|
colorStops.reverse(); // mutates array
|
|
@@ -143,16 +155,17 @@ class Gradient {
|
|
|
143
155
|
colorStop.offset = 1 - colorStop.offset;
|
|
144
156
|
});
|
|
145
157
|
}
|
|
146
|
-
const minRadius = Math.min(
|
|
158
|
+
const minRadius = Math.min(sr1, sr2);
|
|
147
159
|
if (minRadius > 0) {
|
|
148
160
|
// i have to shift all colorStops and add new one in 0.
|
|
149
|
-
const maxRadius = Math.max(
|
|
161
|
+
const maxRadius = Math.max(sr1, sr2),
|
|
150
162
|
percentageShift = minRadius / maxRadius;
|
|
151
163
|
colorStops.forEach(colorStop => {
|
|
152
164
|
colorStop.offset += percentageShift * (1 - colorStop.offset);
|
|
153
165
|
});
|
|
154
166
|
}
|
|
155
167
|
}
|
|
168
|
+
// todo make a malicious script tag injection test with color and also apply a fix with escapeXml
|
|
156
169
|
colorStops.forEach(_ref => {
|
|
157
170
|
let {
|
|
158
171
|
color,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Gradient.mjs","sources":["../../../src/gradient/Gradient.ts"],"sourcesContent":["import { iMatrix } from '../constants';\nimport { parseTransformAttribute } from '../parser/parseTransformAttribute';\nimport type { FabricObject } from '../shapes/Object/FabricObject';\nimport type { TMat2D } from '../typedefs';\nimport { uid } from '../util/internals/uid';\nimport { pick } from '../util/misc/pick';\nimport { matrixToSVG } from '../util/misc/svgExport';\nimport { linearDefaultCoords, radialDefaultCoords } from './constants';\nimport { parseColorStops } from './parser/parseColorStops';\nimport { parseCoords } from './parser/parseCoords';\nimport { parseType, parseGradientUnits } from './parser/misc';\nimport type {\n ColorStop,\n GradientCoords,\n GradientOptions,\n GradientType,\n GradientUnits,\n SVGOptions,\n SerializedGradientProps,\n} from './typedefs';\nimport { classRegistry } from '../ClassRegistry';\nimport { isPath } from '../util/typeAssertions';\n\n/**\n * Gradient class\n * @class Gradient\n * @see {@link http://fabric5.fabricjs.com/fabric-intro-part-2#gradients}\n */\nexport class Gradient<\n S,\n T extends GradientType = S extends GradientType ? S : 'linear',\n> {\n /**\n * Horizontal offset for aligning gradients coming from SVG when outside pathgroups\n * @type Number\n * @default 0\n */\n declare offsetX: number;\n\n /**\n * Vertical offset for aligning gradients coming from SVG when outside pathgroups\n * @type Number\n * @default 0\n */\n declare offsetY: number;\n\n /**\n * A transform matrix to apply to the gradient before painting.\n * Imported from svg gradients, is not applied with the current transform in the center.\n * Before this transform is applied, the origin point is at the top left corner of the object\n * plus the addition of offsetY and offsetX.\n * @type Number[]\n * @default null\n */\n declare gradientTransform?: TMat2D;\n\n /**\n * coordinates units for coords.\n * If `pixels`, the number of coords are in the same unit of width / height.\n * If set as `percentage` the coords are still a number, but 1 means 100% of width\n * for the X and 100% of the height for the y. It can be bigger than 1 and negative.\n * allowed values pixels or percentage.\n * @type GradientUnits\n * @default 'pixels'\n */\n declare gradientUnits: GradientUnits;\n\n /**\n * Gradient type linear or radial\n * @type GradientType\n * @default 'linear'\n */\n declare type: T;\n\n /**\n * Defines how the gradient is located in space and spread\n * @type GradientCoords\n */\n declare coords: GradientCoords<T>;\n\n /**\n * Defines how many colors a gradient has and how they are located on the axis\n * defined by coords\n * @type GradientCoords\n */\n declare colorStops: ColorStop[];\n\n /**\n * If true, this object will not be exported during the serialization of a canvas\n * @type boolean\n */\n declare excludeFromExport?: boolean;\n\n /**\n * ID used for SVG export functionalities\n * @type number | string\n */\n declare readonly id: string | number;\n\n static type = 'Gradient';\n\n constructor(options: GradientOptions<T>) {\n const {\n type = 'linear' as T,\n gradientUnits = 'pixels',\n coords = {},\n colorStops = [],\n offsetX = 0,\n offsetY = 0,\n gradientTransform,\n id,\n } = options || {};\n Object.assign(this, {\n type,\n gradientUnits,\n coords: {\n ...(type === 'radial' ? radialDefaultCoords : linearDefaultCoords),\n ...coords,\n },\n colorStops,\n offsetX,\n offsetY,\n gradientTransform,\n id: id ? `${id}_${uid()}` : uid(),\n });\n }\n\n /**\n * Adds another colorStop\n * @param {Record<string, string>} colorStop Object with offset and color\n * @return {Gradient} thisArg\n */\n addColorStop(colorStops: Record<string, string>) {\n for (const position in colorStops) {\n this.colorStops.push({\n offset: parseFloat(position),\n color: colorStops[position],\n });\n }\n return this;\n }\n\n /**\n * Returns object representation of a gradient\n * @param {string[]} [propertiesToInclude] Any properties that you might want to additionally include in the output\n * @return {object}\n */\n toObject(\n propertiesToInclude?: (keyof this | string)[],\n ): SerializedGradientProps<T> {\n return {\n ...pick(this, propertiesToInclude as (keyof this)[]),\n type: this.type,\n coords: { ...this.coords },\n colorStops: this.colorStops.map((colorStop) => ({ ...colorStop })),\n offsetX: this.offsetX,\n offsetY: this.offsetY,\n gradientUnits: this.gradientUnits,\n gradientTransform: this.gradientTransform\n ? [...this.gradientTransform]\n : undefined,\n };\n }\n\n /* _TO_SVG_START_ */\n /**\n * Returns SVG representation of an gradient\n * @param {FabricObject} object Object to create a gradient for\n * @return {String} SVG representation of an gradient (linear/radial)\n */\n toSVG(\n object: FabricObject,\n {\n additionalTransform: preTransform,\n }: { additionalTransform?: string } = {},\n ) {\n const markup = [],\n transform = (\n this.gradientTransform\n ? this.gradientTransform.concat()\n : iMatrix.concat()\n ) as TMat2D,\n gradientUnits =\n this.gradientUnits === 'pixels'\n ? 'userSpaceOnUse'\n : 'objectBoundingBox';\n // colorStops must be sorted ascending, and guarded against deep mutations\n const colorStops = this.colorStops\n .map((colorStop) => ({ ...colorStop }))\n .sort((a, b) => {\n return a.offset - b.offset;\n });\n\n let offsetX = -this.offsetX,\n offsetY = -this.offsetY;\n if (gradientUnits === 'objectBoundingBox') {\n offsetX /= object.width;\n offsetY /= object.height;\n } else {\n offsetX += object.width / 2;\n offsetY += object.height / 2;\n }\n // todo what about polygon/polyline?\n if (isPath(object) && this.gradientUnits !== 'percentage') {\n offsetX -= object.pathOffset.x;\n offsetY -= object.pathOffset.y;\n }\n transform[4] -= offsetX;\n transform[5] -= offsetY;\n\n const commonAttributes = [\n `id=\"SVGID_${this.id}\"`,\n `gradientUnits=\"${gradientUnits}\"`,\n `gradientTransform=\"${\n preTransform ? preTransform + ' ' : ''\n }${matrixToSVG(transform)}\"`,\n '',\n ].join(' ');\n\n if (this.type === 'linear') {\n const { x1, y1, x2, y2 } = this.coords;\n markup.push(\n '<linearGradient ',\n commonAttributes,\n ' x1=\"',\n x1,\n '\" y1=\"',\n y1,\n '\" x2=\"',\n x2,\n '\" y2=\"',\n y2,\n '\">\\n',\n );\n } else if (this.type === 'radial') {\n const { x1, y1, x2, y2, r1, r2 } = this\n .coords as GradientCoords<'radial'>;\n const needsSwap = r1 > r2;\n // svg radial gradient has just 1 radius. the biggest.\n markup.push(\n '<radialGradient ',\n commonAttributes,\n ' cx=\"',\n needsSwap ? x1 : x2,\n '\" cy=\"',\n needsSwap ? y1 : y2,\n '\" r=\"',\n needsSwap ? r1 : r2,\n '\" fx=\"',\n needsSwap ? x2 : x1,\n '\" fy=\"',\n needsSwap ? y2 : y1,\n '\">\\n',\n );\n if (needsSwap) {\n // svg goes from internal to external radius. if radius are inverted, swap color stops.\n colorStops.reverse(); // mutates array\n colorStops.forEach((colorStop) => {\n colorStop.offset = 1 - colorStop.offset;\n });\n }\n const minRadius = Math.min(r1, r2);\n if (minRadius > 0) {\n // i have to shift all colorStops and add new one in 0.\n const maxRadius = Math.max(r1, r2),\n percentageShift = minRadius / maxRadius;\n colorStops.forEach((colorStop) => {\n colorStop.offset += percentageShift * (1 - colorStop.offset);\n });\n }\n }\n\n colorStops.forEach(({ color, offset }) => {\n markup.push(\n `<stop offset=\"${offset * 100}%\" style=\"stop-color:${color};\"/>\\n`,\n );\n });\n\n markup.push(\n this.type === 'linear' ? '</linearGradient>' : '</radialGradient>',\n '\\n',\n );\n\n return markup.join('');\n }\n /* _TO_SVG_END_ */\n\n /**\n * Returns an instance of CanvasGradient\n * @param {CanvasRenderingContext2D} ctx Context to render on\n * @return {CanvasGradient}\n */\n toLive(ctx: CanvasRenderingContext2D): CanvasGradient {\n const { x1, y1, x2, y2, r1, r2 } = this.coords as GradientCoords<'radial'>;\n const gradient =\n this.type === 'linear'\n ? ctx.createLinearGradient(x1, y1, x2, y2)\n : ctx.createRadialGradient(x1, y1, r1, x2, y2, r2);\n\n this.colorStops.forEach(({ color, offset }) => {\n gradient.addColorStop(offset, color);\n });\n\n return gradient;\n }\n\n static async fromObject(\n options: GradientOptions<'linear'>,\n ): Promise<Gradient<'linear'>>;\n static async fromObject(\n options: GradientOptions<'radial'>,\n ): Promise<Gradient<'radial'>>;\n static async fromObject(\n options: GradientOptions<'linear'> | GradientOptions<'radial'>,\n ) {\n const { colorStops, gradientTransform } = options;\n return new this({\n ...options,\n colorStops: colorStops\n ? colorStops.map((colorStop) => ({ ...colorStop }))\n : undefined,\n gradientTransform: gradientTransform ? [...gradientTransform] : undefined,\n });\n }\n\n /* _FROM_SVG_START_ */\n /**\n * Returns {@link Gradient} instance from an SVG element\n * @param {SVGGradientElement} el SVG gradient element\n * @param {FabricObject} instance\n * @param {String} opacity A fill-opacity or stroke-opacity attribute to multiply to each stop's opacity.\n * @param {SVGOptions} svgOptions an object containing the size of the SVG in order to parse correctly gradients\n * that uses gradientUnits as 'userSpaceOnUse' and percentages.\n * @return {Gradient} Gradient instance\n * @see http://www.w3.org/TR/SVG/pservers.html#LinearGradientElement\n * @see http://www.w3.org/TR/SVG/pservers.html#RadialGradientElement\n *\n * @example\n *\n * <linearGradient id=\"linearGrad1\">\n * <stop offset=\"0%\" stop-color=\"white\"/>\n * <stop offset=\"100%\" stop-color=\"black\"/>\n * </linearGradient>\n *\n * OR\n *\n * <linearGradient id=\"linearGrad2\">\n * <stop offset=\"0\" style=\"stop-color:rgb(255,255,255)\"/>\n * <stop offset=\"1\" style=\"stop-color:rgb(0,0,0)\"/>\n * </linearGradient>\n *\n * OR\n *\n * <radialGradient id=\"radialGrad1\">\n * <stop offset=\"0%\" stop-color=\"white\" stop-opacity=\"1\" />\n * <stop offset=\"50%\" stop-color=\"black\" stop-opacity=\"0.5\" />\n * <stop offset=\"100%\" stop-color=\"white\" stop-opacity=\"1\" />\n * </radialGradient>\n *\n * OR\n *\n * <radialGradient id=\"radialGrad2\">\n * <stop offset=\"0\" stop-color=\"rgb(255,255,255)\" />\n * <stop offset=\"0.5\" stop-color=\"rgb(0,0,0)\" />\n * <stop offset=\"1\" stop-color=\"rgb(255,255,255)\" />\n * </radialGradient>\n *\n */\n static fromElement(\n el: SVGGradientElement,\n instance: FabricObject,\n svgOptions: SVGOptions,\n ): Gradient<GradientType> {\n const gradientUnits = parseGradientUnits(el);\n const center = instance._findCenterFromElement();\n return new this({\n id: el.getAttribute('id') || undefined,\n type: parseType(el),\n coords: parseCoords(el, {\n width: svgOptions.viewBoxWidth || svgOptions.width,\n height: svgOptions.viewBoxHeight || svgOptions.height,\n }),\n colorStops: parseColorStops(el, svgOptions.opacity),\n gradientUnits,\n gradientTransform: parseTransformAttribute(\n el.getAttribute('gradientTransform') || '',\n ),\n ...(gradientUnits === 'pixels'\n ? {\n offsetX: instance.width / 2 - center.x,\n offsetY: instance.height / 2 - center.y,\n }\n : {\n offsetX: 0,\n offsetY: 0,\n }),\n });\n }\n /* _FROM_SVG_END_ */\n}\n\nclassRegistry.setClass(Gradient, 'gradient');\nclassRegistry.setClass(Gradient, 'linear');\nclassRegistry.setClass(Gradient, 'radial');\n"],"names":["Gradient","constructor","options","type","gradientUnits","coords","colorStops","offsetX","offsetY","gradientTransform","id","Object","assign","radialDefaultCoords","linearDefaultCoords","uid","addColorStop","position","push","offset","parseFloat","color","toObject","propertiesToInclude","pick","map","colorStop","undefined","toSVG","object","additionalTransform","preTransform","arguments","length","markup","transform","concat","iMatrix","sort","a","b","width","height","isPath","pathOffset","x","y","commonAttributes","matrixToSVG","join","x1","y1","x2","y2","r1","r2","needsSwap","reverse","forEach","minRadius","Math","min","maxRadius","max","percentageShift","_ref","toLive","ctx","gradient","createLinearGradient","createRadialGradient","_ref2","fromObject","fromElement","el","instance","svgOptions","parseGradientUnits","center","_findCenterFromElement","getAttribute","parseType","parseCoords","viewBoxWidth","viewBoxHeight","parseColorStops","opacity","parseTransformAttribute","_defineProperty","classRegistry","setClass"],"mappings":";;;;;;;;;;;;;AAuBA;AACA;AACA;AACA;AACA;AACO,MAAMA,QAAQ,CAGnB;EAsEAC,WAAWA,CAACC,OAA2B,EAAE;IACvC,MAAM;AACJC,MAAAA,IAAI,GAAG,QAAa;AACpBC,MAAAA,aAAa,GAAG,QAAQ;MACxBC,MAAM,GAAG,EAAE;AACXC,MAAAA,UAAU,GAAG,EAAE;AACfC,MAAAA,OAAO,GAAG,CAAC;AACXC,MAAAA,OAAO,GAAG,CAAC;MACXC,iBAAiB;AACjBC,MAAAA;AACF,KAAC,GAAGR,OAAO,IAAI,EAAE;AACjBS,IAAAA,MAAM,CAACC,MAAM,CAAC,IAAI,EAAE;MAClBT,IAAI;MACJC,aAAa;AACbC,MAAAA,MAAM,EAAE;AACN,QAAA,IAAIF,IAAI,KAAK,QAAQ,GAAGU,mBAAmB,GAAGC,mBAAmB,CAAC;QAClE,GAAGT;OACJ;MACDC,UAAU;MACVC,OAAO;MACPC,OAAO;MACPC,iBAAiB;AACjBC,MAAAA,EAAE,EAAEA,EAAE,GAAG,CAAA,EAAGA,EAAE,CAAA,CAAA,EAAIK,GAAG,EAAE,CAAA,CAAE,GAAGA,GAAG;AACjC,KAAC,CAAC;AACJ,EAAA;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C1F,QAAQ,EAAE,UAAU,CAAC;AAC5CyF,aAAa,CAACC,QAAQ,CAAC1F,QAAQ,EAAE,QAAQ,CAAC;AAC1CyF,aAAa,CAACC,QAAQ,CAAC1F,QAAQ,EAAE,QAAQ,CAAC;;;;"}
|
|
1
|
+
{"version":3,"file":"Gradient.mjs","sources":["../../../src/gradient/Gradient.ts"],"sourcesContent":["import { iMatrix } from '../constants';\nimport { parseTransformAttribute } from '../parser/parseTransformAttribute';\nimport type { FabricObject } from '../shapes/Object/FabricObject';\nimport type { TMat2D } from '../typedefs';\nimport { uid } from '../util/internals/uid';\nimport { pick } from '../util/misc/pick';\nimport { matrixToSVG } from '../util/misc/svgExport';\nimport { linearDefaultCoords, radialDefaultCoords } from './constants';\nimport { parseColorStops } from './parser/parseColorStops';\nimport { parseCoords } from './parser/parseCoords';\nimport { parseType, parseGradientUnits } from './parser/misc';\nimport type {\n ColorStop,\n GradientCoords,\n GradientOptions,\n GradientType,\n GradientUnits,\n SVGOptions,\n SerializedGradientProps,\n} from './typedefs';\nimport { classRegistry } from '../ClassRegistry';\nimport { isPath } from '../util/typeAssertions';\nimport { escapeXml } from '../util/lang_string';\n\n/**\n * Gradient class\n * @class Gradient\n * @see {@link http://fabric5.fabricjs.com/fabric-intro-part-2#gradients}\n */\nexport class Gradient<\n S,\n T extends GradientType = S extends GradientType ? S : 'linear',\n> {\n /**\n * Horizontal offset for aligning gradients coming from SVG when outside pathgroups\n * @type Number\n * @default 0\n */\n declare offsetX: number;\n\n /**\n * Vertical offset for aligning gradients coming from SVG when outside pathgroups\n * @type Number\n * @default 0\n */\n declare offsetY: number;\n\n /**\n * A transform matrix to apply to the gradient before painting.\n * Imported from svg gradients, is not applied with the current transform in the center.\n * Before this transform is applied, the origin point is at the top left corner of the object\n * plus the addition of offsetY and offsetX.\n * @type Number[]\n * @default null\n */\n declare gradientTransform?: TMat2D;\n\n /**\n * coordinates units for coords.\n * If `pixels`, the number of coords are in the same unit of width / height.\n * If set as `percentage` the coords are still a number, but 1 means 100% of width\n * for the X and 100% of the height for the y. It can be bigger than 1 and negative.\n * allowed values pixels or percentage.\n * @type GradientUnits\n * @default 'pixels'\n */\n declare gradientUnits: GradientUnits;\n\n /**\n * Gradient type linear or radial\n * @type GradientType\n * @default 'linear'\n */\n declare type: T;\n\n /**\n * Defines how the gradient is located in space and spread\n * @type GradientCoords\n */\n declare coords: GradientCoords<T>;\n\n /**\n * Defines how many colors a gradient has and how they are located on the axis\n * defined by coords\n * @type GradientCoords\n */\n declare colorStops: ColorStop[];\n\n /**\n * If true, this object will not be exported during the serialization of a canvas\n * @type boolean\n */\n declare excludeFromExport?: boolean;\n\n /**\n * ID used for SVG export functionalities\n * @type number | string\n */\n declare readonly id: string | number;\n\n static type = 'Gradient';\n\n constructor(options: GradientOptions<T>) {\n const {\n type = 'linear' as T,\n gradientUnits = 'pixels',\n coords = {},\n colorStops = [],\n offsetX = 0,\n offsetY = 0,\n gradientTransform,\n id,\n } = options || {};\n Object.assign(this, {\n type,\n gradientUnits,\n coords: {\n ...(type === 'radial' ? radialDefaultCoords : linearDefaultCoords),\n ...coords,\n },\n colorStops,\n offsetX,\n offsetY,\n gradientTransform,\n id: id ? `${id}_${uid()}` : uid(),\n });\n }\n\n /**\n * Adds another colorStop\n * @param {Record<string, string>} colorStop Object with offset and color\n * @return {Gradient} thisArg\n */\n addColorStop(colorStops: Record<string, string>) {\n for (const position in colorStops) {\n this.colorStops.push({\n offset: parseFloat(position),\n color: colorStops[position],\n });\n }\n return this;\n }\n\n /**\n * Returns object representation of a gradient\n * @param {string[]} [propertiesToInclude] Any properties that you might want to additionally include in the output\n * @return {object}\n */\n toObject(\n propertiesToInclude?: (keyof this | string)[],\n ): SerializedGradientProps<T> {\n return {\n ...pick(this, propertiesToInclude as (keyof this)[]),\n type: this.type,\n coords: { ...this.coords },\n colorStops: this.colorStops.map((colorStop) => ({ ...colorStop })),\n offsetX: this.offsetX,\n offsetY: this.offsetY,\n gradientUnits: this.gradientUnits,\n gradientTransform: this.gradientTransform\n ? [...this.gradientTransform]\n : undefined,\n };\n }\n\n /* _TO_SVG_START_ */\n /**\n * Returns SVG representation of an gradient\n * @param {FabricObject} object Object to create a gradient for\n * @return {String} SVG representation of an gradient (linear/radial)\n */\n toSVG(\n object: FabricObject,\n {\n additionalTransform: preTransform,\n }: { additionalTransform?: string } = {},\n ) {\n const markup = [],\n transform = (\n this.gradientTransform\n ? this.gradientTransform.concat()\n : iMatrix.concat()\n ) as TMat2D,\n gradientUnits =\n this.gradientUnits === 'pixels'\n ? 'userSpaceOnUse'\n : 'objectBoundingBox';\n // colorStops must be sorted ascending, and guarded against deep mutations\n const colorStops = this.colorStops\n .map((colorStop) => ({ ...colorStop }))\n .sort((a, b) => {\n return a.offset - b.offset;\n });\n\n let offsetX = -this.offsetX,\n offsetY = -this.offsetY;\n if (gradientUnits === 'objectBoundingBox') {\n offsetX /= object.width;\n offsetY /= object.height;\n } else {\n offsetX += object.width / 2;\n offsetY += object.height / 2;\n }\n // todo what about polygon/polyline?\n if (isPath(object) && this.gradientUnits !== 'percentage') {\n offsetX -= object.pathOffset.x;\n offsetY -= object.pathOffset.y;\n }\n transform[4] -= offsetX;\n transform[5] -= offsetY;\n\n const commonAttributes = [\n `id=\"SVGID_${escapeXml(String(this.id))}\"`,\n `gradientUnits=\"${gradientUnits}\"`,\n `gradientTransform=\"${\n preTransform ? preTransform + ' ' : ''\n }${matrixToSVG(transform)}\"`,\n '',\n ].join(' ');\n\n const sanitizeCoord = (value: unknown) => parseFloat(String(value));\n\n if (this.type === 'linear') {\n const { x1, y1, x2, y2 } = this.coords;\n const sx1 = sanitizeCoord(x1);\n const sy1 = sanitizeCoord(y1);\n const sx2 = sanitizeCoord(x2);\n const sy2 = sanitizeCoord(y2);\n markup.push(\n '<linearGradient ',\n commonAttributes,\n ' x1=\"',\n sx1,\n '\" y1=\"',\n sy1,\n '\" x2=\"',\n sx2,\n '\" y2=\"',\n sy2,\n '\">\\n',\n );\n } else if (this.type === 'radial') {\n const { x1, y1, x2, y2, r1, r2 } = this\n .coords as GradientCoords<'radial'>;\n const sx1 = sanitizeCoord(x1);\n const sy1 = sanitizeCoord(y1);\n const sx2 = sanitizeCoord(x2);\n const sy2 = sanitizeCoord(y2);\n const sr1 = sanitizeCoord(r1);\n const sr2 = sanitizeCoord(r2);\n const needsSwap = sr1 > sr2;\n // svg radial gradient has just 1 radius. the biggest.\n markup.push(\n '<radialGradient ',\n commonAttributes,\n ' cx=\"',\n needsSwap ? sx1 : sx2,\n '\" cy=\"',\n needsSwap ? sy1 : sy2,\n '\" r=\"',\n needsSwap ? sr1 : sr2,\n '\" fx=\"',\n needsSwap ? sx2 : sx1,\n '\" fy=\"',\n needsSwap ? sy2 : sy1,\n '\">\\n',\n );\n if (needsSwap) {\n // svg goes from internal to external radius. if radius are inverted, swap color stops.\n colorStops.reverse(); // mutates array\n colorStops.forEach((colorStop) => {\n colorStop.offset = 1 - colorStop.offset;\n });\n }\n const minRadius = Math.min(sr1, sr2);\n if (minRadius > 0) {\n // i have to shift all colorStops and add new one in 0.\n const maxRadius = Math.max(sr1, sr2),\n percentageShift = minRadius / maxRadius;\n colorStops.forEach((colorStop) => {\n colorStop.offset += percentageShift * (1 - colorStop.offset);\n });\n }\n }\n // todo make a malicious script tag injection test with color and also apply a fix with escapeXml\n colorStops.forEach(({ color, offset }) => {\n markup.push(\n `<stop offset=\"${offset * 100}%\" style=\"stop-color:${color};\"/>\\n`,\n );\n });\n\n markup.push(\n this.type === 'linear' ? '</linearGradient>' : '</radialGradient>',\n '\\n',\n );\n\n return markup.join('');\n }\n /* _TO_SVG_END_ */\n\n /**\n * Returns an instance of CanvasGradient\n * @param {CanvasRenderingContext2D} ctx Context to render on\n * @return {CanvasGradient}\n */\n toLive(ctx: CanvasRenderingContext2D): CanvasGradient {\n const { x1, y1, x2, y2, r1, r2 } = this.coords as GradientCoords<'radial'>;\n const gradient =\n this.type === 'linear'\n ? ctx.createLinearGradient(x1, y1, x2, y2)\n : ctx.createRadialGradient(x1, y1, r1, x2, y2, r2);\n\n this.colorStops.forEach(({ color, offset }) => {\n gradient.addColorStop(offset, color);\n });\n\n return gradient;\n }\n\n static async fromObject(\n options: GradientOptions<'linear'>,\n ): Promise<Gradient<'linear'>>;\n static async fromObject(\n options: GradientOptions<'radial'>,\n ): Promise<Gradient<'radial'>>;\n static async fromObject(\n options: GradientOptions<'linear'> | GradientOptions<'radial'>,\n ) {\n const { colorStops, gradientTransform } = options;\n return new this({\n ...options,\n colorStops: colorStops\n ? colorStops.map((colorStop) => ({ ...colorStop }))\n : undefined,\n gradientTransform: gradientTransform ? [...gradientTransform] : undefined,\n });\n }\n\n /* _FROM_SVG_START_ */\n /**\n * Returns {@link Gradient} instance from an SVG element\n * @param {SVGGradientElement} el SVG gradient element\n * @param {FabricObject} instance\n * @param {String} opacity A fill-opacity or stroke-opacity attribute to multiply to each stop's opacity.\n * @param {SVGOptions} svgOptions an object containing the size of the SVG in order to parse correctly gradients\n * that uses gradientUnits as 'userSpaceOnUse' and percentages.\n * @return {Gradient} Gradient instance\n * @see http://www.w3.org/TR/SVG/pservers.html#LinearGradientElement\n * @see http://www.w3.org/TR/SVG/pservers.html#RadialGradientElement\n *\n * @example\n *\n * <linearGradient id=\"linearGrad1\">\n * <stop offset=\"0%\" stop-color=\"white\"/>\n * <stop offset=\"100%\" stop-color=\"black\"/>\n * </linearGradient>\n *\n * OR\n *\n * <linearGradient id=\"linearGrad2\">\n * <stop offset=\"0\" style=\"stop-color:rgb(255,255,255)\"/>\n * <stop offset=\"1\" style=\"stop-color:rgb(0,0,0)\"/>\n * </linearGradient>\n *\n * OR\n *\n * <radialGradient id=\"radialGrad1\">\n * <stop offset=\"0%\" stop-color=\"white\" stop-opacity=\"1\" />\n * <stop offset=\"50%\" stop-color=\"black\" stop-opacity=\"0.5\" />\n * <stop offset=\"100%\" stop-color=\"white\" stop-opacity=\"1\" />\n * </radialGradient>\n *\n * OR\n *\n * <radialGradient id=\"radialGrad2\">\n * <stop offset=\"0\" stop-color=\"rgb(255,255,255)\" />\n * <stop offset=\"0.5\" stop-color=\"rgb(0,0,0)\" />\n * <stop offset=\"1\" stop-color=\"rgb(255,255,255)\" />\n * </radialGradient>\n *\n */\n static fromElement(\n el: SVGGradientElement,\n instance: FabricObject,\n svgOptions: SVGOptions,\n ): Gradient<GradientType> {\n const gradientUnits = parseGradientUnits(el);\n const center = instance._findCenterFromElement();\n return new this({\n id: el.getAttribute('id') || undefined,\n type: parseType(el),\n coords: parseCoords(el, {\n width: svgOptions.viewBoxWidth || svgOptions.width,\n height: svgOptions.viewBoxHeight || svgOptions.height,\n }),\n colorStops: parseColorStops(el, svgOptions.opacity),\n gradientUnits,\n gradientTransform: parseTransformAttribute(\n el.getAttribute('gradientTransform') || '',\n ),\n ...(gradientUnits === 'pixels'\n ? {\n offsetX: instance.width / 2 - center.x,\n offsetY: instance.height / 2 - center.y,\n }\n : {\n offsetX: 0,\n offsetY: 0,\n }),\n });\n }\n /* _FROM_SVG_END_ */\n}\n\nclassRegistry.setClass(Gradient, 'gradient');\nclassRegistry.setClass(Gradient, 'linear');\nclassRegistry.setClass(Gradient, 'radial');\n"],"names":["Gradient","constructor","options","type","gradientUnits","coords","colorStops","offsetX","offsetY","gradientTransform","id","Object","assign","radialDefaultCoords","linearDefaultCoords","uid","addColorStop","position","push","offset","parseFloat","color","toObject","propertiesToInclude","pick","map","colorStop","undefined","toSVG","object","additionalTransform","preTransform","arguments","length","markup","transform","concat","iMatrix","sort","a","b","width","height","isPath","pathOffset","x","y","commonAttributes","escapeXml","String","matrixToSVG","join","sanitizeCoord","value","x1","y1","x2","y2","sx1","sy1","sx2","sy2","r1","r2","sr1","sr2","needsSwap","reverse","forEach","minRadius","Math","min","maxRadius","max","percentageShift","_ref","toLive","ctx","gradient","createLinearGradient","createRadialGradient","_ref2","fromObject","fromElement","el","instance","svgOptions","parseGradientUnits","center","_findCenterFromElement","getAttribute","parseType","parseCoords","viewBoxWidth","viewBoxHeight","parseColorStops","opacity","parseTransformAttribute","_defineProperty","classRegistry","setClass"],"mappings":";;;;;;;;;;;;;;AAwBA;AACA;AACA;AACA;AACA;AACO,MAAMA,QAAQ,CAGnB;EAsEAC,WAAWA,CAACC,OAA2B,EAAE;IACvC,MAAM;AACJC,MAAAA,IAAI,GAAG,QAAa;AACpBC,MAAAA,aAAa,GAAG,QAAQ;MACxBC,MAAM,GAAG,EAAE;AACXC,MAAAA,UAAU,GAAG,EAAE;AACfC,MAAAA,OAAO,GAAG,CAAC;AACXC,MAAAA,OAAO,GAAG,CAAC;MACXC,iBAAiB;AACjBC,MAAAA;AACF,KAAC,GAAGR,OAAO,IAAI,EAAE;AACjBS,IAAAA,MAAM,CAACC,MAAM,CAAC,IAAI,EAAE;MAClBT,IAAI;MACJC,aAAa;AACbC,MAAAA,MAAM,EAAE;AACN,QAAA,IAAIF,IAAI,KAAK,QAAQ,GAAGU,mBAAmB,GAAGC,mBAAmB,CAAC;QAClE,GAAGT;OACJ;MACDC,UAAU;MACVC,OAAO;MACPC,OAAO;MACPC,iBAAiB;AACjBC,MAAAA,EAAE,EAAEA,EAAE,GAAG,CAAA,EAAGA,EAAE,CAAA,CAAA,EAAIK,GAAG,EAAE,CAAA,CAAE,GAAGA,GAAG;AACjC,KAAC,CAAC;AACJ,EAAA;;AAEA;AACF;AACA;AACA;AACA;EACEC,YAAYA,CAACV,UAAkC,EAAE;AAC/C,IAAA,KAAK,MAAMW,QAAQ,IAAIX,UAAU,EAAE;AACjC,MAAA,IAAI,CAACA,UAAU,CAACY,IAAI,CAAC;AACnBC,QAAAA,MAAM,EAAEC,UAAU,CAACH,QAAQ,CAAC;QAC5BI,KAAK,EAAEf,UAAU,CAACW,QAAQ;AAC5B,OAAC,CAAC;AACJ,IAAA;AACA,IAAA,OAAO,IAAI;AACb,EAAA;;AAEA;AACF;AACA;AACA;AACA;EACEK,QAAQA,CACNC,mBAA6C,EACjB;IAC5B,OAAO;AACL,MAAA,GAAGC,IAAI,CAAC,IAA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A;AAGE;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AAGE;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAGE;AACF;AACA;AACA;AACA;AAGE;AACF;AACA;AACA;AAGE;AACF;AACA;AACA;AACA;AAGE;AACF;AACA;AACA;AAGE;AACF;AACA;AACA;AAHE0F,eAAA,CAjEWlG,QAAQ,EAAA,MAAA,EAuEL,UAAU,CAAA;AA0T1BmG,aAAa,CAACC,QAAQ,CAACpG,QAAQ,EAAE,UAAU,CAAC;AAC5CmG,aAAa,CAACC,QAAQ,CAACpG,QAAQ,EAAE,QAAQ,CAAC;AAC1CmG,aAAa,CAACC,QAAQ,CAACpG,QAAQ,EAAE,QAAQ,CAAC;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Circle.d.ts","sourceRoot":"","sources":["../../../src/shapes/Circle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAOrD,OAAO,EAAE,YAAY,EAAmB,MAAM,uBAAuB,CAAC;AACtE,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACzE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAC/E,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"Circle.d.ts","sourceRoot":"","sources":["../../../src/shapes/Circle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAOrD,OAAO,EAAE,YAAY,EAAmB,MAAM,uBAAuB,CAAC;AACtE,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACzE,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CA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IpB;;;OAGG;IACH,UAAU,IAAI,MAAM;IAIpB;;OAEG;IACH,SAAS,CAAC,KAAK,EAAE,MAAM;IAKvB;;;;OAIG;IACH,QAAQ,CACN,CAAC,SAAS,IAAI,CAAC,KAAK,GAAG,gBAAgB,CAAC,IAAI,CAAC,EAAE,MAAM,MAAM,CAAC,EAC5D,CAAC,SAAS,MAAM,CAAC,GAAG,KAAK,EACzB,mBAAmB,GAAE,CAAC,EAAO,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,MAAM;IAMrD;;;;OAIG;IACH,MAAM,IAAI,MAAM,EAAE;IAgClB;;;OAGG;IACH,MAAM,CAAC,eAAe,WAA2C;IAEjE;;;;;OAKG;WACU,WAAW,CACtB,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE,SAAS,EAClB,QAAQ,CAAC,EAAE,QAAQ,GAClB,OAAO,CAAC,MAAM,CAAC;IAwBlB;;OAEG;IACH,MAAM,CAAC,UAAU,CAAC,CAAC,SAAS,QAAQ,CAAC,qBAAqB,CAAC,EAAE,MAAM,EAAE,CAAC;CAGvE"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{defineProperty as t}from"../../_virtual/_rollupPluginBabelHelpers.min.mjs";import{SHARED_ATTRIBUTES as s}from"../parser/attributes.min.mjs";import{parseAttributes as e}from"../parser/parseAttributes.min.mjs";import{cos as r}from"../util/misc/cos.min.mjs";import{degreesToRadians as i}from"../util/misc/radiansDegreesConversion.min.mjs";import{sin as n}from"../util/misc/sin.min.mjs";import{classRegistry as o}from"../ClassRegistry.min.mjs";import{FabricObject as a}from"./Object/FabricObject.min.mjs";import{SCALE_X as
|
|
1
|
+
import{defineProperty as t}from"../../_virtual/_rollupPluginBabelHelpers.min.mjs";import{SHARED_ATTRIBUTES as s}from"../parser/attributes.min.mjs";import{parseAttributes as e}from"../parser/parseAttributes.min.mjs";import{cos as r}from"../util/misc/cos.min.mjs";import{degreesToRadians as i}from"../util/misc/radiansDegreesConversion.min.mjs";import{sin as n}from"../util/misc/sin.min.mjs";import{classRegistry as o}from"../ClassRegistry.min.mjs";import{FabricObject as a}from"./Object/FabricObject.min.mjs";import{SCALE_X as m,SCALE_Y as u}from"../constants.min.mjs";import{escapeXml as c}from"../util/lang_string.min.mjs";import{cacheProperties as l}from"./Object/defaultValues.min.mjs";const p=["radius","startAngle","endAngle","counterClockwise"],d={radius:0,startAngle:0,endAngle:360,counterClockwise:!1};class h extends a{static getDefaults(){return{...super.getDefaults(),...h.ownDefaults}}constructor(t){super(),Object.assign(this,h.ownDefaults),this.setOptions(t)}_set(t,s){return super._set(t,s),"radius"===t&&this.setRadius(s),this}_render(t){t.beginPath(),t.arc(0,0,this.radius,i(this.startAngle),i(this.endAngle),this.counterClockwise),this._renderPaintInOrder(t)}getRadiusX(){return this.get("radius")*this.get(m)}getRadiusY(){return this.get("radius")*this.get(u)}setRadius(t){this.radius=t,this.set({width:2*t,height:2*t})}toObject(){let t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];return super.toObject([...p,...t])}_toSVG(){const{radius:t,startAngle:s,endAngle:e}=this,o=(e-s)%360;if(0===o)return["<circle ","COMMON_PARTS",'cx="0" cy="0" ','r="',`${c(t)}`,'" />\n'];{const a=i(s),m=i(e),u=r(a)*t,c=n(a)*t,l=r(m)*t,p=n(m)*t;return[`<path d="M ${u} ${c} A ${t} ${t} 0 ${o>180?1:0} ${this.counterClockwise?0:1} ${l} ${p}" `,"COMMON_PARTS"," />\n"]}}static async fromElement(t,s,r){const{left:i=0,top:n=0,radius:o=0,...a}=e(t,this.ATTRIBUTE_NAMES,r);return new this({...a,radius:o,left:i-o,top:n-o})}static fromObject(t){return super._fromObject(t)}}t(h,"type","Circle"),t(h,"cacheProperties",[...l,...p]),t(h,"ownDefaults",d),t(h,"ATTRIBUTE_NAMES",["cx","cy","r",...s]),o.setClass(h),o.setSVGClass(h);export{h as Circle,d as circleDefaultValues};
|
|
2
2
|
//# sourceMappingURL=Circle.min.mjs.map
|