elm-lang/core/2.0.1

NOTE: this package moved to elm/core

NOTE: this package is not compatible with Elm 0.19.1

Graphics.Element

Graphical elements that snap together to build complex widgets and layouts. Each Element is a rectangle with a known width and height, making them easy to combine and position.

Show Anything

show : a -> Element

Convert anything to its textual representation and make it displayable in the browser. Excellent for debugging.

main : Element
main =
  show "Hello World!"


show value =
    leftAligned (Text.monospace (Text.fromString (toString value)))

Images

image : Int -> Int -> String -> Element

Create an image given a width, height, and image source.

fittedImage : Int -> Int -> String -> Element

Create a fitted image given a width, height, and image source. This will crop the picture to best fill the given dimensions.

croppedImage : ( Int, Int ) -> Int -> Int -> String -> Element

Create a cropped image. Take a rectangle out of the picture starting at the given top left coordinate. If you have a 140-by-140 image, the following will cut a 100-by-100 square out of the middle of it.

croppedImage (20,20) 100 100 "yogi.jpg"
tiledImage : Int -> Int -> String -> Element

Text

Each of the following functions places Text into a box. The function you use determines the alignment of the text.

leftAligned : Text -> Element

Align text along the left side of the text block. This is sometimes known as ragged right.

rightAligned : Text -> Element

Align text along the right side of the text block. This is sometimes known as ragged left.

centered : Text -> Element

Center text in the text block. There is equal spacing on either side of a line of text.

justified : Text -> Element

Align text along the left and right sides of the text block. Word spacing is adjusted to make this possible.

Styling

width : Int -> Element -> Element

Create an Element with a given width.

height : Int -> Element -> Element

Create an Element with a given height.

size : Int -> Int -> Element -> Element

Create an Element with a new width and height.

color : Color -> Element -> Element

Create an Element with a given background color.

opacity : Float -> Element -> Element

Create an Element with a given opacity. Opacity is a number between 0 and 1 where 0 means totally clear.

tag : String -> Element -> Element

Create an Element with a tag. This lets you link directly to it. The element (tag "all-about-badgers" thirdParagraph) can be reached with a link like this: /facts-about-animals.elm#all-about-badgers

Inspection

widthOf : Element -> Int

Get the width of an Element

heightOf : Element -> Int

Get the height of an Element

sizeOf : Element -> ( Int, Int )

Get the width and height of an Element

Layout

flow : Direction -> List Element -> Element

Have a list of elements flow in a particular direction. The Direction starts from the first element in the list.

flow right [a,b,c]


    +---+---+---+
    | a | b | c |
    +---+---+---+
up : Direction
down : Direction
left : Direction
inward : Direction
outward : Direction

Layout Aliases

There are also some convenience functions for working with flow in specific cases:

layers : List Element -> Element

Layer elements on top of each other, starting from the bottom: layers == flow outward

above : Element -> Element -> Element

Stack elements vertically. To put a above b you would say: a `above` b

below : Element -> Element -> Element

Stack elements vertically. To put a below b you would say: a `below` b

beside : Element -> Element -> Element

Put elements beside each other horizontally. To put a beside b you would say: a `beside` b

Positioning

empty : Element

An Element that takes up no space. Good for things that appear conditionally:

flow down [ img1, if showMore then img2 else empty ]
spacer : Int -> Int -> Element

Create an empty box. This is useful for getting your spacing right and for making borders.

container : Int -> Int -> Position -> Element -> Element

Put an element in a container. This lets you position the element really easily, and there are tons of ways to set the Position. To center element exactly in a 300-by-300 square you would say:

container 300 300 middle element

By setting the color of the container, you can create borders.

Specific Positions

To create a Position you can use any of the built-in positions which cover nine common positions.

middle : Position
midTop : Position
midBottom : Position
midLeft : Position
midRight : Position
topLeft : Position
topRight : Position
bottomLeft : Position
bottomRight : Position

If you need more precision, you can create custom positions.

absolute : Int -> Pos
relative : Float -> Pos
middleAt : Pos -> Pos -> Position
midTopAt : Pos -> Pos -> Position
midBottomAt : Pos -> Pos -> Position
midLeftAt : Pos -> Pos -> Position
midRightAt : Pos -> Pos -> Position
topLeftAt : Pos -> Pos -> Position
topRightAt : Pos -> Pos -> Position
bottomLeftAt : Pos -> Pos -> Position
bottomRightAt : Pos -> Pos -> Position