samizdatco / skia-canvas

A GPU-accelerated 2D graphics environment for Node.js
MIT License
1.67k stars 63 forks source link

Can this Canvas be converted to raw? #116

Closed LmanTW closed 6 months ago

LmanTW commented 1 year ago

I am using Automattic/node-canvas, I want to change to this canvas, but I tried it and it seems that I can't convert canvas to raw, is there a way to convert canvas to raw?

//in Automattic/node-canvas
canvas.toBuffer('raw')
vincaslt commented 6 months ago

You can get raw pixel data on context2d (it's rgba order, unlike bgra in node-canvas) and then do whatever you want with it like stream into ffmpeg:

const imageData = ctx.getImageData(0, 0, 1080, 1920).data
const buffer = Buffer.from(imageData)
writeFileSync(`out/${frameNum}.raw`, buffer);
LmanTW commented 6 months ago

It works, thanks for helping!

mpaperno commented 6 months ago

However, getImageData() is a synchronous operation and will be relatively slow. My fork has an async option (toBuffer('raw')), in case that is useful. (I'd submit a PR for that, but this project appears to no longer be maintained.)