-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCanvas.purs
More file actions
797 lines (646 loc) · 23.4 KB
/
Canvas.purs
File metadata and controls
797 lines (646 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
-- | This module defines foreign types and functions for working with the 2D
-- | Canvas API.
module Graphics.Canvas
( CanvasElement
, Context2D
, ImageData
, CanvasImageSource
, Arc
, Composite(..)
, Dimensions
, LineCap(..)
, LineJoin(..)
, Rectangle
, ScaleTransform
, TextMetrics
, Transform
, TranslateTransform
, TextAlign(..)
, TextBaseline(..)
, CanvasPattern
, PatternRepeat(..)
, CanvasGradient
, LinearGradient
, RadialGradient
, QuadraticCurve
, BezierCurve
, BlobFormat(..)
, getCanvasElementById
, getContext2D
, getCanvasWidth
, setCanvasWidth
, getCanvasHeight
, setCanvasHeight
, getCanvasDimensions
, setCanvasDimensions
, canvasToDataURL
, setLineWidth
, setLineDash
, setFillStyle
, setStrokeStyle
, setShadowBlur
, setShadowOffsetX
, setShadowOffsetY
, setShadowColor
, setMiterLimit
, setLineCap
, setLineJoin
, setGlobalCompositeOperation
, setGlobalAlpha
, beginPath
, stroke
, fill
, clip
, lineTo
, moveTo
, closePath
, strokePath
, fillPath
, arc
, rect
, fillRect
, strokeRect
, clearRect
, scale
, rotate
, translate
, transform
, setTransform
, textAlign
, setTextAlign
, textBaseline
, setTextBaseline
, font
, setFont
, fillText
, strokeText
, measureText
, save
, restore
, withContext
, tryLoadImage
, getImageData
, putImageData
, putImageDataFull
, createImageData
, createImageDataCopy
, createImageDataWith
, imageDataWidth
, imageDataHeight
, imageDataBuffer
, toBlob
, toBlob'
, blobPNG
, blobJPEG
, canvasElementToImageSource
, drawImage
, drawImageScale
, drawImageFull
, createPattern
, setPatternFillStyle
, createLinearGradient
, createRadialGradient
, addColorStop
, setGradientFillStyle
, quadraticCurveTo
, bezierCurveTo
) where
import Prelude
import Effect (Effect)
import Effect.Exception.Unsafe (unsafeThrow)
import Data.ArrayBuffer.Types (Uint8ClampedArray)
import Data.Either (Either(Right))
import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3)
import Data.Maybe (Maybe(..))
import Data.MediaType (MediaType(..))
import Data.MediaType.Common (imagePNG, imageJPEG)
import Web.File.Blob (Blob)
import Effect.Aff (Aff, makeAff)
-- | A canvas HTML element.
foreign import data CanvasElement :: Type
-- | A 2D graphics context.
foreign import data Context2D :: Type
-- | An image data object, used to store raster data outside the canvas.
foreign import data ImageData :: Type
-- | Opaque object for drawing elements and things to the canvas.
foreign import data CanvasImageSource :: Type
-- | Opaque object describing a pattern.
foreign import data CanvasPattern :: Type
-- | Opaque object describing a gradient.
foreign import data CanvasGradient :: Type
foreign import canvasElementToImageSource :: CanvasElement -> CanvasImageSource
foreign import tryLoadImageImpl
:: String
-> Effect Unit
-> (CanvasImageSource -> Effect Unit)
-> Effect Unit
-- | Asynchronously load an image file by specifying its path.
tryLoadImage
:: String
-> (Maybe CanvasImageSource -> Effect Unit)
-> Effect Unit
tryLoadImage path k = tryLoadImageImpl path (k Nothing) (k <<< Just)
foreign import getCanvasElementByIdImpl
:: forall r
. Fn3 String
(CanvasElement -> r)
r
(Effect r)
-- | Get a canvas element by ID, or `Nothing` if the element does not exist.
getCanvasElementById :: String -> Effect (Maybe CanvasElement)
getCanvasElementById elId = runFn3 getCanvasElementByIdImpl elId Just Nothing
-- | Get the 2D graphics context for a canvas element.
foreign import getContext2D :: CanvasElement -> Effect Context2D
-- | Get the canvas width in pixels.
foreign import getCanvasWidth :: CanvasElement -> Effect Number
-- | Get the canvas height in pixels.
foreign import getCanvasHeight :: CanvasElement -> Effect Number
-- | Set the canvas width in pixels.
foreign import setCanvasWidth :: CanvasElement -> Number -> Effect Unit
-- | Set the canvas height in pixels.
foreign import setCanvasHeight :: CanvasElement -> Number -> Effect Unit
-- | Canvas dimensions (width and height) in pixels.
type Dimensions = { width :: Number, height :: Number }
-- | Get the canvas dimensions in pixels.
getCanvasDimensions :: CanvasElement -> Effect Dimensions
getCanvasDimensions ce = do
w <- getCanvasWidth ce
h <- getCanvasHeight ce
pure {width : w, height : h}
-- | Set the canvas dimensions in pixels.
setCanvasDimensions :: CanvasElement -> Dimensions -> Effect Unit
setCanvasDimensions ce d = setCanvasHeight ce d.height *> setCanvasWidth ce d.width
-- | Create a data URL for the current canvas contents
foreign import canvasToDataURL :: CanvasElement -> Effect String
-- | Set the current line width.
foreign import setLineWidth :: Context2D -> Number -> Effect Unit
-- | Set the current line dash pattern.
foreign import setLineDash :: Context2D -> Array Number -> Effect Unit
-- | Set the current fill style/color.
foreign import setFillStyle :: Context2D -> String -> Effect Unit
-- | Set the current stroke style/color.
foreign import setStrokeStyle :: Context2D -> String -> Effect Unit
-- | Set the current shadow color.
foreign import setShadowColor :: Context2D -> String -> Effect Unit
-- | Set the current shadow blur radius.
foreign import setShadowBlur :: Context2D -> Number -> Effect Unit
-- | Set the current shadow x-offset.
foreign import setShadowOffsetX :: Context2D -> Number -> Effect Unit
-- | Set the current shadow y-offset.
foreign import setShadowOffsetY :: Context2D -> Number -> Effect Unit
-- | Set the current miter limit.
foreign import setMiterLimit :: Context2D -> Number -> Effect Unit
-- | Enumerates the different types of line cap.
data LineCap = Round | Square | Butt
derive instance eqLineCap :: Eq LineCap
foreign import setLineCapImpl :: Context2D -> String -> Effect Unit
-- | Set the current line cap type.
setLineCap :: Context2D -> LineCap -> Effect Unit
setLineCap context Round = setLineCapImpl context "round"
setLineCap context Square = setLineCapImpl context "square"
setLineCap context Butt = setLineCapImpl context "butt"
-- Note that we can't re-use `Round` from LineCap, so I've added `Join` to all of these
-- | Enumerates the different types of line join
data LineJoin = BevelJoin | RoundJoin | MiterJoin
derive instance eqLineJoin :: Eq LineJoin
foreign import setLineJoinImpl :: Context2D -> String -> Effect Unit
-- | Set the current line join type.
setLineJoin :: Context2D -> LineJoin -> Effect Unit
setLineJoin context BevelJoin = setLineJoinImpl context "bevel"
setLineJoin context RoundJoin = setLineJoinImpl context "round"
setLineJoin context MiterJoin = setLineJoinImpl context "miter"
-- | Enumerates the different types of composite operations and blend modes.
data Composite
-- Composite Operations
= SourceOver
| SourceIn
| SourceOut
| SourceAtop
| DestinationOver
| DestinationIn
| DestinationOut
| DestinationAtop
| Lighter
| Copy
| Xor
-- Blend Modes
| Multiply
| Screen
| Overlay
| Darken
| Lighten
| ColorDodge
| ColorBurn
| HardLight
| SoftLight
| Difference
| Exclusion
| Hue
| Saturation
| Color
| Luminosity
derive instance eqComposite :: Eq Composite
instance showComposite :: Show Composite where
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 :: Context2D -> String -> Effect Unit
-- | Set the current composite operation.
setGlobalCompositeOperation :: Context2D -> Composite -> Effect Unit
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 :: Context2D -> Number -> Effect Unit
-- | Begin a path object.
foreign import beginPath :: Context2D -> Effect Unit
-- | Stroke the current object.
foreign import stroke :: Context2D -> Effect Unit
-- | Fill the current object.
foreign import fill :: Context2D -> Effect Unit
-- | Clip to the current object.
foreign import clip :: Context2D -> Effect Unit
-- | Move the path to the specified coordinates, drawing a line segment.
foreign import lineTo :: Context2D -> Number -> Number -> Effect Unit
-- | Move the path to the specified coordinates, without drawing a line segment.
foreign import moveTo :: Context2D -> Number -> Number -> Effect Unit
-- | Close the current path.
foreign import closePath :: Context2D -> Effect Unit
-- | A convenience function for drawing a stroked path.
-- |
-- | For example:
-- |
-- | ```purescript
-- | strokePath ctx $ do
-- | moveTo ctx 10.0 10.0
-- | lineTo ctx 20.0 20.0
-- | lineTo ctx 10.0 20.0
-- | closePath ctx
-- | ```
strokePath :: forall a. Context2D -> Effect a -> Effect a
strokePath ctx path = do
_ <- beginPath ctx
a <- path
_ <- stroke ctx
pure a
-- | A convenience function for drawing a filled path.
-- |
-- | For example:
-- |
-- | ```purescript
-- | fillPath ctx $ do
-- | moveTo ctx 10.0 10.0
-- | lineTo ctx 20.0 20.0
-- | lineTo ctx 10.0 20.0
-- | closePath ctx
-- | ```
fillPath :: forall a. Context2D -> Effect a -> Effect a
fillPath ctx path = do
_ <- beginPath ctx
a <- path
_ <- fill ctx
pure a
-- | A type representing an arc object:
-- |
-- | - The center coordinates `x` and `y`,
-- | - The radius `r`,
-- | - The starting and ending angles, `start` and `end`.
-- | - Whether to draw the arc counter-clockwise (true) or clockwise (false) direction.
-- | Normally, this value is `false`.
type Arc =
{ x :: Number
, y :: Number
, radius :: Number
, start :: Number
, end :: Number
, useCounterClockwise :: Boolean
}
-- | Render an arc object.
foreign import arc :: Context2D -> Arc -> Effect Unit
-- | A type representing a rectangle object:
-- |
-- | - The top-left corner coordinates `x` and `y`,
-- | - The width and height `w` and `h`.
type Rectangle =
{ x :: Number
, y :: Number
, width :: Number
, height :: Number
}
-- | Render a rectangle.
foreign import rect :: Context2D -> Rectangle -> Effect Unit
-- | Fill a rectangle.
foreign import fillRect :: Context2D -> Rectangle -> Effect Unit
-- | Stroke a rectangle.
foreign import strokeRect :: Context2D -> Rectangle -> Effect Unit
-- | Clear a rectangle.
foreign import clearRect :: Context2D -> Rectangle -> Effect Unit
-- | An object representing a scaling transform:
-- |
-- | - The scale factors in the `x` and `y` directions, `scaleX` and `scaleY`.
type ScaleTransform =
{ scaleX :: Number
, scaleY :: Number
}
-- | Apply a scaling transform.
foreign import scale :: Context2D -> ScaleTransform -> Effect Unit
-- | Apply a rotation.
foreign import rotate :: Context2D -> Number -> Effect Unit
-- | An object representing a translation:
-- |
-- | - The translation amounts in the `x` and `y` directions, `translateX` and `translateY`.
type TranslateTransform =
{ translateX :: Number
, translateY :: Number
}
-- | Apply a translation
foreign import translate :: Context2D -> TranslateTransform -> Effect Unit
-- | An object representing a general transformation as a homogeneous matrix.
type Transform =
{ a :: Number
, b :: Number
, c :: Number
, d :: Number
, e :: Number
, f :: Number
}
-- | Apply a general transformation to the current transformation matrix
foreign import transform :: Context2D -> Transform -> Effect Unit
-- | Set the transformation matrix
foreign import setTransform :: Context2D -> Transform -> Effect Unit
-- | Enumerates types of text alignment.
data TextAlign
= AlignLeft | AlignRight | AlignCenter | AlignStart | AlignEnd
derive instance eqTextAlign :: Eq TextAlign
instance showTextAlign :: Show TextAlign where
show AlignLeft = "AlignLeft"
show AlignRight = "AlignRight"
show AlignCenter = "AlignCenter"
show AlignStart = "AlignStart"
show AlignEnd = "AlignEnd"
foreign import textAlignImpl :: Context2D -> Effect String
-- | Get the current text alignment.
textAlign :: Context2D -> Effect TextAlign
textAlign ctx = unsafeParseTextAlign <$> textAlignImpl ctx
where
unsafeParseTextAlign :: String -> TextAlign
unsafeParseTextAlign "left" = AlignLeft
unsafeParseTextAlign "right" = AlignRight
unsafeParseTextAlign "center" = AlignCenter
unsafeParseTextAlign "start" = AlignStart
unsafeParseTextAlign "end" = AlignEnd
unsafeParseTextAlign align = unsafeThrow $ "invalid TextAlign: " <> align
-- ^ dummy to silence compiler warnings
foreign import setTextAlignImpl :: Context2D -> String -> Effect Unit
-- | Set the current text alignment.
setTextAlign :: Context2D -> TextAlign -> Effect Unit
setTextAlign ctx textalign =
setTextAlignImpl ctx (toString textalign)
where
toString AlignLeft = "left"
toString AlignRight = "right"
toString AlignCenter = "center"
toString AlignStart = "start"
toString AlignEnd = "end"
-- | Enumerates types of text baseline.
data TextBaseline
= BaselineTop
| BaselineHanging
| BaselineMiddle
| BaselineAlphabetic
| BaselineIdeographic
| BaselineBottom
instance showTextBaseline :: Show TextBaseline where
show BaselineTop = "BaselineTop"
show BaselineHanging = "BaselineHanging"
show BaselineMiddle = "BaselineMiddle"
show BaselineAlphabetic = "BaselineAlphabetic"
show BaselineIdeographic = "BaselineIdeographic"
show BaselineBottom = "BaselineBottom"
foreign import textBaselineImpl :: Context2D -> Effect String
-- | Get the current text baseline.
textBaseline :: Context2D -> Effect TextBaseline
textBaseline ctx = unsafeParseTextBaseline <$> textBaselineImpl ctx
where
unsafeParseTextBaseline :: String -> TextBaseline
unsafeParseTextBaseline "top" = BaselineTop
unsafeParseTextBaseline "hanging" = BaselineHanging
unsafeParseTextBaseline "middle" = BaselineMiddle
unsafeParseTextBaseline "alphabetic" = BaselineAlphabetic
unsafeParseTextBaseline "ideographic" = BaselineIdeographic
unsafeParseTextBaseline "bottom" = BaselineBottom
unsafeParseTextBaseline align = unsafeThrow $ "invalid TextBaseline: " <> align
-- ^ dummy to silence compiler warnings
foreign import setTextBaselineImpl :: Context2D -> String -> Effect Unit
-- | Set the current text baseline.
setTextBaseline :: Context2D -> TextBaseline -> Effect Unit
setTextBaseline ctx textbaseline =
setTextBaselineImpl ctx (toString textbaseline)
where
toString BaselineTop = "top"
toString BaselineHanging = "hanging"
toString BaselineMiddle = "middle"
toString BaselineAlphabetic = "alphabetic"
toString BaselineIdeographic = "ideographic"
toString BaselineBottom = "bottom"
-- | Text metrics:
-- |
-- | - The text width in pixels.
type TextMetrics = { width :: Number }
-- | Get the current font.
foreign import font :: Context2D -> Effect String
-- | Set the current font.
foreign import setFont :: Context2D -> String -> Effect Unit
-- | Fill some text.
foreign import fillText :: Context2D -> String -> Number -> Number -> Effect Unit
-- | Stroke some text.
foreign import strokeText :: Context2D -> String -> Number -> Number -> Effect Unit
-- | Measure some text.
foreign import measureText :: Context2D -> String -> Effect TextMetrics
-- | Save the current context.
foreign import save :: Context2D -> Effect Unit
-- | Restore the previous context.
foreign import restore :: Context2D -> Effect Unit
-- | A convenience function: run the action, preserving the existing context.
-- |
-- | For example, outside this block, the fill style is preseved:
-- |
-- | ```purescript
-- | withContext ctx $ do
-- | setFillStyle ctx "red"
-- | ...
-- | ```
withContext :: forall a. Context2D -> Effect a -> Effect a
withContext ctx action = do
_ <- save ctx
a <- action
_ <- restore ctx
pure a
-- | Get image data for a portion of the canvas.
foreign import getImageData :: Context2D -> Number -> Number -> Number -> Number -> Effect ImageData
-- | Set image data for a portion of the canvas.
foreign import putImageDataFull :: Context2D -> ImageData -> Number -> Number -> Number -> Number -> Number -> Number -> Effect Unit
-- | Set image data for a portion of the canvas.
foreign import putImageData :: Context2D -> ImageData -> Number -> Number -> Effect Unit
-- | Create an image data object.
foreign import createImageData :: Context2D -> Number -> Number -> Effect ImageData
-- | Create a copy of an image data object.
foreign import createImageDataCopy :: Context2D -> ImageData -> Effect ImageData
-- | Create an image data object given a `Uint8ClampedArray` containing the underlying pixel representation of the image.
-- | The height is inferred from the array's size and the given width.
foreign import createImageDataWith :: Uint8ClampedArray -> Int -> Effect ImageData
-- | Get the width of an `ImageData` object.
foreign import imageDataWidth :: ImageData -> Int
-- | Get the height of an `ImageData` object.
foreign import imageDataHeight :: ImageData -> Int
-- | Get the underlying buffer from an `ImageData` object.
foreign import imageDataBuffer :: ImageData -> Uint8ClampedArray
-- | Format arguments for `toBlob'`.
-- | Quality for lossy compression is given as a `Number` between `0.0` and `1.0`.
data BlobFormat = Lossless MediaType | Lossy MediaType Number
-- Testing in my Firefox, these are the only two types supported,
-- with no support for `image/gif` or `image/svg+xml`... but is that standard?
blobPNG :: BlobFormat
blobPNG = Lossless imagePNG
blobJPEG :: Number -> BlobFormat
blobJPEG = Lossy imageJPEG
-- Thanks to mikesol for letting me know about makeAff!
blobCBToAff :: forall a. ((a -> Effect Unit) -> Effect Unit) -> Aff a
blobCBToAff b = makeAff \cb -> b (cb <<< Right) $> mempty
foreign import toBlobDefault :: CanvasElement -> (Blob -> Effect Unit) -> Effect Unit
-- | Create a `Blob` of the image data on the canvas, as a PNG file.
toBlob :: CanvasElement -> Aff Blob
toBlob = blobCBToAff <<< toBlobDefault
foreign import toBlobFormat :: Fn2 String CanvasElement ((Blob -> Effect Unit) -> Effect Unit)
foreign import toBlobFormatQuality :: Fn3 String Number CanvasElement ((Blob -> Effect Unit) -> Effect Unit)
-- | Create a `Blob` of the image data on the canvas, in the specified format.
toBlob' :: BlobFormat -> CanvasElement -> Aff Blob
toBlob' (Lossless (MediaType format)) = blobCBToAff <<< runFn2 toBlobFormat format
toBlob' (Lossy (MediaType format) quality) = blobCBToAff <<< runFn3 toBlobFormatQuality format quality
foreign import drawImage :: Context2D -> CanvasImageSource -> Number -> Number -> Effect Unit
foreign import drawImageScale :: Context2D -> CanvasImageSource -> Number -> Number -> Number -> Number -> Effect Unit
foreign import drawImageFull :: Context2D -> CanvasImageSource -> Number -> Number -> Number -> Number -> Number -> Number -> Number -> Number -> Effect Unit
-- | Enumerates the different types of pattern repetitions.
data PatternRepeat = Repeat | RepeatX | RepeatY | NoRepeat
derive instance eqPatternRepeat :: Eq PatternRepeat
instance showPatternRepeat :: Show PatternRepeat where
show Repeat = "Repeat"
show RepeatX = "RepeatX"
show RepeatY = "RepeatY"
show NoRepeat = "NoRepeat"
foreign import createPatternImpl :: Context2D -> CanvasImageSource -> String -> Effect CanvasPattern
-- | Create a new canvas pattern (repeatable image).
createPattern :: Context2D -> CanvasImageSource -> PatternRepeat -> Effect CanvasPattern
createPattern context img repeat = createPatternImpl context 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 :: Context2D -> CanvasPattern -> Effect Unit
-- | A type representing a linear gradient.
-- | - Starting point coordinates: (`x0`, `y0`)
-- | - Ending point coordinates: (`x1`, `y1`)
type LinearGradient =
{ x0 :: Number
, y0 :: Number
, x1 :: Number
, y1 :: Number
}
-- | Create a linear CanvasGradient.
foreign import createLinearGradient :: Context2D -> LinearGradient -> Effect CanvasGradient
-- | 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`
type RadialGradient =
{ x0 :: Number
, y0 :: Number
, r0 :: Number
, x1 :: Number
, y1 :: Number
, r1 :: Number
}
-- | Create a radial CanvasGradient.
foreign import createRadialGradient :: Context2D -> RadialGradient -> Effect CanvasGradient
-- | Add a single color stop to a CanvasGradient.
foreign import addColorStop :: CanvasGradient -> Number -> String -> Effect Unit
-- | Set the Context2D fillstyle to the CanvasGradient.
foreign import setGradientFillStyle :: Context2D -> CanvasGradient -> Effect Unit
-- | A type representing a quadratic Bézier curve.
-- | - Bézier control point: (`cpx`, `cpy`)
-- | - Ending point coordinates: (`x`, `y`)
type QuadraticCurve =
{ cpx :: Number
, cpy :: Number
, x :: Number
, y :: Number
}
-- | Draw a quadratic Bézier curve.
foreign import quadraticCurveTo :: Context2D -> QuadraticCurve -> Effect Unit
-- | 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`)
type BezierCurve =
{ cp1x :: Number
, cp1y :: Number
, cp2x :: Number
, cp2y :: Number
, x :: Number
, y :: Number
}
-- | Draw a cubic Bézier curve.
foreign import bezierCurveTo :: Context2D -> BezierCurve -> Effect Unit
-- | Set the image smoothing enabled flag.
foreign import imageSmoothingEnabled :: Context2D -> Boolean -> Effect Unit