npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

offscreen-canvas

v0.1.1

Published

Polyfill for OffscreenCanvas to move Three.js/WebGL/2D canvas to Web Worker

Downloads

1,745

Readme

Offscreen Canvas Polyfill

JS polyfill (375 bytes) for OffscreenCanvas to move Three.js, WebGL or 2D canvas to Web Worker.

It will improve performance in Chrome and will load worker by <script> in Firefox, Safari, and other browsers.

The tutorial for this library: Faster WebGL/Three.js 3D graphics with OffscreenCanvas and Web Workers.

// index.js
import createWorker from 'offscreen-canvas/create-worker'

const worker = createWorker(canvas, '/worker.js', e => {
  // Messages from the worker
})

button.addEventListener('click', () => {
  worker.post({ message: 'update' })
})
// worker.js
import insideWorker from 'offscreen-canvas/inside-worker'

const worker = insideWorker(e => {
  if (e.data.canvas) {
    // Draw on the canvas
  } else if (e.data.message === 'move') {
    // Messages from main thread
  }
})

Usage

Create separated bundle in webpack, Parcel or any other bundler:

  entry: {
    app: './src/app.js',
+   worker: './src/worker.js'
  }

Move all code working with <canvas> to worker.js. It means to move all WebGL or Three.js imports and scene related code.

import insideWorker from 'offscreen-canvas/inside-worker'
// Move Three.js imports here if you use Three.js

const worker = insideWorker(e => {
  if (e.data.canvas) {
    // Move scene building code here
  }
})

Some of Three.js code (mostly loaders) will not work in Web Worker. Use worker.isWorker to switch loaders:

    if (worker.isWorker) {
      loader = new ImageBitmapLoader()
    } else {
      loader = new ImageLoader()
    }

Put preload link to HTML templates with a URL to worker.js. Your bundle will add cache buster to bundle names, so bundle names will change every time you deploy application. This is why we need to store path to worker.js in HTML:

+   <link type="preload" as="script" href="./worker.js">
  </head>

Load worker in main app.js:

import createWorker from 'offscreen-canvas/create-worker'

const workerUrl = document.querySelector('[rel=preload][as=script]').href
const canvas = document.querySelector('canvas')

const worker = createWorker(canvas, workerUrl)

Keep all UI interaction code (listeners for clicks, mouse move, etc) in app.js. Send message to Worker when your need to update <canvas> after user actions:

button.addEventListener('click', () => {
  worker.post({ message: 'move' })
})

Process this messages in the worker:

  const worker = insideWorker(e => {
    if (e.data.canvas) {
      // Move scene building code here
-   }
+   } else if (e.data.message === 'move') {
+     // Move object on the scene
+   }
  })