TheSpyder / rescript-webapi

ReScript bindings to the DOM and other Web APIs
http://tinymce.github.io/rescript-webapi/api/Webapi/
Other
149 stars 36 forks source link

Add drawImage bindings to canvas2d context #83

Closed jlrickert closed 2 years ago

jlrickert commented 2 years ago

Relates to issue #62

Things added

Example usage

open Webapi.Canvas
open Webapi.Canvas.Canvas2d
open Webapi.Dom

let canvasEl = document->Document.createElement("canvas")
let ctx = canvasEl->CanvasElement.getContext2d

let image = HtmlImageElement.makeWithSize(16, 16)
let _: unit = image->HtmlImageElement.drawImage(~ctx, ~dx=0.0, ~dy=0.0)
let _: unit =
  image->HtmlImageElement.drawImageScale(~ctx, ~dx=0.0, ~dy=0.0, ~dWidth=16.0, ~dHeight=16.0)
let _: unit =
  image->HtmlImageElement.drawImageFull(
    ~ctx,
    ~sx=0.0,
    ~sy=0.0,
    ~sWidth=16.0,
    ~sHeight=16.0,
    ~dx=0.0,
    ~dy=0.0,
    ~dHeight=16.0,
    ~dWidth=16.0,
  )

Things to do

CanvasImageSource could also be applied to a bunch of other modules. However, they don't seem to exist yet.

jlrickert commented 2 years ago

This could also be implemented with poly variants:

For example:

@send
external draw: (
  t,
  @unwrap [#Image(Webapi__Dom__HtmlImageElement.t) | #Canvas(Webapi__Dom__HtmlCanvasElement.t)],
  ~dx: float,
  ~dy: float,
) => unit = "drawImage"

let _: unit = ctx->draw(#Image(image), ~dx=0.0, ~dy=0.0)

Output:

ctx.drawImage(image, 0.0, 0.0);
spocke commented 2 years ago

This could also be implemented with poly variants:

I keep forgetting about that unwrap unary polymorphic variant constructors feature. That would be a really good fit for this case.

TheSpyder commented 2 years ago

I suspect @spocke forgot to update his request for changes so I'm just going to admin merge this.

TheSpyder commented 2 years ago

Released as 0.5.0