-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
67 lines (57 loc) · 1.94 KB
/
script.js
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
function handleFiles(files) {
if (files.length) {
const watermarkText = prompt('Enter watermark text:', 'OVERLAY');
const reader = new FileReader();
reader.onload = function (e) {
createWatermark(e.target.result, watermarkText);
};
reader.readAsDataURL(files[0]);
}
}
function createWatermark(imageSrc, watermarkText) {
const img = new Image();
img.src = imageSrc;
img.onload = function () {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
// Dynamic font size: 2% of the image width
const fontSize = Math.max(canvas.width * 0.02, 10); // Minimum font size of 10px
ctx.font = fontSize + 'px Arial';
ctx.fillStyle = 'rgba(255, 255, 255, 0.5)';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
// Add text shadow for better visibility
ctx.shadowColor = 'black';
ctx.shadowBlur = 7;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
// Calculate text width and adjust step dynamically
const textWidth = ctx.measureText(watermarkText).width;
const xStep = textWidth + fontSize; // Horizontal spacing based on text width and font size
const yStep = fontSize * 1.5; // Vertical spacing based on font size
ctx.save();
ctx.rotate((-45 * Math.PI) / 180);
for (let x = -canvas.width; x < canvas.width * 2; x += xStep) {
for (let y = -canvas.height; y < canvas.height * 2; y += yStep) {
ctx.fillText(watermarkText, x, y);
}
}
ctx.restore();
};
}
// Drag and Drop
const dropArea = document.getElementById('drop-area');
dropArea.addEventListener('dragover', (e) => {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
});
dropArea.addEventListener('drop', (e) => {
e.stopPropagation();
e.preventDefault();
const files = e.dataTransfer.files;
handleFiles(files);
});