-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
285 lines (222 loc) · 7.85 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
const addFont = font => document.fonts.add(font);
new FontFace('Ruda Black', 'url("fonts/Ruda Black/ruda-black.woff2")').load().then((font) => addFont(font));
new FontFace('Bebas Neue', 'url("fonts/Bebas Neue/bebas-neue.woff2")').load().then((font) => addFont(font));
new FontFace('Source Sans Pro', 'url("fonts/Source Sans Pro/source-sans-pro.woff2")').load().then((font) => addFont(font));
const fileButton = document.querySelector('.file-button');
const fileChooser = document.querySelector('.file-chooser');
const title = document.querySelector('input[name="title"]');
const price = document.querySelector('input[name="price"]');
const phone = document.querySelector('input[name="phone"]');
const city = document.querySelector('input[name="city"]');
const state = document.querySelector('input[name="state"]');
const used = document.querySelector('.used');
const accept = document.querySelector('.accept');
const buttons = document.querySelector('.buttons');
const buttonGenerate = document.querySelector('.button-generate');
const buttonView = document.querySelector('.button-view');
let canvas;
let isUsed;
let acceptExchange;
let imageWidth;
let imageHeight;
let marginTopTitle = 385;
const addEvent = ({ element = null, type, func, elems }) => {
if (element) {
elems ? element.addEventListener(type, () => func(elems)) : element.addEventListener(type, func);
}
}
const isChecked = (element, checked) => {
if (!checked) {
return element.getElementsByTagName('input')[0].setAttribute('checked', true);
}
element.getElementsByTagName('input')[0].removeAttribute('checked');
}
const toggleChecked = element => {
const checked = element.getElementsByTagName('input')[0].checked;
isChecked(element, checked);
}
// Converte para maiúscula o primeiro caractere de uma string
const capitalize = string => {
return string.charAt(0).toUpperCase() + string.slice(1);
}
// Quebra de linha para textos longos
const customText = (context, text, x, y, lineHeight, fitWidth) => {
let words = text.split(' ');
let currentLine = 0;
let idx = 1;
while (idx <= words.length) {
const string = words.slice(0, idx).join(' ');
const { width } = context.measureText(string);
if (width > fitWidth) {
y = marginTopTitle - 60;
if (idx === 1) {
idx = 2;
}
context.fillText(words.slice(0, idx - 1).join(' '), x, y + (lineHeight * currentLine));
currentLine++;
words = words.splice(idx - 1);
idx = 1;
} else {
idx++;
}
}
if (idx > 0) {
context.fillText(words.join(' '), x, y + (lineHeight * currentLine));
}
}
const load = result => {
return new Promise((fulfill, _reject) => {
let imageObj = new Image();
imageObj.onload = () => fulfill(imageObj);
imageObj.src = result;
});
}
const sleep = ms => {
return new Promise(resolve => setTimeout(resolve, ms));
}
const mask = ({ target }, func) => {
sleep(1).then(() => {
target.value = func(target.value);
});
}
const regexPrice = v => {
v = v.replace(/\D/g, ""); // Remove tudo que não é dígito
v = v.replace(/([0-9]{2})$/g, ",$1"); // Adiciona vírgula
if (v.length > 6) {
v = v.replace(/(\d)(?=(\d{3})+\,)/g, "$1."); // Adiciona ponto
}
v = `R$ ${v}`; // Adiciona o prefixo R$
return v;
}
const regexPhone = v => {
v = v.replace(/\D/g, ""); // Remove tudo que não é dígito
v = v.replace(/^(\d{2})(\d)/g, "($1) $2"); // Adiciona parênteses
v = v.replace(/(\d)(\d{4})$/, "$1-$2"); // Adiciona hífen
return v;
}
const resize = (originalWidth, originalHeight) => {
if (originalWidth >= 450 && originalHeight < 583) {
imageWidth = 450;
imageHeight = originalHeight * (imageWidth / originalWidth);
}
if (originalWidth >= 450 && originalHeight >= 583) {
imageWidth = 450;
imageHeight = originalHeight * (imageWidth / originalWidth);
}
}
const canvasTemplate = (canvas, context, template, image) => {
template.onload = () => {
canvas.width = template.width;
canvas.height = template.height;
context.drawImage(template, 0, 0);
context.drawImage(image, 20, 457, imageWidth, imageHeight);
context.font = "40.80pt Ruda Black";
context.fillStyle = "#3B4854";
context.fillText((isUsed) ? 'USADO' : 'NOVO', 20, 170);
context.font = "80.80pt Bebas Neue";
context.textAlign = "center";
context.fillStyle = "#fff";
customText(context, title.value, canvas.width / 2, marginTopTitle, 95, 1070);
context.font = "40.80pt Ruda Black";
context.textAlign = "start";
context.fillStyle = "#3B4854";
context.fillText(price.value, (canvas.width / 2) + 20, 525);
context.font = "40.80pt Ruda Black";
context.textAlign = "start";
context.fillStyle = "#3B4854";
context.fillText((acceptExchange) ? 'Aceita troca' : 'Não aceita troca', (canvas.width / 2) + 20, 675);
context.font = "40.80pt Ruda Black";
context.textAlign = "start";
context.fillStyle = "#3B4854";
context.fillText(phone.value, (canvas.width / 2) + 20, 825);
context.font = "bold 30pt Source Sans Pro";
context.textAlign = "start";
context.fillStyle = "#3B4854";
context.fillText(capitalize(city.value), (canvas.width / 2) + 70, 955);
context.font = "italic 25pt Source Sans Pro";
context.textAlign = "start";
context.fillStyle = "#3B4854";
context.fillText(capitalize(state.value), (canvas.width / 2) + 70, 995);
}
}
const generate = () => {
isUsed = used.querySelector('input').checked;
acceptExchange = accept.querySelector('input').checked;
const file = fileChooser.files[0];
if (file) {
canvas = document.createElement('canvas');
buttons.insertAdjacentElement('afterEnd', canvas);
let context = canvas.getContext("2d");
const reader = new FileReader();
reader.onload = event => {
Promise.all([
load(event.target.result), // images[0] ( Foto do produto )
load(reader.result), // images[1] ( Template )
])
.then(images => {
originalWidth = images[0].width;
originalHeight = images[0].height;
imageWidth = originalWidth;
imageHeight = originalHeight;
resize(originalWidth, originalHeight);
if (imageHeight > 583) {
imageHeight = 583;
imageWidth = originalWidth * (imageHeight / originalHeight);
}
images[1].src = "templates/default.png";
canvasTemplate(canvas, context, images[1], images[0]);
})
}
reader.readAsDataURL(file);
buttonGenerate.classList.add('hidden');
}
}
// Visualizar o anúncio em tamanho real
const view = () => {
if (canvas) {
const base64 = document.querySelector('canvas').toDataURL("image/png", 1.0);
document.write('<img src="'+base64+'"/>');
}
}
addEvent({
element: fileButton,
type: 'click',
func: () => fileChooser.click(),
elems: null
});
addEvent({
element: price,
type: 'keyup',
func: (event) => mask(event, regexPrice),
elems: null
});
addEvent({
element: phone,
type: 'keyup',
func: (event) => mask(event, regexPhone),
elems: null
});
addEvent({
element: used,
type: 'click',
func: toggleChecked,
elems: used
});
addEvent({
element: accept,
type: 'click',
func: toggleChecked,
elems: accept
});
addEvent({
element: buttonGenerate,
type: 'click',
func: generate,
elems: null
});
addEvent({
element: buttonView,
type: 'click',
func: view,
elems: null
});