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")
|
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 |
|
28 | See the [main documentation](https://github.com/jimp-dev/jimp) for the full API documenatinon.
|
29 |
|
30 | ## WebWorkers
|
31 |
|
32 | 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`):
|
33 |
|
34 | ```js
|
35 | // index.html
|
36 |
|
37 | var worker = new Worker("jimp-worker.js");
|
38 | worker.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 | };
|
44 | worker.postMessage("lenna.png"); // message the worker thread
|
45 | ```
|
46 |
|
47 | ```js
|
48 | // jimp-worker.js
|
49 |
|
50 | importScripts("jimp.min.js");
|
51 |
|
52 | self.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 |
|
67 | Jimp is licensed under the MIT license.
|