Hi Guys, Any one with experience with canvas? Im...
# android
y
Hi Guys, Any one with experience with canvas? Im developing a simple real time drawing app. Basically I'm sending trough web socket the path offsets that the user is drawing. And I'm drawing it in the app that is listening. But I'm facing a problem when I draw it on large phone and receive in a small phone, I think is because of pixels difference. So I wanted to know what the approach to make it work correct? eg.:
😶 4
For those flagging this "not kotlin" is there a place to ask Android questions in slack?
l
I'd recommend mapping coordinates to a [0,1],[0,1] space before sending, then map to the receiver device's size when drawing.
If the aspect ratio changes, that approach won't work as well, though. In that case, the math is more complicated.
This approach will also help if the user rotates the phone. That's a good way to make sure your scaling is how you want it.
c
For those flagging this “not kotlin” is there a place to ask Android questions in slack?
check the channel description
a
To be fair it seems ~90% off the questions here are 😶 these days. (...don't come for me 😄)
👍 1
l
It's hard to find something that's a. Not compose b. Related to Android c. Specifically to do with Kotlin d. Interesting and easy enough to understand that it doesn't make this Slack intimidating to browse without deep domain knowledge
plus1 4
😸 1
p
Not an expert in graphic stuff but what I would do is create a process in the receiver phone that maps the incoming pixels to my screen pixels. For that you have to send the source screen full width/height too. Let’s say, if one screen has 3 times more pixels than the other, then 3 pixels of this screen will map to the same pixel in the other. Similarly, mapping back, 1 pixel will correspond to 3 pixels in the more density screen. Obviously there would be some distortions when the aspect ration of the 2 screens vary considerably. Perhaps some work with image compression and transformation help but as every body says is off-topic. Try researching on gpt 🤷‍♂️
l
If you map to [0,1] space when sending, you don't need to send the source dimensions
p
I got you, yeah you right, I didn’t understood your syntax at first, but I guess now I understand now what you come with
y
Can you elaborate more whats would be map to [0,1], I don't if I understood well.
l
0 means far left/top, 1 means far right/bottom of screen.
I'm on mobile, but code is roughly points.map { Point(it.x*1.0/w, it.y*1.0/h) }
1
p
What I was saying is above thinking will give you the pixel when you compress, from high density to low density. But when you expand, from low density to high density. You still can apply the above formulae but map n amount of pixels in the higher density. Where n = screenBigerHeightPixel/screenSmallerHeightPixel.
So lets say if screen A has twice more width pixels than B and three times more height. Then when you map from B to A, will mab as follow. pixelB (x=0,y=0) -> pixelA(0,0) + pixelA(0,1) width and pixelA(1,0) + pixelA(2,0) + pixelA(3,0)
I mean I really suggest you do some research on image compression, you may end up dealing with some Fourier transform, stuff way more advanced than what I just suggested
l
I'm not sure you'll need compression on the list of points (besides, ktor should do some compression for you when sending). The main thing is taking care of different screen sizes. If you convert to a known resolution (1.0x1.0 is common since it's easy to convert to), it's easy to convert it back to the new screen's resolution, with the plus that you don't need to know the other device's resolution (think about scaling to more devices).
👀 1
p
Sorry compression is not the right term, Image processing is
f
Please @Landry Norris can you give us feedback on the outcome of when you implement @Pablichjenkov suggestions? The topics is quite interesting. We can learn from your result and all improve.
p
I insist on doing a quick look up for image processing libraries in Android
y
Just sharing the solution that I made here and worked for me. In the end I solved my problem with a simple math. I passed the percentage width and heigh instead of the actual value.
👍 1
p
Simple enough, you could have sent the full screen, and the imageview in the receiver device knows how to scale it to the new dimensions. Android ImageView has some image processing mechanisms built in for scaling up and down. But yeah that would be a waste of data over the internet when you only need a point. Cool!
🙂 1
g
https://kotlinlang.slack.com/archives/C0B8M7BUY/p1670526498841999?thread_ts=1670525514.142339&cid=C0B8M7BUY @Yacov Rosenberg there is no channel for non-kotlin related questions except #random Please check this Slack guidelines: https://kotlinlang.org/community/slackccugl.html