Blog> Categories: XR, Libraries
Table of Contents
JavaScript has revolutionized how images are processed directly in the browser, thanks to a plethora of open-source libraries like JIMP, Pica, and Fabric.js. These libraries allow developers to handle image-related tasks such as resizing, filtering, and editing without relying on server-side processing.
In this article, we explore six real-world applications of JavaScript image manipulation libraries using modern ES6 imports via CDNs, making the code easier to integrate into web projects.
1. Building Online Image Editors #
Online image editors, akin to Canva, rely heavily on libraries like Fabric.js for creating interactive tools. Features like cropping, resizing, and adding text or overlays are achievable entirely in the browser.
Example: #
Using Fabric.js via CDN to add images and shapes:
<script type="module">
import { fabric } from 'https://cdn.jsdelivr.net/npm/fabric@5.4.0/dist/fabric.min.js';
const canvas = new fabric.Canvas('canvas');
fabric.Image.fromURL('image.jpg', (img) => {
canvas.add(img);
});
</script>
<canvas id="canvas" width="600" height="400"></canvas>
2. Dynamic Image Resizing for Web Applications #
Client-side image resizing is critical for web performance and responsive design. Libraries like Pica enable high-quality resizing directly in the browser.
Application: #
- Compressing images before upload.
- Generating responsive thumbnails for galleries.
Example: #
Resizing an image with Pica via CDN:
<script type="module">
import pica from 'https://cdn.jsdelivr.net/npm/pica@8.1.1/dist/pica.min.js';
const resizer = new pica();
const sourceCanvas = document.querySelector('#sourceCanvas');
const targetCanvas = document.querySelector('#targetCanvas');
resizer.resize(sourceCanvas, targetCanvas).then(() => {
console.log('Resizing complete!');
});
</script>
<canvas id="sourceCanvas" width="800" height="600"></canvas>
<canvas id="targetCanvas" width="400" height="300"></canvas>
3. Real-Time Filters and Effects #
Libraries like CamanJS and glfx.js allow developers to apply filters and effects to images in real time. These libraries are commonly used in photo enhancement tools and social media apps.
Example: #
Applying a filter with CamanJS:
<script type="module" src="https://cdn.jsdelivr.net/npm/caman@5.0.0/dist/caman.full.min.js"></script>
<script>
Caman('#image', function () {
this.sepia(50).render();
});
</script>
<img id="image" src="image.jpg" width="400" />
4. Facial Recognition and Augmented Reality #
Image manipulation libraries often preprocess images for applications like facial recognition and AR. Tools like tracking.js detect features, which can then be enhanced or used for further analysis.
Example: #
Face detection using tracking.js:
<script type="module" src="https://cdn.jsdelivr.net/npm/tracking/build/tracking-min.js"></script>
<script>
const tracker = new tracking.ObjectTracker('face');
tracker.on('track', (event) => {
event.data.forEach((rect) => console.log(rect));
});
tracking.track('#video', tracker);
</script>
<video id="video" width="640" height="480" autoplay></video>
5. Customizing Product Images in E-Commerce #
E-commerce platforms can allow users to customize product images, such as previewing personalized designs. Libraries like Fabric.js simplify this process by enabling real-time editing.
Example: #
Using Fabric.js for customizing product images:
<script type="module">
import { fabric } from 'https://cdn.jsdelivr.net/npm/fabric@5.4.0/dist/fabric.min.js';
const canvas = new fabric.Canvas('productCanvas');
fabric.Image.fromURL('tshirt.jpg', (img) => {
canvas.add(img);
});
</script>
<canvas id="productCanvas" width="600" height="400"></canvas>
6. Generating and Watermarking Images #
Watermarking is a common requirement for protecting intellectual property. Libraries like JIMP allow developers to overlay logos or text onto images directly in the browser.
Example: #
Adding a watermark using JIMP via CDN:
<script type="module">
import Jimp from 'https://cdn.jsdelivr.net/npm/jimp/browser/lib/jimp.min.js';
const addWatermark = async () => {
const image = await Jimp.read('image.jpg');
const watermark = await Jimp.read('watermark.png');
image.composite(watermark, 10, 10).getBase64(Jimp.MIME_JPEG, (err, src) => {
if (!err) {
document.querySelector('#output').src = src;
}
});
};
addWatermark();
</script>
<img id="output" />
Explore these libraries in your next project and unlock the true potential of in-browser image manipulation! Experiment using this notebook: Scribbler Notebook for Fabric.js.