Skip to content

Commit

Permalink
Add setters (.font .fillStyle .strokeStyle) to maintain compatibility…
Browse files Browse the repository at this point in the history
… with

the CanvasRenderingContext2D API.
  • Loading branch information
ronyeh committed Jul 4, 2021
1 parent f349614 commit 76d9ee9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
23 changes: 22 additions & 1 deletion src/canvascontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class CanvasContext implements RenderContext {

/**
* This constructor is only called if Renderer.USE_CANVAS_PROXY is true.
* In most instances, we do not need to
* In most instances, we do not need to create a CanvasContext object.
* See Renderer.bolsterCanvasContext().
* @param context
*/
Expand Down Expand Up @@ -241,4 +241,25 @@ export class CanvasContext implements RenderContext {
this.vexFlowCanvasContext.restore();
return this;
}

/** Maintain compatibility with the CanvasRenderingContext2D API. */
set font(value: string) {
debugger;
console.log('CanvasContext .font! [' + value + ']');
this.vexFlowCanvasContext.font = value;
}

/** Maintain compatibility with the CanvasRenderingContext2D API. */
set fillStyle(style: string) {
debugger;
console.log('CanvasContext .fillStyle! [' + style + ']');
this.vexFlowCanvasContext.fillStyle = style;
}

/** Maintain compatibility with the CanvasRenderingContext2D API. */
set strokeStyle(style: string) {
debugger;
console.log('CanvasContext .strokeStyle! [' + style + ']');
this.vexFlowCanvasContext.strokeStyle = style;
}
}
11 changes: 9 additions & 2 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export class Renderer {

// Modify the CanvasRenderingContext2D to include the following methods, if they do not already exist.
// setLineDash exists natively: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash
// TODO: Is a Proxy object appropriate here?
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
const methodNames = [
'clear',
'setFont',
Expand All @@ -90,9 +92,14 @@ export class Renderer {

ctx.vexFlowCanvasContext = ctx;

// TODO: Do we get called with the same context over and over again?
// TODO: Keep references to the context to see if it's the same one.
// ANSWER: PROBABLY YES!!!
methodNames.forEach((methodName) => {
// eslint-disable-next-line
ctx[methodName] = ctx[methodName] || (CanvasContext.prototype as any)[methodName];
if (!(methodName in ctx)) {
// eslint-disable-next-line
ctx[methodName] = (CanvasContext.prototype as any)[methodName];
}
});

return ctx;
Expand Down
18 changes: 18 additions & 0 deletions src/svgcontext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,4 +709,22 @@ export class SVGContext implements RenderContext {
}
return this;
}

/** Maintain compatibility with the CanvasRenderingContext2D API. */
set font(value: string) {
console.log('SVG Context .font! [' + value + ']');
this.setRawFont(value);
}

/** Maintain compatibility with the CanvasRenderingContext2D API. */
set fillStyle(style: string) {
console.log('SVG Context .fillStyle! [' + style + ']');
this.setFillStyle(style);
}

/** Maintain compatibility with the CanvasRenderingContext2D API. */
set strokeStyle(style: string) {
console.log('SVG Context .strokeStyle! [' + style + ']');
this.setStrokeStyle(style);
}
}
13 changes: 10 additions & 3 deletions src/types/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,17 @@ export interface RenderContext {
closeGroup(): void;
add(child: any): void;

/**
* canvas returns TextMetrics and SVG returns SVGRect.
*/
/** canvas returns TextMetrics and SVG returns SVGRect. */
measureText(text: string): { width: number; height?: number };

/** Maintain compatibility with the CanvasRenderingContext2D API. */
set font(value: string);

/** Maintain compatibility with the CanvasRenderingContext2D API. */
set fillStyle(style: string);

/** Maintain compatibility with the CanvasRenderingContext2D API. */
set strokeStyle(style: string);
}

export interface TieNotes {
Expand Down
6 changes: 4 additions & 2 deletions tests/annotation_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ const AnnotationTests = (function () {
simple: function (options, contextBuilder) {
var ctx = contextBuilder(options.elementId, 500, 240);
ctx.scale(1.5, 1.5);
ctx.fillStyle = '#221';
ctx.strokeStyle = '#221';
// ctx.fillStyle = '#221';
// ctx.strokeStyle = '#221';
ctx.fillStyle = '#F21';
ctx.strokeStyle = '#F21';
ctx.font = ' 10pt Arial';
var stave = new VF.TabStave(10, 10, 450).addTabGlyph().setContext(ctx).draw();

Expand Down

0 comments on commit 76d9ee9

Please sign in to comment.