You can edit the below JavaScript code to customize the image tool.
Apply Changes
function processImage(originalImg, transitionType = "dissolve", durationSec = 2) {
// Supported transitionType values:
// "dissolve", "fade", "wipeRight", "wipeLeft", "wipeDown", "wipeUp",
// "circle", "diamond", "bars", "venetianBlinds", "checkerboard",
// "pixelate", "crossWipe", "zoom"
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// Set canvas dimensions to match the image
const width = originalImg.width;
const height = originalImg.height;
canvas.width = width;
canvas.height = height;
let startTime = null;
let duration = parseFloat(durationSec) * 1000;
if (isNaN(duration) || duration <= 0) {
duration = 2000;
}
// Offscreen canvas (used for pixelate effect) to avoid Garbage Collection spikes
const offCanvas = document.createElement('canvas');
const offCtx = offCanvas.getContext('2d');
canvas.hasBeenConnected = false;
function draw(time) {
// Prevent memory leaks if canvas has been removed from DOM
if (!canvas.isConnected && canvas.hasBeenConnected) {
return;
}
if (canvas.isConnected) {
canvas.hasBeenConnected = true;
}
if (!startTime) startTime = time;
let progress = (time - startTime) / duration;
// Loop the transition with a brief pause at the end
if (progress > 1.2) {
startTime = time;
progress = 0;
}
let p = Math.min(progress, 1.0); // Clamped progress from 0.0 to 1.0
ctx.clearRect(0, 0, width, height);
// Draw standard black background mimicking video
ctx.fillStyle = "#000000";
ctx.fillRect(0, 0, width, height);
ctx.save();
const w = width;
const h = height;
if (transitionType === "fade") {
ctx.globalAlpha = p;
ctx.drawImage(originalImg, 0, 0, w, h);
} else if (transitionType === "wipeRight") {
ctx.beginPath();
ctx.rect(0, 0, w * p, h);
ctx.clip();
ctx.drawImage(originalImg, 0, 0, w, h);
} else if (transitionType === "wipeLeft") {
ctx.beginPath();
ctx.rect(w - w * p, 0, w * p, h);
ctx.clip();
ctx.drawImage(originalImg, 0, 0, w, h);
} else if (transitionType === "wipeDown") {
ctx.beginPath();
ctx.rect(0, 0, w, h * p);
ctx.clip();
ctx.drawImage(originalImg, 0, 0, w, h);
} else if (transitionType === "wipeUp") {
ctx.beginPath();
ctx.rect(0, h - h * p, w, h * p);
ctx.clip();
ctx.drawImage(originalImg, 0, 0, w, h);
} else if (transitionType === "circle") {
ctx.beginPath();
const maxRadius = Math.sqrt(w * w + h * h) / 2;
ctx.arc(w / 2, h / 2, maxRadius * p, 0, Math.PI * 2);
ctx.clip();
ctx.drawImage(originalImg, 0, 0, w, h);
} else if (transitionType === "diamond") {
ctx.beginPath();
const extent = Math.max(w, h); // Max spread
ctx.moveTo(w / 2, h / 2 - extent * p);
ctx.lineTo(w / 2 + extent * p, h / 2);
ctx.lineTo(w / 2, h / 2 + extent * p);
ctx.lineTo(w / 2 - extent * p, h / 2);
ctx.closePath();
ctx.clip();
ctx.drawImage(originalImg, 0, 0, w, h);
} else if (transitionType === "bars") {
const numBars = 10;
const barHeight = h / numBars;
ctx.beginPath();
for (let i = 0; i < numBars; i++) {
if (i % 2 === 0) {
ctx.rect(0, i * barHeight, w * p, barHeight);
} else {
ctx.rect(w - w * p, i * barHeight, w * p, barHeight);
}
}
ctx.clip();
ctx.drawImage(originalImg, 0, 0, w, h);
} else if (transitionType === "venetianBlinds") {
const numBlinds = 12;
const blindHeight = h / numBlinds;
ctx.beginPath();
for (let i = 0; i < numBlinds; i++) {
ctx.rect(0, i * blindHeight, w, blindHeight * p);
}
ctx.clip();
ctx.drawImage(originalImg, 0, 0, w, h);
} else if (transitionType === "checkerboard") {
const cols = 10;
const rows = Math.ceil(h / (w / cols));
const cellW = w / cols;
const cellH = h / rows;
ctx.beginPath();
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
const delay = ((r + c) / (cols + rows)) * 0.5;
let cellP = (p - delay) * 2;
if (cellP < 0) cellP = 0;
if (cellP > 1) cellP = 1;
const cw = cellW * cellP;
const ch = cellH * cellP;
ctx.rect(c * cellW + (cellW - cw) / 2, r * cellH + (cellH - ch) / 2, cw, ch);
}
}
ctx.clip();
ctx.drawImage(originalImg, 0, 0, w, h);
} else if (transitionType === "pixelate") {
if (p < 0.05) {
// Initial solid black
} else if (p >= 0.95) {
ctx.drawImage(originalImg, 0, 0, w, h);
} else {
const mappedP = (p - 0.05) / 0.90;
let pixelSize = 50 * (1 - mappedP);
if (pixelSize < 1) pixelSize = 1;
const smW = Math.max(1, Math.floor(w / pixelSize));
const smH = Math.max(1, Math.floor(h / pixelSize));
offCanvas.width = smW; // Resizing inherently clears the canvas
offCanvas.height = smH;
offCtx.drawImage(originalImg, 0, 0, smW, smH);
ctx.imageSmoothingEnabled = false;
ctx.drawImage(offCanvas, 0, 0, w, h);
ctx.imageSmoothingEnabled = true; // reset
}
} else if (transitionType === "crossWipe") {
ctx.beginPath();
const extent = Math.max(w, h) * 2;
ctx.moveTo(0, 0);
ctx.lineTo(extent * p, 0);
ctx.lineTo(0, extent * p);
ctx.closePath();
ctx.clip();
ctx.drawImage(originalImg, 0, 0, w, h);
} else if (transitionType === "zoom") {
ctx.globalAlpha = p;
const scale = 0.5 + 0.5 * p;
ctx.translate(w / 2, h / 2);
ctx.scale(scale, scale);
ctx.translate(-w / 2, -h / 2);
ctx.drawImage(originalImg, 0, 0, w, h);
} else {
// Default: 'dissolve' (TV noise resolution pattern)
const wSegments = 40;
const sw = w / wSegments;
const hSegments = Math.ceil(h / sw);
const sh = h / hSegments;
ctx.beginPath();
for (let r = 0; r < hSegments; r++) {
for (let c = 0; c < wSegments; c++) {
const n = Math.abs(Math.sin(r * 12.9898 + c * 78.233) * 43758.5453) % 1;
if (p > n) {
ctx.rect(c * sw, r * sh, sw, sh);
}
}
}
ctx.clip();
ctx.drawImage(originalImg, 0, 0, w, h);
}
ctx.restore();
requestAnimationFrame(draw);
}
// Start animation loop
requestAnimationFrame(draw);
return canvas;
}
Apply Changes