-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbannerDistorted1.htm
458 lines (388 loc) · 14.2 KB
/
bannerDistorted1.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Distorted Image Generator</title>
<style>
html,body{margin:0;padding:0;background-color:white}
canvas{text-rendering:optimizeLegibility}
div{position:absolute;left:0;right:0;top:0;width:1900px}
input[type=text]{height:30px;width:40px;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}
.node{width:24px;height:24px;display:none;position:absolute;top:0;left:0;margin-top:20px;margin-left:-5px;border:1px solid red}
</style>
</head>
<body>
<div>
<input type="text" id="x1" placeholder="x1">
<input type="text" id="y1" placeholder="y1">
<input type="text" id="x2" placeholder="x2">
<input type="text" id="y2" placeholder="y2">
<input type="text" id="x3" placeholder="x3">
<input type="text" id="y3" placeholder="y3">
<input type="text" id="x4" placeholder="x4">
<input type="text" id="y4" placeholder="y4">
<input type="file" id="screenshot1">
<input type="file" id="screenshot2"><br />
<canvas width="500" height="500" id="canvasHandler"></canvas>
</div>
<script>
// BASED ON https://stackoverflow.com/questions/4097688/draw-distorted-image-on-html5s-canvas
var controls = [];
var canvas;
var context;
var triangles = [];
var mustExecuteMouseMove = false;
var mustExecuteMouseMoveTarget = null;
var finalImage1 = null;
var finalImage2 = null;
window.addEventListener("load", function()
{
// CLEARING ALL THE INPUTTED VALUES (IF ANY)
document.getElementById("screenshot1").value = null;
document.getElementById("screenshot2").value = null;
document.getElementById("x1").value = "100";
document.getElementById("y1").value = "75";
document.getElementById("x2").value = "200";
document.getElementById("y2").value = "50";
document.getElementById("x3").value = "175";
document.getElementById("y3").value = "175";
document.getElementById("x4").value = "75";
document.getElementById("y4").value = "200";
// SETTING WHAT WILL HAPPEN WHEN THE USER CLICKS ON THE DOCUMENT
document.body.addEventListener("mousedown", function(event)
{
// CHECKING IF THE USER IS CLICKING ON A NODE
if (event.target.className == "node")
{
// SETTING THAT THE MOUSE MOVE MUST BE EXECUTED
mustExecuteMouseMove = true;
// SETTING THE NODE TARGET THAT MUST BE USED
mustExecuteMouseMoveTarget = event.target;
}
else
{
// CLEARING THE VALUES
mustExecuteMouseMove = false;
mustExecuteMouseMoveTarget = null;
}
});
// SETTING WHAT WILL HAPPEN WHEN THE USER IS MOVING THE MOUSE
document.body.addEventListener("mousemove", function(event)
{
// CHECKING IF THE MOUSE MOVE MUST BE EXECUTED AND THAT THERE IS A TARGET
if (mustExecuteMouseMove==true && mustExecuteMouseMoveTarget!=null)
{
// GETTING THE X AND Y VALUE
var x = event.pageX - 7;
var y = event.pageY - 40;
// MOVING THE TARGET
mustExecuteMouseMoveTarget.style.left = x + "px";
mustExecuteMouseMoveTarget.style.top = y + "px";
document.getElementById("x" + mustExecuteMouseMoveTarget.indexValue).value = x;
document.getElementById("y" + mustExecuteMouseMoveTarget.indexValue).value = y;
}
});
// SETTING WHAT WILL HAPPEN WHEN THE USER IS STOPS PRESSING THE MOUSE BUTTON
document.body.addEventListener("mouseup", function(event)
{
// CLEARING THE VALUES
mustExecuteMouseMove = false;
mustExecuteMouseMoveTarget = null;
});
function update()
{
try
{
// CREATING THE FILEREADER
var filereader = new FileReader();
// GETTING THE EXTENSION
var extension = document.getElementById("screenshot1").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 image1 = new Image;
image1.onload = function()
{
// UPDATING THE FINAL IMAGE 1 VALUE
finalImage1 = image1;
// RESIZING THE CANVAS
canvas.width = finalImage1.width;
canvas.height= finalImage1.height;
};
image1.src = filereader.result;
};
// READING/LOADING THE FILE
filereader.readAsDataURL(document.getElementById("screenshot1").files[0]);
}
}
catch(err)
{
}
try
{
// CREATING THE FILEREADER
var filereader2 = new FileReader();
// GETTING THE EXTENSION
var extension = document.getElementById("screenshot2").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
filereader2.onload = function()
{
// LOADING THE IMAGE FILE
var image2 = new Image;
image2.onload = function()
{
// UPDATING THE FINAL IMAGE 2 VALUE
finalImage2 = image2;
// SHOWING AND MOVING THE CONTROL 1
controls[0].style.display = "block";
controls[0].style.left = "100px";
controls[0].style.top = "75px";
document.getElementById("x1").value = "100";
document.getElementById("y1").value = "75";
// SHOWING AND MOVING THE CONTROL 2
controls[1].style.display = "block";
controls[1].style.left = "200px";
controls[1].style.top = "50px";
document.getElementById("x2").value = "200";
document.getElementById("y2").value = "50";
// SHOWING AND MOVING THE CONTROL 3
controls[2].style.display = "block";
controls[2].style.left = "175px";
controls[2].style.top = "175px";
document.getElementById("x3").value = "175";
document.getElementById("y3").value = "175";
// SHOWING AND MOVING THE CONTROL 4
controls[3].style.display = "block";
controls[3].style.left = "75px";
controls[3].style.top = "200px";
document.getElementById("x4").value = "75";
document.getElementById("y4").value = "200";
};
image2.src = filereader2.result;
};
// READING/LOADING THE FILE
filereader2.readAsDataURL(document.getElementById("screenshot2").files[0]);
}
}
catch(err)
{
}
}
function updateNodesLocation()
{
try
{
for (var i = 0; i<controls.length;i++)
{
controls[i].style.left = document.getElementById("x" + (i+1)).value + "px";
controls[i].style.top = document.getElementById("y" + (i+1)).value + "px";
}
}
catch(err)
{
}
}
function draw()
{
try
{
// CHECKING IF THE FIRST IMAGE WAS LOADED
if (finalImage1!=null)
{
// CLEARING THE IMAGE
context.clearRect(0,0,finalImage1.width, finalImage1.height);
// DRAWING THE FIRST IMAGE
context.drawImage(finalImage1, 0, 0, finalImage1.width, finalImage1.height);
}
// CHECKING IF THE SECOND IMAGE WAS LOADED
if (finalImage2!=null)
{
// CALCULATING THE IMAGE GEOMETRY
calculateGeometry();
// LOOPING EVERY TRIANGLE
for (triangle of triangles)
{
// DRAWING EVERY TRIANGLE
drawTriangle(context, finalImage2,
triangle.p0.x, triangle.p0.y,
triangle.p1.x, triangle.p1.y,
triangle.p2.x, triangle.p2.y,
triangle.t0.u, triangle.t0.v,
triangle.t1.u, triangle.t1.v,
triangle.t2.u, triangle.t2.v);
}
}
}
catch(err)
{
}
}
function calculateGeometry()
{
// CLEARING THE TRIANGLES
triangles = [];
// GENERATING THE SUBDIVISION
var subs = 7; // VERTICAL SUBDIVISIONS
var divs = 7; // HORIZONTAL SUBDIVISIONS
var p1 = new Point(parseInt(document.getElementById("x1").value) + 6, parseInt(document.getElementById("y1").value) + 6);
var p2 = new Point(parseInt(document.getElementById("x2").value) + 6, parseInt(document.getElementById("y2").value) + 6);
var p3 = new Point(parseInt(document.getElementById("x3").value) + 6, parseInt(document.getElementById("y3").value) + 6);
var p4 = new Point(parseInt(document.getElementById("x4").value) + 6, parseInt(document.getElementById("y4").value) + 6);
var dx1 = p4.x - p1.x;
var dy1 = p4.y - p1.y;
var dx2 = p3.x - p2.x;
var dy2 = p3.y - p2.y;
var imgW = finalImage2.naturalWidth;
var imgH = finalImage2.naturalHeight;
for (var sub = 0; sub < subs; ++sub)
{
var curRow = sub / subs;
var nextRow = (sub + 1) / subs;
var curRowX1 = p1.x + dx1 * curRow;
var curRowY1 = p1.y + dy1 * curRow;
var curRowX2 = p2.x + dx2 * curRow;
var curRowY2 = p2.y + dy2 * curRow;
var nextRowX1 = p1.x + dx1 * nextRow;
var nextRowY1 = p1.y + dy1 * nextRow;
var nextRowX2 = p2.x + dx2 * nextRow;
var nextRowY2 = p2.y + dy2 * nextRow;
for (var div = 0; div < divs; ++div)
{
var curCol = div / divs;
var nextCol = (div + 1) / divs;
var dCurX = curRowX2 - curRowX1;
var dCurY = curRowY2 - curRowY1;
var dNextX = nextRowX2 - nextRowX1;
var dNextY = nextRowY2 - nextRowY1;
var p1x = curRowX1 + dCurX * curCol;
var p1y = curRowY1 + dCurY * curCol;
var p2x = curRowX1 + (curRowX2 - curRowX1) * nextCol;
var p2y = curRowY1 + (curRowY2 - curRowY1) * nextCol;
var p3x = nextRowX1 + dNextX * nextCol;
var p3y = nextRowY1 + dNextY * nextCol;
var p4x = nextRowX1 + dNextX * curCol;
var p4y = nextRowY1 + dNextY * curCol;
var u1 = curCol * imgW;
var u2 = nextCol * imgW;
var v1 = curRow * imgH;
var v2 = nextRow * imgH;
var triangle1 = new Triangle(
new Point(p1x-1, p1y),
new Point(p3x+2, p3y+1),
new Point(p4x-1, p4y+1),
new TextCoord(u1, v1),
new TextCoord(u2, v2),
new TextCoord(u1, v2)
);
var triangle2 = new Triangle(
new Point(p1x-2, p1y),
new Point(p2x+1, p2y),
new Point(p3x+1, p3y+1),
new TextCoord(u1, v1),
new TextCoord(u2, v1),
new TextCoord(u2, v2)
);
triangles.push(triangle1);
triangles.push(triangle2);
}
}
}
// http://tulrich.com/geekstuff/canvas/jsgl.js
function drawTriangle(ctx, im, x0, y0, x1, y1, x2, y2, sx0, sy0, sx1, sy1, sx2, sy2)
{
ctx.save();
ctx.beginPath();
ctx.moveTo(x0, y0);
ctx.lineTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.closePath();
ctx.clip();
var denom = sx0 * (sy2 - sy1) - sx1 * sy2 + sx2 * sy1 + (sx1 - sx2) * sy0;
if (denom == 0)
{
return;
}
var m11 = -(sy0 * (x2 - x1) - sy1 * x2 + sy2 * x1 + (sy1 - sy2) * x0) / denom;
var m12 = (sy1 * y2 + sy0 * (y1 - y2) - sy2 * y1 + (sy2 - sy1) * y0) / denom;
var m21 = (sx0 * (x2 - x1) - sx1 * x2 + sx2 * x1 + (sx1 - sx2) * x0) / denom;
var m22 = -(sx1 * y2 + sx0 * (y1 - y2) - sx2 * y1 + (sx2 - sx1) * y0) / denom;
var dx = (sx0 * (sy2 * x1 - sy1 * x2) + sy0 * (sx1 * x2 - sx2 * x1) + (sx2 * sy1 - sx1 * sy2) * x0) / denom;
var dy = (sx0 * (sy2 * y1 - sy1 * y2) + sy0 * (sx1 * y2 - sx2 * y1) + (sx2 * sy1 - sx1 * sy2) * y0) / denom;
ctx.transform(m11, m12, m21, m22, dx, dy);
ctx.drawImage(im, 0, 0);
ctx.restore();
}
function TextCoord(u,v)
{
this.u = u?u:0;
this.v = v?v:0;
}
function Triangle(p0, p1, p2, t0, t1, t2)
{
this.p0 = p0;
this.p1 = p1;
this.p2 = p2;
this.t0 = t0;
this.t1 = t1;
this.t2 = t2;
}
function Point(x,y)
{
this.x = x ? x : 0;
this.y = y ? y : 0;
}
var p = Point.prototype;
p.length = function(point)
{
point = point ? point : new Point();
var xs =0, ys =0;
xs = point.x - this.x;
xs = xs * xs;
ys = point.y - this.y;
ys = ys * ys;
return Math.sqrt( xs + ys );
}
// SETTING AN INTERVAL TO DRAW THE IMAGE
setInterval(draw, 1000 / 24);
// GETTING THE CANVAS
canvas = document.getElementById("canvasHandler");
// GETTING THE CANVAS CONTEXT
context = canvas.getContext("2d");
// DRAWING THE NODES
for (var i = 0; i < 4; ++i)
{
// CREATING A NODE
var control = document.createElement("div");
// SETTING THE NODE CLASSNAME
control.className = "node";
// SETTING AN INDEX VALUE
control.indexValue = i + 1;
// ADDING THE NODE TO THE DOCUMENT
document.body.appendChild(control);
// ADDING THE NODE THE CONTROLS ARRAY
controls.push(control);
}
// SETTING WHAT WILL HAPPEN WHEN THE USER TYPES IN A INPUT BOX
document.getElementById("x1").addEventListener("input",function(e){updateNodesLocation()});
document.getElementById("y1").addEventListener("input",function(e){updateNodesLocation()});
document.getElementById("x2").addEventListener("input",function(e){updateNodesLocation()});
document.getElementById("y2").addEventListener("input",function(e){updateNodesLocation()});
document.getElementById("x3").addEventListener("input",function(e){updateNodesLocation()});
document.getElementById("y3").addEventListener("input",function(e){updateNodesLocation()});
document.getElementById("x4").addEventListener("input",function(e){updateNodesLocation()});
document.getElementById("y4").addEventListener("input",function(e){updateNodesLocation()});
// SETTING WHAT WILL HAPPEN WHEN THE USER SELECTS A FILE
document.getElementById("screenshot1").addEventListener("change",function(e){update()});
document.getElementById("screenshot2").addEventListener("change",function(e){update()});
});
</script>
</body>
</html>