2

How can I change the origin of a Canvas to the lower-left corner instead of the default top-left corner?

void draw(SkCanvas* canvas) {
  SkPaint p;
  p.setColor(SK_ColorRED);
  p.setAntiAlias(true);
  p.setStyle(SkPaint::kStroke_Style);
  p.setStrokeWidth(10);

  canvas->drawLine(200, 20, 100, 100, p);
}

https://fiddle.skia.org/c/e5fe08a701838b078c0ec6c98ef052c8

Vahid
  • 5,144
  • 13
  • 70
  • 146
  • 1
    Is this really C# code (as it is tagged as `c#`)? Looks more like C or C++ to me. – Uwe Keim Jun 06 '19 at 06:46
  • 2
    Use Scale(1, -1) to invert the Y-axis, Translate(0, height) to move the origin. – Hans Passant Jun 06 '19 at 07:46
  • Do not use canvas->scale(1, -1); that will flip text upside down. To invert the y-axis in skia, I was not able to find a better method than manually adding a negative sign to every y coordinate. – Kevin Yin Jan 30 '20 at 22:16

2 Answers2

1

While I do not believe you can change the origin, You can use the bounds of the canvas to get the desired points.

void draw(SkCanvas* canvas) {
    SkPaint p;
    p.setColor(SK_ColorRED);
    p.setAntiAlias(true);
    p.setStyle(SkPaint::kStroke_Style);
    p.setStrokeWidth(10);
    SkRect bounds = canvas->getLocalClipBounds();
    canvas->drawLine(bounds.fLeft + 200, bounds.fBottom - 20, bounds.fLeft + 100, bounds.fBottom - 100, p);
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
1

You can get the height and width from canvas, then subtracting from it, we can get the drawing looks like origin changed to lower left corner.

Try this code

void draw(SkCanvas* canvas) {
    SkPaint p;

    p.setColor(SK_ColorRED);
    p.setAntiAlias(true);
    p.setStyle(SkPaint::kStroke_Style);
    p.setStrokeWidth(10);

    canvas->drawLine(200, canvas->getDeviceClipBounds().height()-20, 100, canvas->getDeviceClipBounds().height()-100, p);
}
Surenthar Pitchai
  • 1,231
  • 15
  • 18