Skip to content

Document captureElementImage() approach to OffscreenCanvas#103

Open
foolip wants to merge 1 commit intomainfrom
captureElementImage
Open

Document captureElementImage() approach to OffscreenCanvas#103
foolip wants to merge 1 commit intomainfrom
captureElementImage

Conversation

@foolip
Copy link
Copy Markdown
Member

@foolip foolip commented Mar 27, 2026

No description provided.

readonly attribute unsigned long width;
readonly attribute unsigned long height;

// value of `id` attribute on element, or the empty string
Copy link
Copy Markdown
Collaborator

@progers progers Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreeing with the idea you shared with me offline, that we should remove the id attribute.

GPUImageCopyTextureTagged destination);
}

[Exposed=(Window,Worker)]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove Worker?


// value of `id` attribute on element, or the empty string
readonly attribute DOMString id;
};
Copy link
Copy Markdown
Collaborator

@progers progers Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stephen mentioned this in the chat; can you add void close();? This mirrors ImageBitmap and allows developers to explicitly release memory, rather than relying on GC.

Copy link
Copy Markdown
Collaborator

@progers progers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I'm okay with addressing these issues as followups.

@@ -159,10 +180,6 @@
};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add ElementImage captureElementImage(Element element);?

worker.postMessage({ canvas: offscreen }, [offscreen]);

// 3. Synchronize the element's CSS transform to match its drawn location.
// 2. Transfer control to the worker.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 2. Transfer control to the worker.
const offscreen = canvas.transferControlToOffscreen();
worker.postMessage({ canvas: offscreen }, [offscreen]);

// 3. Forward ElementImage snapshots to the worker.
canvas.onpaint = (event) => {
const changedMap = new Map();
for (const changedElement of event.changedElements) {
changedMap.set(changedElement.id, canvas.captureElementImage(changedElement));
}
worker.postMessage({ changed: changedMap }, [...changedMap.values()]);
};

`worker.js`:

```javascript
let ctx;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let ctx;
onmessage = ({data}) => {
// 1. Initial setup.
if (data.canvas) {
ctx = data.canvas.getContext('2d');
}

// 2. Respond to changes.
if (data.changed) {
if (data.changed.has('label')) {
let transform = ctx.drawElementImage(data.changed.get('label'), 0, 0);
self.postMessage({id: 'label', transform: transform});
}
if (data.changed.has('input')) {
let transform = ctx.drawElementImage(data.changed.get('input'), 100, 0);
self.postMessage({id: 'input', transform: transform});
}
}
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants