Image-TAA / index.html
AnOrdinaryUser's picture
Update index.html
533b1fd verified
Raw History Blame Contribute Delete
8.1 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Anti-Aliasing (TAA-like)</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
text-align: center;
}
#drop-area {
border: 2px dashed #ccc;
border-radius: 8px;
padding: 40px;
margin-bottom: 20px;
cursor: pointer;
}
#drop-area.highlight {
border-color: #4CAF50;
background-color: #f0fff0;
}
#preview-container {
display: flex;
justify-content: space-between;
margin: 20px 0;
}
.image-box {
border: 1px solid #ddd;
padding: 10px;
width: 48%;
}
.image-box img, .image-box canvas {
max-width: 100%;
max-height: 300px;
display: block;
margin: 0 auto;
}
.slider-container {
margin: 20px 0;
}
.slider-label {
display: flex;
justify-content: space-between;
}
#strength-slider {
width: 100%;
}
#file-input {
display: none;
}
button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 10px 2px;
cursor: pointer;
border-radius: 4px;
}
button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
</style>
</head>
<body>
<h1>Image Anti-Aliasing (TAA-like)</h1>
<p>Drag & drop an image below or click to select</p>
<div id="drop-area">
<p>Drop image here</p>
<input type="file" id="file-input" accept="image/*">
</div>
<div class="slider-container">
<div class="slider-label">
<span>Original</span>
<span>Anti-Aliasing Strength: <span id="strength-value">0.5</span></span>
<span>Processed</span>
</div>
<input type="range" id="strength-slider" min="0" max="1" step="0.01" value="0.5">
</div>
<div id="preview-container">
<div class="image-box">
<h3>Original</h3>
<img id="original-image" src="" alt="Original Image">
</div>
<div class="image-box">
<h3>Processed</h3>
<canvas id="processed-canvas"></canvas>
</div>
</div>
<button id="save-button" disabled>Save Processed Image</button>
<script>
// DOM elements
const dropArea = document.getElementById('drop-area');
const fileInput = document.getElementById('file-input');
const originalImage = document.getElementById('original-image');
const processedCanvas = document.getElementById('processed-canvas');
const strengthSlider = document.getElementById('strength-slider');
const strengthValue = document.getElementById('strength-value');
const saveButton = document.getElementById('save-button');
// Prevent default drag behaviors
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false);
document.body.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
// Highlight drop area when item is dragged over it
['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false);
});
function highlight() {
dropArea.classList.add('highlight');
}
function unhighlight() {
dropArea.classList.remove('highlight');
}
// Handle dropped files
dropArea.addEventListener('drop', handleDrop, false);
dropArea.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', handleFiles);
function handleDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
handleFiles({ target: { files } });
}
function handleFiles(e) {
const file = e.target.files[0];
if (!file.type.match('image.*')) {
alert('Please upload an image file');
return;
}
const reader = new FileReader();
reader.onload = function(event) {
originalImage.src = event.target.result;
originalImage.onload = function() {
processImage();
saveButton.disabled = false;
};
};
reader.readAsDataURL(file);
}
// Process image with anti-aliasing
function processImage() {
const strength = parseFloat(strengthSlider.value);
strengthValue.textContent = strength.toFixed(2);
const img = originalImage;
const canvas = processedCanvas;
const ctx = canvas.getContext('2d');
// Set canvas dimensions to match image
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
// Draw original image
ctx.drawImage(img, 0, 0);
// Apply anti-aliasing effect (edge-preserving)
if (strength > 0) {
// Create a temporary canvas for processing
const tempCanvas = document.createElement('canvas');
tempCanvas.width = canvas.width;
tempCanvas.height = canvas.height;
const tempCtx = tempCanvas.getContext('2d');
// Apply smart blur (better than simple blur for edges)
smartBlur(canvas, tempCtx, strength * 3);
// Blend the blurred version with the original
ctx.globalAlpha = 0.7 * strength; // Adjust opacity based on strength
ctx.drawImage(tempCanvas, 0, 0);
ctx.globalAlpha = 1.0;
}
}
// Better blur function (preserves edges)
function smartBlur(sourceCanvas, targetCtx, radius) {
targetCtx.filter = `blur(${radius}px)`;
targetCtx.drawImage(sourceCanvas, 0, 0);
targetCtx.filter = 'none';
// Edge preservation hack (crude but effective)
targetCtx.globalCompositeOperation = 'lighten';
targetCtx.drawImage(sourceCanvas, 0, 0);
targetCtx.globalCompositeOperation = 'source-over';
}
// Update when slider changes
strengthSlider.addEventListener('input', function() {
if (originalImage.src) {
processImage();
}
});
// Save button functionality
saveButton.addEventListener('click', function() {
if (!processedCanvas) return;
// Create a temporary link to download the image
const link = document.createElement('a');
link.download = 'anti-aliased-image.png';
link.href = processedCanvas.toDataURL('image/png');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});
</script>
</body>
</html>