Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use elm-geometry types? #30

Open
luca-aurelia opened this issue Jan 3, 2021 · 1 comment
Open

Use elm-geometry types? #30

luca-aurelia opened this issue Jan 3, 2021 · 1 comment

Comments

@luca-aurelia
Copy link

Hi @joakin! Thanks for all the great work you've done on this package.

Recently I've been playing with elm-canvas and elm-geometry. They work really nicely together. elm-geometry has a lot of useful functions that help you do things like find the center of a rectangle, or work with vectors. And of course, elm-canvas has a great API for drawing shapes.

My one problem is that it requires a lot of code to convert elm-geometry types to elm-canvas types.

For example, elm-canvas's, Canvas.circle gets called like this:

center = ( 100, 100 )
radius = 25
Canvas.circle center radius

But if I want to use elm-geometry types, I have to do this:

-- Define a helper function to convert elm-geometry types to something that elm-canvas understands.
circle : Quantity Int Pixels -> Point2d Pixels CanvasCoordinates -> Canvas.Shape
circle radius center =
    let
        -- Convert our Point2d [1] to a tuple like ( x, y ).
        centerTuple =
            Point2d.toTuple Pixels.inPixels center

        -- Now convert radius to a Float. It starts as an instance of the `Quantity Pixels`
        -- type [2], which is how elm-geometry does type-safe math and unit conversions.
        radiusInPixels =
            radius
                |> Pixels.toInt
                |> toFloat
    in
    Canvas.circle
        centerTuple
        radiusInPixels

[1]: Here are the Point2d docs.
[2]: And here are the Quantity docs.

Thanks again for all your hard work! I really like using this library, and I think that adding support for elm-geometry types would make it even more fun and enjoyable.

@joakin
Copy link
Owner

joakin commented Sep 16, 2021

I think that would be wonderful, but usually that kind of library mixins happen as a new package in the Elm ecosystem. So for example you could create the package elm-canvas-geometry or the other way around, and publish an elm-geometry friendly interface to use this drawing lib.

Due to this library needing some manual JS installation it would be a bit clunkier than with others like elm-css or elm-accesible-html etc, but it is definitely something that is often done.

For this library I'd rather keep the barrier to entry lower, elm-geometry is awesome, but it definitely creates some more ceremony and boilerplate when dealing with numbers (because it forces you to think about what they are and how to mix them, which is a good thing, but also adds cognitive cost to the drawing).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants