Module

Graphics.Canvas

This module defines foreign types and functions for working with the 2D Canvas API.

#CanvasElement

data CanvasElement :: Type

A canvas HTML element.

#Context2D

data Context2D :: Type

A 2D graphics context.

#ImageData

data ImageData :: Type

An image data object, used to store raster data outside the canvas.

#CanvasImageSource

data CanvasImageSource :: Type

Opaque object for drawing elements and things to the canvas.

#Arc

type Arc = { end :: Number, radius :: Number, start :: Number, x :: Number, y :: Number }

A type representing an arc object:

  • The center coordinates x and y,
  • The radius r,
  • The starting and ending angles, start and end.

#Dimensions

type Dimensions = { height :: Number, width :: Number }

Canvas dimensions (width and height) in pixels.

#LineCap

data LineCap

Enumerates the different types of line cap.

Constructors

Instances

#LineJoin

data LineJoin

Enumerates the different types of line join

Constructors

Instances

#Rectangle

type Rectangle = { height :: Number, width :: Number, x :: Number, y :: Number }

A type representing a rectangle object:

  • The top-left corner coordinates x and y,
  • The width and height w and h.

#ScaleTransform

type ScaleTransform = { scaleX :: Number, scaleY :: Number }

An object representing a scaling transform:

  • The scale factors in the x and y directions, scaleX and scaleY.

#TextMetrics

type TextMetrics = { width :: Number }

Text metrics:

  • The text width in pixels.

#Transform

type Transform = { m11 :: Number, m12 :: Number, m21 :: Number, m22 :: Number, m31 :: Number, m32 :: Number }

An object representing a general transformation as a homogeneous matrix.

#TranslateTransform

type TranslateTransform = { translateX :: Number, translateY :: Number }

An object representing a translation:

  • The translation amounts in the x and y directions, translateX and translateY.

#TextAlign

data TextAlign

Enumerates types of text alignment.

Constructors

Instances

#CanvasPattern

data CanvasPattern :: Type

Opaque object describing a pattern.

#PatternRepeat

data PatternRepeat

Enumerates the different types of pattern repetitions.

Constructors

Instances

#CanvasGradient

data CanvasGradient :: Type

Opaque object describing a gradient.

#LinearGradient

type LinearGradient = { x0 :: Number, x1 :: Number, y0 :: Number, y1 :: Number }

A type representing a linear gradient.

  • Starting point coordinates: (x0, y0)
  • Ending point coordinates: (x1, y1)

#RadialGradient

type RadialGradient = { r0 :: Number, r1 :: Number, x0 :: Number, x1 :: Number, y0 :: Number, y1 :: Number }

A type representing a radial gradient.

  • Starting circle center coordinates: (x0, y0)
  • Starting circle radius: r0
  • Ending circle center coordinates: (x1, y1)
  • Ending circle radius: r1

#QuadraticCurve

type QuadraticCurve = { cpx :: Number, cpy :: Number, x :: Number, y :: Number }

A type representing a quadratic Bézier curve.

  • Bézier control point: (cpx, cpy)
  • Ending point coordinates: (x, y)

#BezierCurve

type BezierCurve = { cp1x :: Number, cp1y :: Number, cp2x :: Number, cp2y :: Number, x :: Number, y :: Number }

A type representing a cubic Bézier curve.

  • First Bézier control point: (cp1x, cp1y)
  • Second Bézier control point: (cp2x, cp2y)
  • Ending point: (x, y)

#getCanvasElementById

getCanvasElementById :: String -> Effect (Maybe CanvasElement)

Get a canvas element by ID, or Nothing if the element does not exist.

#getContext2D

getContext2D :: CanvasElement -> Effect Context2D

Get the 2D graphics context for a canvas element.

#getCanvasWidth

getCanvasWidth :: CanvasElement -> Effect Number

Get the canvas width in pixels.

#setCanvasWidth

setCanvasWidth :: CanvasElement -> Number -> Effect Unit

Set the canvas width in pixels.

#getCanvasHeight

getCanvasHeight :: CanvasElement -> Effect Number

