1 | # Jimp ... in a browser
|
2 |
|
3 | Browser 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 |
|
5 | Example usage:
|
6 |
|
7 | ```html
|
8 | <script src="jimp.min.js"></script>
|
9 | <script>
|
10 | Jimp.read("lenna.png").then(function (lenna) {
|
11 | lenna.resize(256, 256) // resize
|
12 | .quality(60) // set JPEG quality
|
13 | .greyscale() // set greyscale
|
14 | .getBase64(Jimp.MIME_JPEG, function (err, src) {
|
15 | var img = document.createElement("img");
|
16 | img.setAttribute("src", src);
|
17 | document.body.appendChild(img);
|
18 | });
|
19 | }).catch(function (err) {
|
20 | console.error(err);
|
21 | });
|
22 | </script>
|
23 | ```
|
24 |
|
25 | See the [main documentation](https://github.com/oliver-moran/jimp) for the full API documenatinon.
|
26 |
|
27 | ## WebWorkers
|
28 |
|
29 | For 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`):
|
30 |
|
31 | ```js
|
32 | // index.html
|
33 |
|
34 | var worker = new Worker('jimp-worker.js');
|
35 | worker.onmessage = function(e) {
|
36 | // append a new img element using the base 64 image
|
37 | var img = document.createElement('img');
|
38 | img.setAttribute('src', e.data);
|
39 | document.body.appendChild(img);
|
40 | };
|
41 | worker.postMessage('lenna.png'); // message the worker thread
|
42 | ```
|
43 |
|
44 | ```js
|
45 | // jimp-worker.js
|
46 |
|
47 | importScripts('jimp.min.js');
|
48 |
|
49 | self.addEventListener('message', function(e) {
|
50 | Jimp.read(e.data).then(function(lenna) {
|
51 | lenna
|
52 | .resize(256, 256) // resize
|
53 | .quality(60) // set JPEG quality
|
54 | .greyscale() // set greyscale
|
55 | .getBase64(Jimp.MIME_JPEG, function(err, src) {
|
56 | self.postMessage(src); // message the main thread
|
57 | });
|
58 | });
|
59 | });
|
60 | ```
|
61 |
|
62 | ## License
|
63 |
|
64 | Jimp is licensed under the MIT license.
|