Graphics.CanvasAction
- Package
- purescript-canvas-action
- Repository
- 3ddyy/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.
#SkewTransform Source
type SkewTransform = { skewX :: Number, skewY :: Number }Type synonym for skew transformations.
#TextBaseline Source
#CanvasStyle Source
data CanvasStyle :: TypeA 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 whereClass 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 whereClass describing types that can be turned into a string representing a canvas color.
Members
Instances
#createCanvas Source
createCanvas :: forall s. ToSize Number s => s -> CanvasActionM CanvasElementCreate a CanvasElement of the given size in the CanvasActionM monad
#createCanvas' Source
createCanvas' :: forall s. ToSize Number s => Document -> s -> CanvasActionM CanvasElementSame as createCanvas, but allows for specifying the Document object to
create the canvas with
#createCanvasEffect Source
createCanvasEffect :: forall s. ToSize Number s => s -> Effect CanvasElementCreate a CanvasElement of the given size in the Effect monad
#createCanvasEffect' Source
createCanvasEffect' :: forall s. ToSize Number s => Document -> s -> Effect CanvasElementSame as createCanvasEffect, but allows for specifying the Document
object to create the canvas with
#CanvasActionM Source
type CanvasActionM = ReaderT Context2D EffectThe 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 UnitType synonym for a CanvasActionM without a result, as it's very common
#runAction Source
runAction :: forall a. Context2D -> CanvasActionM a -> Effect aRun a CanvasActionM in the Effect monad, on the provided Context2D
#runAction' Source
runAction' :: forall a. Context2D -> CanvasActionM a -> CanvasActionM aRun a CanvasActionM in the CanvasActionM monad, on the provided
Context2D. In essence, this allows drawing to a different canvas than
the action was run on. This can be useful for caching drawings on offscreen
canvases, to later draw them to the "main" canvas.
#runActionOffscreen Source
runActionOffscreen :: forall s a. ToSize Number s => s -> CanvasActionM a -> CanvasActionM aRun a CanvasActionM in the CanvasActionM monad, 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 >>= flip 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. CanvasActionM a -> CanvasActionM aRun a CanvasActionM in the CanvasActionM monad, 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.
#withCtx Source
withCtx :: forall a. (Context2D -> Effect a) -> CanvasActionM aConvenience function for constructing CanvasActionMs from
Graphics.Canvas-style functions with no arguments apart from the
Context2D.
#withCtx1 Source
withCtx1 :: forall b a. (Context2D -> a -> Effect b) -> (a -> CanvasActionM b)Convenience function for constructing CanvasActionMs from
Graphics.Canvas-style functions with one argument apart from the
Context2D.
#withCtx2 Source
withCtx2 :: forall c b a. (Context2D -> a -> b -> Effect c) -> (a -> b -> CanvasActionM c)Convenience function for constructing CanvasActionMs from
Graphics.Canvas-style functions with two arguments apart from the
Context2D.
#withCtx3 Source
withCtx3 :: forall d c b a. (Context2D -> a -> b -> c -> Effect d) -> (a -> b -> c -> CanvasActionM d)Convenience function for constructing CanvasActionMs from
Graphics.Canvas-style functions with three arguments apart from the
Context2D.
#withCtx4 Source
withCtx4 :: forall e d c b a. (Context2D -> a -> b -> c -> d -> Effect e) -> (a -> b -> c -> d -> CanvasActionM e)Convenience function for constructing CanvasActionMs from
Graphics.Canvas-style functions with four arguments apart from the
Context2D.
#withCtx5 Source
withCtx5 :: forall f e d c b a. (Context2D -> a -> b -> c -> d -> e -> Effect f) -> (a -> b -> c -> d -> e -> CanvasActionM f)Convenience function for constructing CanvasActionMs from
Graphics.Canvas-style functions with five arguments apart from the
Context2D.
#withCtx6 Source
withCtx6 :: forall g f e d c b a. (Context2D -> a -> b -> c -> d -> e -> f -> Effect g) -> (a -> b -> c -> d -> e -> f -> CanvasActionM g)Convenience function for constructing CanvasActionMs from
Graphics.Canvas-style functions with six arguments apart from the
Context2D.
#withCtx7 Source
withCtx7 :: forall h g f e d c b a. (Context2D -> a -> b -> c -> d -> e -> f -> g -> Effect h) -> (a -> b -> c -> d -> e -> f -> g -> CanvasActionM h)Convenience function for constructing CanvasActionMs 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. (Context2D -> a -> b -> c -> d -> e -> f -> g -> h -> Effect i) -> (a -> b -> c -> d -> e -> f -> g -> h -> CanvasActionM i)Convenience function for constructing CanvasActionMs 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. (Context2D -> a -> b -> c -> d -> e -> f -> g -> h -> i -> Effect j) -> (a -> b -> c -> d -> e -> f -> g -> h -> i -> CanvasActionM j)Convenience function for constructing CanvasActionMs from
Graphics.Canvas-style functions with nine arguments apart from the
Context2D.
#withFull Source
withFull :: forall a. (forall r. ToRegion Number r => r -> CanvasActionM a) -> CanvasActionM aFrom 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. (forall p. ToPos Number p => p -> CanvasActionM a) -> CanvasActionM aFrom 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 CanvasElementGet the canvas of a Context2D
#getCanvas Source
getCanvas :: CanvasActionM CanvasElementGet the canvas as a CanvasActionM
#withCanvas Source
withCanvas :: forall a. (CanvasElement -> Effect a) -> CanvasActionM aConvenience function for constructing CanvasActionMs from
Graphics.Canvas-style functions with no arguments apart from the
CanvasElement.
#withCanvas1 Source
withCanvas1 :: forall b a. (CanvasElement -> a -> Effect b) -> (a -> CanvasActionM b)Convenience function for constructing CanvasActionMs from
Graphics.Canvas-style functions with one argument apart from the
CanvasElement.
#withCanvas2 Source
withCanvas2 :: forall c b a. (CanvasElement -> a -> b -> Effect c) -> (a -> b -> CanvasActionM c)Convenience function for constructing CanvasActionMs from
Graphics.Canvas-style functions with two arguments apart from the
CanvasElement.
#withCanvas3 Source
withCanvas3 :: forall d c b a. (CanvasElement -> a -> b -> c -> Effect d) -> (a -> b -> c -> CanvasActionM d)Convenience function for constructing CanvasActionMs from
Graphics.Canvas-style functions with three arguments apart from the
CanvasElement.
#fillRectFull Source
fillRectFull :: CanvasActionFill a rectangular area that covers the entire canvas
#strokeRect Source
strokeRect :: forall r. ToRegion Number r => r -> CanvasActionStroke a rectangular area
#strokeRectFull Source
strokeRectFull :: CanvasActionStroke a rectangular area that covers the entire canvas
#clearRect Source
clearRect :: forall r. ToRegion Number r => r -> CanvasActionClear a rectangular area
#clearRectFull Source
clearRectFull :: CanvasActionClear a rectangular area that covers the entire canvas
#setFillStyle Source
setFillStyle :: forall r. CanvasStyleRep r => r -> CanvasAction#setStrokeStyle Source
setStrokeStyle :: forall r. CanvasStyleRep r => r -> CanvasAction#filled Source
filled :: forall r a. CanvasStyleRep r => r -> CanvasActionM a -> CanvasActionM aRun a CanvasActionM with the given fillStyle, resetting it to the
previous value after
#stroked Source
stroked :: forall r a. CanvasStyleRep r => r -> CanvasActionM a -> CanvasActionM aRun a CanvasActionM with the given strokeStyle, resetting it to the
previous value after
#setLineWidth Source
setLineWidth :: Number -> CanvasAction#setLineDash Source
setLineDash :: Array Number -> CanvasAction#setShadowBlur Source
setShadowBlur :: Number -> CanvasAction#setShadowOffset Source
setShadowOffset :: forall p. ToPos Number p => p -> CanvasActionSet x and y shadow offset at the same time
#setShadowColor Source
setShadowColor :: forall r. CanvasColorRep r => r -> CanvasAction#setMiterLimit Source
setMiterLimit :: Number -> CanvasAction#setLineCap Source
setLineCap :: LineCap -> CanvasAction#setLineJoin Source
setLineJoin :: LineJoin -> CanvasAction#setGlobalAlpha Source
setGlobalAlpha :: Number -> CanvasAction#setFont Source
setFont :: String -> CanvasAction#strokeText Source
strokeText :: forall p. ToPos Number p => String -> p -> CanvasAction#dimensionsToSize Source
dimensionsToSize :: forall s. FromSize Number s => Dimensions -> s#setHeight Source
setHeight :: Number -> CanvasAction#setWidth Source
setWidth :: Number -> CanvasAction#toDataUrl Source
toDataUrl :: CanvasActionM StringCreate a data URL for the current canvas contents
#getImageData Source
getImageData :: forall r. ToRegion Number r => r -> CanvasActionM ImageData#putImageDataFull Source
putImageDataFull :: forall r p. ToPos Number p => ToRegion Number r => p -> r -> ImageData -> CanvasActionRender 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. ToPos Number p => p -> ImageData -> CanvasActionRender 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. ToSize Number s => s -> CanvasActionM ImageData#drawImage Source
drawImage :: forall p. ToPos Number p => p -> CanvasImageSource -> CanvasAction#drawImageScale Source
drawImageScale :: forall r. ToRegion Number r => r -> CanvasImageSource -> CanvasActionDraw an image, scaled to fit the provided region
#drawImageFull Source
drawImageFull :: forall r. ToRegion Number r => r -> r -> CanvasImageSource -> CanvasActionDraw 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.
#setImageSmoothing Source
setImageSmoothing :: Boolean -> CanvasActionSet the context's imageSmoothingEnabled property
#createPattern Source
createPattern :: CanvasImageSource -> PatternRepeat -> CanvasActionM CanvasPatternCreate a canvas pattern from an image, which can be used as a fill- or strokeStyle
#createLinearGradient Source
createLinearGradient :: LinearGradient -> CanvasActionM CanvasGradientConstructs a blank linear CanvasGradient that can be modified with
addColorStop.
#createRadialGradient Source
createRadialGradient :: RadialGradient -> CanvasActionM CanvasGradientConstructs a blank radial CanvasGradient that can be modified with
addColorStop.
#addColorStop Source
addColorStop :: forall r. CanvasColorRep r => CanvasGradient -> Number -> r -> CanvasActionNote: Mutates the original CanvasGradient and returns Unit.
It is recommended to construct gradients with linearGradient and
radialGradient instead.
#linearGradient Source
linearGradient :: forall f r. CanvasColorRep r => Foldable f => LinearGradient -> f (Tuple Number r) -> CanvasActionM CanvasGradient#radialGradient Source
radialGradient :: forall f r. CanvasColorRep r => Foldable f => RadialGradient -> f (Tuple Number r) -> CanvasActionM CanvasGradient#scale Source
scale :: ScaleTransform -> CanvasAction#skew Source
skew :: SkewTransform -> CanvasAction#rotate Source
rotate :: Number -> CanvasAction#fill Source
fill :: CanvasAction#clip Source
clip :: CanvasAction#arc Source
arc :: Arc -> CanvasAction#save Source
save :: CanvasActionSaves the context, see save on MDN
#restore Source
restore :: CanvasActionRestores the context, see restore on MDN
#restoreAfter Source
restoreAfter :: forall a. CanvasActionM a -> CanvasActionM aRuns 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
xandydirections,translateXandtranslateY.
#ScaleTransform Source
type ScaleTransform = { scaleX :: Number, scaleY :: Number }An object representing a scaling transform:
- The scale factors in the
xandydirections,scaleXandscaleY.
#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 CompositeEnumerates the different types of composite operations and blend modes.
Constructors
SourceOverSourceInSourceOutSourceAtopDestinationOverDestinationInDestinationOutDestinationAtopLighterCopyXorMultiplyScreenOverlayDarkenLightenColorDodgeColorBurnHardLightSoftLightDifferenceExclusionHueSaturationColorLuminosity
Instances
#CanvasPattern Source
data CanvasPattern :: TypeOpaque object describing a pattern.
#CanvasImageSource Source
data CanvasImageSource :: TypeOpaque object for drawing elements and things to the canvas.
#CanvasGradient Source
data CanvasGradient :: TypeOpaque object describing a gradient.
#CanvasElement Source
data CanvasElement :: TypeA canvas HTML element.
#imageDataWidth Source
imageDataWidth :: ImageData -> IntGet the width of an ImageData object.
#imageDataHeight Source
imageDataHeight :: ImageData -> IntGet the height of an ImageData object.
#imageDataBuffer Source
imageDataBuffer :: ImageData -> Uint8ClampedArrayGet the underlying buffer from an ImageData object.