Get the canvas height in pixels.

#setCanvasHeight

setCanvasHeight :: CanvasElement -> Number -> Effect Unit

Set the canvas height in pixels.

#getCanvasDimensions

getCanvasDimensions :: CanvasElement -> Effect Dimensions

Get the canvas dimensions in pixels.

#setCanvasDimensions

setCanvasDimensions :: CanvasElement -> Dimensions -> Effect Unit

Set the canvas dimensions in pixels.

#canvasToDataURL

canvasToDataURL :: CanvasElement -> Effect String

Create a data URL for the current canvas contents

#setLineWidth

setLineWidth :: Context2D -> Number -> Effect Unit

Set the current line width.

#setLineDash

setLineDash :: Context2D -> Array Number -> Effect Unit

Set the current line dash pattern.

#setFillStyle

setFillStyle :: Context2D -> String -> Effect Unit

Set the current fill style/color.

#setStrokeStyle

setStrokeStyle :: Context2D -> String -> Effect Unit

Set the current stroke style/color.

#setShadowBlur

setShadowBlur :: Context2D -> Number -> Effect Unit

Set the current shadow blur radius.

#setShadowOffsetX

setShadowOffsetX :: Context2D -> Number -> Effect Unit

Set the current shadow x-offset.

#setShadowOffsetY

setShadowOffsetY :: Context2D -> Number -> Effect Unit

Set the current shadow y-offset.

#setShadowColor

setShadowColor :: Context2D -> String -> Effect Unit

Set the current shadow color.

#setMiterLimit

setMiterLimit :: Context2D -> Number -> Effect Unit

Set the current miter limit.

#setLineCap

setLineCap :: Context2D -> LineCap -> Effect Unit

Set the current line cap type.

#setLineJoin

setLineJoin :: Context2D -> LineJoin -> Effect Unit

Set the current line join type.

#setGlobalCompositeOperation

setGlobalCompositeOperation :: Context2D -> Composite -> Effect Unit

Set the current composite operation.

#setGlobalAlpha

setGlobalAlpha :: Context2D -> Number -> Effect Unit

Set the current global alpha level.

#beginPath

beginPath :: Context2D -> Effect Unit

Begin a path object.

#stroke

stroke :: Context2D -> Effect Unit

Stroke the current object.

#fill

fill :: Context2D -> Effect Unit

Fill the current object.

#clip

clip :: Context2D -> Effect Unit

Clip to the current object.

#lineTo

lineTo :: Context2D -> Number -> Number -> Effect Unit

Move the path to the specified coordinates, drawing a line segment.

#moveTo

moveTo :: Context2D -> Number -> Number -> Effect Unit

Move the path to the specified coordinates, without drawing a line segment.

#closePath

closePath :: Context2D -> Effect Unit

Close the current path.

#strokePath

strokePath :: forall a. Context2D -> Effect a -> Effect a

A convenience function for drawing a stroked path.

For example:

strokePath ctx $ do
  moveTo ctx 10.0 10.0
  lineTo ctx 20.0 20.0
  lineTo ctx 10.0 20.0
  closePath ctx

#fillPath

fillPath :: forall a. Context2D -> Effect a -> Effect a

A convenience function for drawing a filled path.

For example:

fillPath ctx $ do
  moveTo ctx 10.0 10.0
  lineTo ctx 20.0 20.0
  lineTo ctx 10.0 20.0
  closePath ctx

#arc

arc :: Context2D -> Arc -> Effect Unit

Render an arc object.

#rect

rect :: Context2D -> Rectangle -> Effect Unit

Render a rectangle.

#fillRect

fillRect :: Context2D -> Rectangle -> Effect Unit

Fill a rectangle.

#strokeRect

strokeRect :: Context2D -> Rectangle -> Effect Unit

Stroke a rectangle.

#clearRect

clearRect :: Context2D -> Rectangle -> Effect Unit

Clear a rectangle.

#scale

scale :: Context2D -> ScaleTransform -> Effect Unit

Apply a scaling transform.

#rotate

rotate :: Context2D -> Number -> Effect Unit

