beeware / toga

A Python native, OS native GUI toolkit.
https://toga.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
4.23k stars 654 forks source link

Canvas transformation bugs on WinForms and Android #2206

Open mhsmith opened 8 months ago

mhsmith commented 8 months ago

Describe the bug

The HTML spec says that points "must be transformed according to the current transformation matrix before being added to the path".

Toga does this correctly on Cocoa and GTK, but on WinForms and Android it doesn't apply the matrix until the path is actually drawn, so the entire path is transformed with whatever happens to be the matrix at that moment.

```js ctx.lineWidth = 2 ctx.moveTo(40, 20) ctx.lineTo(80, 20) ctx.rotate(Math.PI / 4) ctx.lineTo(180, 20) ctx.stroke() ``` ```py ctx.move_to(40, 20) ctx.line_to(80, 20) ctx.rotate(math.pi / 4) ctx.line_to(180, 20) ctx.stroke() ```

HTML (https://jsfiddle.net/mhsmith/9ewqnvz5/14/), and Toga on Cocoa and GTK:

Screenshot 2023-11-08 at 12 53 40

Toga on WinForms and Android:

Screenshot 2023-11-08 at 12 28 03

Environment

mhsmith commented 8 months ago

The transformation should also apply to the stroke width. This is happening on Cocoa, GTK and Android, but not on WinForms.

This cannot be done simply by multiplying the width by a constant. For example, if there are different scale factors in the X and Y dimensions, the stroke width of a circle will change continuously around its circumference.

```js ctx.lineWidth = 2 ctx.moveTo(0, 25) ctx.lineTo(50, 25) ctx.moveTo(25, 0) ctx.lineTo(25, 50) ctx.scale(3, 1) ctx.stroke() ``` ```py ctx.move_to(0, 25) ctx.line_to(50, 25) ctx.move_to(25, 0) ctx.line_to(25, 50) ctx.scale(3, 1) ctx.stroke() ```

HTML (https://jsfiddle.net/mhsmith/21ehgadk/9/), and Toga on Cocoa and GTK:

Screenshot 2023-11-08 at 12 37 35

Toga on Android:

Screenshot 2023-11-08 at 12 38 21

Toga on WinForms:

Screenshot 2023-11-08 at 12 45 39