fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 6.42 kB
Source Map (JSON)
{"version":3,"file":"Ellipse.mjs","names":[],"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"],"mappings":";;;;;;;;;AAWA,MAAa,uBAA2D;CACtE,IAAI;CACJ,IAAI;CACL;AAYD,MAAM,gBAAgB,CAAC,MAAM,KAAK;AAElC,IAAa,UAAb,MAAa,gBAKH,aAEV;CAmBE,OAAO,cAAmC;AACxC,SAAO;GACL,GAAG,MAAM,aAAa;GACtB,GAAG,QAAQ;GACZ;;;;;;CAOH,YAAY,SAAiB;AAC3B,SAAO;AACP,SAAO,OAAO,MAAM,QAAQ,YAAY;AACxC,OAAK,WAAW,QAAQ;;;;;;;;CAS1B,KAAK,KAAa,OAAY;AAC5B,QAAM,KAAK,KAAK,MAAM;AACtB,UAAQ,KAAR;GACE,KAAK;AACH,SAAK,KAAK;AACV,SAAK,IAAI,SAAS,QAAQ,EAAE;AAC5B;GAEF,KAAK;AACH,SAAK,KAAK;AACV,SAAK,IAAI,UAAU,QAAQ,EAAE;AAC7B;;AAEJ,SAAO;;;;;;CAOT,QAAQ;AACN,SAAO,KAAK,IAAI,KAAK,GAAG,KAAK,IAAI,QAAQ;;;;;;CAO3C,QAAQ;AACN,SAAO,KAAK,IAAI,KAAK,GAAG,KAAK,IAAI,QAAQ;;;;;;;CAQ3C,SAGE,sBAA2B,EAAE,EAAuB;AACpD,SAAO,MAAM,SAAS,CAAC,GAAG,eAAe,GAAG,oBAAoB,CAAC;;;;;;;CAQnE,SAAmB;AACjB,SAAO;GACL;GACA;G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