Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add small canvas sample #7

Merged
merged 9 commits into from
Nov 16, 2024
Merged

Add small canvas sample #7

merged 9 commits into from
Nov 16, 2024

Conversation

nojaf
Copy link
Collaborator

@nojaf nojaf commented Nov 13, 2024

Trying out the sample from https://www.youtube.com/watch?v=2osWIorQDMU
It was immediately apparent that CanvasAPI is currently incomplete.
This will unfortunately be the case for a lot of cases.

document->Document.getElementById("myCanvas")->Prelude.unsafeConversation
let ctx = myCanvas->HTMLCanvasElement.getContext(~contextId="2d")

ctx.fillStyle = Prelude.unsafeConversation("red")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fillStyle is a property that can be multiple things:

"properties": {
          "property": {
            "fillStyle": {
              "name": "fillStyle",
              "type": [
                {
                  "type": "DOMString",
                  "nullable": false
                },
                {
                  "type": "CanvasGradient",
                  "nullable": false
                },
                {
                  "type": "CanvasPattern",
                  "nullable": false
                }
              ],
              "nullable": false,
              "static": false,
              "stringifier": false,
              "readonly": false,
              "secureContext": false,
              "mdnUrl": "https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/fillStyle"
            }
          },

This is a gap in the current approach:

  • It is impossible to model this as a unboxed variant
  • The property can only be defined once, so we can't have multiple versions of that.

Workaround I see here is get and set functions.

type canvasRenderingContext2D

type canvasGradient

type canvasPattern

@get external fillStyle : canvasRenderingContext2D => string = "fillStyle"
@get external fillStyleAsCanvasGradient : canvasRenderingContext2D => canvasGradient = "fillStyle"

let c : canvasRenderingContext2D = %todo
let fs = c->fillStyle
let fs2 = c->fillStyleAsCanvasGradient

@set external setFillStyle : (canvasRenderingContext2D,string) => unit = "fillStyle"

setFillStyle(c, "red")

While the setter is fine, the getter could still be unsafe if the wrong assumption is made.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some other train of thought to model this:

type canvasGradient

type canvasPattern

type fillStyle

external instanceof: 't => string = "instanceof"
external unsafeCast: 't => 'u = "%identity"

let castWhen = condition => t => condition(t) ? Some(unsafeCast(t)) : None

module FillStyle = {
  external fromString: string => fillStyle = "%identity"
  external fromCanvasGradient: canvasGradient => fillStyle = "%identity"
  external fromCanvasPattern: canvasGradient => fillStyle = "%identity"

  let asString: fillStyle => option<string> = castWhen(t =>
    Type.typeof(t) == #string
  )

  let asCanvasGradient: fillStyle => option<canvasGradient> = castWhen(t =>
    instanceof(t) == "CanvasGradient"
  )

  let asCanvasPattern: fillStyle => option<canvasPattern> = castWhen(t =>
    instanceof(t) === "CanvasPattern"
  )
}

type canvasRenderingContext2D = {fillStyle: fillStyle}

let c: canvasRenderingContext2D = %todo

let fs = c.fillStyle

switch FillStyle.asString(c.fillStyle) {
| None => Console.log("no string")
| Some(v) => Console.log(`value is ${v}`)
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a pretty common way to model this:

If it's just about setting a value, it's easy to model with an abstract type and makers, just like you did above:

type canvasGradient
type canvasPattern

module FillStyle = {
  type t

  external fromString: string => t = "%identity"
  external fromCanvasGradient: canvasGradient => t = "%identity"
  external fromCanvasPattern: canvasGradient => t = "%identity"
}

let x: xx = {
  fillStyle: FillStyle.fromString("red"),
}

The tooling is also built to handle this scenario, so if you do completions at fillStyle: <com> you'll get this, which is quite helpful:
image

Now, if you also want to decode this into something usable, you indeed need to drop into unsafe land. I like to do it just with %raw:

type canvasGradient
type canvasPattern

module FillStyle = {
  type t
  
  external fromString: string => t = "%identity"
  external fromCanvasGradient: canvasGradient => t = "%identity"
  external fromCanvasPattern: canvasGradient => t = "%identity"
  
  @tag("type")
  type decoded = String(string) | CanvasGradient(canvasGradient) | CanvasPattern(canvasPattern)
  
  let decode: t => decoded = %raw(`function decodeFillStyle(t) {
    if (t instanceof CanvasGradient) {
        return {type: "CanvasGradient", _0: t};
    } else if (t instanceof CanvasPattern) {
        return {type: "CanvasPattern", _0: t};
    } else {
        return {type: "String", _0: t};
    }
  }`)
}

type xx = {fillStyle: FillStyle.t}

let x: xx = {
  fillStyle: FillStyle.fromString("red"),
}

switch x.fillStyle->FillStyle.decode {
| String(s) => Console.log(s)
| CanvasGradient(g) => Console.log(g)
| CanvasPattern(p) => Console.log(p)
}

This obviously isn't great, but still pretty OK I'd say given that it's contained in there. Tooling helps a bit here as well, although we could make this more accessible:
image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is very insightful!
I'm not sure what users will do with fillStyle.
Maybe read, maybe write. This is a more common problem with this project, it is hard to say upfront what user will do precisely.
I'd say both should be possible.

@nojaf
Copy link
Collaborator Author

nojaf commented Nov 14, 2024

@zth would you mind reviewing this?

Please focus on the new test, approach to fillStyle and getContext modelling.
Does this seems reasonable?

@nojaf
Copy link
Collaborator Author

nojaf commented Nov 16, 2024

@zth I'm gonna merge this, sorry for my lack of patience.
I've add a great deal of lore in the contributing section of the docs.
Feel free to point out any remarks you might have, we can always revisit things later.

@nojaf nojaf merged commit 9c2a3bd into main Nov 16, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants