GoogleChromeLabs / houdini-samples

Demos for different Houdini APIs
https://googlechromelabs.github.io/houdini-samples/
Apache License 2.0
973 stars 110 forks source link

Worklet scope compared to Web Worker scope #42

Closed YerkoPalma closed 6 years ago

YerkoPalma commented 6 years ago

Hi πŸ‘‹ Thanks for this module! Your examples have been very helpful. Now, I'm confused about the scope of Worklets, the posts about worklets say they are like a lightweight web wroker, so, because of some of their similarities I though I could use the sames APIs availaible for workers, but that isn't true.

My specific use case, is that I was trying to draw an Image from a paintWorklet.

Here is the code of my worklet

registerPaint('tiles', class {
  paint (ctx, geom, properties) {
    fetch('https://mdn.mozillademos.org/files/5397/rhino.jpg')
    .then(response => response.blob())
    .then(blob => createImageBitmap(blob))
    .then(bitmap => {
      ctx.drawImage(bitmap, 33, 71, 104, 124, 21, 20, 87, 104)
    })
  }
})

This throws that fetch is not defined

image

So the question is, what APIs are availaible from a paintworklet? or any worklet?

surma commented 6 years ago

they are β€œlike” a lightweight web worker in the sense that they are a separate scope and (potentially) thread. But they have almost no APIs exposed to them.

If you need to fetch an image for a paint worklet, you need to do that on the main thread and pass the image via custom properties.

YerkoPalma commented 6 years ago

@surma thanks for your response. One last thing

If you need to fetch an image for a paint worklet, you need to do that on the main thread and pass the image via custom properties.

Is possible to pass image data through custom properties? wow how do you do that?

surma commented 6 years ago

Yes, you can in combination with Typed OM and Properties & Values API, although none of these are currently fully implemented in Chrome to support images as far as I know.

YerkoPalma commented 6 years ago

Oh I see, just found this example about image loading. Hope this get implemented soon :) thanks again