{"id":13394078,"url":"https://github.com/ai/offscreen-canvas","last_synced_at":"2025-04-04T20:06:43.185Z","repository":{"id":38420628,"uuid":"177840462","full_name":"ai/offscreen-canvas","owner":"ai","description":"Polyfill for OffscreenCanvas to move Three.js/WebGL/2D canvas to Web Worker","archived":false,"fork":false,"pushed_at":"2023-01-03T18:35:01.000Z","size":1438,"stargazers_count":335,"open_issues_count":7,"forks_count":13,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-11-24T17:54:35.381Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ai.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-26T17:47:44.000Z","updated_at":"2024-10-23T06:45:10.000Z","dependencies_parsed_at":"2023-02-01T08:47:07.340Z","dependency_job_id":null,"html_url":"https://github.com/ai/offscreen-canvas","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Foffscreen-canvas","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Foffscreen-canvas/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Foffscreen-canvas/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ai%2Foffscreen-canvas/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ai","download_url":"https://codeload.github.com/ai/offscreen-canvas/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247242670,"owners_count":20907133,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T17:01:08.214Z","updated_at":"2025-04-04T20:06:43.161Z","avatar_url":"https://github.com/ai.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Svg/Canvas"],"sub_categories":[],"readme":"# Offscreen Canvas Polyfill\n\nJS polyfill (**375 bytes**) for `OffscreenCanvas` to move **Three.js**,\n**WebGL** or 2D canvas to **Web Worker**.\n\nIt will improve performance in Chrome and will load worker by `\u003cscript\u003e`\nin Firefox, Safari, and other browsers.\n\nThe tutorial for this library:\n**[Faster WebGL/Three.js 3D graphics with OffscreenCanvas and Web Workers]**.\n\n```js\n// index.js\nimport createWorker from 'offscreen-canvas/create-worker'\n\nconst worker = createWorker(canvas, '/worker.js', e =\u003e {\n  // Messages from the worker\n})\n\nbutton.addEventListener('click', () =\u003e {\n  worker.post({ message: 'update' })\n})\n```\n\n```js\n// worker.js\nimport insideWorker from 'offscreen-canvas/inside-worker'\n\nconst worker = insideWorker(e =\u003e {\n  if (e.data.canvas) {\n    // Draw on the canvas\n  } else if (e.data.message === 'move') {\n    // Messages from main thread\n  }\n})\n```\n\n[Faster WebGL/Three.js 3D graphics with OffscreenCanvas and Web Workers]: https://dev.to/evilmartians/faster-webgl-three-js-3d-graphics-with-offscreencanvas-and-web-workers-43he\n\n\u003ca href=\"https://evilmartians.com/?utm_source=offscreen-canvas\"\u003e\n  \u003cimg src=\"https://evilmartians.com/badges/sponsored-by-evil-martians.svg\"\n       alt=\"Sponsored by Evil Martians\" width=\"236\" height=\"54\"\u003e\n\u003c/a\u003e\n\n## Usage\n\nCreate separated bundle in webpack, Parcel or any other bundler:\n\n```diff js\n  entry: {\n    app: './src/app.js',\n+   worker: './src/worker.js'\n  }\n```\n\nMove all code working with `\u003ccanvas\u003e` to `worker.js`. It means to move all WebGL\nor Three.js imports and scene related code.\n\n```js\nimport insideWorker from 'offscreen-canvas/inside-worker'\n// Move Three.js imports here if you use Three.js\n\nconst worker = insideWorker(e =\u003e {\n  if (e.data.canvas) {\n    // Move scene building code here\n  }\n})\n```\n\nSome of Three.js code (mostly loaders) will not work in Web Worker.\nUse `worker.isWorker` to switch loaders:\n\n```js\n    if (worker.isWorker) {\n      loader = new ImageBitmapLoader()\n    } else {\n      loader = new ImageLoader()\n    }\n```\n\nPut preload link to HTML templates with a URL to `worker.js`.\nYour bundle will add cache buster to bundle names, so bundle names will\nchange every time you deploy application. This is why we need to store\npath to `worker.js` in HTML:\n\n```diff html\n+   \u003clink rel=\"preload\" as=\"script\" href=\"./worker.js\"\u003e\n  \u003c/head\u003e\n```\n\nLoad worker in main `app.js`:\n\n```js\nimport createWorker from 'offscreen-canvas/create-worker'\n\nconst workerUrl = document.querySelector('[rel=preload][as=script]').href\nconst canvas = document.querySelector('canvas')\n\nconst worker = createWorker(canvas, workerUrl)\n```\n\nKeep all UI interaction code (listeners for clicks, mouse move, etc)\nin `app.js`. Send message to Worker when your need to update `\u003ccanvas\u003e`\nafter user actions:\n\n```js\nbutton.addEventListener('click', () =\u003e {\n  worker.post({ message: 'move' })\n})\n```\n\nProcess this messages in the worker:\n\n```diff js\n  const worker = insideWorker(e =\u003e {\n    if (e.data.canvas) {\n      // Move scene building code here\n-   }\n+   } else if (e.data.message === 'move') {\n+     // Move object on the scene\n+   }\n  })\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fai%2Foffscreen-canvas","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fai%2Foffscreen-canvas","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fai%2Foffscreen-canvas/lists"}