-
Notifications
You must be signed in to change notification settings - Fork 0
Move processing to worker #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db400ed
WIP: moved processing to a worker and switched to JS modules
shanecranor a97253e
add loading bar for larger images
shanecranor ab95671
cleanup
shanecranor e57fcdc
use const
shanecranor 6f6ebab
handle worker not supported error
shanecranor 18b6b33
fix import
shanecranor 85d013c
use template literals
shanecranor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { createInfo, modifyInfo } from "/qbist.js" | ||
| import { loadStateFromParam } from "/qbistListeners.js" | ||
| function drawQbist(canvas, info, oversampling = 0) { | ||
| return new Promise((resolve, reject) => { | ||
| if (typeof Worker === 'undefined') { | ||
| reject(new Error('Web Workers are not supported in this browser')); | ||
| return; | ||
| } | ||
| const ctx = canvas.getContext("2d") | ||
| const width = canvas.width | ||
| const height = canvas.height | ||
|
|
||
| // Create the worker instance | ||
| const worker = new Worker("worker.js", { type: "module" }) | ||
| // Listen for messages from the worker | ||
| worker.addEventListener("message", (e) => { | ||
| const { command } = e.data | ||
| if (command === "progress") { | ||
| const { progress } = e.data | ||
| loadingOverlay.style.display = "flex" | ||
| loadingBar.style.width = `${progress}%` | ||
| } else if (command === "rendered") { | ||
| const { imageData } = e.data | ||
| const data = new Uint8ClampedArray(imageData) | ||
| const imgData = new ImageData(data, width, height) | ||
| ctx.putImageData(imgData, 0, 0) | ||
| worker.terminate() // Clean up the worker | ||
| loadingOverlay.style.display = "none" | ||
| resolve() // Resolve the Promise when rendering is complete | ||
| } | ||
| }) | ||
|
|
||
| // Listen for errors in the worker | ||
| worker.addEventListener("error", (err) => { | ||
| worker.terminate() // Clean up the worker on error | ||
| loadingOverlay.style.display = "none" | ||
| reject(err) | ||
| }) | ||
|
|
||
| // Prepare and send the payload to the worker | ||
| const payload = { | ||
| command: "render", | ||
| info, | ||
| width, | ||
| height, | ||
| oversampling, | ||
| } | ||
| worker.postMessage(payload) | ||
| }) | ||
| } | ||
|
|
||
| // --- Managing the 9-Panel Grid --- | ||
| export const formulas = new Array(9) | ||
| export const mainFormula = createInfo() | ||
|
|
||
| // Generate variations based on the current main formula. | ||
| export function generateFormulas() { | ||
| formulas[0] = mainFormula | ||
| for (let i = 1; i < 9; i++) { | ||
| formulas[i] = modifyInfo(mainFormula) | ||
| } | ||
| } | ||
|
|
||
| // Draw the large main pattern and each preview. | ||
| export function updateAll() { | ||
| const mainCanvas = document.getElementById("mainPattern") | ||
| drawQbist(mainCanvas, mainFormula, 1) | ||
| for (let i = 0; i < 9; i++) { | ||
| const canvas = document.getElementById(`preview${i}`) | ||
| drawQbist(canvas, formulas[i], 1) | ||
| } | ||
| const url = new URL(window.location.href) | ||
| url.searchParams.set("state", btoa(JSON.stringify(mainFormula))) | ||
| window.history.pushState({}, "", url) | ||
| } | ||
|
|
||
| // On page load, check if a state is provided in the URL. | ||
| function checkURLState() { | ||
| const urlParams = new URLSearchParams(window.location.search) | ||
| if (urlParams.has("state")) { | ||
| const state = urlParams.get("state") | ||
| loadStateFromParam(state) | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // --- Initialization --- | ||
| // Check if a state is provided in the URL and load it. | ||
| if (!checkURLState()) { | ||
| generateFormulas() | ||
| updateAll() | ||
| } | ||
|
|
||
| export async function downloadImage(outputWidth, outputHeight, oversampling) { | ||
| const exportCanvas = document.createElement("canvas") | ||
| exportCanvas.width = outputWidth | ||
| exportCanvas.height = outputHeight | ||
|
|
||
| await drawQbist(exportCanvas, mainFormula, oversampling) | ||
|
|
||
| const imageDataURL = exportCanvas.toDataURL("image/png") | ||
|
|
||
| // create a temporary download link and trigger the download | ||
| const link = document.createElement("a") | ||
| link.href = imageDataURL | ||
| link.download = "qbist.png" | ||
| //TODO: add metadata to the image so that it can be regenerated at another resolution | ||
| document.body.appendChild(link) | ||
| link.click() | ||
| document.body.removeChild(link) | ||
| } | ||
shanecranor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const loadingOverlay = document.getElementById("loadingOverlay") | ||
| const loadingBar = document.getElementById("loadingBar") | ||
| loadingOverlay.style.display = "none" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.