Skip to content

Commit d86214d

Browse files
committed
Model fillStyle
1 parent 9469f99 commit d86214d

File tree

9 files changed

+100
-5
lines changed

9 files changed

+100
-5
lines changed

src/CanvasAPI/CanvasGradient.js

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CanvasAPI/CanvasGradient.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ Throws an "IndexSizeError" DOMException if the offset is out of range. Throws a
88
*/
99
@send
1010
external addColorStop: (canvasGradient, ~offset: float, ~color: string) => unit = "addColorStop"
11+
12+
let isInstanceOf = (_: 't): bool => %raw(`param instanceof CanvasGradient`)

src/CanvasAPI/CanvasPattern.js

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CanvasAPI/CanvasPattern.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ Sets the transformation matrix that will be used when rendering the pattern duri
77
*/
88
@send
99
external setTransform: (canvasPattern, ~transform: domMatrix2DInit=?) => unit = "setTransform"
10+
11+
let isInstanceOf = (_: 't): bool => %raw(`param instanceof CanvasPattern`)

src/DOMAPI.res

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ type shareData = {
201201
mutable url?: string,
202202
}
203203

204+
type fillStyle
205+
204206
/**
205207
The location (URL) of the object it is linked to. Changes done on it are reflected on the object it relates to. Both the Document and Window interface have such a linked Location, accessible via Document.location and Window.location respectively.
206208
[See Location on MDN](https://developer.mozilla.org/docs/Web/API/Location)
@@ -9396,7 +9398,7 @@ type canvasRenderingContext2D = {
93969398
/**
93979399
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/fillStyle)
93989400
*/
9399-
mutable fillStyle: unknown,
9401+
mutable fillStyle: fillStyle,
94009402
/**
94019403
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/shadowOffsetX)
94029404
*/

src/DOMAPI/FillStyle.js

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DOMAPI/FillStyle.res

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
open Prelude
2+
open CanvasAPI
3+
open DOMAPI
4+
5+
external fromString: string => fillStyle = "%identity"
6+
external fromCanvasGradient: canvasGradient => fillStyle = "%identity"
7+
external fromCanvasPattern: canvasGradient => fillStyle = "%identity"
8+
9+
type decoded =
10+
| String(string)
11+
| CanvasGradient(canvasGradient)
12+
| CanvasPattern(canvasPattern)
13+
14+
let decode = (t: fillStyle): decoded => {
15+
if CanvasGradient.isInstanceOf(t) {
16+
CanvasGradient(unsafeConversation(t))
17+
} else if CanvasPattern.isInstanceOf(t) {
18+
CanvasPattern(unsafeConversation(t))
19+
} else {
20+
String(unsafeConversation(t))
21+
}
22+
}

tests/DOMAPI/HTMLCanvasInputElement__tes.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/DOMAPI/HTMLCanvasInputElement__tes.res

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ let myCanvas: DOMAPI.htmlCanvasElement =
44
document->Document.getElementById("myCanvas")->Prelude.unsafeConversation
55
let ctx = myCanvas->HTMLCanvasElement.getContext_2D
66

7-
ctx.fillStyle = Prelude.unsafeConversation("red")
7+
ctx.fillStyle = FillStyle.fromString("red")
88
ctx->CanvasRenderingContext2D.fillRect(~x=50., ~y=50., ~w=200., ~h=200.)
99

10-
ctx.fillStyle = Prelude.unsafeConversation("black")
10+
ctx.fillStyle = FillStyle.fromString("black")
1111
ctx.font = "2px Tahoma"
1212
ctx.textBaseline = CanvasAPI.Top
1313
ctx->CanvasRenderingContext2D.fillText(~text="MY TEXT", ~x=60., ~y=60.)
14+
15+
switch ctx.fillStyle->FillStyle.decode {
16+
| FillStyle.String(color) => Console.log(`Color: ${color}`)
17+
| FillStyle.CanvasGradient(_) => Console.log("CanvasGradient")
18+
| FillStyle.CanvasPattern(_) => Console.log("CanvasPattern")
19+
}

0 commit comments

Comments
 (0)