Skip to content

Commit

Permalink
Use image decode function to ensure that image is fully ready (bubkoo…
Browse files Browse the repository at this point in the history
  • Loading branch information
merapi authored and istaiti committed Feb 7, 2023
1 parent cb82c34 commit add8e5c
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 5 deletions.
4 changes: 1 addition & 3 deletions README.md
Expand Up @@ -300,12 +300,10 @@ Only standard lib is currently used, but make sure your browser supports:
- [Promise](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)
- SVG `<foreignObject>` tag

It's tested on latest Chrome and Firefox (49 and 45 respectively at the time of writing), with Chrome performing significantly better on big DOM trees, possibly due to it's more performant SVG support, and the fact that it supports `CSSStyleDeclaration.cssText` property.
It's tested on latest Chrome, Firefox and Safari (49, 45 and 16 respectively at the time of writing), with Chrome performing significantly better on big DOM trees, possibly due to it's more performant SVG support, and the fact that it supports `CSSStyleDeclaration.cssText` property.

*Internet Explorer is not (and will not be) supported, as it does not support SVG `<foreignObject>` tag.*

*Safari is not supported, as it uses a stricter security model on `<foreignObject>` tag. Suggested workaround is to use `toSvg` and render on the server.*

## How it works

There might some day exist (or maybe already exists?) a simple and standard way of exporting parts of the HTML to image (and then this script can only serve as an evidence of all the hoops I had to jump through in order to get such obvious thing done) but I haven't found one so far.
Expand Down
2 changes: 1 addition & 1 deletion src/embed-images.ts
Expand Up @@ -40,7 +40,7 @@ async function embedImageNode<T extends HTMLElement | SVGImageElement>(

const dataURL = await resourceToDataURL(url, getMimeType(url), options)
await new Promise((resolve, reject) => {
clonedNode.onload = resolve
clonedNode.decode = resolve
clonedNode.onerror = reject
if (clonedNode instanceof HTMLImageElement) {
clonedNode.srcset = ''
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Expand Up @@ -183,7 +183,7 @@ export function canvasToBlob(
export function createImage(url: string): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
const img = new Image()
img.onload = () => resolve(img)
img.decode = () => resolve(img)
img.onerror = reject
img.crossOrigin = 'anonymous'
img.decoding = 'sync'
Expand Down

0 comments on commit add8e5c

Please sign in to comment.