How to Scale Matrix4 for Offscreen 4K Canvas Rendering? #3069
Replies: 5 comments 1 reply
-
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Update What I discovered: Where I suspect the issue lies:
What I suspect: What's next: |
Beta Was this translation helpful? Give feedback.
-
this tutorial should nicely the approach that needs to be taken here:
https://www.youtube.com/watch?v=5yM4NPcTwY4
…On Fri, Mar 28, 2025 at 11:11 PM Yevhenii Mudrahel ***@***.***> wrote:
Update
I've been investigating the issue further and found that the problem is
not related to how the Matrix4 is scaled or applied.
What I discovered:
Even if I don't change the image position (no translation or adjusting x/y
values) and keep the default matrix (all values as 0 or 1), the image still
renders in the wrong position when saved offscreen.
Where I suspect the issue lies:
The issue seems to be related to this part of the code where I scale and
place the image on the canvas:
const srcRect = { x: 0, y: 0, width: imgWidth, height: imgHeight };
const destRect = { x: 0, y: 0, width: scaledWidth, height: scaledHeight };
const paint = Skia.Paint();
tempCanvas.save();
tempCanvas.translate(frameWidth / 2, frameHeight / 2);
tempCanvas.concat(adjustedTransformMatrix);
tempCanvas.translate(-scaledWidth / 2, -scaledHeight / 2);
tempCanvas.drawImageRect(image, srcRect, destRect, paint);
tempCanvas.restore();
What I suspect:
It seems the way I am scaling the image and centering it on the frame
could be the root cause of the issue. Maybe there's something off in how I
translate and center the image before drawing it on the canvas.
What's next:
I'm trying to figure out if the scaling or positioning logic here needs
correction. Any insights or suggestions would be much appreciated.
—
Reply to this email directly, view it on GitHub
<#3069 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACKXVTEMABATUXGQPKWZM32WW3JNAVCNFSM6AAAAABZ3KKSPWVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTENRVHA4DANI>
.
You are receiving this because you are subscribed to this thread.Message
ID: <Shopify/react-native-skia/repo-discussions/3069/comments/12658805@
github.com>
|
Beta Was this translation helpful? Give feedback.
-
I found the root cause – the issue was not in the offscreen rendering but in the onscreen canvas. I was applying a 0.9 height multiplier to the canvas to create visual margins, which shifted the images slightly. Removing the multiplier fixes the alignment issue. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm building a React Native app using react-native-skia for photo collages. The preview uses a small canvas (~700x400px), but for saving, I use an offscreen canvas (4K: 2160x3840px). I'm trying to scale the Matrix4 from the gesture handler with:
I apply these factors to matrix translation values like:
However, I face two issues:
How should I properly scale Matrix4 to keep image positions consistent between preview and offscreen canvas?
Current implementation for more context:
Beta Was this translation helpful? Give feedback.
All reactions