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

Nothing gets drawn when using Path #54

Open offizium-berndstorath opened 2 months ago

offizium-berndstorath commented 2 months 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.

I am trying to create a rectangle for ui-chart. Where top left and top right corners are rounded. I found a solution on stackoverflow https://stackoverflow.com/questions/5896234/how-to-use-android-canvas-to-draw-a-rectangle-with-only-topleft-and-topright-cor The problem i am facing it just doesnt draw anything. When drawing a normal rectangle or when drawing a rounded rectangle everything looks fine. But with the path solution as suggestion on so nothing gets drawn

Is there any code involved?

chart.customRenderer = {
  drawBar(c, e, dataSet, left, top, right, bottom, paint) {
    let delta = 0;
    if (!e.last) {
      delta+=9;
    }
    const path = new Path();
    const corners = [
      5, 5,        // Top left radius in px
      5, 5,        // Top right radius in px
      0, 0,          // Bottom right radius in px
      0, 0           // Bottom left radius in px
    ]
    const rec = createRect(left+delta, 100-(bottom-top), 9, bottom-top);
    path.addRoundRect(rec, corners, Direction.CW)
    c.drawPath(path, paint);
  }
}
offizium-berndstorath commented 2 months ago

When using the second solution i get the error

Method not implemented: rLineTo
offizium-berndstorath commented 2 months ago

Using this as a workaround

chart.customRenderer = {
  drawBar(c, e, dataSet, left, top, right, bottom, paint) {
    let delta = 0;
    if (!e.last) {
      delta+=7;
    }
    const rect = createRect(left+delta, 100-(bottom-top), 9, bottom-top);
    c.drawRoundRect(rect, 4, 4, paint);
    c.drawRect(rect.left, rect.top + 4, rect.right, rect.bottom, paint)
  }
}

It draws a a rounded rect and then a normal on top. But this doesnt work with alpha colors

farfromrefug commented 2 months ago

@offizium-berndstorath indeed there is a issue with the addRoundRect you used. you can use that signature instead:

public void addRoundRect (float left, 
                float top, 
                float right, 
                float bottom, 
                float rx, 
                float ry, 
               Path.Direction dir)
offizium-berndstorath commented 2 months ago

Using

const rect = createRect(left+delta, 100-(bottom-top), 9, bottom-top);
path.addRoundRect(rect.left, rect.top, rect.right, rect.bottom, 5, 5, Direction.CW)
c.drawPath(path, paint);

crashes to app without showing a error message. And i suspect because the parameters are only rx,ry like canvas.drawRoundRect this would round all corners

farfromrefug commented 2 months ago

@offizium-berndstorath indeed it is not the signature you want (sorry). Now i misread your issue and thought it was on Android :s On iOS for now i dont have a way to create a rounded rectangle with different radii. And i am not on macos right now. Will look at it later