I’m using Paper.js canvas as a source and a simple canvas as a target. Basically copying a part of the visible image via getImageData
and pasting it into the target canvas with putImageData
.
The pasted image does not have the alpha channel. For example when the copied area includes just a part of an object and the rest should be empty. I tried adding { colorSpace: 'display-p3' }
for both functions, but I see that the color space stays srgb
and still no alpha channel.
Is it possible to copy/paste the image data with getImageData
preserving the alpha channel with Paper.js?
Here’s the copy/paste code.
const rasterTemp = RasterObject.rasterize() // This to reset all the transformations.
const subRasterTemp = rasterTemp.getSubRaster(new data.$paper.Rectangle({
point: [0, 0], // These and other coordinates are just for the sake of demonstration
size: [100, 100],
}))
const imageData = subRasterTemp.getImageData(new data.$paper.Rectangle({
point: [0, 0],
size: [100, 100],
}), { colorSpace: 'display-p3' })
canvasTargetContext.putImageData(imageData, 0, 0)
2
Answers
Ok, it's not actually the case. Transparency is being preserved. I just visually got an impression that it wasn't. But when I simply saved the canvas as an image I saw the transparency.
I can’t really explain the reason why this works, but a workaround for this can be to first draw the image data on a temporary canvas and then draw this temporary canvas on your target canvas. I got the idea from this thread.
And here’s a fiddle demonstrating a possible solution.