Skip to content

Commit

Permalink
Merge pull request #32 from igagen/patterns
Browse files Browse the repository at this point in the history
Add Blend Modes and Canonical Show instances for Enumerations
  • Loading branch information
paf31 committed Mar 11, 2016
2 parents 637ed3a + e2b19f8 commit f8d1fa2
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 27 deletions.
28 changes: 24 additions & 4 deletions docs/Graphics/Canvas.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,24 @@ data Composite
| Lighter
| Copy
| Xor
```

Enumerates the different types of alpha composite operations.
| Multiply
| Screen
| Overlay
| Darken
| Lighten
| ColorDodge
| ColorBurn
| HardLight
| SoftLight
| Difference
| Exclusion
| Hue
| Saturation
| Color
| Luminosity
```

Enumerates the different types of composite operations and blend modes.

##### Instances
``` purescript
Expand Down Expand Up @@ -669,6 +684,11 @@ data PatternRepeat

Enumerates the different types of pattern repetitions.

##### Instances
``` purescript
Show PatternRepeat
```

#### `createPattern`

``` purescript
Expand All @@ -680,7 +700,7 @@ Create a new canvas pattern (repeatable image).
#### `setPatternFillStyle`

``` purescript
setPatternFillStyle :: forall eff. CanvasGradient -> Context2D -> Eff (canvas :: Canvas | eff) Context2D
setPatternFillStyle :: forall eff. CanvasPattern -> Context2D -> Eff (canvas :: Canvas | eff) Context2D
```

Set the Context2D fillstyle to the CanvasPattern.
Expand Down
120 changes: 97 additions & 23 deletions src/Graphics/Canvas.purs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@ setLineCap Round = setLineCapImpl "round"
setLineCap Square = setLineCapImpl "square"
setLineCap Butt = setLineCapImpl "butt"

-- | Enumerates the different types of alpha composite operations.
-- | Enumerates the different types of composite operations and blend modes.
data Composite
-- Composite Operations
= SourceOver
| SourceIn
| SourceOut
Expand All @@ -227,24 +228,83 @@ data Composite
| Copy
| Xor

-- Blend Modes
| Multiply
| Screen
| Overlay
| Darken
| Lighten
| ColorDodge
| ColorBurn
| HardLight
| SoftLight
| Difference
| Exclusion
| Hue
| Saturation
| Color
| Luminosity

instance showComposite :: Show Composite where
show SourceOver = "source-over"
show SourceIn = "source-in"
show SourceOut = "source-out"
show SourceAtop = "source-atop"
show DestinationOver = "destination-over"
show DestinationIn = "destination-in"
show DestinationOut = "destination-out"
show DestinationAtop = "destination-atop"
show Lighter = "lighter"
show Copy = "copy"
show Xor = "xor"
show SourceOver = "SourceOver"
show SourceIn = "SourceIn"
show SourceOut = "SourceOut"
show SourceAtop = "SourceAtop"
show DestinationOver = "DestinationOver"
show DestinationIn = "DestinationIn"
show DestinationOut = "DestinationOut"
show DestinationAtop = "DestinationAtop"
show Lighter = "Lighter"
show Copy = "Copy"
show Xor = "Xor"
show Multiply = "Multiply"
show Screen = "Screen"
show Overlay = "Overlay"
show Darken = "Darken"
show Lighten = "Lighten"
show ColorDodge = "ColorDodge"
show ColorBurn = "ColorBurn"
show HardLight = "HardLight"
show SoftLight = "SoftLight"
show Difference = "Difference"
show Exclusion = "Exclusion"
show Hue = "Hue"
show Saturation = "Saturation"
show Color = "Color"
show Luminosity = "Luminosity"

foreign import setGlobalCompositeOperationImpl :: forall eff. Context2D -> String -> Eff (canvas :: Canvas | eff) Context2D

