You can edit the below JavaScript code to customize the image tool.
Apply Changes
function processImage(originalImg, intensity = "3.0", overlayOpacity = "0.7", blurRadius = "15") {
// Parse parameters
const intns = parseFloat(intensity);
const opac = parseFloat(overlayOpacity);
const blurRad = parseInt(blurRadius, 10);
const w = originalImg.width;
const h = originalImg.height;
if (!w || !h) {
throw new Error("Invalid image dimensions.");
}
// 1. Draw original image to get pixel data
const tmpCanvas = document.createElement('canvas');
tmpCanvas.width = w;
tmpCanvas.height = h;
const tCtx = tmpCanvas.getContext('2d', { willReadFrequently: true });
tCtx.drawImage(originalImg, 0, 0);
const imgData = tCtx.getImageData(0, 0, w, h);
const data = imgData.data;
// 2. Create edge map to simulate high-activity / motion areas
const edgeCanvas = document.createElement('canvas');
edgeCanvas.width = w;
edgeCanvas.height = h;
const eCtx = edgeCanvas.getContext('2d', { willReadFrequently: true });
// Fill with black first so borders remain inactive mapping
eCtx.fillStyle = 'black';
eCtx.fillRect(0, 0, w, h);
const edgeData = eCtx.getImageData(0, 0, w, h);
const eArr = edgeData.data;
// Use Central Difference approach for edge detection
for (let y = 1; y < h - 1; y++) {
for (let x = 1; x < w - 1; x++) {
const idx = (y * w + x) * 4;
const left = (y * w + (x - 1)) * 4;
const right = (y * w + (x + 1)) * 4;
const top = ((y - 1) * w + x) * 4;
const bottom = ((y + 1) * w + x) * 4;
// Compute luminance
const lumL = data[left] * 0.299 + data[left+1] * 0.587 + data[left+2] * 0.114;
const lumR = data[right] * 0.299 + data[right+1] * 0.587 + data[right+2] * 0.114;
const lumT = data[top] * 0.299 + data[top+1] * 0.587 + data[top+2] * 0.114;
const lumB = data[bottom] * 0.299 + data[bottom+1] * 0.587 + data[bottom+2] * 0.114;
const dx = lumR - lumL;
const dy = lumB - lumT;
// Calculate gradient magnitude and apply intensity scale
const mag = Math.min(255, Math.sqrt(dx * dx + dy * dy) * intns);
eArr[idx] = mag; // R
eArr[idx + 1] = mag; // G
eArr[idx + 2] = mag; // B
eArr[idx + 3] = 255; // OPAQUE
}
}
eCtx.putImageData(edgeData, 0, 0);
// 3. Blur the edge map to simulate the accumulation and spread of a motion heatmap
const blurCanvas = document.createElement('canvas');
blurCanvas.width = w;
blurCanvas.height = h;
const bCtx = blurCanvas.getContext('2d', { willReadFrequently: true });
bCtx.filter = `blur(${blurRad}px)`;
bCtx.drawImage(edgeCanvas, 0, 0);
// 4. Generate a thermal gradient palette setup (Transparent -> Blue -> Cyan -> Green -> Yellow -> Red)
const gradCanvas = document.createElement('canvas');
gradCanvas.width = 256;
gradCanvas.height = 1;
const gCtx = gradCanvas.getContext('2d', { willReadFrequently: true });
const grad = gCtx.createLinearGradient(0, 0, 256, 0);
grad.addColorStop(0.00, 'rgba(0, 0, 0, 0)'); // Fully transparent for low activity
grad.addColorStop(0.15, 'rgba(0, 0, 255, 0.4)'); // Cool blue
grad.addColorStop(0.40, 'rgba(0, 255, 255, 0.6)'); // Cyan
grad.addColorStop(0.65, 'rgba(0, 255, 0, 0.8)'); // Green
grad.addColorStop(0.85, 'rgba(255, 255, 0, 0.9)'); // Yellow
grad.addColorStop(1.00, 'rgba(255, 0, 0, 1.0)'); // Hot red
gCtx.fillStyle = grad;
gCtx.fillRect(0, 0, 256, 1);
const palette = gCtx.getImageData(0, 0, 256, 1).data;
// 5. Apply the thermal palette to the blurred edges based on grayscale intensity
const bData = bCtx.getImageData(0, 0, w, h);
const bArr = bData.data;
const heatCanvas = document.createElement('canvas');
heatCanvas.width = w;
heatCanvas.height = h;
const hCtx = heatCanvas.getContext('2d');
const heatData = hCtx.createImageData(w, h);
const hArr = heatData.data;
for (let i = 0; i < bArr.length; i += 4) {
// Red channel used as standard grayscale intensity reference since R=G=B in our edge map
const val = bArr[i];
const pIdx = val * 4;
hArr[i] = palette[pIdx]; // R
hArr[i+1] = palette[pIdx + 1]; // G
hArr[i+2] = palette[pIdx + 2]; // B
hArr[i+3] = palette[pIdx + 3]; // Alpha transparency
}
hCtx.putImageData(heatData, 0, 0);
// 6. Draw final composite with base image and the motion heatmap layered on top
const finalCanvas = document.createElement('canvas');
finalCanvas.width = w;
finalCanvas.height = h;
const fCtx = finalCanvas.getContext('2d');
// Base background
fCtx.drawImage(originalImg, 0, 0);
// Add Heatmap Layer
fCtx.globalAlpha = opac;
fCtx.drawImage(heatCanvas, 0, 0);
fCtx.globalAlpha = 1.0; // Reset just to be safe
return finalCanvas;
}
Apply Changes