Spaces:
Running
Running
File size: 8,102 Bytes
533b1fd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | <!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> |