flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
11.3k stars 438 forks source link

rotation property not implemented in PaintGradients #1505

Closed BC-clegras closed 1 year ago

BC-clegras commented 1 year ago

Description

The Flet canvas documentation mentions the rotation property for PaintLinearGradient, PaintRadialGradient, and PaintSweepGradient. However, when trying to use that property, the code errors out with an unexpected keyboard error for rotation.

Code example to reproduce the issue:

# Create the paint for the gauge
gauge_stroke_paint = ft.Paint(
    gradient=ft.PaintSweepGradient(
        (50, 50),
        colors=[ft.colors.YELLOW, ft.colors.PURPLE],
        color_stops=[0.0, 1.0],
        start_angle=0,
        end_angle=2 * pi,
        rotation=3 * pi / 4, # This line creates the issue
    ),
    stroke_width=15,
    stroke_join=ft.StrokeJoin.ROUND,
    style=ft.PaintingStyle.STROKE,
)
# Create the gauge
myNewGauge = NewGauge(
    min_angle=3 * pi / 4,
    sweep_angle=3 * pi / 2,
    x=0,
    y=0,
    width=100,
    height=100,
    paint=gauge_stroke_paint,
)
# Add the gauge to the canvas (which is in a Stack)
self.canvas.add_gauge(myNewGauge)
self.update()

Describe the results you received:

image

Describe the results you expected:

I am trying to make a gauge with a gradient with part of the arc missing. The arc looks like this: image

As you can see, the gradient abruptly stops at 2 * pi and continues as the initial color for the rest of the arc. In flutter, I know you can make the gradient's end_angle greater than 2 * pi to solve this issue (source), or use transform: GradientRotation() (source), but neither of those work currently work in Flet.

I want the gradient to go all the way to the end of my gauge (pictured at what it's maximum value would be) instead of the abrupt stop I currently have. I was hoping to use the rotation property to rotate the gradient and solve my issue.

Additional information you deem important (e.g. issue happens only occasionally):

Flet version (pip show flet):

Name: flet
Version: 0.7.4
Summary: Flet for Python - easily build interactive multi-platform apps in Python
Home-page:
Author: Appveyor Systems Inc.
Author-email: hello@flet.dev
License: Apache-2.0
Location: c:\users\clegras\appdata\local\programs\python\python310\lib\site-packages
Requires: flet-core, httpx, oauthlib, packaging, watchdog, websocket-client, websockets
Required-by:

Operating system:

Windows 10 22H2 Additional environment details: Running the application from VS Code 1.79.2

FeodorFitsner commented 1 year ago

Gradient shader from UI is different from the one in Painting. Its constructors don't have "rotation", but they have matrix4 transformation. We can do rotation through matrix. Let's call this issue a bug.

FeodorFitsner commented 1 year ago

Added rotation to sweep gradient.

Result:

image

Code:

import math

import flet as ft
import flet.canvas as cv

def main(page: ft.Page):
    cp = cv.Canvas(
        [
            cv.Rect(
                10,
                10,
                100,
                100,
                5,
                ft.Paint(
                    gradient=ft.PaintLinearGradient(
                        (0, 10),
                        (100, 50),
                        colors=[ft.colors.BLUE, ft.colors.YELLOW],
                        # rotation=math.pi / 2,
                    ),
                    style=ft.PaintingStyle.FILL,
                ),
            ),
            cv.Circle(
                60,
                170,
                50,
                ft.Paint(
                    gradient=ft.PaintRadialGradient(
                        (60, 170), 50, colors=[ft.colors.YELLOW, ft.colors.BLUE]
                    ),
                    style=ft.PaintingStyle.FILL,
                ),
            ),
            cv.Path(
                [cv.Path.Arc(10, 230, 100, 100, 3 * math.pi / 4, 3 * math.pi / 2)],
                ft.Paint(
                    gradient=ft.PaintSweepGradient(
                        (60, 280),
                        colors=[ft.colors.YELLOW, ft.colors.PURPLE],
                        color_stops=[0.0, 1.0],
                        start_angle=0,
                        end_angle=math.pi * 2,
                        rotation=3 * math.pi / 4,
                    ),
                    stroke_width=15,
                    stroke_join=ft.StrokeJoin.ROUND,
                    style=ft.PaintingStyle.STROKE,
                ),
            ),
        ],
        width=float("inf"),
        expand=True,
    )

    page.add(cp)

ft.app(main)
BC-clegras commented 1 year ago

Awesome, thanks a lot for getting it done so quickly! How can I get this running on my machine currently? I assume the package isn't pushed to pip instantly, but can I just copy over the files changed in the commit to my computer?

FeodorFitsner commented 1 year ago

That will be merged later today and be available as "dev" package.

FeodorFitsner commented 1 year ago

You can try it with https://pypi.org/project/flet/0.8.0.dev1590/