UNPKG

fabric

Version:

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

1 lines 3.25 kB
{"version":3,"file":"index.mjs","sources":["../../../extensions/data_updaters/gradient/index.ts"],"sourcesContent":["import type { GradientOptions, ColorStop } from 'fabric';\nimport { Color, Gradient } from 'fabric';\n\n/**\n * Updates the fromObject function of a Gradient to return a version that can restore old data\n * with opactiy in color Stops\n * Used to upgrade from fabric 6 to fabric 7\n * @param originalFn the original fromObject function of an object,\n * @returns a wrapped fromObject function for the object\n */\n\ntype OldColorStop = ColorStop & {\n opacity?: number;\n};\n\nexport const gradientUpdaterWrapper = <S, T extends Gradient<S> = Gradient<S>>(\n originalFn: (\n options: GradientOptions<'linear'> | GradientOptions<'radial'>,\n ) => Promise<T>,\n): ((\n options: GradientOptions<'linear'> | GradientOptions<'radial'>,\n) => Promise<T>) =>\n async function (this: T, serializedGradient) {\n // we default to left and top because those are defaults before deprecation\n const { colorStops } = serializedGradient;\n // and we do not want to pass those properties on the object anymore\n const newColorStops: ColorStop[] = (\n colorStops as OldColorStop[]\n )?.map<ColorStop>(({ color, opacity, offset }) => {\n if (opacity === undefined || opacity === 1) {\n return {\n color,\n offset,\n };\n }\n const col = new Color(color).setAlpha(opacity).toRgba();\n return {\n color: col,\n offset,\n };\n });\n const gradient = await originalFn.call(this, {\n ...serializedGradient,\n colorStops: newColorStops,\n });\n return gradient;\n };\n\n/**\n * Wraps and override the current fabricJS fromObject static functions\n * Used to upgrade from fabric 7 to fabric 8\n * If you used to export with includeDefaultValues = false, you have to specify\n * which were yours default origins values\n */\nexport const installGradientUpdater = () => {\n // @ts-expect-error untypable\n Gradient.fromObject = gradientUpdaterWrapper(Gradient.fromObject);\n};\n"],"names":["gradientUpdaterWrapper","originalFn","serializedGradient","colorStops","newColorStops","map","_ref","color","opacity","offset","undefined","col","Color","setAlpha","toRgba","gradient","call","installGradientUpdater","Gradient","fromObject"],"mappings":";;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;;MAMaA,sBAAsB,GACjCC,UAEe,IAIf,gBAAyBC,kBAAkB,EAAE;AAC3C;EACA,MAAM;AAAEC,IAAAA;AAAW,GAAC,GAAGD,kBAAkB;AACzC;EACA,MAAME,aAA0B,GAC9BD,UAAU,KAAA,IAAA,IAAVA,UAAU,KAAA,MAAA,GAAA,MAAA,GAAVA,UAAU,CACTE,GAAG,CAAYC,IAAA,IAAgC;IAAA,IAA/B;MAAEC,KAAK;MAAEC,OAAO;AAAEC,MAAAA;AAAO,KAAC,GAAAH,IAAA;AAC3C,IAAA,IAAIE,OAAO,KAAKE,SAAS,IAAIF,OAAO,KAAK,CAAC,EAAE;MAC1C,OAAO;QACLD,KAAK;AACLE,QAAAA;OACD;AACH,IAAA;AACA,IAAA,MAAME,GAAG,GAAG,IAAIC,KAAK,CAACL,KAAK,CAAC,CAACM,QAAQ,CAACL,OAAO,CAAC,CAACM,MAAM,EAAE;IACvD,OAAO;AACLP,MAAAA,KAAK,EAAEI,GAAG;AACVF,MAAAA;KACD;AACH,EAAA,CAAC,CAAC;EACF,MAAMM,QAAQ,GAAG,MAAMd,UAAU,CAACe,IAAI,CAAC,IAAI,EAAE;AAC3C,IAAA,GAAGd,kBAAkB;AACrBC,IAAAA,UAAU,EAAEC;AACd,GAAC,CAAC;AACF,EAAA,OAAOW,QAAQ;AACjB;;AAEF;AACA;AACA;AACA;AACA;AACA;AACO,MAAME,sBAAsB,GAAGA,MAAM;AAC1C;EACAC,QAAQ,CAACC,UAAU,GAAGnB,sBAAsB,CAACkB,QAAQ,CAACC,UAAU,CAAC;AACnE;;;;"}