Apply a rotation.

#translate

translate :: Context2D -> TranslateTransform -> Effect Unit

Apply a translation

#transform

transform :: Context2D -> Transform -> Effect Unit

Apply a general transformation to the current transformation matrix

#setTransform

setTransform :: Context2D -> Transform -> Effect Unit

Set the transformation matrix

#textAlign

textAlign :: Context2D -> Effect TextAlign

Get the current text alignment.

#setTextAlign

setTextAlign :: Context2D -> TextAlign -> Effect Unit

Set the current text alignment.

#textBaseline

textBaseline :: Context2D -> Effect TextBaseline

Get the current text baseline.

#setTextBaseline

setTextBaseline :: Context2D -> TextBaseline -> Effect Unit

Set the current text baseline.

#font

font :: Context2D -> Effect String

Get the current font.

#setFont

setFont :: Context2D -> String -> Effect Unit

Set the current font.

#fillText

fillText :: Context2D -> String -> Number -> Number -> Effect Unit

Fill some text.

#strokeText

strokeText :: Context2D -> String -> Number -> Number -> Effect Unit

Stroke some text.

#measureText

measureText :: Context2D -> String -> Effect TextMetrics

Measure some text.

#save

save :: Context2D -> Effect Unit

Save the current context.

#restore

restore :: Context2D -> Effect Unit

Restore the previous context.

#withContext

withContext :: forall a. Context2D -> Effect a -> Effect a

A convenience function: run the action, preserving the existing context.

For example, outside this block, the fill style is preseved:

withContext ctx $ do
  setFillStyle ctx "red"
  ...

#tryLoadImage

tryLoadImage :: String -> (Maybe CanvasImageSource -> Effect Unit) -> Effect Unit

Asynchronously load an image file by specifying its path.

#getImageData

getImageData :: Context2D -> Number -> Number -> Number -> Number -> Effect ImageData

Get image data for a portion of the canvas.

#putImageData

putImageData :: Context2D -> ImageData -> Number -> Number -> Effect Unit

Set image data for a portion of the canvas.

#putImageDataFull

putImageDataFull :: Context2D -> ImageData -> Number -> Number -> Number -> Number -> Number -> Number -> Effect Unit

Set image data for a portion of the canvas.

#createImageData

createImageData :: Context2D -> Number -> Number -> Effect ImageData

Create an image data object.

#createImageDataCopy

createImageDataCopy :: Context2D -> ImageData -> Effect ImageData

Create a copy of an image data object.

#imageDataWidth

imageDataWidth :: ImageData -> Int

Get the width of an ImageData object.

#imageDataHeight

imageDataHeight :: ImageData -> Int

Get the height of an ImageData object.

#imageDataBuffer

imageDataBuffer :: ImageData -> Uint8ClampedArray

Get the underlying buffer from an ImageData object.

#createPattern

createPattern :: Context2D -> CanvasImageSource -> PatternRepeat -> Effect CanvasPattern

Create a new canvas pattern (repeatable image).

#setPatternFillStyle

setPatternFillStyle :: Context2D -> CanvasPattern -> Effect Unit

Set the Context2D fillstyle to the CanvasPattern.

#createLinearGradient

createLinearGradient :: Context2D -> LinearGradient -> Effect CanvasGradient

Create a linear CanvasGradient.

#createRadialGradient

createRadialGradient :: Context2D -> RadialGradient -> Effect CanvasGradient

Create a radial CanvasGradient.

#addColorStop

addColorStop :: CanvasGradient -> Number -> String -> Effect Unit

Add a single color stop to a CanvasGradient.

#setGradientFillStyle

setGradientFillStyle :: Context2D -> CanvasGradient -> Effect Unit

Set the Context2D fillstyle to the CanvasGradient.

#quadraticCurveTo

quadraticCurveTo :: Context2D -> QuadraticCurve -> Effect Unit

Draw a quadratic Bézier curve.

#bezierCurveTo

bezierCurveTo :: Context2D -> BezierCurve -> Effect Unit

Draw a cubic Bézier curve.

Modules