I'm wondering how to create a and set a fill style using bs-webapi and Canvas2d interface in ReasonML.
I think the definition I might need is this:
let fillStyle = (ctx: t) =>
ctx |> fillStyle |> reifyStyle;
But I'm having trouble understanding it.
I have previously used this project as a source of examples, but I think the bs-webapi has changed since that project was authored. At least the following line modeled after the example:
Canvas2d.fillStyle(ctx, "rgba(0,255,255,255)");
gives me this error:
Error: This function has type
Webapi.Canvas.Canvas2d.t => (Webapi.Canvas.Canvas2d.style('a), 'a)
It is applied to too many arguments; maybe you forgot a `;'.
The function you want is
setFillStyle
:This was renamed from
fillStyle
long ago, because there was no getter back then and the convention is to name the getterfillStyle
and the settersetFillStyle
. At the same time it was made to support gradients and pattern.The way
setFillStyle
now works is that the second argument determines the type of the third. If you passString
as the second argument, the third is required to be astring
. If you passGradient
it needs to be agradient
, which you can get fromcreateLinearGradient
orcreateRadialGradient
. And lastly you can passPattern
, which requires the third argument be apattern
obtained fromcreatePattern
.In general, you should refer to the Canvas2d test file for usage examples.