You can edit the below JavaScript code to customize the image tool.
Apply Changes
/**
* Applies a visual effect to an image, interpreting a "verb" as an action.
*
* @param {Image} originalImg The original JavaScript Image object.
* @param {string} verb The verb describing the effect. Supported verbs: 'blur', 'pixelate', 'grayscale', 'invert', 'shake', 'ripple', 'age'. Defaults to 'blur'.
* @param {number} intensity The strength of the effect. The meaning varies by verb. Defaults to 10.
* @returns {HTMLCanvasElement} A new canvas element with the processed image.
*/
function processImage(originalImg, verb = 'blur', intensity = 10) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const width = originalImg.naturalWidth;
const height = originalImg.naturalHeight;
canvas.width = width;
canvas.height = height;
// Sanitize verb input
const v = verb.toLowerCase().trim();
switch (v) {
case 'blur':
// intensity: blur radius in pixels (e.g., 5)
ctx.filter = `blur(${intensity}px)`;
ctx.drawImage(originalImg, 0, 0);
break;
case 'pixelate':
// intensity: the size of each block in pixels (e.g., 20)
const size = Math.max(1, Math.round(intensity));
const w = width / size;
const h = height / size;
// 1. Draw the image scaled down. This will average the pixels.
ctx.drawImage(originalImg, 0, 0, w, h);
// 2. Scale it back up, disabling image smoothing to get the blocky effect.
ctx.imageSmoothingEnabled = false;
ctx.drawImage(canvas, 0, 0, w, h, 0, 0, width, height);
break;
case 'grayscale':
// intensity: percentage of grayscale effect (0-100)
ctx.filter = `grayscale(${Math.min(100, Math.max(0, intensity))}%)`;
ctx.drawImage(originalImg, 0, 0);
break;
case 'invert':
// intensity: percentage of color inversion (0-100)
ctx.filter = `invert(${Math.min(100, Math.max(0, intensity))}%)`;
ctx.drawImage(originalImg, 0, 0);
break;
case 'age':
// intensity: percentage of sepia effect (0-100)
ctx.filter = `sepia(${Math.min(100, Math.max(0, intensity))}%)`;
ctx.drawImage(originalImg, 0, 0);
break;
case 'shake':
// intensity: max shake distance in pixels, creates a chromatic aberration effect (e.g., 15)
const shakeAmount = Math.max(0, intensity);
// Use a temporary canvas to draw tinted versions of the image
const scratchCanvas = document.createElement('canvas');
scratchCanvas.width = width;
scratchCanvas.height = height;
const scratchCtx = scratchCanvas.getContext('2d', { willReadFrequently: true });
const drawTinted = (color, dx, dy) => {
scratchCtx.clearRect(0, 0, width, height);
// Draw original image onto scratch
scratchCtx.drawImage(originalImg, 0, 0);
// Overlay a color layer
scratchCtx.globalCompositeOperation = 'multiply';
scratchCtx.fillStyle = color;
scratchCtx.fillRect(0, 0, width, height);
// Use original image's alpha channel
scratchCtx.globalCompositeOperation = 'destination-in';
scratchCtx.drawImage(originalImg, 0, 0);
// Reset for next operation
scratchCtx.globalCompositeOperation = 'source-over';
// Draw the tinted, offset image onto the main canvas
ctx.drawImage(scratchCanvas, dx, dy);
};
// Use 'lighter' to additively blend the color channels
ctx.globalCompositeOperation = 'lighter';
drawTinted('rgb(255,0,0)', (Math.random() - 0.5) * shakeAmount, (Math.random() - 0.5) * shakeAmount);
drawTinted('rgb(0,255,0)', (Math.random() - 0.5) * shakeAmount, (Math.random() - 0.5) * shakeAmount);
drawTinted('rgb(0,0,255)', (Math.random() - 0.5) * shakeAmount, (Math.random() - 0.5) * shakeAmount);
break;
case 'ripple':
// intensity: amplitude of the wave in pixels (e.g., 20)
ctx.drawImage(originalImg, 0, 0);
const imageData = ctx.getImageData(0, 0, width, height);
const data = imageData.data;
const originalData = new Uint8ClampedArray(data); // Work on a copy
const amplitude = intensity;
const frequency = height / 30; // Make wave frequency proportional to image height
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const waveOffset = Math.sin(y / frequency) * amplitude;
const srcX = Math.round(x + waveOffset);
const destIndex = (y * width + x) * 4;
// Check if the source pixel is within the image bounds
if (srcX >= 0 && srcX < width) {
const srcIndex = (y * width + srcX) * 4;
data[destIndex] = originalData[srcIndex]; // R
data[destIndex + 1] = originalData[srcIndex + 1]; // G
data[destIndex + 2] = originalData[srcIndex + 2]; // B
data[destIndex + 3] = originalData[srcIndex + 3]; // A
}
}
}
ctx.putImageData(imageData, 0, 0);
break;
default:
// If the verb is not recognized, just draw the original image.
ctx.drawImage(originalImg, 0, 0);
break;
}
return canvas;
}
Apply Changes