You can edit the below JavaScript code to customize the image tool.
Apply Changes
function processImage(originalImg, waveFreqStr = "1.5", waveAmpStr = "35", yawStr = "-35", pitchStr = "55") {
// Parse parameters
const paramFreq = parseFloat(waveFreqStr) || 1.5;
const paramAmp = parseFloat(waveAmpStr) || 35;
const paramYaw = parseFloat(yawStr) || -35;
const paramPitch = parseFloat(pitchStr) || 55;
// Create the final destination canvas
const destW = 800;
const destH = 600;
const destCanvas = document.createElement("canvas");
destCanvas.width = destW;
destCanvas.height = destH;
const ctx = destCanvas.getContext("2d", { willReadFrequently: true });
// 1. Draw magical sky background
const grad = ctx.createLinearGradient(0, 0, 0, destH);
grad.addColorStop(0, "#1a2a6c"); // Deep night/dusk blue
grad.addColorStop(0.5, "#1180d5"); // Bright blue
grad.addColorStop(1, "#b5d8f6"); // Light horizon
ctx.fillStyle = grad;
ctx.fillRect(0, 0, destW, destH);
// Draw some simple fluffy clouds
function drawCloud(cx, cy, scale) {
ctx.save();
ctx.translate(cx, cy);
ctx.scale(scale, scale);
ctx.fillStyle = "rgba(255, 255, 255, 0.4)";
ctx.beginPath();
ctx.arc(0, 0, 25, Math.PI, 0);
ctx.arc(25, -15, 30, Math.PI, 0);
ctx.arc(60, -20, 35, Math.PI, 0);
ctx.arc(95, -10, 30, Math.PI, 0);
ctx.arc(115, 5, 20, Math.PI, 0);
ctx.fill();
ctx.restore();
}
drawCloud(100, 200, 1.2);
drawCloud(550, 150, 1.5);
drawCloud(200, 480, 0.9);
drawCloud(650, 420, 1.1);
drawCloud(350, 80, 0.7);
// Add wind streaks to convey flying motion
for (let i = 0; i < 20; i++) {
ctx.beginPath();
let wx = Math.random() * destW;
let wy = Math.random() * destH;
let length = 30 + Math.random() * 80;
ctx.moveTo(wx, wy);
ctx.lineTo(wx + length, wy);
ctx.lineWidth = 1 + Math.random() * 2;
ctx.strokeStyle = "rgba(255, 255, 255, 0.2)";
ctx.lineCap = "round";
ctx.stroke();
}
// 2. Prepare the flat carpet canvas
// Constrain max size for reasonable iteration performace
const maxDim = 400;
const scaleFactor = maxDim / Math.max(originalImg.width, originalImg.height);
const sImgW = Math.round(originalImg.width * scaleFactor);
const sImgH = Math.round(originalImg.height * scaleFactor);
const pad = 50; // padding for fringes
const sW = sImgW + pad * 2;
const sH = sImgH;
const sCanvas = document.createElement("canvas");
sCanvas.width = sW;
sCanvas.height = sH;
const sCtx = sCanvas.getContext("2d", { willReadFrequently: true });
// Draw carpet tassels (fringes) on left and right edges
sCtx.lineCap = "round";
for (let y = 0; y < sH; y += 3) {
// Left fringe
sCtx.strokeStyle = (y % 6 === 0) ? "#f2d25e" : "#b88a25";
sCtx.lineWidth = 1.5 + Math.random();
sCtx.beginPath();
sCtx.moveTo(pad, y);
let curX = pad - (15 + Math.random() * 25);
let curY = y + (Math.random() - 0.5) * 15;
sCtx.quadraticCurveTo(pad - 10, y + 2, curX, curY);
sCtx.stroke();
// Right fringe
sCtx.beginPath();
sCtx.moveTo(sW - pad, y);
curX = sW - pad + (15 + Math.random() * 25);
curY = y + (Math.random() - 0.5) * 15;
sCtx.quadraticCurveTo(sW - pad + 10, y + 2, curX, curY);
sCtx.stroke();
}
// Fill a solid base in case original image has transparency
sCtx.fillStyle = "#5c0e11";
sCtx.fillRect(pad, 0, sImgW, sImgH);
// Draw the main image onto the carpet
sCtx.drawImage(originalImg, pad, 0, sImgW, sImgH);
// Draw decorative carpet borders framing the image
sCtx.lineWidth = 14;
sCtx.strokeStyle = "#5c0e11"; // deep red thick border
sCtx.strokeRect(pad + 7, 7, sImgW - 14, sImgH - 14);
sCtx.lineWidth = 4;
sCtx.strokeStyle = "#deb857"; // gold inline
sCtx.strokeRect(pad + 16, 16, sImgW - 32, sImgH - 32);
sCtx.lineWidth = 2;
sCtx.strokeStyle = "#1b2b4d"; // dark blue inner framing
sCtx.strokeRect(pad + 20, 20, sImgW - 40, sImgH - 40);
// 3. 3D Projection & Rendering
const destImgData = ctx.getImageData(0, 0, destW, destH);
const destData = destImgData.data;
const srcImgData = sCtx.getImageData(0, 0, sW, sH);
const srcData = srcImgData.data;
const zBuffer = new Float32Array(destW * destH).fill(-Infinity);
const shadowMap = new Uint8Array(destW * destH);
// Math conversions
const pitch = paramPitch * Math.PI / 180;
const yaw = paramYaw * Math.PI / 180;
const cYaw = Math.cos(yaw), sYaw = Math.sin(yaw);
const cPitch = Math.cos(pitch), sPitch = Math.sin(pitch);
const camDist = 700;
const centerX = destW / 2;
const centerY = destH / 2 - 20;
const phase = 0.5; // Controls the snapshot moment of the wave
const waveFreq = paramFreq * Math.PI * 2 / sW;
// Pass 1: Render a drop shadow flat on the "ground"
for (let y = 0; y < sH; y += 2) {
for (let x = 0; x < sW; x += 2) {
let pIdx = (y * sW + x) * 4;
if (srcData[pIdx + 3] < 30) continue; // skip highly transparent source pixels
let cx = x - sW / 2;
let cy = y - sH / 2;
let rx = cx * cYaw - cy * sYaw;
let ry = cx * sYaw + cy * cYaw;
let rz = 0; // flat on floor
let fx = rx;
let fy = ry * cPitch - rz * sPitch;
let fz = ry * sPitch + rz * cPitch;
let scale = camDist / (camDist + fz + 100);
let sX = centerX + fx * scale + 50; // shift right
let sY = centerY + fy * scale + 160; // shift deeply down for hover effect
let px = Math.floor(sX);
let py = Math.floor(sY);
let splat = Math.max(1, Math.ceil(scale * 2.2));
for(let dy = 0; dy < splat; dy++) {
for(let dx = 0; dx < splat; dx++) {
let tx = px + dx, ty = py + dy;
if (tx >= 0 && tx < destW && ty >= 0 && ty < destH) {
let idx = ty * destW + tx;
if (!shadowMap[idx]) {
shadowMap[idx] = 1;
let dIdx = idx * 4;
destData[dIdx] = Math.max(0, destData[dIdx] * 0.4);
destData[dIdx+1] = Math.max(0, destData[dIdx+1] * 0.4);
destData[dIdx+2] = Math.max(0, destData[dIdx+2] * 0.5); // Deep blue cool shadow
}
}
}
}
}
}
// Pass 2: Render the wavy magic carpet with Z-buffering (Painter's approximation)
for (let y = 0; y < sH; y++) {
for (let x = 0; x < sW; x++) {
let pIdx = (y * sW + x) * 4;
let a = srcData[pIdx + 3];
if (a < 5) continue;
let r = srcData[pIdx];
let g = srcData[pIdx + 1];
let b = srcData[pIdx + 2];
let cx = x - sW / 2;
let cy = y - sH / 2;
// Generate nice rippling 3D wave mapping
let waveZ = Math.sin(cx * waveFreq - phase) * paramAmp;
// Cross wave for organic flapping effect
waveZ += Math.cos(cy * waveFreq * 0.4) * (paramAmp * 0.3);
let rx = cx * cYaw - cy * sYaw;
let ry = cx * sYaw + cy * cYaw;
let rz = waveZ;
let fx = rx;
let fy = ry * cPitch - rz * sPitch;
let fz = ry * sPitch + rz * cPitch;
// Fake perspective scale
let scale = camDist / (camDist + fz + 100);
// Dynamic lighting derived from the derivative of the sine wave (slope)
let slope = Math.cos(cx * waveFreq - phase);
let lightAmt = 1 + slope * 0.25;
let finalR = Math.min(255, r * lightAmt);
let finalG = Math.min(255, g * lightAmt);
let finalB = Math.min(255, b * lightAmt);
let sX = centerX + fx * scale;
let sY = centerY + fy * scale;
let px = Math.floor(sX);
let py = Math.floor(sY);
// Splat size (slightly oversized to seal subpixel holes in rendering)
let splat = Math.max(1, Math.ceil(scale * 1.6));
for(let dy = 0; dy <= splat; dy++) {
for(let dx = 0; dx <= splat; dx++) {
let tx = px + dx, ty = py + dy;
if (tx >= 0 && tx < destW && ty >= 0 && ty < destH) {
let idx = ty * destW + tx;
// Z-buffer check (smaller fz is closer to camera -> so larger -fz)
if (-fz > zBuffer[idx]) {
zBuffer[idx] = -fz;
let dIdx = idx * 4;
destData[dIdx] = finalR;
destData[dIdx+1] = finalG;
destData[dIdx+2] = finalB;
destData[dIdx+3] = a; // Keep original alpha for softness
}
}
}
}
}
}
// Apply projected modifications back to destination canvas
ctx.putImageData(destImgData, 0, 0);
return destCanvas;
}
Apply Changes