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

HtmlImageElement missing bindings for onload #85

Closed jlrickert closed 2 years ago

jlrickert commented 2 years ago

I am trying to draw an image onto a canvas. This is what I am trying to do:

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

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

let image = HtmlImageElement.make()
image->HtmlImageElement.setSrc("image.png")
image->HtmlImageElement.onloadSet(() => {
  ctx->drawImage(image->CanvasImageSource.ofImage, ~dx=0.0, ~dy=0.0)
})

There are two things missing here, CanvasImageSource and onloadSet. The CanvasImageSource is related to issue #62 and pr #83. onloadSet is related to GlobalEventHandlers.onload and is part of the GlobalEventHandlers mixin.

TheSpyder commented 2 years ago

What is the JS equivalent of what you're trying to do? Can't you use addLoadEventListener?

In general we don't support the "set function as a property" event handlers such as image.onload, since we consider image.addEventListener('load', ...) to be the more correct way to manage events.

jlrickert commented 2 years ago

I wasn't aware of addLoadEventListener. I was going off of the example at https://developer.mozilla.org/en-US/docs/Web/API/createImageBitmap#example where they are mutation onload directly.