UNPKG

fabric

Version:

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

1 lines 7.02 kB
{"version":3,"file":"Ellipse.min.mjs","sources":["../../../src/shapes/Ellipse.ts"],"sourcesContent":["import { SCALE_X, SCALE_Y, twoMathPi } from '../constants';\nimport { SHARED_ATTRIBUTES } from '../parser/attributes';\nimport { parseAttributes } from '../parser/parseAttributes';\nimport type { Abortable, TClassProperties, TOptions } from '../typedefs';\nimport { classRegistry } from '../ClassRegistry';\nimport { FabricObject, cacheProperties } from './Object/FabricObject';\nimport type { FabricObjectProps, SerializedObjectProps } from './Object/types';\nimport type { ObjectEvents } from '../EventTypeDefs';\nimport type { CSSRules } from '../parser/typedefs';\nimport { escapeXml } from '../util/lang_string';\n\nexport const ellipseDefaultValues: Partial<TClassProperties<Ellipse>> = {\n rx: 0,\n ry: 0,\n};\n\ninterface UniqueEllipseProps {\n rx: number;\n ry: number;\n}\n\nexport interface SerializedEllipseProps\n extends SerializedObjectProps, UniqueEllipseProps {}\n\nexport interface EllipseProps extends FabricObjectProps, UniqueEllipseProps {}\n\nconst ELLIPSE_PROPS = ['rx', 'ry'] as const;\n\nexport class Ellipse<\n Props extends TOptions<EllipseProps> = Partial<EllipseProps>,\n SProps extends SerializedEllipseProps = SerializedEllipseProps,\n EventSpec extends ObjectEvents = ObjectEvents,\n>\n extends FabricObject<Props, SProps, EventSpec>\n implements EllipseProps\n{\n /**\n * Horizontal radius\n * @type Number\n */\n declare rx: number;\n\n /**\n * Vertical radius\n * @type Number\n */\n declare ry: number;\n\n static type = 'Ellipse';\n\n static cacheProperties = [...cacheProperties, ...ELLIPSE_PROPS];\n\n static ownDefaults = ellipseDefaultValues;\n\n static getDefaults(): Record<string, any> {\n return {\n ...super.getDefaults(),\n ...Ellipse.ownDefaults,\n };\n }\n\n /**\n * Constructor\n * @param {Object} [options] Options object\n */\n constructor(options?: Props) {\n super();\n Object.assign(this, Ellipse.ownDefaults);\n this.setOptions(options);\n }\n\n /**\n * @private\n * @param {String} key\n * @param {*} value\n * @return {Ellipse} thisArg\n */\n _set(key: string, value: any) {\n super._set(key, value);\n switch (key) {\n case 'rx':\n this.rx = value;\n this.set('width', value * 2);\n break;\n\n case 'ry':\n this.ry = value;\n this.set('height', value * 2);\n break;\n }\n return this;\n }\n\n /**\n * Returns horizontal radius of an object (according to how an object is scaled)\n * @return {Number}\n */\n getRx() {\n return this.get('rx') * this.get(SCALE_X);\n }\n\n /**\n * Returns Vertical radius of an object (according to how an object is scaled)\n * @return {Number}\n */\n getRy() {\n return this.get('ry') * this.get(SCALE_Y);\n }\n\n /**\n * Returns object representation of an instance\n * @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output\n * @return {Object} object representation of an instance\n */\n toObject<\n T extends Omit<Props & TClassProperties<this>, keyof SProps>,\n K extends keyof T = never,\n >(propertiesToInclude: K[] = []): Pick<T, K> & SProps {\n return super.toObject([...ELLIPSE_PROPS, ...propertiesToInclude]);\n }\n\n /**\n * Returns svg representation of an instance\n * @return {Array} an array of strings with the specific svg representation\n * of the instance\n */\n _toSVG(): string[] {\n return [\n '<ellipse ',\n 'COMMON_PARTS',\n `cx=\"0\" cy=\"0\" rx=\"${escapeXml(this.rx)}\" ry=\"${escapeXml(this.ry)}\" />\\n`,\n ];\n }\n\n /**\n * @private\n * @param {CanvasRenderingContext2D} ctx context to render on\n */\n _render(ctx: CanvasRenderingContext2D) {\n ctx.beginPath();\n ctx.save();\n ctx.transform(1, 0, 0, this.ry / this.rx, 0, 0);\n ctx.arc(0, 0, this.rx, 0, twoMathPi, false);\n ctx.restore();\n this._renderPaintInOrder(ctx);\n }\n\n /* _FROM_SVG_START_ */\n\n /**\n * List of attribute names to account for when parsing SVG element (used by {@link Ellipse.fromElement})\n * @see http://www.w3.org/TR/SVG/shapes.html#EllipseElement\n */\n static ATTRIBUTE_NAMES = [...SHARED_ATTRIBUTES, 'cx', 'cy', 'rx', 'ry'];\n\n /**\n * Returns {@link Ellipse} instance from an SVG element\n * @param {HTMLElement} element Element to parse\n * @return {Ellipse}\n */\n static async fromElement(\n element: HTMLElement,\n options?: Abortable,\n cssRules?: CSSRules,\n ) {\n const parsedAttributes = parseAttributes(\n element,\n this.ATTRIBUTE_NAMES,\n cssRules,\n );\n\n parsedAttributes.left = (parsedAttributes.left || 0) - parsedAttributes.rx;\n parsedAttributes.top = (parsedAttributes.top || 0) - parsedAttributes.ry;\n return new this(parsedAttributes);\n }\n\n /* _FROM_SVG_END_ */\n}\n\nclassRegistry.setClass(Ellipse);\nclassRegistry.setSVGClass(Ellipse);\n"],"names":["ellipseDefaultValues","rx","ry","ELLIPSE_PROPS","Ellipse","FabricObject","getDefaults","super","ownDefaults","constructor","options","Object","assign","this","setOptions","_set","key","value","set","getRx","get","SCALE_X","getRy","SCALE_Y","toObject","propertiesToInclude","arguments","length","undefined","_toSVG","escapeXml","_render","ctx","beginPath","save","transform","arc","twoMathPi","restore","_renderPaintInOrder","fromElement","element","cssRules","parsedAttributes","parseAttributes","ATTRIBUTE_NAMES","left","top","_defineProperty","cacheProperties","SHARED_ATTRIBUTES","classRegistry","setClass","setSVGClass"],"mappings":"ihBAWO,MAAMA,EAA2D,CACtEC,GAAI,EACJC,GAAI,GAaAC,EAAgB,CAAC,KAAM,MAEtB,MAAMC,UAKHC,EAqBR,kBAAOC,GACL,MAAO,IACFC,MAAMD,iBACNF,EAAQI,YAEf,CAMAC,WAAAA,CAAYC,GACVH,QACAI,OAAOC,OAAOC,KAAMT,EAAQI,aAC5BK,KAAKC,WAAWJ,EAClB,CAQAK,IAAAA,CAAKC,EAAaC,GAEhB,OADAV,MAAMQ,KAAKC,EAAKC,GACRD,GACN,IAAK,KACHH,KAAKZ,GAAKgB,EACVJ,KAAKK,IAAI,QAAiB,EAARD,GAClB,MAEF,IAAK,KACHJ,KAAKX,GAAKe,EACVJ,KAAKK,IAAI,SAAkB,EAARD,GAGvB,OAAOJ,IACT,CAMAM,KAAAA,GACE,OAAON,KAAKO,IAAI,MAAQ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