Graphics.CanvasAction
- Package
- purescript-canvas-action
- Repository
- artemisSystem/purescript-canvas-action
This module defines all functions from Graphics.Canvas
(and some extra)
as functions without the Context2D
parameter, and in the CanvasActionM
monad instead of the Effect
monad. This is to make canvas actions easily
composable without having to worry about passing the Context2D
to every
single function. They can be composed using do
notation / bind or
applicative composition (<*>
, <*
, and *>
). Semigroup composition
(<>
) also works if the return types are the same and also a Semigroup
(this includes Unit
). It also has a MonadRec
instance. Functions from
this module can be used with any MonadCanvasActionM
, which is a type
class with instances for monads which support canvas actions. Instances are
provided for CanvasActionM
, Run
, and the standard monad transformers.
#SkewTransform Source
type SkewTransform = { skewX :: Number, skewY :: Number }
Type synonym for skew transformations.
#TextBaseline Source
#CanvasStyle Source
data CanvasStyle :: Type
A value that can be passed to setFillStyle
and similar functions.
Runtime representation should be either a String
, a CanvasGradient
or
a CanvasPattern
.
Instances
#styleIsString Source
styleIsString :: CanvasStyle -> Boolean
#styleIsPattern Source
styleIsPattern :: CanvasStyle -> Boolean
#styleToString Source
styleToString :: CanvasStyle -> Maybe String
#styleToString' Source
styleToString' :: Partial => CanvasStyle -> String
#styleToGradient' Source
styleToGradient' :: Partial => CanvasStyle -> CanvasGradient
#styleToPattern' Source
styleToPattern' :: Partial => CanvasStyle -> CanvasPattern
#CanvasStyleRep Source
class CanvasStyleRep rep where
Class describing types that can be turned into a valid CanvasStyle
for
use with setFillStyle
and similar functions. This way, there is no
need for functions like setPatternFillStyle
, and values of types like
Color
can easily used as a fillStyle without problem.
Members
toStyle :: rep -> CanvasStyle
Instances
#CanvasColorRep Source
class CanvasColorRep rep where
Class describing types that can be turned into a string representing a canvas color.
Members
Instances
#createCanvas Source
createCanvas :: forall s m. MonadEffect m => ToSize Number s => s -> m CanvasElement
Create a CanvasElement
of the given size in any MonadCanvasAction
#createCanvas' Source
createCanvas' :: forall s m. MonadEffect m => ToSize Number s => Document -> s -> m CanvasElement
Same as createCanvas
, but allows for specifying the Document
object to
create the canvas with
#createCanvasEffect Source
createCanvasEffect :: forall s. ToSize Number s => s -> Effect CanvasElement
Create a CanvasElement
of the given size in the Effect
monad
#createCanvasEffect' Source
createCanvasEffect' :: forall s. ToSize Number s => Document -> s -> Effect CanvasElement
Same as createCanvasEffect
, but allows for specifying the Document
object to create the canvas with
#getCanvasElementById Source
getCanvasElementById :: forall m. MonadEffect m => String -> m (Maybe CanvasElement)
#querySelectCanvas Source
querySelectCanvas :: forall m. MonadEffect m => QuerySelector -> m (Maybe CanvasElement)
#getContext2D Source
getContext2D :: forall m. MonadEffect m => CanvasElement -> m Context2D
#getContext2DById Source
getContext2DById :: forall m. MonadEffect m => String -> m (Maybe Context2D)
#querySelectContext2D Source
querySelectContext2D :: forall m. MonadEffect m => QuerySelector -> m (Maybe Context2D)
#runActionOffscreen Source
runActionOffscreen :: forall s a m. MonadEffect m => ToSize Number s => s -> CanvasActionM a -> m a
Run a CanvasActionM
in a MonadEffect
, on a created canvas with the
provided size. This can be useful for creating patterns for use as a
fillStyle or strokeStyle.
For example:
action ∷ CanvasAction
action = do
pattern ← runActionOffscreen (20.0 >< 20.0) do
filled "#aaf" fillRectFull
filled "#afa" $ fillRect (makeRect 0.0 10.0 10.0 10.0)
filled "#faa" $ fillRect (makeRect 10.0 0.0 10.0 10.0)
imageSource >>= (_ `createPattern` Repeat)
fillPathWith pattern do
circle (200.0 >< 200.0) 175.0
circle ( 50.0 >< 50.0) 50.0
circle ( 50.0 >< 350.0) 50.0
circle (350.0 >< 50.0) 50.0
circle (350.0 >< 350.0) 50.0
#runActionOffscreen' Source
runActionOffscreen' :: forall a m. MonadCanvasAction m => CanvasActionM a -> m a
Run a CanvasActionM
in a MonadCanvasAction
, on a created canvas with
the same size as the "main" canvas. This can be useful for creating
patterns for use as a fillStyle or strokeStyle. See runActionOffscreen
for an example.
#asEffect Source
asEffect :: forall a f m. Functor f => MonadEffect m => Context2D -> f (CanvasActionM a) -> f (m a)
Runs a CanvasActionM
in a MonadEffect
inside a functor context. Useful
for turning a function that returns a CanvasActionM
into a function that
returns an Effect
.
#withCtx Source
withCtx :: forall a m. MonadCanvasAction m => (Context2D -> Effect a) -> m a
Convenience function for constructing MonadCanvasAction
s from
Graphics.Canvas
-style functions with no arguments apart from the
Context2D
.
#withCtx1 Source
withCtx1 :: forall b a m. MonadCanvasAction m => (Context2D -> a -> Effect b) -> (a -> m b)
Convenience function for constructing MonadCanvasAction
s from
Graphics.Canvas
-style functions with one argument apart from the
Context2D
.
#withCtx2 Source
withCtx2 :: forall c b a m. MonadCanvasAction m => (Context2D -> a -> b -> Effect c) -> (a -> b -> m c)
Convenience function for constructing MonadCanvasAction
s from
Graphics.Canvas
-style functions with two arguments apart from the
Context2D
.
#withCtx3 Source
withCtx3 :: forall d c b a m. MonadCanvasAction m => (Context2D -> a -> b -> c -> Effect d) -> (a -> b -> c -> m d)
Convenience function for constructing MonadCanvasAction
s from
Graphics.Canvas
-style functions with three arguments apart from the
Context2D
.
#withCtx4 Source
withCtx4 :: forall e d c b a m. MonadCanvasAction m => (Context2D -> a -> b -> c -> d -> Effect e) -> (a -> b -> c -> d -> m e)
Convenience function for constructing MonadCanvasAction
s from
Graphics.Canvas
-style functions with four arguments apart from the
Context2D
.
#withCtx5 Source
withCtx5 :: forall f e d c b a m. MonadCanvasAction m => (Context2D -> a -> b -> c -> d -> e -> Effect f) -> (a -> b -> c -> d -> e -> m f)
Convenience function for constructing MonadCanvasAction
s from
Graphics.Canvas
-style functions with five arguments apart from the
Context2D
.
#withCtx6 Source
withCtx6 :: forall g f e d c b a m. MonadCanvasAction m => (Context2D -> a -> b -> c -> d -> e -> f -> Effect g) -> (a -> b -> c -> d -> e -> f -> m g)
Convenience function for constructing MonadCanvasAction
s from
Graphics.Canvas
-style functions with six arguments apart from the
Context2D
.
#withCtx7 Source
withCtx7 :: forall h g f e d c b a m. MonadCanvasAction m => (Context2D -> a -> b -> c -> d -> e -> f -> g -> Effect h) -> (a -> b -> c -> d -> e -> f -> g -> m h)
Convenience function for constructing MonadCanvasAction
s from
Graphics.Canvas
-style functions with seven arguments apart from the
Context2D
.
#withCtx8 Source
withCtx8 :: forall i h g f e d c b a m. MonadCanvasAction m => (Context2D -> a -> b -> c -> d -> e -> f -> g -> h -> Effect i) -> (a -> b -> c -> d -> e -> f -> g -> h -> m i)
Convenience function for constructing MonadCanvasAction
s from
Graphics.Canvas
-style functions with eight arguments apart from the
Context2D
.
#withCtx9 Source
withCtx9 :: forall j i h g f e d c b a m. MonadCanvasAction m => (Context2D -> a -> b -> c -> d -> e -> f -> g -> h -> i -> Effect j) -> (a -> b -> c -> d -> e -> f -> g -> h -> i -> m j)
Convenience function for constructing MonadCanvasAction
s from
Graphics.Canvas
-style functions with nine arguments apart from the
Context2D
.
#withFull Source
withFull :: forall a m. MonadCanvasAction m => (forall r. ToRegion Number r => r -> m a) -> m a
From a function taking some region and returning a CanvasActionM
, make
a CanvasActionM
that calls the original function with the whole canvas
as the region. This can for example be used to draw an image scaled to fill
the entire canvas: withFull \r → drawImageScale r image
#withMidPos Source
withMidPos :: forall a m. MonadCanvasAction m => (forall p. ToPos Number p => p -> m a) -> m a
From a function taking some position and returning a CanvasActionM
, make
a CanvasActionM
that calls the original function with the center of the
canvas as a position.
#getCanvasEffect Source
getCanvasEffect :: Context2D -> Effect CanvasElement
Get the canvas of a Context2D
#getCanvas Source
getCanvas :: forall m. MonadCanvasAction m => m CanvasElement
Get the canvas as a CanvasActionM
#withCanvas Source
withCanvas :: forall a m. MonadCanvasAction m => (CanvasElement -> Effect a) -> m a
Convenience function for constructing CanvasActionM
s from
Graphics.Canvas
-style functions with no arguments apart from the
CanvasElement
.
#withCanvas1 Source
withCanvas1 :: forall b a m. MonadCanvasAction m => (CanvasElement -> a -> Effect b) -> (a -> m b)
Convenience function for constructing MonadCanvasAction
s from
Graphics.Canvas
-style functions with one argument apart from the
CanvasElement
.
#withCanvas2 Source
withCanvas2 :: forall c b a m. MonadCanvasAction m => (CanvasElement -> a -> b -> Effect c) -> (a -> b -> m c)
Convenience function for constructing MonadCanvasAction
s from
Graphics.Canvas
-style functions with two arguments apart from the
CanvasElement
.
#withCanvas3 Source
withCanvas3 :: forall d c b a m. MonadCanvasAction m => (CanvasElement -> a -> b -> c -> Effect d) -> (a -> b -> c -> m d)
Convenience function for constructing MonadCanvasAction
s from
Graphics.Canvas
-style functions with three arguments apart from the
CanvasElement
.
#fillRect Source
fillRect :: forall r m. MonadCanvasAction m => ToRegion Number r => r -> m Unit
Fill a rectangular area
#fillRectFull Source
fillRectFull :: forall m. MonadCanvasAction m => m Unit
Fill a rectangular area that covers the entire canvas
#strokeRect Source
strokeRect :: forall r m. MonadCanvasAction m => ToRegion Number r => r -> m Unit
Stroke a rectangular area
#strokeRectFull Source
strokeRectFull :: forall m. MonadCanvasAction m => m Unit
Stroke a rectangular area that covers the entire canvas
#clearRect Source
clearRect :: forall r m. MonadCanvasAction m => ToRegion Number r => r -> m Unit
Clear a rectangular area
#clearRectFull Source
clearRectFull :: forall m. MonadCanvasAction m => m Unit
Clear a rectangular area that covers the entire canvas
#setFillStyle Source
setFillStyle :: forall r m. MonadCanvasAction m => CanvasStyleRep r => r -> m Unit
#setStrokeStyle Source
setStrokeStyle :: forall r m. MonadCanvasAction m => CanvasStyleRep r => r -> m Unit
#getFillStyle Source
getFillStyle :: forall m. MonadCanvasAction m => m CanvasStyle
#getStrokeStyle Source
getStrokeStyle :: forall m. MonadCanvasAction m => m CanvasStyle
#filled Source
filled :: forall r a m. MonadCanvasAction m => CanvasStyleRep r => r -> m a -> m a
Run a CanvasActionM
with the given fillStyle, resetting it to the
previous value after
#stroked Source
stroked :: forall r a m. MonadCanvasAction m => CanvasStyleRep r => r -> m a -> m a
Run a CanvasActionM
with the given strokeStyle, resetting it to the
previous value after
#setLineWidth Source
setLineWidth :: forall m. MonadCanvasAction m => Number -> m Unit
#setLineDash Source
setLineDash :: forall m. MonadCanvasAction m => Array Number -> m Unit
#setShadowBlur Source
setShadowBlur :: forall m. MonadCanvasAction m => Number -> m Unit
#setShadowOffsetX Source
setShadowOffsetX :: forall m. MonadCanvasAction m => Number -> m Unit
#setShadowOffsetY Source
setShadowOffsetY :: forall m. MonadCanvasAction m => Number -> m Unit
#setShadowOffset Source
setShadowOffset :: forall p m. MonadCanvasAction m => ToPos Number p => p -> m Unit
Set x and y shadow offset at the same time
#setShadowColor Source
setShadowColor :: forall r m. MonadCanvasAction m => CanvasColorRep r => r -> m Unit
#setMiterLimit Source
setMiterLimit :: forall m. MonadCanvasAction m => Number -> m Unit
#setLineCap Source
setLineCap :: forall m. MonadCanvasAction m => LineCap -> m Unit
#setLineJoin Source
setLineJoin :: forall m. MonadCanvasAction m => LineJoin -> m Unit
#setGlobalCompositeOperation Source
setGlobalCompositeOperation :: forall m. MonadCanvasAction m => Composite -> m Unit
#setGlobalAlpha Source
setGlobalAlpha :: forall m. MonadCanvasAction m => Number -> m Unit
#getTextAlign Source
getTextAlign :: forall m. MonadCanvasAction m => m TextAlign
#setTextAlign Source
setTextAlign :: forall m. MonadCanvasAction m => TextAlign -> m Unit
#getTextBaseline Source
getTextBaseline :: forall m. MonadCanvasAction m => m TextBaseline
#setTextBaseline Source
setTextBaseline :: forall m. MonadCanvasAction m => TextBaseline -> m Unit
#getFont Source
getFont :: forall m. MonadCanvasAction m => m String
#strokeText Source
strokeText :: forall p m. MonadCanvasAction m => ToPos Number p => String -> p -> m Unit
#measureText Source
measureText :: forall m. MonadCanvasAction m => String -> m TextMetrics
#dimensionsToSize Source
dimensionsToSize :: forall s. FromSize Number s => Dimensions -> s
#getDimensions Source
getDimensions :: forall m. MonadCanvasAction m => m Dimensions
#setDimensions Source
setDimensions :: forall m. MonadCanvasAction m => Dimensions -> m Unit
#getHeight Source
getHeight :: forall m. MonadCanvasAction m => m Number
#getWidth Source
getWidth :: forall m. MonadCanvasAction m => m Number
#toDataUrl Source
toDataUrl :: forall m. MonadCanvasAction m => m String
Create a data URL for the current canvas contents
#getImageData Source
getImageData :: forall r m. MonadCanvasAction m => ToRegion Number r => r -> m ImageData
#putImageDataFull Source
putImageDataFull :: forall r p m. MonadCanvasAction m => ToPos Number p => ToRegion Number r => p -> r -> ImageData -> m Unit
Render image data on the canvas. The first argument (p
) is the point on
the canvas to place the topleft of the data. The second argument (r
) is
the region of the ImageData
to render.
#putImageData Source
putImageData :: forall p m. MonadCanvasAction m => ToPos Number p => p -> ImageData -> m Unit
Render image data on the canvas. The first argument (p
) is the point on
the canvas to place the topleft of the data.
#createImageData Source
createImageData :: forall s m. MonadCanvasAction m => ToSize Number s => s -> m ImageData
#createImageDataCopy Source
createImageDataCopy :: forall m. MonadCanvasAction m => ImageData -> m ImageData
#imageSource Source
imageSource :: forall m. MonadCanvasAction m => m CanvasImageSource
#drawImage Source
drawImage :: forall p m. MonadCanvasAction m => ToPos Number p => p -> CanvasImageSource -> m Unit
#drawImageScale Source
drawImageScale :: forall r m. MonadCanvasAction m => ToRegion Number r => r -> CanvasImageSource -> m Unit
Draw an image, scaled to fit the provided region
#drawImageFull Source
drawImageFull :: forall r m. MonadCanvasAction m => ToRegion Number r => r -> r -> CanvasImageSource -> m Unit
Draw an image on the canvas. The first arugment is the region of the image to draw, and the second argument is the region to draw it in.
#tryLoadImage Source
tryLoadImage :: forall m. MonadCanvasAction m => String -> (Maybe CanvasImageSource -> CanvasAction) -> m Unit
Asynchronously load an image file by specifying its path and a callback
CanvasAction
.
#tryLoadImage' Source
tryLoadImage' :: forall m. MonadEffect m => String -> (Maybe CanvasImageSource -> Effect Unit) -> m Unit
Asynchronously load an image file by specifying its path and a callback
Effect Unit
.
#loadImageAff Source
loadImageAff :: String -> Aff CanvasImageSource
Asynchrounously load an image file by specifying its path. The returned
Aff
will throw an error if the image wasn't found.
#setImageSmoothing Source
setImageSmoothing :: forall m. MonadCanvasAction m => Boolean -> m Unit
Set the context's imageSmoothingEnabled
property
#getImageSmoothing Source
getImageSmoothing :: forall m. MonadCanvasAction m => m Boolean
#createPattern Source
createPattern :: forall m. MonadCanvasAction m => CanvasImageSource -> PatternRepeat -> m CanvasPattern
Create a canvas pattern from an image, which can be used as a fill- or strokeStyle
#createLinearGradient Source
createLinearGradient :: forall m. MonadCanvasAction m => LinearGradient -> m CanvasGradient
Constructs a blank linear CanvasGradient
that can be modified with
addColorStop
.
#createRadialGradient Source
createRadialGradient :: forall m. MonadCanvasAction m => RadialGradient -> m CanvasGradient
Constructs a blank radial CanvasGradient
that can be modified with
addColorStop
.
#addColorStop Source
addColorStop :: forall r m. MonadEffect m => CanvasColorRep r => CanvasGradient -> Number -> r -> m Unit
Note: Mutates the original CanvasGradient
and returns Unit
.
It is recommended to construct gradients with linearGradient
and
radialGradient
instead.
#linearGradient Source
linearGradient :: forall f r m. MonadCanvasAction m => CanvasColorRep r => Foldable f => LinearGradient -> f (Tuple Number r) -> m CanvasGradient
#radialGradient Source
radialGradient :: forall f r m. MonadCanvasAction m => CanvasColorRep r => Foldable f => RadialGradient -> f (Tuple Number r) -> m CanvasGradient
#resetTransform_ Source
resetTransform_ :: forall m. MonadCanvasAction m => m Unit
#setTransform_ Source
setTransform_ :: forall m. MonadCanvasAction m => MatrixTransform -> m Unit
#getTransform_ Source
getTransform_ :: forall m. MonadCanvasAction m => m MatrixTransform
#translate_ Source
translate_ :: forall m. MonadCanvasAction m => TranslateTransform -> m Unit
#scale_ Source
scale_ :: forall m. MonadCanvasAction m => ScaleTransform -> m Unit
#skew_ Source
skew_ :: forall m. MonadCanvasAction m => SkewTransform -> m Unit
#matrixTransform_ Source
matrixTransform_ :: forall m. MonadCanvasAction m => MatrixTransform -> m Unit
#beginPath_ Source
beginPath_ :: forall m. MonadCanvasAction m => m Unit
#stroke_ Source
stroke_ :: forall m. MonadCanvasAction m => m Unit
#fill_ Source
fill_ :: forall m. MonadCanvasAction m => m Unit
#clip_ Source
clip_ :: forall m. MonadCanvasAction m => m Unit
#closePath_ Source
closePath_ :: forall m. MonadCanvasAction m => m Unit
#quadraticCurveTo_ Source
quadraticCurveTo_ :: forall m. MonadCanvasAction m => QuadraticCurve -> m Unit
#bezierCurveTo_ Source
bezierCurveTo_ :: forall m. MonadCanvasAction m => BezierCurve -> m Unit
#save Source
save :: forall m. MonadCanvasAction m => m Unit
Saves the context, see save
on MDN
#restore Source
restore :: forall m. MonadCanvasAction m => m Unit
Restores the context, see restore
on MDN
#restoreAfter Source
restoreAfter :: forall a m. MonadCanvasAction m => m a -> m a
Runs save
, then the provided action, then restore
Re-exports from Graphics.Canvas
#TranslateTransform Source
type TranslateTransform = { translateX :: Number, translateY :: Number }
An object representing a translation:
- The translation amounts in the
x
andy
directions,translateX
andtranslateY
.
#ScaleTransform Source
type ScaleTransform = { scaleX :: Number, scaleY :: Number }
An object representing a scaling transform:
- The scale factors in the
x
andy
directions,scaleX
andscaleY
.
#QuadraticCurve Source
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
)
#PatternRepeat Source
#LinearGradient Source
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
)
#Dimensions Source
type Dimensions = { height :: Number, width :: Number }
Canvas dimensions (width and height) in pixels.
#Composite Source
data Composite
Enumerates the different types of composite operations and blend modes.
Constructors
SourceOver
SourceIn
SourceOut
SourceAtop
DestinationOver
DestinationIn
DestinationOut
DestinationAtop
Lighter
Copy
Xor
Multiply
Screen
Overlay
Darken
Lighten
ColorDodge
ColorBurn
HardLight
SoftLight
Difference
Exclusion
Hue
Saturation
Color
Luminosity
Instances
#CanvasPattern Source
data CanvasPattern :: Type
Opaque object describing a pattern.
#CanvasImageSource Source
data CanvasImageSource :: Type
Opaque object for drawing elements and things to the canvas.
#CanvasGradient Source
data CanvasGradient :: Type
Opaque object describing a gradient.
#CanvasElement Source
data CanvasElement :: Type
A canvas HTML element.
#imageDataWidth Source
imageDataWidth :: ImageData -> Int
Get the width of an ImageData
object.
#imageDataHeight Source
imageDataHeight :: ImageData -> Int
Get the height of an ImageData
object.
#imageDataBuffer Source
imageDataBuffer :: ImageData -> Uint8ClampedArray
Get the underlying buffer from an ImageData
object.
Re-exports from Graphics.CanvasAction.Class
#MonadCanvasAction Source
class (MonadEffect m) <= MonadCanvasAction m where
The MonadCanvasAction
class captures those monads which support canvas
actions.
Instances are provided for CanvasActionM
itself, and the standard monad
transformers.
liftCanvasAction
can be used in any appropriate monad transformer stack
to lift an action of type CanvasActionM a
into the monad.
Members
Instances
MonadCanvasAction (ReaderT Context2D Effect)
(MonadCanvasAction m) => MonadCanvasAction (ReaderT r m)
(TypeEquals (RProxy r1) (RProxy (canvas :: CANVAS, effect :: EFFECT | r2))) => MonadCanvasAction (Run r1)
(MonadCanvasAction m) => MonadCanvasAction (ContT r m)
(MonadCanvasAction m) => MonadCanvasAction (ExceptT e m)
(MonadCanvasAction m) => MonadCanvasAction (ListT m)
(MonadCanvasAction m) => MonadCanvasAction (MaybeT m)
(Monoid w, MonadCanvasAction m) => MonadCanvasAction (RWST r w s m)
(MonadCanvasAction m) => MonadCanvasAction (StateT s m)
(Monoid w, MonadCanvasAction m) => MonadCanvasAction (WriterT w m)
Re-exports from Graphics.CanvasAction.Types
#CanvasActionM Source
type CanvasActionM = ReaderT Context2D Effect
The CanvasActionM
monad is a monad transformer stack, more specifically
the ReaderT
monad transformer applied to the Effect
monad, reading a
value of type Context2D
.
#CanvasAction Source
type CanvasAction = CanvasActionM Unit
Type synonym for a CanvasActionM
without a result
#runAction Source
runAction :: forall a m. MonadEffect m => Context2D -> CanvasActionM a -> m a
Run a CanvasActionM
in a MonadEffect
, on the provided Context2D
.
Re-exports from Web.DOM.ParentNode
#QuerySelector
newtype QuerySelector
Constructors
QuerySelector String
Instances
Eq QuerySelector
Ord QuerySelector
Newtype QuerySelector _