You can edit the below JavaScript code to customize the image tool.
Apply Changes
async function processImage(originalImg, maxPredictions = "3", overlayStyle = "scifi") {
const numPredictions = parseInt(maxPredictions, 10) || 3;
// Create the primary output canvas
const canvas = document.createElement('canvas');
canvas.width = originalImg.width;
canvas.height = originalImg.height;
const ctx = canvas.getContext('2d');
// Draw the original image onto the canvas
ctx.drawImage(originalImg, 0, 0);
// Helper function to dynamically load external scripts safely
const loadScript = (src, globalVar) => new Promise((resolve, reject) => {
if (window[globalVar]) {
return resolve();
}
const script = document.createElement('script');
script.src = src;
script.crossOrigin = 'anonymous';
script.onload = () => resolve();
script.onerror = () => reject(new Error(`Failed to load ${src}`));
document.head.appendChild(script);
});
try {
// Load TensorFlow.js and MobileNet for image identification
await loadScript('https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@3.11.0/dist/tf.min.js', 'tf');
await loadScript('https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@2.1.0/dist/mobilenet.min.js', 'mobilenet');
// Initialize model computationally
const model = await window.mobilenet.load({ version: 2, alpha: 1.0 });
// Pass the untainted canvas drawn with the image for classification
const predictions = await model.classify(canvas);
const topPredictions = predictions.slice(0, numPredictions);
// Apply "Scanner" Visual HUD Filter
const primaryColor = overlayStyle === 'scifi' ? '#00FF00' : '#00AAFF';
const darkHazeColor = overlayStyle === 'scifi' ? 'rgba(0, 20, 0, 0.75)' : 'rgba(0, 0, 20, 0.75)';
const scanHazeColor = overlayStyle === 'scifi' ? 'rgba(0, 255, 0, 0.05)' : 'rgba(0, 170, 255, 0.05)';
// 1. Subtle overall screen tint
ctx.fillStyle = scanHazeColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 2. Scanline effect
ctx.beginPath();
ctx.moveTo(0, canvas.height * 0.45);
ctx.lineTo(canvas.width, canvas.height * 0.45);
ctx.strokeStyle = primaryColor;
ctx.globalAlpha = 0.5;
ctx.lineWidth = Math.max(2, canvas.height * 0.005);
ctx.stroke();
ctx.globalAlpha = 1.0; // Reset alpha
// 3. HUD Target Crosshairs
ctx.strokeStyle = primaryColor;
ctx.lineWidth = Math.max(2, Math.min(canvas.width, canvas.height) * 0.015);
const cw = canvas.width;
const ch = canvas.height;
const length = Math.min(cw, ch) * 0.1;
const offset = Math.min(cw, ch) * 0.03;
ctx.beginPath();
// Top Left
ctx.moveTo(offset, offset + length);
ctx.lineTo(offset, offset);
ctx.lineTo(offset + length, offset);
// Top Right
ctx.moveTo(cw - offset - length, offset);
ctx.lineTo(cw - offset, offset);
ctx.lineTo(cw - offset, offset + length);
// Bottom Right
ctx.moveTo(cw - offset, ch - offset - length);
ctx.lineTo(cw - offset, ch - offset);
ctx.lineTo(cw - offset - length, ch - offset);
// Bottom Left
ctx.moveTo(offset + length, ch - offset);
ctx.lineTo(offset, ch - offset);
ctx.lineTo(offset, ch - offset - length);
ctx.stroke();
// 4. Data Overlay Backdrop
const fontSizeTitle = Math.max(16, ch * 0.035);
const fontSizeText = Math.max(14, ch * 0.03);
const padding = Math.max(10, ch * 0.02);
const overlayHeight = padding * 3 + fontSizeTitle + topPredictions.length * (fontSizeText + 8) + 10;
ctx.fillStyle = darkHazeColor;
ctx.fillRect(0, ch - overlayHeight, cw, overlayHeight);
ctx.strokeStyle = primaryColor;
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(0, ch - overlayHeight);
ctx.lineTo(cw, ch - overlayHeight);
ctx.stroke();
// 5. Draw Title and Target Labels
ctx.fillStyle = primaryColor;
ctx.font = `bold ${fontSizeTitle}px monospace`;
ctx.fillText('SCANNER IDENTIFICATION RESULTS:', padding, ch - overlayHeight + padding + fontSizeTitle);
ctx.font = `${fontSizeText}px monospace`;
topPredictions.forEach((p, i) => {
ctx.fillStyle = '#FFFFFF';
const accuracy = (p.probability * 100).toFixed(1);
const textY = ch - overlayHeight + padding * 2 + fontSizeTitle + i * (fontSizeText + 8) + 10;
// Draw bullet point / bar
ctx.fillStyle = primaryColor;
ctx.fillRect(padding, textY - fontSizeText * 0.75, 5, fontSizeText);
ctx.fillStyle = '#FFFFFF';
ctx.fillText(` [${accuracy}% MATCH] ${p.className.toUpperCase()}`, padding + 10, textY);
});
} catch (err) {
// Fallback drawing if Neural Network fails to load due to network/CORS
console.error("Scanner Error:", err);
ctx.fillStyle = 'rgba(255, 0, 0, 0.8)';
ctx.fillRect(0, canvas.height - 60, canvas.width, 60);
ctx.fillStyle = '#FFFFFF';
ctx.font = 'bold 16px monospace';
ctx.fillText('ERROR: SCANNER NETWORK OFFLINE OR CORS BLOCKED', 15, canvas.height - 25);
}
return canvas;
}
Apply Changes