You can simply define a typescript class that extends the fabric class.
export interface IMindObjectOptions { item: IMindItem; } export interface IMindExpanderOptions extends IMindObjectOptions { x: number; y: number; radius: number; isExpanded: boolean; } export class MindExpander extends fabric.Circle implements IMindObjectOptions { public constructor(options: IMindExpanderOptions) { const circleOptions: ICircleOptions = { originX: "center", originY: "center", left: options.x, top: options.y, radius: options.radius, stroke: "#000000", fill: options.isExpanded ? "#00FFFF": "#00FF00", selectable: false }; super(circleOptions); this.item = options.item; } public readonly item: IMindItem; }
!!! The above code works fine in the browser, but not in the enjiornment node.
To work in a nodejs environment, for example, run tests without a browser, you need:
import {assert} from "chai"; import * as f from 'fabric'; import {ITextOptions} from "fabric"; declare function require(s: string): any; declare const __dirname: string; const fabric = require("fabric").fabric; interface ITextConstructor { new(text: string, options: ITextOptions): f.Text; } function getTextConstructor(): ITextConstructor { return fabric.Text; } describe("nodejs setup", function () { it("fabric is imported correctly", function () { const x = fabric.Text as f.Text; assert.equal(typeof x, "function"); }); it("fabric class is extensible", function () { class SubText extends getTextConstructor() { public constructor(text: string, options: fabric.ITextOptions) { super(text, options); } } const c = new SubText("Hello", {left: 100, top: 100}); assert.equal(c.text, "Hello"); assert.isNotNull(c); }); });
source share