rexrainbow / phaser3-rex-notes

Notes of phaser3 engine
MIT License
1.21k stars 262 forks source link

Possible canvas data bug #458

Closed Bsandor453 closed 1 month ago

Bsandor453 commented 2 months ago

I downloaded this library since I needed a tool to get texture data from sprites. I wanted to ignore hover effect on transparent pixels. I had problems with Spritesheets, only the first frame was processed right, other frames were not, I essentially got back empy data, or I guess black pixels. After a lot of investigation changing this line (the one that is commented out) to the line below fixed the problem: (path: phaser3-rex-plugins\plugins\utils\texture\CopyFrameToCanvas.js)

var CopyFrameToCanvas = function (frame, canvas) {
    canvas.width = frame.cutWidth;
    canvas.height = frame.cutHeight;
    var context = canvas.getContext('2d', { willReadFrequently: true });
    // context.drawImage(frame.source.image, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight);
    context.drawImage(frame.source.image, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, 0, 0, canvas.width, canvas.height);
    return canvas;
}

export default CopyFrameToCanvas;

(this is called in phaser3-rex-plugins\plugins\data\canvasdata\TextureToColorMap.js for my case)

From official docs:

drawImage(image, dx, dy)
drawImage(image, dx, dy, dWidth, dHeight)
drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)

Notice that for the 5 param (and 3 too) one, the 2nd and 3rd parameter is the destination coordinates, for the 9 param function, it's different. We need to use the 9 param function if we want to specify the source coordinates, the other ones only take the destination coordinates into consideration. So for the canvas data every frame should be rendered at (0,0) regarless of cutX or cutY, but also need to specify source coordinates.

rexrainbow commented 2 months ago

Still can't get your requirement, could you please provide a simple test code for demonstrating this issue?

Do you need to load a single frame of a spritesheet? Or whole spritesheet?