-
Notifications
You must be signed in to change notification settings - Fork 3
/
bannerPhone_With_Text_Portrait.htm
308 lines (280 loc) · 10.9 KB
/
bannerPhone_With_Text_Portrait.htm
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Phone With Text Banner Generator</title>
<style>
html,body{margin:0;padding:0;background-color:white}
canvas{display:none;text-rendering:optimizeLegibility}
div{display:inline-block;width:100%;background-color:silver}
input[type=text]{height:30px;width:70px;font-family:Arial;font-size:13px;margin:0;padding:0;box-sizing:border-box}
input[type=file]{height:30px;font-family:Arial;font-size:13px;line-height:30px;vertical-align:top;margin:0;padding:0;box-sizing:border-box}
img{display:inline-block}
</style>
</head>
<body>
<canvas width="750" height="1334" id="myCanvas"></canvas>
<div>
<input type="text" id="title" placeholder="Title">
<input type="text" id="titleFont" placeholder="Title Name">
<input type="text" id="titleSize" placeholder="Title Size">
<input type="text" id="titleColor" placeholder="Title Color">
<input type="text" id="titleMarginTop" placeholder="Title Margin Top">
<input type="checkbox" id="shadowEnabled">
<input type="text" id="backColor" placeholder="Background Color">
<input type="text" id="imgSize" placeholder="Image size">
<input type="text" id="imgMarginTop" placeholder="Image Margin Top">
<input type="file" id="screenshot">
</div>
<img width="750" height="1334" id="finalResult" alt="banner" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wQQEjQ35agTNQAAAAxJREFUCNdjOH78OAAErgJW23qXwQAAAABJRU5ErkJggg==">
<script>
window.addEventListener("load", function()
{
// GETTING ALL THE ELEMENTS IN THE WEB FORM
var title = document.getElementById("title");
var titleFont = document.getElementById("titleFont");
var titleSize = document.getElementById("titleSize");
var titleColor = document.getElementById("titleColor");
var titleMarginTop = document.getElementById("titleMarginTop");
var shadowEnabled = document.getElementById("shadowEnabled");
var backColor = document.getElementById("backColor");
var imgSize = document.getElementById("imgSize");
var imgMarginTop = document.getElementById("imgMarginTop");
var canvas = document.getElementById("myCanvas");
// FOCUSING THE TITLE
title.focus();
// CLEARING ALL THE INPUTTED VALUES (IF ANY)
title.value = "";
titleFont.value = "Verdana";
titleSize.value = "27";
titleColor.value = "#FFFFFF";
titleMarginTop.value = "70";
shadowEnabled.checked = false;
backColor.value = "#3A76B1";
imgSize.value = "700";
imgMarginTop.value = "125";
document.getElementById("screenshot").value = null;
function update()
{
// GETTING THE CONTEXT
var context = canvas.getContext("2d");
// DRAWING A WHITE BACKGROUND
context.fillStyle = backColor.value;
context.fillRect(0, 0, 750, 1334);
// SETTING THE FONT STYLE
context.font = titleSize.value + "px " + titleFont.value;
// GETTING THE CENTER X VALUE FOR THE TEXT
var centeredTitle = context.measureText(title.value).width;
centeredTitle = 750 / 2 - centeredTitle / 2;
// CHECKING IF A SHADOW TITLE MUST BE CREATED
if (shadowEnabled.checked==true)
{
// DRAWING THE SHADOW TITLE
context.fillStyle = "black";
context.fillText(title.value, centeredTitle + 2, parseInt(titleMarginTop.value)+2);
}
// DRAWING THE TITLE
context.fillStyle = titleColor.value;
context.fillText(title.value, centeredTitle, titleMarginTop.value);
try
{
// CREATING THE FILEREADER
var filereader = new FileReader();
// GETTING THE EXTENSION
var extension = document.getElementById("screenshot").files[0].name.split(".").pop().toLowerCase();
// CHECKING THE EXTENSION IN ORDER TO KNOW IF THE FILE IS AN IMAGE
if (extension=="jpg" | extension=="jpeg" | extension=="png")
{
// SETTING WHAT WILL HAPPEN WHEN THE FILE IS LOADED
filereader.onload = function()
{
// LOADING THE IMAGE FILE
var image3 = new Image;
image3.onload = function()
{
// GETTING THE IMAGE RATIO
var ratio = image3.width/image3.height;
var width = document.getElementById("imgSize").value;
var height = document.getElementById("imgSize").value/ratio;
try
{
// SETTING THE IMAGE SCALE
var scale = width * 1 / image3.width;
// GETTING THE RESIZED IMAGE
var imagenResized = downScaleImage(image3, scale);
// DRAWING THE RESIZED IMAGE
context.drawImage(imagenResized, 750 / 2 - width / 2, imgMarginTop.value, width, height);
}
catch(err)
{
// IF THERE IS AN ERROR, IT MEANS THAT THE FILE IMAGE IS SMALLER THAN
// THE SIZE THAT IT SUPPOSED TO FILL. SO, BECAUSE OF THIS,
// IN THE FOLLOWING LINE THE IMAGE IS STRETCHED TO FILL.
context.drawImage(image3, 750 / 2 - width / 2, imgMarginTop.value, width, height);
}
// EXPORTING THE PICTURE AS A PNG IMAGE IN BASE64 FORMAT
document.getElementById("finalResult").src = canvas.toDataURL("data/png");
};
image3.src = filereader.result;
};
// READING/LOADING THE FILE
filereader.readAsDataURL(document.getElementById("screenshot").files[0]);
}
else
{
// EXPORTING THE PICTURE AS A PNG IMAGE IN BASE64 FORMAT
document.getElementById("finalResult").src = canvas.toDataURL("data/png");
}
}
catch(err)
{
// EXPORTING THE PICTURE AS A PNG IMAGE IN BASE64 FORMAT
document.getElementById("finalResult").src = canvas.toDataURL("data/png");
}
}
function downScaleImage(img, scale)
{
var imgCV = document.createElement("canvas");
imgCV.width = img.width;
imgCV.height = img.height;
var imgCtx = imgCV.getContext("2d");
imgCtx.drawImage(img, 0, 0);
return downScaleCanvas(imgCV, scale);
}
function downScaleCanvas(cv, scale)
{
if (!(scale < 1) || !(scale > 0)) throw ("scale must be a positive number <1 ");
var sqScale = scale * scale;
var sw = cv.width;
var sh = cv.height;
var tw = Math.floor(sw * scale);
var th = Math.floor(sh * scale);
var sx = 0, sy = 0, sIndex = 0;
var tx = 0, ty = 0, yIndex = 0, tIndex = 0;
var tX = 0, tY = 0;
var w = 0, nw = 0, wx = 0, nwx = 0, wy = 0, nwy = 0;
var crossX = false;
var crossY = false;
var sBuffer = cv.getContext("2d").getImageData(0, 0, sw, sh).data;
var tBuffer = new Float32Array(4 * sw * sh);
var sR = 0, sG = 0, sB = 0;
var sA = 0;
for (sy = 0; sy < sh; sy++)
{
ty = sy * scale;
tY = 0 | ty;
yIndex = 4 * tY * tw;
crossY = (tY != (0 | ty + scale));
if (crossY)
{
wy = (tY + 1 - ty);
nwy = (ty + scale - tY - 1);
}
for (sx = 0; sx < sw; sx++, sIndex += 4)
{
tx = sx * scale;
tX = 0 | tx;
tIndex = yIndex + tX * 4;
crossX = (tX != (0 | tx + scale));
if (crossX)
{
wx = (tX + 1 - tx);
nwx = (tx + scale - tX - 1);
}
sR = sBuffer[sIndex];
sG = sBuffer[sIndex + 1];
sB = sBuffer[sIndex + 2];
sA = sBuffer[sIndex + 3];
if (!crossX && !crossY)
{
tBuffer[tIndex] += sR * sqScale;
tBuffer[tIndex + 1] += sG * sqScale;
tBuffer[tIndex + 2] += sB * sqScale;
tBuffer[tIndex + 3] += sA * sqScale;
}
else if (crossX && !crossY)
{
w = wx * scale;
tBuffer[tIndex] += sR * w;
tBuffer[tIndex + 1] += sG * w;
tBuffer[tIndex + 2] += sB * w;
tBuffer[tIndex + 3] += sA * w;
nw = nwx * scale
tBuffer[tIndex + 4] += sR * nw;
tBuffer[tIndex + 5] += sG * nw;
tBuffer[tIndex + 6] += sB * nw;
tBuffer[tIndex + 7] += sA * nw;
}
else if (crossY && !crossX)
{
w = wy * scale;
tBuffer[tIndex] += sR * w;
tBuffer[tIndex + 1] += sG * w;
tBuffer[tIndex + 2] += sB * w;
tBuffer[tIndex + 3] += sA * w;
nw = nwy * scale
tBuffer[tIndex + 4 * tw ] += sR * nw;
tBuffer[tIndex + 4 * tw + 1] += sG * nw;
tBuffer[tIndex + 4 * tw + 2] += sB * nw;
tBuffer[tIndex + 4 * tw + 3] += sA * nw;
}
else
{
w = wx * wy;
tBuffer[tIndex] += sR * w;
tBuffer[tIndex + 1] += sG * w;
tBuffer[tIndex + 2] += sB * w;
tBuffer[tIndex + 3] += sA * w;
nw = nwx * wy;
tBuffer[tIndex + 4] += sR * nw;
tBuffer[tIndex + 5] += sG * nw;
tBuffer[tIndex + 6] += sB * nw;
tBuffer[tIndex + 7] += sA * nw;
nw = wx * nwy;
tBuffer[tIndex + 4 * tw] += sR * nw;
tBuffer[tIndex + 4 * tw + 1] += sG * nw;
tBuffer[tIndex + 4 * tw + 2] += sB * nw;
tBuffer[tIndex + 4 * tw + 3] += sA * nw;
nw = nwx * nwy;
tBuffer[tIndex + 4 * tw + 4] += sR * nw;
tBuffer[tIndex + 4 * tw + 5] += sG * nw;
tBuffer[tIndex + 4 * tw + 6] += sB * nw;
tBuffer[tIndex + 4 * tw + 7] += sA * nw;
}
}
}
var resCV = document.createElement("canvas");
resCV.width = tw;
resCV.height = th;
var resCtx = resCV.getContext("2d");
var imgRes = resCtx.getImageData(0, 0, tw, th);
var tByteBuffer = imgRes.data;
var pxIndex = 0;
for (sIndex = 0, tIndex = 0; pxIndex < tw * th; sIndex += 4, tIndex += 4, pxIndex++)
{
tByteBuffer[tIndex] = Math.ceil(tBuffer[sIndex]);
tByteBuffer[tIndex + 1] = Math.ceil(tBuffer[sIndex + 1]);
tByteBuffer[tIndex + 2] = Math.ceil(tBuffer[sIndex + 2]);
tByteBuffer[tIndex + 3] = Math.ceil(tBuffer[sIndex + 3]);
}
resCtx.putImageData(imgRes, 0, 0);
return resCV;
}
// DRAWING THE BANNER THE FIRST TIME
update();
// SETTING WHAT WILL HAPPEN WHEN THE USER TYPES IN A INPUT BOX
document.getElementById("title").addEventListener("input",function(e){update()});
document.getElementById("titleFont").addEventListener("input",function(e){update()});
document.getElementById("titleSize").addEventListener("input",function(e){update()});
document.getElementById("titleColor").addEventListener("input",function(e){update()});
document.getElementById("titleMarginTop").addEventListener("input",function(e){update()});
document.getElementById("shadowEnabled").addEventListener("click",function(e){update()});
document.getElementById("backColor").addEventListener("input",function(e){update()});
document.getElementById("imgSize").addEventListener("input",function(e){update()});
document.getElementById("imgMarginTop").addEventListener("input",function(e){update()});
// SETTING WHAT WILL HAPPEN WHEN THE USER SELECTS A FILE
document.getElementById("screenshot").addEventListener("change",function(e){update()});
});
</script>
</body>
</html>