Skip to content

Commit

Permalink
Edit signatures of Renders' constructor to support passing HTMLCanvas…
Browse files Browse the repository at this point in the history
…Element directly
  • Loading branch information
aleen42 committed Jul 26, 2021
1 parent 4587fbe commit ed41367
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type ContextBuilder = typeof Renderer.getSVGContext | typeof Renderer.get
* Support Canvas & SVG rendering contexts.
*/
export class Renderer {
protected elementId?: string | HTMLElement;
protected elementId?: string | HTMLCanvasElement;
protected element: HTMLCanvasElement;
protected backend: number;

Expand Down Expand Up @@ -43,7 +43,7 @@ export class Renderer {
static lastContext: RenderContext | undefined = undefined;

static buildContext(
elementId: string | HTMLElement,
elementId: string | HTMLCanvasElement,
backend: number,
width: number,
height: number,
Expand Down Expand Up @@ -145,18 +145,20 @@ export class Renderer {
context.stroke();
}

constructor(elementId: string | HTMLElement, backend: number) {
constructor(elementId: string | HTMLCanvasElement, backend: number) {
if (!elementId) {
throw new RuntimeError('BadArgument', 'Invalid id for renderer.');
}

this.element = document.getElementById(elementId as string) as HTMLCanvasElement;
if (!this.element) this.element = elementId as HTMLCanvasElement;
this.element =
typeof elementId === 'string' && typeof document !== 'undefined'
? (document.getElementById(elementId as string) as HTMLCanvasElement)
: (elementId as HTMLCanvasElement);

// Verify backend and create context
this.backend = backend;
if (this.backend === Renderer.Backends.CANVAS) {
if (!this.element.getContext) {
if (!this.element?.getContext) {
throw new RuntimeError('BadElement', `Can't get canvas context from element: ${elementId}`);
}
this.ctx = Renderer.bolsterCanvasContext(this.element.getContext('2d'));
Expand Down

0 comments on commit ed41367

Please sign in to comment.