Skip to content

Use elm-geometry types? #30

@luca-aurelia

Description

@luca-aurelia

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions