You can edit the below JavaScript code to customize the image tool.
Apply Changes
function processImage(originalImg, paintColor = '#c95e34', caveColor = '#857567', vignetteIntensity = 0.8) {
// Parse parameters
vignetteIntensity = vignetteIntensity !== undefined ? Number(vignetteIntensity) : 0.8;
if (isNaN(vignetteIntensity)) vignetteIntensity = 0.8;
const width = originalImg.width || 800;
const height = originalImg.height || 600;
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
// Helper to generate fast procedural noise
function createNoiseCanvas(w, h) {
const nc = document.createElement('canvas');
nc.width = Math.max(2, Math.floor(w));
nc.height = Math.max(2, Math.floor(h));
const nCtx = nc.getContext('2d');
const imgData = nCtx.createImageData(nc.width, nc.height);
const data = imgData.data;
for (let i = 0; i < data.length; i += 4) {
const v = Math.floor(Math.random() * 256);
data[i] = v;
data[i+1] = v;
data[i+2] = v;
data[i+3] = 255;
}
nCtx.putImageData(imgData, 0, 0);
return nc;
}
// 1. Fill base cave rock color
ctx.fillStyle = caveColor;
ctx.fillRect(0, 0, width, height);
// 2. Add large cavern ridges and shadows
ctx.globalAlpha = 0.6;
ctx.globalCompositeOperation = 'overlay';
ctx.filter = 'blur(8px)';
const noise64 = createNoiseCanvas(width / 64, height / 64);
ctx.drawImage(noise64, 0, 0, noise64.width, noise64.height, 0, 0, width, height);
// 3. Add medium rock bumps
ctx.globalAlpha = 0.4;
ctx.filter = 'blur(4px)';
const noise16 = createNoiseCanvas(width / 16, height / 16);
ctx.drawImage(noise16, 0, 0, noise16.width, noise16.height, 0, 0, width, height);
// Reset filters before proceeding
ctx.filter = 'none';
// Prepare the paint layer
const paintCanvas = document.createElement('canvas');
paintCanvas.width = width;
paintCanvas.height = height;
const pCtx = paintCanvas.getContext('2d');
// Draw scaled original out to extract pixels
const tempCanvas = document.createElement('canvas');
tempCanvas.width = width;
tempCanvas.height = height;
const tCtx = tempCanvas.getContext('2d');
tCtx.drawImage(originalImg, 0, 0, width, height);
let usePixelLoop = true;
let iData;
try {
iData = tCtx.getImageData(0, 0, width, height);
} catch (e) {
// Fallback if image originates from a CORS-tainted source
usePixelLoop = false;
}
if (usePixelLoop) {
// Safely parse the provided paint color using a 1x1 canvas measure
const colorTester = document.createElement('canvas');
colorTester.width = 1;
colorTester.height = 1;
const cCtx = colorTester.getContext('2d');
cCtx.fillStyle = paintColor;
cCtx.fillRect(0, 0, 1, 1);
const [pR, pG, pB] = cCtx.getImageData(0, 0, 1, 1).data;
const pData = pCtx.createImageData(width, height);
const pd = pData.data;
const d = iData.data;
// Advanced "Stencil/Ochre" pixel filtering
for (let i = 0; i < d.length; i += 4) {
// Determine grayscale luminance
const lum = 0.299 * d[i] + 0.587 * d[i+1] + 0.114 * d[i+2];
// Inverse luminance means darker pixels deposit more opaque pigment while white remains blank (rock shows through)
let paintAlpha = 255 - lum;
// Allow alpha channel (e.g. from transparent PNGs) to dictate paint boundary
let origAlpha = d[i+3] / 255;
paintAlpha *= origAlpha;
// Boost mask contrast
paintAlpha = Math.min(255, paintAlpha * 1.5);
// Emulate aged flaking / dusting
if (Math.random() < 0.2) {
paintAlpha *= (Math.random() * 0.5);
}
pd[i] = pR;
pd[i+1] = pG;
pd[i+2] = pB;
pd[i+3] = paintAlpha;
}
pCtx.putImageData(pData, 0, 0);
} else {
// Fallback for cross-origin restricted images using native CSS filters
pCtx.globalCompositeOperation = 'source-over';
pCtx.filter = 'sepia(1) hue-rotate(-50deg) saturate(3) brightness(70%) contrast(150%)';
pCtx.drawImage(originalImg, 0, 0, width, height);
pCtx.filter = 'none';
// Give it slight translucency so rock can peer through
pCtx.globalCompositeOperation = 'destination-out';
pCtx.fillStyle = 'rgba(255, 255, 255, 0.2)';
pCtx.fillRect(0, 0, width, height);
}
// 4. Draw paint layer onto cave wall
ctx.globalAlpha = 0.85;
ctx.globalCompositeOperation = 'source-over';
ctx.drawImage(paintCanvas, 0, 0);
// 5. Add fine crevices OVER everything to visually bond the paint to the rock
ctx.globalAlpha = 0.3;
ctx.globalCompositeOperation = 'overlay';
const noise4 = createNoiseCanvas(width / 4, height / 4);
ctx.drawImage(noise4, 0, 0, noise4.width, noise4.height, 0, 0, width, height);
// 6. Dusting/Micro grit
ctx.globalAlpha = 0.2;
const noise1 = createNoiseCanvas(width, height);
ctx.drawImage(noise1, 0, 0, noise1.width, noise1.height, 0, 0, width, height);
// 7. Render cavern darkness / deep shadow vignette
if (vignetteIntensity > 0) {
ctx.globalAlpha = vignetteIntensity;
ctx.globalCompositeOperation = 'multiply';
const rx = width / 2;
const ry = height / 2;
const gradient = ctx.createRadialGradient(
rx, ry, Math.min(width, height) * 0.1,
rx, ry, Math.max(width, height) * 0.8
);
// Multiplicative gradient: pure white = no effect, pure black = darkened
gradient.addColorStop(0, 'rgba(255, 255, 255, 1)');
gradient.addColorStop(1, 'rgba(0, 0, 0, 1)');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, width, height);
}
// Reset standard contexts
ctx.globalAlpha = 1.0;
ctx.globalCompositeOperation = 'source-over';
return canvas;
}
Apply Changes