You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to get canvas image data in JSDOM. I wrapped headless-gl functions to jsdom to add web gl functionality, however headless-gl does not seem to have canvas.toDataURL() option. Is there a way to add this functionality?
Thanks
importglfrom'gl';importjsdomfrom"jsdom";constJSDOM=jsdom.JSDOM;constdom=newJSDOM(`<html><head></head><body></body></html>`,{url: "https://example.com",beforeParse(window){window=beforeParse(window);}});dom.window.eval(`const webgl = window.document.createElement('canvas');// window.WebGLRenderingContext.prototype.getParameter.name// window.WebGLRenderingContext.prototype.getParameterconst g = webgl.getContext("webgl")console.log(g.canvas.toDataURL());//^ need to get the canvas to get the image data (returns undefined)`)functionbeforeParse(){constorig_getContext=window.HTMLCanvasElement.prototype.getContextwindow.HTMLCanvasElement.prototype.getContext=function(){if(arguments[0]==='webgl'){constctx=gl.create(1,1,arguments[1])ctx.resize=ctx.getExtension('STACKGL_resize_drawingbuffer').resizereturnctx}else{returnorig_getContext.apply(this,arguments)}}}
The text was updated successfully, but these errors were encountered:
headless-gl itself does not provide a canvas element or image output functionality.
However, if you have a way to add toDataURL a method to HTMLCanvasElement, you can implement it yourself with something like:
// `width` and `height` are the dimensions of your canvasconstpixels=newUint8Array(4*width*height);// `gl` is the gl context of this context.// we pass the `pixels` array as the last argument // RGBA format is used gl.readPixels(0,0,width,height,gl.RGBA,gl.UNSIGNED_BYTE,pixels);// get the data buffer from the pixels arrayconstbuffer=Buffer.from(pixels.buffer);// pass this bitmap data to a method that can encode it as a PNG / JPEG. I use `sharp`, but there are others, such as `jpeg-js`, `pngjs3`, etc.// ...
When creating your gl context, make sure to pass preserveDrawingBuffer: true to the constructor options. You may also want, antialias: true and alpha: true
Hello,
I am trying to get canvas image data in JSDOM. I wrapped headless-gl functions to jsdom to add web gl functionality, however headless-gl does not seem to have canvas.toDataURL() option. Is there a way to add this functionality?
Thanks
The text was updated successfully, but these errors were encountered: