Expose CanvasRenderingContext2D via the `canvas` package
See original GitHub issueClear and concise description of the problem
I tried to use vitest in a project where I extensively use pixi.js and therefore, the Canvas element. As far as I know, jsdom does not expose canvas functionality out of the box but it can be emulated by using the node-canvas package (https://github.com/jsdom/jsdom/issues/3042).
Suggested solution
What I tried was bootstraping the canvas package in the jsdom environment with the following code
try {
const { Context2d } = await importModule("canvas");
Object.defineProperty(global, "CanvasRenderingContext2D", {
get() {
return Context2d
}
})
} catch (e) { }
inserted somewhere at https://github.com/vitest-dev/vitest/blob/27d6bcb10d6c00ae678769c6f48422ffe9d7d142/packages/vitest/src/env/jsdom.ts#L28-L30
Not the nicest implementation, but I hope you get the idea.
Alternative
No response
Additional context
By the way: Thank’s for this awesome package. It was so easy to setup and works damn good!
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that request the same feature to avoid creating a duplicate.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
CanvasRenderingContext2D - Web APIs | MDN
Chrome Edge
CanvasRenderingContext2D Full support. Chrome1. Toggle history Full support. Edge12. T...
arc Full support. Chrome1. Toggle history Full support. Edge12. T...
arcTo Full support. Chrome1....
Read more >React + Rust + Wasm: Canvas Rendering | by Nikhil Gupta
Let's go through the code step-by-step. Expose a function called `render` that takes the ID of Canvas element already added to the DOM;...
Read more >canvas-sequencer - npm
Store, serialize, parse, and execute sequences of canvas context instructions.. Latest version: 3.1.0, last published: 7 months ago.
Read more >How to fix getImageData() error The canvas has been tainted ...
Would it work for you to save a copy of the image to your server using PHP and ... edit-1: peeps pointed out...
Read more >canvas | Yarn - Package Manager
Installation. $ npm install canvas. By default, binaries for macOS, Linux and Windows will be downloaded. If you want to build from ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@hetsch could maybe share some code how you solved it? Trying to test a konvajs project where I need
canvastoo. Can’t figure it out how to test it withjsdomandvitest.I’m running into:
Thank you, @antfu and @patak-dev. Was my bad. Putting the code in a setup file works perfectly fine.