UXInteract / task_038 /solution /code /index.html
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
6.41 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Upload · Automatic Watermark</title>
<style>
* { box-sizing: border-box; }
body { font-family: system-ui, sans-serif; margin: 0; background: #f6f8fa; color: #1f2328; }
header { background: #24292f; color: #fff; padding: 16px 24px; }
header h1 { margin: 0; font-size: 20px; }
main { max-width: 860px; margin: 0 auto; padding: 24px; }
.panel { background: #fff; border: 1px solid #d0d7de; border-radius: 10px; padding: 20px; }
.controls { display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-end; margin-top: 16px; }
.controls label { display: flex; flex-direction: column; font-size: 13px; gap: 4px; }
.controls input, .controls select { padding: 6px 8px; border: 1px solid #c7ced6; border-radius: 6px; }
canvas { display: block; margin-top: 20px; max-width: 100%; border: 1px solid #d0d7de; border-radius: 8px; background: #fafbfc; }
.hint { color: #57606a; margin-top: 8px; }
.result { margin-top: 16px; }
.result img { max-width: 100%; border: 1px solid #d0d7de; border-radius: 8px; }
</style>
</head>
<body>
<header><h1>Image Upload · Automatic Watermark</h1></header>
<main>
<section class="panel">
<p>Select an image to upload. The system adds a text watermark automatically while preserving the original aspect ratio.</p>
<input data-testid="image-upload" type="file" accept="image/*" aria-label="upload image">
<div class="controls">
<label>Watermark Text
<input data-testid="wm-text" type="text" value="Answer">
</label>
<label>Font Size
<input data-testid="wm-size" type="number" value="24" min="8" max="120">
</label>
<label>Opacity
<input data-testid="wm-opacity" type="range" min="0" max="1" step="0.05" value="0.45">
</label>
<label>Position
<select data-testid="wm-position">
<option value="bottom-right" selected>Bottom Right</option>
<option value="bottom-left">Bottom Left</option>
<option value="top-right">Top Right</option>
<option value="top-left">Top Left</option>
<option value="center">Center</option>
</select>
</label>
</div>
<p class="hint" data-testid="status">No image uploaded</p>
<canvas data-testid="watermark-canvas" width="400" height="280"></canvas>
<!-- local in-memory store for original + watermarked images -->
<div data-testid="image-store" data-storage="local" hidden></div>
<div class="result">
<img data-testid="result-image" alt="watermarked result" hidden>
</div>
</section>
</main>
<script>
(function () {
var store = { originals: [], watermarked: [] }; // local in-memory store
window.__getImageStore=()=>JSON.parse(JSON.stringify(store));
var input = document.querySelector('[data-testid="image-upload"]');
var canvas = document.querySelector('[data-testid="watermark-canvas"]');
var ctx = canvas.getContext('2d');
var status = document.querySelector('[data-testid="status"]');
var resultImg = document.querySelector('[data-testid="result-image"]');
var cfg = {
text: document.querySelector('[data-testid="wm-text"]'),
size: document.querySelector('[data-testid="wm-size"]'),
opacity: document.querySelector('[data-testid="wm-opacity"]'),
position: document.querySelector('[data-testid="wm-position"]')
};
var currentImage = null; // last uploaded HTMLImageElement (original, untouched)
function placeWatermark(w, h, fontSize, pos) {
var pad = Math.round(fontSize * 0.6);
switch (pos) {
case 'bottom-left': return { x: pad, y: h - pad, align: 'left', baseline: 'alphabetic' };
case 'top-right': return { x: w - pad, y: pad + fontSize, align: 'right', baseline: 'alphabetic' };
case 'top-left': return { x: pad, y: pad + fontSize, align: 'left', baseline: 'alphabetic' };
case 'center': return { x: w / 2, y: h / 2, align: 'center', baseline: 'middle' };
default: return { x: w - pad, y: h - pad, align: 'right', baseline: 'alphabetic' };
}
}
function render() {
if (!currentImage) return;
// Preserve the original image dimensions / aspect ratio exactly.
var w = currentImage.naturalWidth || currentImage.width;
var h = currentImage.naturalHeight || currentImage.height;
canvas.width = w;
canvas.height = h;
ctx.clearRect(0, 0, w, h);
ctx.drawImage(currentImage, 0, 0, w, h);
var text = cfg.text.value || 'Answer';
var fontSize = Math.max(8, parseInt(cfg.size.value, 10) || 24);
var opacity = Math.min(1, Math.max(0, parseFloat(cfg.opacity.value)));
var pos = placeWatermark(w, h, fontSize, cfg.position.value);
ctx.save();
ctx.globalAlpha = opacity;
ctx.font = 'bold ' + fontSize + 'px system-ui, sans-serif';
ctx.textAlign = pos.align;
ctx.textBaseline = pos.baseline;
ctx.lineWidth = Math.max(1, fontSize / 12);
ctx.strokeStyle = 'rgba(0,0,0,0.55)';
ctx.fillStyle = '#ffffff';
ctx.strokeText(text, pos.x, pos.y);
ctx.fillText(text, pos.x, pos.y);
ctx.restore();
var dataUrl = canvas.toDataURL('image/png');
store.watermarked[store.watermarked.length - 1] = dataUrl;
resultImg.src = dataUrl;
resultImg.hidden = false;
status.textContent = 'Watermark "' + text + '" added · Original dimensions: ' + w + '×' + h;
}
input.addEventListener('change', function (e) {
var file = (e.target.files || [])[0];
if (!file) return;
var reader = new FileReader();
reader.onload = function () {
var img = new Image();
img.onload = function () {
currentImage = img;
store.originals.push(reader.result);
store.watermarked.push(null);
render();
};
img.src = reader.result;
};
reader.readAsDataURL(file);
});
// Re-apply the watermark when configuration changes (existing originals untouched).
Object.keys(cfg).forEach(function (k) {
cfg[k].addEventListener('input', render);
cfg[k].addEventListener('change', render);
});
})();
</script>
</body>
</html>