fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 8.34 kB
Source Map (JSON)
{"version":3,"file":"linearGradientHandlers.mjs","names":[],"sources":["../../extensions/linear_gradient_controls/linearGradientHandlers.ts"],"sourcesContent":["import type {\n TMat2D,\n FabricObject,\n TransformActionHandler,\n Control,\n ControlRenderingStyleOverride,\n InteractiveFabricObject,\n Gradient,\n} from 'fabric';\nimport { Point, util, controlsUtils, iMatrix } from 'fabric';\n\n/** A deduping code block */\nconst commonGradientInfo = (\n fabricObject: FabricObject,\n gradient: Gradient<'linear'>,\n) => {\n const { width, height } = fabricObject;\n const { colorStops, coords, gradientUnits } = gradient;\n const isPerc = gradientUnits === 'percentage';\n const matrix = fabricObject.calcTransformMatrix();\n const vpt = fabricObject.getViewportTransform();\n const _finalMatrix = util.multiplyTransformMatrixArray([vpt, matrix]);\n return {\n width,\n height,\n colorStops,\n coords,\n isPerc,\n _finalMatrix,\n };\n};\n\nexport const linearGradientColorPositionHandlerGenerator = (\n gradient: Gradient<'linear'>,\n stopIndex: number,\n) =>\n function linearGradientColorPositionHandler(\n this: Control,\n dim: Point, // currentDimension\n finalMatrix: TMat2D,\n fabricObject: FabricObject,\n // currentControl: Control,) => {};\n ) {\n const { width, height, isPerc, coords, colorStops, _finalMatrix } =\n commonGradientInfo(fabricObject, gradient);\n const p1 = new Point(\n coords.x1 * (isPerc ? width : 1) - width / 2,\n coords.y1 * (isPerc ? height : 1) - height / 2,\n );\n const p2 = new Point(\n coords.x2 * (isPerc ? width : 1) - width / 2,\n coords.y2 * (isPerc ? height : 1) - height / 2,\n );\n const offset = colorStops[stopIndex].offset;\n return p1.lerp(p2, offset).transform(_finalMatrix);\n };\n\nexport const linearGradientCoordPositionHandlerGenerator = (\n gradient: Gradient<'linear'>,\n pointNumber: 1 | 2,\n) =>\n function linearGradientCoordPositionHandler(\n this: Control,\n dim: Point, // currentDimension\n finalMatrix: TMat2D,\n fabricObject: FabricObject,\n // currentControl: Control,) => {};\n ) {\n const { width, height, isPerc, coords, _finalMatrix } = commonGradientInfo(\n fabricObject,\n gradient,\n );\n\n return (\n pointNumber === 1\n ? new Point(\n coords.x1 * (isPerc ? width : 1) - width / 2,\n coords.y1 * (isPerc ? height : 1) - height / 2,\n )\n : new Point(\n coords.x2 * (isPerc ? width : 1) - width / 2,\n coords.y2 * (isPerc ? height : 1) - height / 2,\n )\n ).transform(_finalMatrix);\n };\n\nexport const linearGradientColorActionHandlerGenerator =\n (gradient: Gradient<'linear'>, colorIndex: number): TransformActionHandler =>\n (eventData, { target }, x, y) => {\n const {\n width,\n height,\n isPerc,\n coords: { x1, x2, y1, y2 },\n colorStops,\n } = commonGradientInfo(target, gradient);\n\n // find point in the space inside the object.\n const point = util\n .sendPointToPlane(\n new Point(x, y),\n undefined,\n target.calcTransformMatrix(),\n )\n .add(new Point(width / 2, height / 2));\n\n const p1 = new Point(x1 * (isPerc ? width : 1), y1 * (isPerc ? height : 1));\n const v = util.createVector(\n p1,\n new Point(x2 * (isPerc ? width : 1), y2 * (isPerc ? height : 1)),\n );\n const u = util.createVector(p1, point);\n const t = util.dotProduct(u, v) / util.dotProduct(v, v);\n colorStops[colorIndex].offset = util.capValue(0, t, 1);\n target.set('dirty', true);\n return true;\n };\n\nexport const linearGradientCoordsActionHandlerGenerator =\n (gradient: Gradient<'linear'>, pointIndex: 1 | 2): TransformActionHandler =>\n (eventData, { target }, x, y) => {\n const { width, height, isPerc, coords } = commonGradientInfo(\n target,\n gradient,\n );\n // find point in the space inside the object.\n const point = util\n .sendPointToPlane(\n new Point(x, y),\n undefined,\n target.calcTransformMatrix(),\n )\n .add(new Point(width / 2, height / 2));\n if (pointIndex === 1) {\n coords.x1 = point.x / (isPerc ? width : 1);\n coords.y1 = point.y / (isPerc ? height : 1);\n }\n\n if (pointIndex === 2) {\n coords.x2 = point.x / (isPerc ? width : 1);\n coords.y2 = point.y / (isPerc ? height : 1);\n }\n target.set('dirty', true);\n return true;\n };\n\nexport const linearGradientControlLineRender = (gradient: Gradient<'linear'>) =>\n function renderCircleControlWithLine(\n this: Control,\n ctx: CanvasRenderingContext2D,\n left: number,\n top: number,\n styleOverride: ControlRenderingStyleOverride,\n fabricObject: InteractiveFabricObject,\n ) {\n // we are position in center of coords.x1/y1\n ctx.save();\n const { width, height, isPerc, coords } = commonGradientInfo(\n fabricObject as FabricObject,\n gradient,\n );\n const finalP = util.sendPointToPlane(\n new Point(\n coords.x2 * (isPerc ? width : 1) - width / 2,\n coords.y2 * (isPerc ? height : 1) - height / 2,\n ),\n util.multiplyTransformMatrices(\n fabricObject.canvas?.viewportTransform ?? iMatrix,\n fabricObject.calcTransformMatrix(),\n ),\n );\n ctx.lineWidth = fabricObject.borderScaleFactor;\n ctx.beginPath();\n ctx.moveTo(left, top);\n ctx.lineTo(finalP.x, finalP.y);\n ctx.stroke();\n controlsUtils.renderCircleControl.call(\n this,\n ctx,\n left,\n top,\n styleOverride,\n fabricObject,\n );\n ctx.restore();\n };\n"],"mappings":";;;AAYA,MAAM,sBACJ,cACA,aACG;CACH,MAAM,EAAE,OAAO,WAAW;CAC1B,MAAM,EAAE,YAAY,QAAQ,kBAAkB;CAC9C,MAAM,SAAS,kBAAkB;CACjC,MAAM,SAAS,aAAa,qBAAqB;CACjD,MAAM,MAAM,aAAa,sBAAsB;AAE/C,QAAO;EACL;EACA;EACA;EACA;EACA;EACA,cAPmB,KAAK,6BAA6B,CAAC,KAAK,OAAO,CAAC;EAQpE;;AAGH,MAAa,+CACX,UACA,cAEA,SAAS,mCAEP,KACA,aACA,cAEA;CACA,MAAM,EAAE,OAAO,QAAQ,QAAQ,QAAQ,YAAY,iBACjD,mBAAmB,cAAc,SAAS;CAC5C,MAAM,KAAK,IAAI,MACb,OAAO,MAAM,SAAS,QAAQ,KAAK,QAAQ,GAC3C,OAAO,MAAM,SAAS,SAAS,KAAK,SAAS,EAC9C;CACD,MAAM,KAAK,IAAI,MACb,OAAO,MAAM,SAAS,QAAQ,KAA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