Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: canvas instance check #632

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion __test__/canvas-class.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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