nativescript-community / ui-canvas

Implement Canvas into your NativeScript apps.
https://nativescript-community.github.io/ui-canvas/
Apache License 2.0
31 stars 9 forks source link

Android draw size - Pixel ratio #2

Closed otis78 closed 5 years ago

otis78 commented 5 years ago

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:

Please, tell us how to recreate the issue in as much detail as possible.

This si my onDraw code:

`onDraw(event: { canvas: Canvas }) {
    const canvas = <Canvas>event.canvas;

    const bgPaint = new Paint();
    bgPaint.setColor(new Color(255, 255, 0, 0));
    bgPaint.strokeWidth = 10;
    canvas.drawRect(this.createRect(0, 0, 99 , 99 ), bgPaint);
    bgPaint.setStyle(Style.STROKE);
    bgPaint.setColor(new Color(255, 0, 255, 0));
    canvas.drawRect(this.createRect(10, 10, 80, 80), bgPaint);
}`

Running the App in both emulator you can se the diffent size in Android. As a reference I draw below of the CanvasView a simple label with the expected size (100x100dp). Se the attached screenshot.

thanks

screenshot 2019-02-25 at 10 20 06
otis78 commented 5 years ago

I think this issue is due to the pixel ratio. I logged the canvas size and i see that the the real size in pixel in my case is 263x263px equivalent to 100x100dp. I am drawing expecting that the conversion from dp to pixel is done by your plugin but to have the correct draw size I have to multiply every value for the display density (utils.layout.getDisplayDensity()). Is this the expected behavior?

farfromrefug commented 5 years ago

@otis78 thanks for the example. Will fix this today. What is the android simulated device?

otis78 commented 5 years ago

It is a Pixel 2 API 27

farfromrefug commented 5 years ago

@otis78 btw can you share the full sample app? or at list the page template

otis78 commented 5 years ago

Sure, see the attachment demo-angular.zip

farfromrefug commented 5 years ago

@otis78 should be fixed in 1.0.4

otis78 commented 5 years ago

Thanks @farfromrefug. It works. The only difference about this issue are the different values returned by the canvas methods getWidth and getHeight. iOS returns the values in dp (in my example 100x100). Android returns the values in pixels that in my example is 263x263. It would be better to uniform this values. What do you think about this?

farfromrefug commented 5 years ago

@otis78 will do

farfromrefug commented 5 years ago

@otis78 should be fixed in 1.0.5

otis78 commented 5 years ago

Hi @farfromrefug. There are a lot of decimals but it works.

farfromrefug commented 5 years ago

@otis78 hadn't any here. Will look at one point