Skip to content

Commit

Permalink
fix: canvas instance check (#632)
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite committed Feb 10, 2023
1 parent 18fc5e6 commit 08ee680
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion __test__/canvas-class.spec.ts
@@ -1,11 +1,19 @@
import test from 'ava'

import { createCanvas, Canvas } from '../index'
import { createCanvas, Canvas, SvgExportFlag } from '../index'

test('Canvas constructor should be equal to createCanvas', (t) => {
t.true(new Canvas(100, 100) instanceof createCanvas(100, 100).constructor)
})

test('CanvasElement instance should be equal to Canvas', (t) => {
t.true(createCanvas(100, 100) instanceof Canvas)
})

test('SVGCanvas instance should be equal to Canvas', (t) => {
t.true(createCanvas(100, 100, SvgExportFlag.NoPrettyXML) instanceof Canvas)
})

test('ctx.canvas should be equal to canvas', (t) => {
const canvas = createCanvas(100, 100)
const ctx = canvas.getContext('2d')
Expand Down
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -71,6 +71,10 @@ class Canvas {
constructor(width, height, flag) {
return createCanvas(width, height, flag)
}

static [Symbol.hasInstance](instance) {
return instance instanceof CanvasElement || instance instanceof SVGCanvas
}
}

if (!process.env.DISABLE_SYSTEM_FONTS_LOAD) {
Expand Down

0 comments on commit 08ee680

Please sign in to comment.