fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
55 lines (50 loc) • 1.65 kB
JavaScript
import { Color, Gradient } from 'fabric';
/**
* Updates the fromObject function of a Gradient to return a version that can restore old data
* with opactiy in color Stops
* Used to upgrade from fabric 6 to fabric 7
* @param originalFn the original fromObject function of an object,
* @returns a wrapped fromObject function for the object
*/
const gradientUpdaterWrapper = originalFn => async function (serializedGradient) {
// we default to left and top because those are defaults before deprecation
const {
colorStops
} = serializedGradient;
// and we do not want to pass those properties on the object anymore
const newColorStops = colorStops === null || colorStops === void 0 ? void 0 : colorStops.map(_ref => {
let {
color,
opacity,
offset
} = _ref;
if (opacity === undefined || opacity === 1) {
return {
color,
offset
};
}
const col = new Color(color).setAlpha(opacity).toRgba();
return {
color: col,
offset
};
});
const gradient = await originalFn.call(this, {
...serializedGradient,
colorStops: newColorStops
});
return gradient;
};
/**
* Wraps and override the current fabricJS fromObject static functions
* Used to upgrade from fabric 7 to fabric 8
* If you used to export with includeDefaultValues = false, you have to specify
* which were yours default origins values
*/
const installGradientUpdater = () => {
// @ts-expect-error untypable
Gradient.fromObject = gradientUpdaterWrapper(Gradient.fromObject);
};
export { gradientUpdaterWrapper, installGradientUpdater };
//# sourceMappingURL=index.mjs.map