fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
1 lines • 8.94 kB
Source Map (JSON)
{"version":3,"file":"Circle.min.mjs","names":[],"sources":["../../../src/shapes/Circle.ts"],"sourcesContent":["import type { ObjectEvents } from '../EventTypeDefs';\nimport { SHARED_ATTRIBUTES } from '../parser/attributes';\nimport { parseAttributes } from '../parser/parseAttributes';\nimport { cos } from '../util/misc/cos';\nimport { degreesToRadians } from '../util/misc/radiansDegreesConversion';\nimport { sin } from '../util/misc/sin';\nimport { classRegistry } from '../ClassRegistry';\nimport { FabricObject, cacheProperties } from './Object/FabricObject';\nimport type { Abortable, TClassProperties, TOptions } from '../typedefs';\nimport type { FabricObjectProps, SerializedObjectProps } from './Object/types';\nimport type { CSSRules } from '../parser/typedefs';\nimport { SCALE_X, SCALE_Y } from '../constants';\nimport { escapeXml } from '../util/lang_string';\n\ninterface UniqueCircleProps {\n /**\n * Radius of this circle\n * @type Number\n * @default 0\n */\n radius: number;\n\n /**\n * Angle for the start of the circle, in degrees.\n * @type TDegree 0 - 359\n * @default 0\n */\n startAngle: number;\n\n /**\n * Angle for the end of the circle, in degrees\n * @type TDegree 1 - 360\n * @default 360\n */\n endAngle: number;\n\n /**\n * Orientation for the direction of the circle.\n * Setting to true will switch the arc of the circle to traverse from startAngle to endAngle in a counter-clockwise direction.\n * Note: this will only change how the circle is drawn, and does not affect rotational transformation.\n * @default false\n */\n counterClockwise: boolean;\n}\n\nexport interface SerializedCircleProps\n extends SerializedObjectProps, UniqueCircleProps {}\n\nexport interface CircleProps extends FabricObjectProps, UniqueCircleProps {}\n\nconst CIRCLE_PROPS = [\n 'radius',\n 'startAngle',\n 'endAngle',\n 'counterClockwise',\n] as const;\n\nexport const circleDefaultValues: Partial<TClassProperties<Circle>> = {\n radius: 0,\n startAngle: 0,\n endAngle: 360,\n counterClockwise: false,\n};\n\nexport class Circle<\n Props extends TOptions<CircleProps> = Partial<CircleProps>,\n SProps extends SerializedCircleProps = SerializedCircleProps,\n EventSpec extends ObjectEvents = ObjectEvents,\n>\n extends FabricObject<Props, SProps, EventSpec>\n implements UniqueCircleProps\n{\n declare radius: number;\n declare startAngle: number;\n declare endAngle: number;\n declare counterClockwise: boolean;\n\n static type = 'Circle';\n\n static cacheProperties = [...cacheProperties, ...CIRCLE_PROPS];\n\n static ownDefaults = circleDefaultValues;\n\n static getDefaults(): Record<string, any> {\n return {\n ...super.getDefaults(),\n ...Circle.ownDefaults,\n };\n }\n\n /**\n * Constructor\n * @param {Object} [options] Options object\n */\n constructor(options?: Props) {\n super();\n Object.assign(this, Circle.ownDefaults);\n this.setOptions(options);\n }\n\n /**\n * @private\n * @param {String} key\n * @param {*} value\n */\n _set(key: string, value: any) {\n super._set(key, value);\n\n if (key === 'radius') {\n this.setRadius(value);\n }\n\n return this;\n }\n\n /**\n * @private\n * @param {CanvasRenderingContext2D} ctx context to render on\n */\n _render(ctx: CanvasRenderingContext2D) {\n ctx.beginPath();\n ctx.arc(\n 0,\n 0,\n this.radius,\n degreesToRadians(this.startAngle),\n degreesToRadians(this.endAngle),\n this.counterClockwise,\n );\n this._renderPaintInOrder(ctx);\n }\n\n /**\n * Returns horizontal radius of an object (according to how an object is scaled)\n * @return {Number}\n */\n getRadiusX(): number {\n return this.get('radius') * 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 getRadiusY(): number {\n return this.get('radius') * this.get(SCALE_Y);\n }\n\n /**\n * Sets radius of an object (and updates width accordingly)\n */\n setRadius(value: number) {\n this.radius = value;\n this.set({ width: value * 2, height: value * 2 });\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([...CIRCLE_PROPS, ...propertiesToInclude]);\n }\n\n /* _TO_SVG_START_ */\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 const { radius, startAngle, endAngle } = this;\n const angle = (endAngle - startAngle) % 360;\n\n if (angle === 0) {\n return [\n '<circle ',\n 'COMMON_PARTS',\n 'cx=\"0\" cy=\"0\" ',\n 'r=\"',\n `${escapeXml(radius)}`,\n '\" />\\n',\n ];\n } else {\n const start = degreesToRadians(startAngle),\n end = degreesToRadians(endAngle),\n startX = cos(start) * radius,\n startY = sin(start) * radius,\n endX = cos(end) * radius,\n endY = sin(end) * radius,\n largeFlag = angle > 180 ? 1 : 0,\n sweepFlag = this.counterClockwise ? 0 : 1;\n return [\n `<path d=\"M ${startX} ${startY} A ${radius} ${radius} 0 ${largeFlag} ${sweepFlag} ${endX} ${endY}\" `,\n 'COMMON_PARTS',\n ' />\\n',\n ];\n }\n }\n /* _TO_SVG_END_ */\n\n /* _FROM_SVG_START_ */\n /**\n * List of attribute names to account for when parsing SVG element (used by {@link Circle.fromElement})\n * @see: http://www.w3.org/TR/SVG/shapes.html#CircleElement\n */\n static ATTRIBUTE_NAMES = ['cx', 'cy', 'r', ...SHARED_ATTRIBUTES];\n\n /**\n * Returns {@link Circle} instance from an SVG element\n * @param {HTMLElement} element Element to parse\n * @param {Object} [options] Partial Circle object to default missing properties on the element.\n * @throws {Error} If value of `r` attribute is missing or invalid\n */\n static async fromElement(\n element: HTMLElement,\n options: Abortable,\n cssRules?: CSSRules,\n ): Promise<Circle> {\n const {\n left = 0,\n top = 0,\n radius = 0,\n ...otherParsedAttributes\n } = parseAttributes(\n element,\n this.ATTRIBUTE_NAMES,\n cssRules,\n ) as Partial<CircleProps>;\n\n // this probably requires to be fixed for default origins not being top/left.\n\n return new this({\n ...otherParsedAttributes,\n radius,\n left: left - radius,\n top: top - radius,\n });\n }\n\n /* _FROM_SVG_END_ */\n\n /**\n * @todo how do we declare this??\n */\n static fromObject<T extends TOptions<SerializedCircleProps>>(object: T) {\n return super._fromObject<Circle>(object);\n }\n}\n\nclassRegistry.setClass(Circle);\nclassRegistry.setSVGClass(Circle);\n"],"mappings":"6sBAkDA,MAAM,EAAe,CACnB,SACA,aACA,WACA,mBAAA,CAUF,IAAa,EAAb,MAAa,UAKH,CAAA,CAcR,OAAA,aAAO,CACL,MAAO,CAAA,GACF,MAAM,aAAA,CAAA,GACN,EAAO,YAAA,CAQd,YAAY,EAAA,CACV,OAAA,CACA,OAAO,OAAO,KAAM,EAAO,YAAA,CAC3B,KAAK,WAAW,EAAA,CAQlB,KAAK,EAAa,EAAA,CAOhB,OANA,MAAM,KAAK,EAAK,EAAA,CAEZ,IAAQ,UACV,KAAK,UAAU,EAAA,CAGV,KAOT,QAAQ,EAAA,CACN,EAAI,WAAA,CACJ,EAAI,IACF,EACA,EACA,KAAK,OACL,EAAiB,KAAK,WAAA,CACtB,EAAiB,KAAK,SAAA,CACtB,KAAK,iBAAA,CAEP,KAAK,oBAAoB,EAAA,CAO3B,YAAA,CACE,OAAO,KAAK,IAAI,SAAA,CAAY,KAAK,IAAI,EAAA,CAOvC,YAAA,CACE,OAAO,KAAK,IAAI,SAAA,CAAY,KAAK,IAAI,EAAA,CAMvC,UAAU,EAAA,CACR,KAAK,OAAS,EACd,KAAK,IAAI,CAAE,MAAe,EAAR,EAAW,OAAgB,EAAR,EAAA,CAAA,CAQvC,SAGE,EAA2B,EAAA,CAAA,CAC3B,OAAO,MAAM,SAAS,CAAA,GAAI,EAAA,GAAiB,EAAA,CAAA,CAU7C,QAAA,CACE,GAAA,CAAM,OAAE,EAAA,WAAQ,EAAA,SAAY,GAAa,KACnC,GAAS,EAAW,GAAc,IAExC,GAAI,IAAU,EACZ,MAAO,CACL,WACA,eACA,iBACA,MACA,GAAG,EAAU,EAAA,GACb;EAAA,CAEG,CACL,IAAM,EAAQ,EAAiB,EAAA,CAC7B,EAAM,EAAiB,EAAA,CACvB,EAAS,EAAI,EAAA,CAAS,EACtB,EAAS,E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