Skip to content

Commit ef469b8

Browse files
kitsonkkt3k
andauthored
fix: clear rect uses transparent background (#5)
Co-authored-by: Yoshiya Hinosawa <[email protected]>
1 parent 44f25b4 commit ef469b8

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

core.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
import { ChartJs, Rect2D, SvgCanvas, SvgCanvas2DGradient } from "./deps.ts";
44

5+
class ChartSvgCanvas extends SvgCanvas {
6+
public override clearRect(x: number, y: number, w: number, h: number): void {
7+
this.save();
8+
this.fillStyle = "transparent";
9+
this.fillRect(x, y, w, h);
10+
this.restore();
11+
}
12+
13+
public resetTransform() {
14+
this.setTransform(1, 0, 0, 1, 0, 0);
15+
}
16+
}
17+
518
/** Underlying ChartJS defaults which can be modified. */
619
export const defaults: ChartJs.Defaults = ChartJs.defaults;
720
/** Underlying ChartJS plugins. */
@@ -80,18 +93,13 @@ export function chart<
8093
responsive: false,
8194
});
8295

83-
const ctx: SvgCanvas & SvgCanvasExtras = new SvgCanvas();
96+
const ctx: ChartSvgCanvas & SvgCanvasExtras = new ChartSvgCanvas();
8497
ctx.canvas = {
8598
width,
8699
height,
87100
style: { width: `${width}px`, height: `${height}px` },
88101
};
89102
ctx.fontHeightRatio = 2;
90-
ctx.globalAlpha = 1;
91-
// for some reason, SvgCanvas does not provide `.resetTransform()` so using
92-
// `setTransform()` to set to the identity matrix, which is effectively the
93-
// same.
94-
ctx.resetTransform = () => ctx.setTransform(1, 0, 0, 1, 0, 0);
95103
// deno-lint-ignore no-explicit-any
96104
const el: HTMLCanvasElement = { getContext: () => ctx } as any;
97105
const savedGradient = globalThis.CanvasGradient;

0 commit comments

Comments
 (0)