You can edit the below JavaScript code to customize the image tool.
Apply Changes
/**
* Applies a Vaporwave/Synthwave filter to an image.
* This includes chromatic aberration, scan lines, noise, a CRT-like barrel distortion,
* a wave effect, and a final color tint.
*
* @param {Image} originalImg The original source Image object.
* @param {string} tintColor1 The first color for the gradient overlay (e.g., a vaporwave pink/purple). Defaults to '#ff00ff'.
* @param {string} tintColor2 The second color for the gradient overlay (e.g., a synthwave cyan). Defaults to '#00ffff'.
* @param {number} vhsIntensity A number from 0 to 1 controlling the intensity of VHS artifacts (noise, scan lines, chromatic aberration). Defaults to 0.4.
* @param {number} crtIntensity A number from 0 to 1 controlling the intensity of the CRT barrel distortion. Defaults to 0.3.
* @param {number} ondulationIntensity A number from 0 to 1 controlling the intensity of the visual wave/ondulation effect. Defaults to 0.2.
* @returns {HTMLCanvasElement} A new canvas element with the filter applied.
*/
async function processImage(originalImg, tintColor1 = '#ff00ff', tintColor2 = '#00ffff', vhsIntensity = 0.4, crtIntensity = 0.3, ondulationIntensity = 0.2) {
// 1. Coerce parameters to their expected types
vhsIntensity = Number(vhsIntensity);
crtIntensity = Number(crtIntensity);
ondulationIntensity = Number(ondulationIntensity);
// 2. Canvas Setup
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d', {
willReadFrequently: true
});
const width = originalImg.naturalWidth;
const height = originalImg.naturalHeight;
canvas.width = width;
canvas.height = height;
// Draw the original image to get its pixel data
ctx.drawImage(originalImg, 0, 0, width, height);
const originalData = ctx.getImageData(0, 0, width, height);
const originalPixels = originalData.data;
// Create a new image data object to store the manipulated pixels
const newData = ctx.createImageData(width, height);
const newPixels = newData.data;
const centerX = width / 2;
const centerY = height / 2;
// Use the diagonal length for radius normalization to handle all aspect ratios
const maxDist = Math.sqrt(centerX * centerX + centerY * centerY);
// Helper function to get a pixel from the original data with boundary checks (nearest neighbor)
const getPixel = (x, y) => {
x = Math.round(x);
y = Math.round(y);
if (x < 0 || x >= width || y < 0 || y >= height) {
return [0, 0, 0, 0]; // Return transparent black for out-of-bounds
}
const index = (y * width + x) * 4;
return [
originalPixels[index],
originalPixels[index + 1],
originalPixels[index + 2],
originalPixels[index + 3]
];
};
// 3. Main Pixel Manipulation Loop - iterate over destination pixels
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
// a. Calculate Source Coordinates with applied effects (inverse mapping)
let sourceX = x;
let sourceY = y;
// CRT Barrel Distortion Effect
if (crtIntensity > 0) {
const dx = x - centerX;
const dy = y - centerY;
const dist = Math.sqrt(dx * dx + dy * dy);
const normalizedRadius = dist / maxDist;
// Use a formula that creates a bulge in the middle
const k = -crtIntensity * 0.7; // Strength of distortion (negative for barrel)
const theta = 1 + k * normalizedRadius * normalizedRadius;
if (theta !== 0) {
sourceX = centerX + dx / theta;
sourceY = centerY + dy / theta;
}
}
// Low Ondulation (Wave) Effect
if (ondulationIntensity > 0) {
const waveAmount = Math.sin(y / 20 + x / 15) * ondulationIntensity * 15;
sourceY += waveAmount;
}
// b. Chromatic Aberration Sampling
const aberrationOffset = vhsIntensity * 4;
const rChannel = getPixel(sourceX - aberrationOffset, sourceY)[0];
const gChannel = getPixel(sourceX, sourceY)[1];
const bChannel = getPixel(sourceX + aberrationOffset, sourceY)[2];
const alpha = getPixel(sourceX, sourceY)[3];
let r = rChannel;
let g = gChannel;
let b = bChannel;
// c. VHS Artifacts (Scan Lines & Noise)
if (vhsIntensity > 0) {
// Scan lines - darken every Nth line
if (y % 3 === 0) {
const darkness = vhsIntensity * 0.4;
r = Math.max(0, r * (1 - darkness));
g = Math.max(0, g * (1 - darkness));
b = Math.max(0, b * (1 - darkness));
}
// Noise
const noise = (Math.random() - 0.5) * vhsIntensity * 50;
r += noise;
g += noise;
b += noise;
}
// d. Write to new pixel data, clamping values between 0 and 255
const destIndex = (y * width + x) * 4;
newPixels[destIndex] = Math.max(0, Math.min(255, r));
newPixels[destIndex + 1] = Math.max(0, Math.min(255, g));
newPixels[destIndex + 2] = Math.max(0, Math.min(255, b));
newPixels[destIndex + 3] = alpha;
}
}
// 4. Put the manipulated data onto the canvas
ctx.putImageData(newData, 0, 0);
// 5. Apply overall color tints using a gradient and composite operations
const gradient = ctx.createLinearGradient(0, 0, 0, height);
gradient.addColorStop(0, tintColor1);
gradient.addColorStop(1, tintColor2);
ctx.globalCompositeOperation = 'overlay';
ctx.globalAlpha = 0.5;
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, width, height);
// Add a second, different blend mode for more color depth
ctx.globalCompositeOperation = 'color-dodge';
ctx.globalAlpha = 0.15;
ctx.fillRect(0,0,width,height);
// Reset canvas state
ctx.globalCompositeOperation = 'source-over';
ctx.globalAlpha = 1.0;
// Add a subtle vignetting to enhance the CRT feel
const vignetteRadius = width * 0.7;
const vignetteGradient = ctx.createRadialGradient(centerX, centerY, vignetteRadius, centerX, centerY, maxDist);
vignetteGradient.addColorStop(0, "rgba(0,0,0,0)");
vignetteGradient.addColorStop(1, "rgba(0,0,0,0.4)");
ctx.fillStyle = vignetteGradient;
ctx.fillRect(0, 0, width, height);
// 6. Return the final canvas
return canvas;
}
Apply Changes