---
# How encode images to webp in browser with vanilla js without plugins

**URL:** https://modul-r.codekraft.it/how-encode-images-to-webp-in-browser-with-vanilla-js-without-plugins/
Date: 2022-02-26
Author: Erik
Post Type: post
Summary: Some days ago, while using a react repo I discovered this method that uses canvas to convert images to webp. Currently it’s not perfect and in some browsers won’t work on some browsers (guess which one: safari) and has a limited support for icc profiles / color space. You can find more info about project […]
Categories: Blog
---

Some days ago, while using a react repo I discovered this method that uses canvas to convert images to webp. 

Currently it's not perfect and in some browsers won't work on some browsers (guess which one: safari) and has a limited support for icc profiles / color space. You can find more info about project progress [here](https://github.com/WICG/canvas-color-space/blob/main/CanvasColorSpaceProposal.md). 

HTML Canvas works amazingly fast and without any plugin, it needs only browser while many other method requires NodeJS or PHP GD to work.

the trick is to upload an image to a canvas and then export it with `canvas.toDataURL(image/webp, quality).`

  See the Pen 
  Encode to webp images in browser with canvas (without plugins) by erikYO! ([@erikyo](https://codepen.io/erikyo))
  on [CodePen](https://codepen.io).

If you don't know what **webp **is I invite you to have a [look here](https://developers.google.com/speed/webp), webp provides superior **lossless and lossy** compression for images on the web (eg. allows a reduction of up to 1000% compared to a transparent png). 

So if you haven't already done so, it's time to think about how to convert your site images to webp (Modul-r has a [gulp task](https://modul-r.codekraft.it/2019/06/environment-setup/) for this purpose).

*One last thing… it doesn't work with **avif **format, yet at least!*

---

## Categories

- Blog

---

## Navigation

- [Modul*R](https://modul-r.codekraft.it/)

---

## Footer Links

- [WordPress](https://wordpress.org/)