Repository navigation
Technical review: Document HTML-in-Canvas - #46289
chrisdavidmills wants to merge 24 commits into
Conversation
|
|
||
| ### Reporting on which elements were painted | ||
|
|
||
| This example shows how to render DOM elements onto a 2D canvas, and how the `paint` event can be used to expose data on what elements were repainted each time the event fires. |
There was a problem hiding this comment.
An important aspect of the paint event is that dom/css changes made in it do not show up in the current frame, and dom/css changes do not affect how elements are drawn (e.g., with drawElementImage), but canvas drawings made in the paint event DO show up in the current frame. Would you like to include something like that here?
A pattern we are encouraging developers to follow is to switch their requestAnimationFrame loops over to paint+requestPaint loops. Would you like to mention that? In other words, developers using html-in-canvas may want to change:
function update() {
// update state
// draw
requestAnimationFrame(update);
}
requestAnimationFrame(update);
To this:
canvas.onpaint = (e) => {
// update state
// draw
canvas.requestPaint();
};
There was a problem hiding this comment.
I actually talk about this stuff in the paint event reference page, as I thought that would be a better place for it. I've added a few details, but let me know if you think I've got anything wrong there.
I also updated all of my demos at mdn/dom-examples#406 to use the paint/requestPaint pattern rather than rAF, so that all of my stuff is consistent.
I think the main thing wrong with my demos currently is that on the WebGL/WebGPU demos, you can tab to the form and fill it out, but you can't click into the form with the mouse because I'm not updating the form geometry.
I think the best way for me to update this would be to stop the cube rotation when the canvas is moused over, and then update the form geometry to the front cube face as seen when rotation is paused. To do this, I guess I'd need to figure out the DOMMatrix to apply to do this, then clear the geometry when the cube starts spinning again. What do you think?
There was a problem hiding this comment.
In the explainer we have an example of the 3D geometry update for hit testing. It uses a particularly simple situation to make extracting the matrix easier, but you could certainly adapt it to work in more complex cases.
There was a problem hiding this comment.
@schenney-chromium which example should I be looking at to find this?
I managed to work out some code for this myself, with a little help from Claude, but I am wondering if it is the best or most efficient way to do this.
There was a problem hiding this comment.
You can see my work in mdn/dom-examples#406.
There was a problem hiding this comment.
The example is https://github.com/WICG/html-in-canvas/blob/main/Examples/webGL.html
I'll take a look at yours but I too am not an expert in WebGL coding.
schenney-chromium
left a comment
There was a problem hiding this comment.
Thanks so much for working on this.
In addition to the comments here, the drawable attribute also applies to SVGElement and MathMLElement. The former is still an open spec issue, while the latter has agreement from the MATHML Core folks (though it's not in the spec yet as far as I know).
|
|
||
| ### Reporting on which elements were painted | ||
|
|
||
| This example shows how to render DOM elements onto a 2D canvas, and how the `paint` event can be used to expose data on what elements were repainted each time the event fires. |
There was a problem hiding this comment.
In the explainer we have an example of the 3D geometry update for hit testing. It uses a particularly simple situation to make extracting the matrix easier, but you could certainly adapt it to work in more complex cases.
|
|
||
| {{APIRef("Canvas API")}} | ||
|
|
||
| The **`getElementImageDefaultSize()`** method of the {{domxref("HTMLCanvasElement")}} interface returns an object containing an element's original size as it would be rendered in the DOM, once rendered using the [HTML-in-Canvas API](/en-US/docs/Web/API/HTML-in-Canvas_API). The returned size is in canvas grid coordinates. |
There was a problem hiding this comment.
".. an object containing an element's original size as it ..."
We had so many conversations to figure out how best to explain this size!
".. an object containing the default size, in canvas pixels, of an ElementImage when drawn with drawElementImage and no destination rect."
There was a problem hiding this comment.
Right ;-)
I played around with your suggestion a bit, and ended up with:
The
getElementImageDefaultSize()method of the {{domxref("HTMLCanvasElement")}} interface returns an object containing the default size, in canvas units, of an element when drawn with {{domxref("CanvasRenderingContext2D.drawElementImage", "drawElementImage()")}} (specifying no destination size).
Does this work? I used "canvas units" instead of "canvas pixels", as that's what I've said elsewhere.
|
@schenney-chromium, thanks for the review work!
This is good to know. Are these implemented yet? Which version are they available in? Behind a pref? If they aren't in the specs yet, we can always include them on MDN with a "non-standard" flag for now. |
|
Thanks a lot for the reviews so far, @progers and @schenney-chromium. I've answered all your comments so far. The table in the description shows which pages still need a tech review (for reviewed pages, I've included your initials so you know who has looked at what). |
I checked for tests and we do have examples that work but nothing for MathML, although I see no reason for it to fail. I should create a MathML test case to verify. The attribute is not in the SVG or MathML specs yet. There is no special flag and I suspect they have always "just worked". |
schenney-chromium
left a comment
There was a problem hiding this comment.
The changes look good. Is there a preview page somewhere? I would like to do a final review pass on the content as it will appear on MDN.
@schenney-chromium In theory every PR generates one (though they can disappear after a few weeks if there are not edits). In this case you can find the links in #46289 (comment) |
Thanks. I should have known that from HTML specs. I'll try to do a final technical review pass on the pages that are ready in the next couple of days. Once again I appreciate the effort you're all putting into this work. |
@schenney-chromium I've added reference pages for the SVG and MathML I tested the element attributes to make sure you can nest SVG/MathML elements inside a However, when I try to get or set the values of |
Thanks.
I'll take a look at this., and learn something along the way. I'm not sure what the expected behavior is for attributes that do not take a value, that is |
…ringContext.texElementSubImage2D()
|
@schenney-chromium @progers @ewewraw great news — I've finished all the HTML-in-Canvas reference articles. There's quite a bit still to tech review — refer to the table at the top to see which ones you've looked at and which ones are still to do. |
Description
Chrome 157 adds support for the HTML-in-Canvas API; see https://chromestatus.com/feature/5172548013916160.
This PR documents HTML-in-Canvas.
<canvas>elementcontentattribute ✅drawableattribute ✅HTMLCanvasElementinterface landing page ✅HTMLCanvasElement.contentandHTMLElement.drawableproperties ✅HTMLCanvasElement.captureElementImage()method ✅HTMLCanvasElement.getElementImageDefaultSize()method ✅HTMLCanvasElement.requestPaint()method ✅HTMLCanvasElement.updateElementGeometry()method ✅HTMLCanvasElement.clearElementGeometry()method ✅HTMLCanvasElement.getElementTransform()method ✅HTMLCanvasElementpaintevent ✅HTMLCanvasElementelementgeometryupdateevent ✅OffscreenCanvas.updateElementGeometry()method ✅OffscreenCanvas.getElementImageDefaultSize()method ✅OffscreenCanvas.clearElementGeometry()method ✅drawableglobal attribute andSVGElementdrawableproperty ✅drawableglobal attribute andMathMLElementdrawableproperty ✅CanvasRenderingContext2D.drawElementImage()method ✅OffscreenCanvasRenderingContext2D.drawElementImage()method ✅WebGLRenderingContext.texElementSubImage2D()method ✅GPUQueue.drawElementImageToTexture()method ✅CanvasPaintEventinterface and members ✅ElementGeometryUpdateEventinterface and members ✅ElementImageinterface and members ✅Motivation
Additional details
Related issues and pull requests