Skip to content

Commit

Permalink
Address code review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ronyeh committed Sep 5, 2021
1 parent 2fd5d6e commit 24caf2e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ module.exports = (grunt) => {
library: libraryName,
libraryTarget: 'umd',
libraryExport: 'default',
// Support different ways of loading VexFlow.
// The `globalObject` string is assigned to `root` in line 15 of vexflow-debug.js.
// VexFlow is exported as root["Vex"], and can be accessed via:
// - `window.Vex` in browsers
// - `globalThis.Vex` in node JS >= 12
// - `this.Vex` in all other environments
// See: https://webpack.js.org/configuration/output/#outputglobalobject
globalObject: `typeof window !== 'undefined' ? window : typeof globalThis !== 'undefined' ? globalThis : this`,
publicPath: 'auto',
},
Expand Down
20 changes: 10 additions & 10 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,31 +146,31 @@ export class Renderer {
}

/**
* @param id_canvas_div the first parameter can be:
* @param canvasId can be:
* - a string element ID (of a canvas or div element)
* - a canvas element
* - a div element, which will contain the SVG output
* @param backend Renderer.Backends.CANVAS or Renderer.Backends.SVG
*/
constructor(id_canvas_div: string | HTMLCanvasElement | HTMLDivElement, backend: number) {
if (!id_canvas_div) {
constructor(canvasId: string | HTMLCanvasElement | HTMLDivElement, backend: number) {
if (!canvasId) {
throw new RuntimeError('BadArgument', 'Invalid id for renderer.');
} else if (typeof id_canvas_div === 'string') {
this.elementId = id_canvas_div;
this.element = document.getElementById(id_canvas_div as string) as HTMLCanvasElement | HTMLDivElement;
} else if ('getContext' in id_canvas_div /* HTMLCanvasElement */) {
this.element = id_canvas_div as HTMLCanvasElement;
} else if (typeof canvasId === 'string') {
this.elementId = canvasId;
this.element = document.getElementById(canvasId as string) as HTMLCanvasElement | HTMLDivElement;
} else if ('getContext' in canvasId /* HTMLCanvasElement */) {
this.element = canvasId as HTMLCanvasElement;
} else {
// Assume it's a HTMLDivElement.
this.element = id_canvas_div as HTMLDivElement;
this.element = canvasId as HTMLDivElement;
}

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

0 comments on commit 24caf2e

Please sign in to comment.