iddan / react-native-canvas

A Canvas component for React Native
MIT License
981 stars 172 forks source link

Difference in drawImage coordinates and getImageData/putImageData coordinates #301

Open kristopalka opened 1 year ago

kristopalka commented 1 year ago

Hi, i made folllowing code:

    useEffect(() => {
        const canvas = canvasRef.current;
        if (canvas === null) return;
        canvas.width = canvasWidth;
        canvas.height = canvasHeight;

        let context = canvas.getContext('2d');
        const image = new CanvasImage(canvas);
        image.src = imageUrl;

        image.addEventListener("load", async () => {
            context.drawImage(image, 0, 0, canvas.width, canvas.height);
            context.getImageData(0, 0, canvas.width, canvas.height)
                .then((imageData) => {
                    context.putImageData(imageData, canvas.width, canvas.height)
                })
        });
    }, []);

and the canvas

       {React.useMemo(() => <Canvas ref={canvasRef}/>, [])}

and now my canvas looks like this: image

What I think I am doing:

What I got:

It seems like geiImageData and putImageData interpret the coordinates (canvas.width, canvas.height) in other way than drawImage or even fillRect which also works correctly. I calculated this scale, and it seems to be exactly 2.75 (0.36). The proportions are keeped. My canvas size is 1/3 of screen width and proportion is 3:4. But as I checked, it does not affect scale. When I replace canvas.width with the number, it is all the same.

Please, tell me if I am doing something wrong or what could cause this issue

BraveEvidence commented 1 year ago

This might help https://www.youtube.com/watch?v=wKr1kxNccgo

bartproo commented 1 year ago

I have the same issue whereby my getImageData return a crop region of my canvas. This is strange