forked from sirilius/watermarkktp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
211 lines (170 loc) · 5.57 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
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
var val = "";
var inputFile = document.getElementById("inputFile");
inputFile.addEventListener("change", handleImage, false)
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext('2d');
var $canvas = $("#canvas");
var canvasOffset = $canvas.offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
var scrollX = $canvas.scrollLeft();
var scrollY = $canvas.scrollTop();
var startX;
var startY;
var texts = [];
var selectedText = -1;
function textHittest(x, y, textIndex) {
var text = texts[textIndex];
return (x >= text.x && x <= text.x + text.width && y >= text.y - text.height && y <= text.y);
}
function handleMouseDown(e) {
e.preventDefault();
startX = parseInt(e.clientX - offsetX);
startY = parseInt(e.clientY - offsetY);
for (var i = 0; i < texts.length; i++) {
if (textHittest(startX, startY, i)) {
selectedText = i;
}
}
}
function handleMouseUp(e) {
e.preventDefault();
selectedText = -1;
}
function handleMouseOut(e) {
e.preventDefault();
selectedText = -1;
}
function handleMouseMove(e, img) {
if (selectedText < 0) {
return;
}
e.preventDefault();
mouseX = parseInt(e.clientX - offsetX);
mouseY = parseInt(e.clientY - offsetY);
var dx = mouseX - startX;
var dy = mouseY - startY;
startX = mouseX;
startY = mouseY;
var text = texts[selectedText];
text.x += dx;
text.y += dy;
theimg(img);
}
function automate(img) {
document.getElementById("text").addEventListener("keyup", function () {
draggable(img)
})
document.getElementById("colorPicker").addEventListener("change", function () {
draggable(img)
})
document.getElementById("opacity").addEventListener("change", function () {
draggable(img);
})
document.addEventListener("click", function () {
draggable(img)
})
$("#canvas").mousedown(function (e) {
handleMouseDown(e);
});
$("#canvas").mousemove(function (e) {
handleMouseMove(e, img)
})
$("#canvas").mouseup(function (e) {
handleMouseUp(e);
});
$("#canvas").mouseout(function (e) {
handleMouseOut(e);
});
canvas.addEventListener("pointerdown", function (e) {
handleMouseDown(e);
});
canvas.addEventListener("pointermove", function (e) {
handleMouseMove(e, img);
});
canvas.addEventListener("pointerup", function (e) {
handleMouseUp(e);
});
canvas.addEventListener("pointerout", function (e) {
handleMouseOut(e);
});
};
function draggable(img) {
var y = 90;
var x = 400;
var color = document.getElementById("colorPicker").value
// Font Selection
var font = document.getElementById("select-font").value
for (var i = 0; i < texts.length; i++) {
var text = texts[i];
var y = text.y;
var x = text.x;
}
var text = {
text: $("#text").val(),
x: x,
y: y
};
var opacity = $("#opacity").val();
var color = $("#colorPicker").val();
var rgbaCol = 'rgba(' + parseInt(color.slice(-6, -4), 16) + ',' + parseInt(color.slice(-4, -2), 16) + ',' + parseInt(color.slice(-2), 16) + ',' + opacity + ')';
ctx.font = `30px ${font}`;
ctx.fillStyle = rgbaCol;
ctx.textAlign = "center";
text.width = ctx.measureText(text.text).width;
text.height = 30;
texts.push(text);
theimg(img)
}
function theimg(img) {
for (var i = 0; i < texts.length; i++) {
var acanvas = ctx.canvas;
var hRatio = acanvas.width / img.width;
var vRatio = acanvas.height / img.height;
var ratio = Math.min(hRatio, vRatio);
var centerShift_x = (acanvas.width - img.width * ratio) / 2;
var centerShift_y = (acanvas.height - img.height * ratio) / 2;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, img.width, img.height, centerShift_x, centerShift_y, img.width * ratio, img.height * ratio);
var text = texts[i];
ctx.fillText(text.text, text.x, text.y);
}
}
function handleImage(e) {
var reader = new FileReader();
var img = "";
var src = "";
reader.onload = function (event) {
img = new Image();
img.onload = function () {
var canvas = ctx.canvas;
var hRatio = canvas.width / img.width;
var vRatio = canvas.height / img.height;
var ratio = Math.min(hRatio, vRatio);
var centerShift_x = (canvas.width - img.width * ratio) / 2;
var centerShift_y = (canvas.height - img.height * ratio) / 2;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, img.width, img.height, centerShift_x, centerShift_y, img.width * ratio, img.height * ratio);
ctx.fillStyle = '#ffffff00';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
img.src = event.target.result;
src = event.target.result;
canvas.classList.add("show");
theimg(img);
automate(img);
}
reader.readAsDataURL(e.target.files[0]);
}
function download() {
var download = document.getElementById("download");
var image = document.getElementById("canvas").toDataURL("image/png");
download.setAttribute("href", image);
}
$("#inputFile").change(function () {
var filename = $(this).val().split("\\").pop();
$(this).siblings(".label-file").html(filename);
if (document.getElementsByClassName("label-file")[0].innerHTML == "") {
document.getElementsByClassName("label-file")[0].innerHTML = "Pilih Gambar"
}
})