Skip to content
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

feat: add toWebp() function #424

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ All the top level functions accept DOM node and rendering options, and return a
- [toPng](#toPng)
- [toSvg](#toSvg)
- [toJpeg](#toJpeg)
- [toWebp](#toWebp)
- [toBlob](#toBlob)
- [toCanvas](#toCanvas)
- [toPixelData](#toPixelData)
Expand Down Expand Up @@ -100,6 +101,19 @@ htmlToImage.toJpeg(document.getElementById('my-node'), { quality: 0.95 })
});
```

#### toWebp
Save and download a compressed WebP image:

```js
htmlToImage.toWebp(document.getElementById('my-node'), { quality: 0.95 })
.then(function (dataUrl) {
var link = document.createElement('a');
link.download = 'my-image-name.webp';
link.href = dataUrl;
link.click();
});
```

#### toBlob
Get a PNG image blob and download it (using [FileSaver](https://github.com/eligrey/FileSaver.js)):

Expand Down
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ export async function toJpeg<T extends HTMLElement>(
return canvas.toDataURL('image/jpeg', options.quality || 1)
}

export async function toWebp<T extends HTMLElement>(
node: T,
options: Options = {},
): Promise<string> {
const canvas = await toCanvas(node, options)
return canvas.toDataURL('image/webp', options.quality || 1)
}

export async function toBlob<T extends HTMLElement>(
node: T,
options: Options = {},
Expand Down
1 change: 1 addition & 0 deletions test/resources/small/image-webp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data:image/webp;base64,UklGRlgCAABXRUJQVlA4WAoAAAAgAAAAYwAACAAASUNDUMgBAAAAAAHIAAAAAAQwAABtbnRyUkdCIFhZWiAH4AABAAEAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAACRyWFlaAAABFAAAABRnWFlaAAABKAAAABRiWFlaAAABPAAAABR3dHB0AAABUAAAABRyVFJDAAABZAAAAChnVFJDAAABZAAAAChiVFJDAAABZAAAAChjcHJ0AAABjAAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAAgAAAAcAHMAUgBHAEJYWVogAAAAAAAAb6IAADj1AAADkFhZWiAAAAAAAABimQAAt4UAABjaWFlaIAAAAAAAACSgAAAPhAAAts9YWVogAAAAAAAA9tYAAQAAAADTLXBhcmEAAAAAAAQAAAACZmYAAPKnAAANWQAAE9AAAApbAAAAAAAAAABtbHVjAAAAAAAAAAEAAAAMZW5VUwAAACAAAAAcAEcAbwBvAGcAbABlACAASQBuAGMALgAgADIAMAAxADZWUDhMaQAAAC9jAAIAHyAQIFOG1BgiEEgC2l9kIQEJkUH+DzHI/Ac+dExwFUmSFXXgsdGDCUykCZzRv/cwENF/hUkbMN19BQAAwLtt27Zt27Zt2x4AAABfnLNt27Zt27Zt297/OEeSJEmSJEmSJLk+DwA=
1 change: 1 addition & 0 deletions test/resources/small/image-webp-low
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data:image/webp;base64,UklGRngCAABXRUJQVlA4WAoAAAAgAAAAYwAACAAASUNDUMgBAAAAAAHIAAAAAAQwAABtbnRyUkdCIFhZWiAH4AABAAEAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAACRyWFlaAAABFAAAABRnWFlaAAABKAAAABRiWFlaAAABPAAAABR3dHB0AAABUAAAABRyVFJDAAABZAAAAChnVFJDAAABZAAAAChiVFJDAAABZAAAAChjcHJ0AAABjAAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAAgAAAAcAHMAUgBHAEJYWVogAAAAAAAAb6IAADj1AAADkFhZWiAAAAAAAABimQAAt4UAABjaWFlaIAAAAAAAACSgAAAPhAAAts9YWVogAAAAAAAA9tYAAQAAAADTLXBhcmEAAAAAAAQAAAACZmYAAPKnAAANWQAAE9AAAApbAAAAAAAAAABtbHVjAAAAAAAAAAEAAAAMZW5VUwAAACAAAAAcAEcAbwBvAGcAbABlACAASQBuAGMALgAgADIAMAAxADZWUDggigAAABAFAJ0BKmQACQA+0VqlTaglIyIkjqkAGgloANHbwjGeCgev+ARF+fOFzxKD0wjQRUAA/sd3rx8E2nJkPAq7j4/xFGrYoTK52SsjW+DJg+zU81x/3/AydGLZghR0AeqBN40JRAppyC7I9DtGHgGsX9FSdiac6/M7pyKXurrXFus5ZWGGbtDdzkAAAA==
16 changes: 16 additions & 0 deletions test/spec/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ describe('basic usage', () => {
.catch(done)
})

it('should render to webp', (done) => {
bootstrap('small/node.html', 'small/style.css', 'small/image-webp')
.then((node) => htmlToImage.toWebp(node))
.then(check)
.then(done)
.catch(done)
})

it('should use quality parameter when rendering to webp', (done) => {
bootstrap('small/node.html', 'small/style.css', 'small/image-webp-low')
.then((node) => htmlToImage.toWebp(node, { quality: 0.5 }))
.then(check)
.then(done)
.catch(done)
})

it('should convert an element to an array of pixels', (done) => {
bootstrap('pixeldata/node.html', 'pixeldata/style.css')
.then((node) =>
Expand Down
Loading