UNPKG

1.89 kBMarkdownView Raw
1# Jimp ... in a browser
2
3Browser support for Jimp was added by Phil Seaton. This enabled Jimp to be used in [Electron](http://electron.atom.io/) applications as well as web browsers.
4
5Example usage:
6
7```html
8<script src="jimp.min.js"></script>
9<script>
10 Jimp.read("lenna.png")
11 .then(function (lenna) {
12 lenna
13 .resize(256, 256) // resize
14 .quality(60) // set JPEG quality
15 .greyscale() // set greyscale
16 .getBase64(Jimp.MIME_JPEG, function (err, src) {
17 var img = document.createElement("img");
18 img.setAttribute("src", src);
19 document.body.appendChild(img);
20 });
21 })
22 .catch(function (err) {
23 console.error(err);
24 });
25</script>
26```
27
28See the [main documentation](https://github.com/jimp-dev/jimp) for the full API documenatinon.
29
30## WebWorkers
31
32For better performance, it recommended that Jimp methods are run on a separate thread using [`WebWorkers`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers). The following shows how using two files (`index.html` and `jimp-worker.js`):
33
34```js
35// index.html
36
37var worker = new Worker("jimp-worker.js");
38worker.onmessage = function (e) {
39 // append a new img element using the base 64 image
40 var img = document.createElement("img");
41 img.setAttribute("src", e.data);
42 document.body.appendChild(img);
43};
44worker.postMessage("lenna.png"); // message the worker thread
45```
46
47```js
48// jimp-worker.js
49
50importScripts("jimp.min.js");
51
52self.addEventListener("message", function (e) {
53 Jimp.read(e.data).then(function (lenna) {
54 lenna
55 .resize(256, 256) // resize
56 .quality(60) // set JPEG quality
57 .greyscale() // set greyscale
58 .getBase64(Jimp.MIME_JPEG, function (err, src) {
59 self.postMessage(src); // message the main thread
60 });
61 });
62});
63```
64
65## License
66
67Jimp is licensed under the MIT license.