-- | Set the current composite operation.
setGlobalCompositeOperation :: forall eff. Context2D -> Composite -> Eff (canvas :: Canvas | eff) Context2D
setGlobalCompositeOperation ctx composite = setGlobalCompositeOperationImpl ctx (show composite)
setGlobalCompositeOperation ctx composite = setGlobalCompositeOperationImpl ctx (toString composite)
where
toString SourceOver = "source-over"
toString SourceIn = "source-in"
toString SourceOut = "source-out"
toString SourceAtop = "source-atop"
toString DestinationOver = "destination-over"
toString DestinationIn = "destination-in"
toString DestinationOut = "destination-out"
toString DestinationAtop = "destination-atop"
toString Lighter = "lighter"
toString Copy = "copy"
toString Xor = "xor"
toString Multiply = "multiply"
toString Screen = "screen"
toString Overlay = "overlay"
toString Darken = "darken"
toString Lighten = "lighten"
toString ColorDodge = "color-dodge"
toString ColorBurn = "color-burn"
toString HardLight = "hard-light"
toString SoftLight = "soft-light"
toString Difference = "difference"
toString Exclusion = "exclusion"
toString Hue = "hue"
toString Saturation = "saturation"
toString Color = "color"
toString Luminosity = "luminosity"

-- | Set the current global alpha level.
foreign import setGlobalAlpha :: forall eff. Context2D -> Number -> Eff (canvas :: Canvas | eff) Context2D
Expand Down Expand Up @@ -388,11 +448,11 @@ data TextAlign
= AlignLeft | AlignRight | AlignCenter | AlignStart | AlignEnd

instance showTextAlign :: Show TextAlign where
show AlignLeft = "left"
show AlignRight = "right"
show AlignCenter = "center"
show AlignStart = "start"
show AlignEnd = "end"
show AlignLeft = "AlignLeft"
show AlignRight = "AlignRight"
show AlignCenter = "AlignCenter"
show AlignStart = "AlignStart"
show AlignEnd = "AlignEnd"

foreign import textAlignImpl :: forall eff. Context2D -> Eff (canvas :: Canvas | eff) String

Expand All @@ -414,7 +474,13 @@ foreign import setTextAlignImpl :: forall eff. Context2D -> String -> (Eff (canv
-- | Set the current text alignment.
setTextAlign :: forall eff. Context2D -> TextAlign -> Eff (canvas :: Canvas | eff) Context2D
setTextAlign ctx textalign =
setTextAlignImpl ctx (show textalign)
setTextAlignImpl ctx (toString textalign)
where
toString AlignLeft = "left"
toString AlignRight = "right"
toString AlignCenter = "center"
toString AlignStart = "start"
toString AlignEnd = "end"

-- | Text metrics:
-- |
Expand Down Expand Up @@ -482,14 +548,22 @@ foreign import drawImageFull :: forall eff. Context2D -> CanvasImageSource -> Nu
-- | Enumerates the different types of pattern repetitions.
data PatternRepeat = Repeat | RepeatX | RepeatY | NoRepeat

instance showPatternRepeat :: Show PatternRepeat where
show Repeat = "Repeat"
show RepeatX = "RepeatX"
show RepeatY = "RepeatY"
show NoRepeat = "NoRepeat"

foreign import createPatternImpl :: forall eff. CanvasImageSource -> String -> Context2D -> Eff (canvas :: Canvas | eff) CanvasPattern

-- | Create a new canvas pattern (repeatable image).
createPattern :: forall eff. CanvasImageSource -> PatternRepeat -> Context2D -> Eff (canvas :: Canvas | eff) CanvasPattern
createPattern img Repeat = createPatternImpl img "repeat"
createPattern img RepeatX = createPatternImpl img "repeat-x"
createPattern img RepeatY = createPatternImpl img "repeat-y"
createPattern img NoRepeat = createPatternImpl img "no-repeat"
createPattern img repeat = createPatternImpl img (toString repeat)
where
toString Repeat = "repeat"
toString RepeatX = "repeat-x"
toString RepeatY = "repeat-y"
toString NoRepeat = "no-repeat"

-- | Set the Context2D fillstyle to the CanvasPattern.
foreign import setPatternFillStyle :: forall eff. CanvasPattern -> Context2D -> Eff (canvas :: Canvas | eff) Context2D
Expand Down

0 comments on commit f8d1fa2

Please sign in to comment.