This repository was archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathWebapi__Canvas__Canvas2d.re
215 lines (179 loc) · 7.78 KB
/
Webapi__Canvas__Canvas2d.re
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
type t; /* Main type, representing the 2d canvas rendering context object */
type gradient;
type pattern;
type measureText;
/* Sub-modules (and their interfaces) for string enum arguments: */
module type CompositeType = {
type t = pri string;
let sourceOver: t;
let sourceIn: t;
let sourceOut: t;
let sourceAtop: t;
let destinationOver: t;
let destinationIn: t;
let destinationOut: t;
let destinationAtop: t;
let lighter: t;
let copy: t;
let xor: t;
};
module Composite: CompositeType = {
type t = string;
let sourceOver: t = "source-over";
let sourceIn: t = "source-in";
let sourceOut: t = "source-out";
let sourceAtop: t = "source-atop";
let destinationOver: t = "destination-over";
let destinationIn: t = "destination-in";
let destinationOut: t = "destination-out";
let destinationAtop: t = "destination-atop";
let lighter: t = "lighter";
let copy: t = "copy";
let xor: t = "xor";
};
module type LineCapType = {
type t = pri string;
let butt: t;
let round: t;
let square: t;
};
module LineCap: LineCapType = {
type t = string;
let butt: t = "butt";
let round: t = "round";
let square: t = "square";
};
module type LineJoinType = {
type t = pri string;
let round: t;
let bevel: t;
let miter: t;
};
module LineJoin: LineJoinType = {
type t = string;
let round: t = "round";
let bevel: t = "bevel";
let miter: t = "miter";
};
type image('a) =
| Number: image(float)
| ImageData: image(Webapi__Dom__Image.t);
type style(_) =
| String: style(string)
| Gradient: style(gradient)
| Pattern: style(pattern);
/* 2d Canvas API, following https://simon.html5.org/dump/html5-canvas-cheat-sheet.html */
[@bs.send.pipe : t] external save : unit = "save";
[@bs.send.pipe : t] external restore : unit = "restore";
/* Transformation */
[@bs.send.pipe : t] external scale : (~x: float, ~y: float) => unit = "scale";
[@bs.send.pipe : t] external rotate : float => unit = "rotate";
[@bs.send.pipe : t] external translate : (~x: float, ~y: float) => unit = "translate";
[@bs.send.pipe : t] external transform : (~m11: float, ~m12: float, ~m21: float, ~m22: float, ~dx: float, ~dy: float) => unit = "transform";
[@bs.send.pipe : t] external setTransform : (~m11: float, ~m12: float, ~m21: float, ~m22: float, ~dx: float, ~dy: float) => unit = "setTransform";
/* Compositing */
[@bs.set] external globalAlpha : (t, float) => unit = "globalAlpha";
[@bs.set] external globalCompositeOperation : (t, Composite.t) => unit = "globalCompositeOperation";
/* Line Styles */
[@bs.set] external lineWidth : (t, float) => unit = "lineWidth";
[@bs.set] external lineCap : (t, LineCap.t) => unit = "lineCap";
[@bs.set] external lineJoin : (t, LineJoin.t) => unit = "lineJoin";
[@bs.set] external miterLimit : (t, float) => unit = "miterLimit";
/* Colors, Styles, and Shadows */
[@bs.set] external setFillStyle : (t, 'a) => unit = "fillStyle";
[@bs.set] external setStrokeStyle : (t, 'a) => unit = "strokeStyle";
/* in re unused warnings
awaiting release of https://github.com/bloomberg/bucklescript/issues/1656
to just use [@@bs.set] directly with an ignored (style a) */
let setStrokeStyle = (type a, ctx: t, _: style(a), v: a) =>
setStrokeStyle(ctx, v);
let setFillStyle = (type a, ctx: t, _: style(a), v: a) =>
setFillStyle(ctx, v);
let reifyStyle = (type a, x: 'a) : (style(a), a) => {
let isCanvasGradient: 'a => bool = [%raw {|
function(x) { return x instanceof CanvasGradient; }
|}];
let isCanvasPattern: 'a => bool = [%raw {|
function(x) { return x instanceof CanvasPattern; }
|}];
(
if (Js.typeof(x) == "string") Obj.magic(String)
else if (isCanvasGradient(x)) Obj.magic(Gradient)
else if (isCanvasPattern(x)) Obj.magic(Pattern)
else invalid_arg(
"Unknown canvas style kind. Known values are: String, CanvasGradient, CanvasPattern"),
Obj.magic(x)
);
};
[@bs.get] external fillStyle : t => 'a = "fillStyle";
[@bs.get] external strokeStyle : t => 'a = "strokeStyle";
let fillStyle = (ctx: t) =>
ctx |> fillStyle |> reifyStyle;
let strokeStyle = (ctx: t) =>
ctx |> strokeStyle |> reifyStyle;
[@bs.set] external shadowOffsetX : (t, float) => unit = "shadowOffsetX";
[@bs.set] external shadowOffsetY : (t, float) => unit = "shadowOffsetY";
[@bs.set] external shadowBlur : (t, float) => unit = "shadowBlur";
[@bs.set] external shadowColor : (t, string) => unit = "shadowColor";
/* Gradients */
[@bs.send.pipe : t] external createLinearGradient : (~x0: float, ~y0: float, ~x1: float, ~y1: float) => gradient = "createLinearGradient";
[@bs.send.pipe : t] external createRadialGradient : (~x0: float, ~y0: float, ~x1: float, ~y1: float, ~r0: float, ~r1: float) => gradient = "createRadialGradient";
[@bs.send.pipe : gradient] external addColorStop : (float, string) => unit = "addColorStop";
[@bs.val] external createPattern : (
t,
Dom.element,
[@bs.string] [
| `repeat
[@bs.as "repeat-x"] | `repeatX
[@bs.as "repeat-y"] | `repeatY
[@bs.as "no-repeat"] | `noRepeat
]
)
=> pattern = "createPattern";
/* Paths */
[@bs.send.pipe : t] external beginPath : unit = "beginPath";
[@bs.send.pipe : t] external closePath : unit = "closePath";
[@bs.send.pipe : t] external fill : unit = "fill";
[@bs.send.pipe : t] external stroke : unit = "stroke";
[@bs.send.pipe : t] external clip : unit = "clip";
[@bs.send.pipe : t] external moveTo : (~x: float, ~y: float) => unit = "moveTo";
[@bs.send.pipe : t] external lineTo : (~x: float, ~y: float) => unit = "lineTo";
[@bs.send.pipe : t] external quadraticCurveTo : (~cp1x: float, ~cp1y: float, ~x: float, ~y: float) => unit = "quadraticCurveTo";
[@bs.send.pipe : t] external bezierCurveTo : (~cp1x: float, ~cp1y: float, ~cp2x: float, ~cp2y: float, ~x: float, ~y: float) => unit = "bezierCurveTo";
[@bs.send.pipe : t] external arcTo : (~x1: float, ~y1: float, ~x2: float, ~y2: float, ~r: float) => unit = "arcTo";
[@bs.send.pipe : t] external arc : (~x: float, ~y: float, ~r: float, ~startAngle: float, ~endAngle: float, ~anticw: bool) => unit = "arc";
[@bs.send.pipe : t] external rect : (~x: float, ~y: float, ~w: float, ~h: float) => unit = "rect";
[@bs.send.pipe : t] external isPointInPath : (~x: float, ~y: float) => bool = "isPointInPath";
/* Text */
[@bs.set] external font : (t, string) => unit = "font";
[@bs.set] external textAlign : (t, string) => unit = "textAlign";
[@bs.set] external textBaseline : (t, string) => unit = "textBaseline";
[@bs.send.pipe : t] external fillText : (string, ~x: float, ~y: float, ~maxWidth: float=?) => unit = "fillText";
[@bs.send.pipe : t] external strokeText : (string, ~x: float, ~y: float, ~maxWidth: float=?) => unit = "strokeText";
[@bs.send.pipe : t] external measureText : string => measureText = "measureText";
[@bs.get] external width : measureText => float = "measureText";
/* Rectangles */
[@bs.send.pipe : t] external fillRect : (~x: float, ~y: float, ~w: float, ~h: float) => unit = "fillRect";
[@bs.send.pipe : t] external strokeRect : (~x: float, ~y: float, ~w: float, ~h: float) => unit = "strokeRect";
[@bs.send.pipe : t] external clearRect : (~x: float, ~y: float, ~w: float, ~h: float) => unit = "clearRect";
[@bs.send] external createImageDataCoords : (t, ~width: float, ~height: float) => Webapi__Dom__Image.t = "createImageData";
[@bs.send] external createImageDataFromImage : (t, Webapi__Dom__Image.t) => Webapi__Dom__Image.t = "createImageData";
[@bs.send] external getImageData : (t, ~sx: float, ~sy: float, ~sw: float, ~sh: float) => Webapi__Dom__Image.t = "getImageData";
[@bs.send] external putImageData : (
t,
~imageData: Webapi__Dom__Image.t,
~dx: float,
~dy: float
)
=> unit = "putImageData";
[@bs.send] external putImageDataWithDirtyRect : (
t,
~imageData: Webapi__Dom__Image.t,
~dx: float,
~dy: float,
~dirtyX: float,
~dirtyY: float,
~dirtyWidth: float,
~dirtyHeight: float
)
=> unit = "putImageData";