MacCASOutreach/graphicsvg/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

GraphicSVG

A library for creating SVG graphics in a way that is compatible with Elm's old Graphics library. Also includes built-in functions for creating games and other applications including keyboard presses and mouse movements.

Basic Types

type Stencil

A primitive template representing the eventual Shape you wish to draw. This must be turned into a Shape before being drawn to the screen with collage.

type Shape notification

A filled object that can be drawn to the screen using collage.

Rendering To Screen

collage : Float -> Float -> List (Shape notification) -> Collage notification

Creates a blank canvas on which you can draw shapes. Takes a width, height and a list of Shape. Use this in your "view" functions in the three types of Apps above.

view = collage 500 500
    [ circle 10 |> filled red
    ]

Graphics App

graphicsApp : JustGraphics a -> Program Never

The simplest way to render graphics to the screen. These graphics will be static (they don't move) and you can't interact with them. Great for beginners or for when you just need basic graphics. Note that your view function is bare, with no parameters:

view = collage 500 500
    [ circle 10 |> filled red
    ]

graphicsApp takes a parameter like this: { view = view }

Notifications App

notificationsApp : GraphicsApp model msgs -> Program Never

Like graphicsApp, but you can add interactivity to your graphics by using the "notify" functions. This allows you to learn Elm's architecture in a fun way with graphics. Note that your view function needs a parameter now:

view model = collage 500 500
    [ circle 10 |> filled red
    ]

notificationApp takes a parameter like: { model = model , view = view , update = update }

Game App

type alias GetKeyState =
( Keys -> KeyState, ( Float, Float ), ( Float, Float ) )

GetKeyState returns a triple where the first argument is of type (Keys -> KeyState) so you can ask if a certain key is presses. The other two are tuples of arrow keys and WASD keys, respectively. They're in the form (x,y) which represents the key presses of each player.

gameApp : InputHandler msgs -> GraphicsApp model msgs -> Program Never

Automatically maps time and keyboard presses to your program. This should be all you need for making complex interactive games and animations.

gameApp takes two parameters: one is your own type of the form (GetKeyState -> Float -> CustomMsg) and the other is { model = model , view = view , update = update }

Shapes

line : ( Float, Float ) -> ( Float, Float ) -> Stencil

Create a line from a point to a point. Use "outlined" to convert to a viewable Shape.

polygon : List ( Float, Float ) -> Stencil

Create a closed shape given a list of points. Can use "outlined" or "filled" to convert to a Shape.

openPolygon : List ( Float, Float ) -> Stencil

Create an open shape given a list of points. Unlike with polygon, the first and last points will not join up automatically. Can use "outlined" or "filled" to convert to a Shape.

ngon : Float -> Float -> Stencil

Create a regular polygon with a given number of sides and radius. Examples:

ngon 3 50 - triangle
ngon 5 50 - pentagon
ngon 8 50 - octogon
triangle : Float -> Stencil

Synonym for "ngon 3". Creates a triangle with a given size.

square : Float -> Stencil

Creates a square with a given side length. (Synonym for rect s s)

rect : Float -> Float -> Stencil

Creates a rectangle with a given width and height.

rectangle : Float -> Float -> Stencil

Synonym for rect.

roundedRect : Float -> Float -> Float -> Stencil

Creates a rectangle with a given width, height, and circular rounded corners with the given radius.

circle : Float -> Stencil

Creates a circle with a given radius.

oval : Float -> Float -> Stencil

Creates an oval with a given width and height.

wedge : Float -> Float -> Stencil

Creates a wedge with a given radius, and a given fraction of a circle.

wedge 50 0.5 - semi-circle
wedge 50 0.25 - quarter-circle
wedge 50 0.75 - three-quarter circle
graphPaper : Float -> Shape notification

Creates a graph paper with squares of a given size.

Filling and Outlining

filled : Color -> Stencil -> Shape notification

Fill a Stencil with a Color, creating a Shape.

circle 10
    |> filled red
outlined : LineType -> Color -> Stencil -> Shape notification

Outline a Stencil with a Linetype and Color, creating a Shape.

circle 10
    |> outlined (solid 5) red
addOutline : LineType -> Color -> Shape notification -> Shape notification

Add an outline to an already-filled Shape.

circle 10
    |> filled red
    |> addOutline (solid 5) white
rgb : Float -> Float -> Float -> Color

Create a custom colour given its red, green and blue components.

rgba : Float -> Float -> Float -> Float -> Color

Create a custom colour given its red, green, blue and alpha components. Alpha is a Float from 0-1 representing the Shape's level of transparency.

hsl : Float -> Float -> Float -> Color

Create a custom colour given its hue, saturation and light components.

hsla : Float -> Float -> Float -> Float -> Color

Create a custom colour given its hue, saturation, light and alpha components. Alpha is a Float from 0-1 representing the Shape's level of transparency. lp

Grouping Shapes

group : List (Shape notification) -> Shape notification

Combine n number of Shape types into one Shape that can be transformed as one.

Curves

curve : ( Float, Float ) -> List Pull -> Stencil

Creates a curve starting at a point, pulled towards a point, ending at a third point.

curve (0,0) [Pull (0,10) (0,20)] - a curve starting at (0,0), pulled towards (0,10), ending at (0,20)

Think about curves as what you get when you take a bunch of bendy sticks with their ends glued down to a board, and then pulling each stick towards another point.

You always need an initial point and at least one Pull, but you can add as many Pulls as you want to add additional curve segments, but each curve segment can only bend one way, since it is pulled in one direction.

type Pull
= Pull ( Float, Float ) ( Float, Float )

To make it easier to read the code defining a curve, and to make sure we always use the right number of curve points and pull points (which is one more curve point than pull points), we define a special Pull type, whose first point is the point we pull towards, and second point is the end point for this curve segmentsment.

Line Styles

solid : Float -> LineType

Define a solid line type with the given width.

dotted : Float -> LineType

Define a dotted line type with the given width.

dashed : Float -> LineType

Define a dashed line type with the given width. Dashes are short line segments, versus dots which are theoretically points, but may be drawn with very sort line segments.

longdash : Float -> LineType

Define a dashed line type with the given width, where the dashes are longer than normal.

dotdash : Float -> LineType

Define a line type with the given width, including alternating dots and dashes.

increasing : Float -> Float -> Float -> LineType

A line of increasing spaces from start to end with a given thickness.

increasing 1 10 5 -- increases from 1 to 10 with a thickness of 5.
custom : List ( Float, Float ) -> Float -> LineType

A custom line defined by a list of (on,off).

custom [(10,5)] 5 -- a line that with dashes 10 long and spaces 5 long
custom [(10,5),(20,5)] -- on for 10, off 5, on 20, off 5

Text

text : String -> Stencil

Creates a text stencil. You can change this stencil using the text helper functions. Note that "filled" or "outlined" must go at the end of the infixes (ie note that all these functions are Stencil -> Stencil).

text "Hello World"
    |> bold
    |> size 14
    |> filled black
size : Float -> Stencil -> Stencil

Apply to a Stencil to render any text in the Stencil in the size given (in points), which depends on the size and type of screen used, but try 12 to start.

bold : Stencil -> Stencil

Apply to a Stencil to make any text in the Stencil bold.

italic : Stencil -> Stencil

Apply to a Stencil to make any text in the Stencil italic (ie slanted).

underline : Stencil -> Stencil

Apply to a Stencil to underline any text in the Stencil.

strikethrough : Stencil -> Stencil

Apply to a Stencil to draw a line through any text in the Stencil.

centered : Stencil -> Stencil

Apply to a Stencil to centre any text in the Stencil.

sansserif : Stencil -> Stencil

Apply to a Stencil to render any text in the Stencil with a Sans Serif font (ie one without thinner and thicker bits).

serif : Stencil -> Stencil

Apply to a Stencil to render any text in the Stencil with a Serif font (ie one with thinner and thicker bits).

fixedwidth : Stencil -> Stencil

Apply to a Stencil to render any text in the Stencil with a font in which every character has the same width so that that letters line up from line to line which is important in programming languages like Elm.

customFont : String -> Stencil -> Stencil

Use a font of your choosing by specifying its string name. Use this sparingly as support for each font will vary accross browsers and devices.

Transformations

move : ( Float, Float ) -> Shape notification -> Shape notification

Move a Shape by a number of units in x and y.

rotate : Float -> Shape notification -> Shape notification

Rotate a Shape by the specified degrees (in radians). Use the "degrees" function to convert from degrees into radians.

scale : Float -> Shape notification -> Shape notification

Scale a Shape by a given factor.

scaleX : Float -> Shape notification -> Shape notification

Scale a Shape in the x-axis by a given factor.

scaleY : Float -> Shape notification -> Shape notification

Scale a Shape in the y-axis by a given factor.

mirrorX : Shape notification -> Shape notification

Flip a Shape along the x-axis.

mirrorY : Shape notification -> Shape notification

Flip a shape along the y-axis.

Notifications

notifyTap : msgs -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification when a Shape is tapped or clicked.

notifyTapAt : (( Float, Float ) -> msgs) -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification with a tuple of position when the screen is tapped / clicked.

notifyEnter : msgs -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification when the mouse enters a Shape.

notifyEnterAt : (( Float, Float ) -> msgs) -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification with a tuple of position when mouse enters a Shape.

notifyLeave : msgs -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification when the mouse leaves a Shape.

notifyLeaveAt : (( Float, Float ) -> msgs) -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification with a tuple of position when the mouse leaves a Shape.

notifyMouseMoveAt : (( Float, Float ) -> msgs) -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification with a tuple of position when the mouse is moved accross a Shape.

notifyMouseDown : msgs -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification when the mouse button is down over a Shape.

notifyMouseDownAt : (( Float, Float ) -> msgs) -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification with a tuple of position when the mouse button is down over a Shape.

notifyMouseUp : msgs -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification when the mouse button goes up over a Shape.

notifyMouseUpAt : (( Float, Float ) -> msgs) -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification with a tuple of position when the mouse goes up over a Shape.

notifyTouchStart : msgs -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification when the user begins touching a Shape.

notifyTouchStartAt : (( Float, Float ) -> msgs) -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification with a tuple of position when the user begins touching a Shape.

notifyTouchEnd : msgs -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification when the user stops touching a Shape.

notifyTouchEndAt : (( Float, Float ) -> msgs) -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification with a tuple of position when the user stops touching a Shape.

notifyTouchMoveAt : (( Float, Float ) -> msgs) -> Shape (Msg msgs) -> Shape (Msg msgs)

Receive a notification with a tuple of position when the moves their finger over a Shape.

Miscallaneous

makeTransparent : Float -> Shape notification -> Shape notification

Make a Shape transparent by the fraction given. Multiplies on top of other transparencies:

circle 10
    |> filled red
    |> makeTransparent 0.5
--results in a transparency of 0.5 (half vislible)


circle 10
    |> filled red
    |> makeTransparent 0.5
    |> makeTransparent 0.5
--results in a transparency of 0.25 (a quarter visible)

Let there be colours!

black : Color
blank : Color
blue : Color
brown : Color
charcoal : Color
darkBlue : Color
darkBrown : Color
darkCharcoal : Color
darkGray : Color
darkGreen : Color
darkGrey : Color
darkOrange : Color
darkPurple : Color
darkRed : Color
darkYellow : Color
gray : Color
green : Color
grey : Color
hotPink : Color
lightBlue : Color
lightBrown : Color
lightGray : Color
lightGreen : Color
lightGrey : Color
lightOrange : Color
lightPurple : Color
lightRed : Color
lightYellow : Color
orange : Color
pink : Color
purple : Color
red : Color
white : Color
yellow : Color