forked from migurski/canvas-warp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
130 lines (112 loc) · 4.45 KB
/
index.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Canvas Warp</title>
<style type="text/css" title="text/css">
<!--
body { background-color: #bbb; }
#c { border: 1px solid black; background-color: #ccc; }
-->
</style>
</head>
<body>
<canvas id="c" width="650" height="400"></canvas>
<script type="text/javascript" language="javascript1.5">
<!--
var img = new Image();
/** Solves a system of linear equations.
t1 = (a * r1) + (b + s1) + c
t2 = (a * r2) + (b + s2) + c
t3 = (a * r3) + (b + s3) + c
r1 - t3 are the known values.
a, b, c are the unknowns to be solved.
returns the a, b, c coefficients.
*/
function linearSolution(r1, s1, t1, r2, s2, t2, r3, s3, t3)
{
// make them all floats
r1 = parseFloat(r1);
s1 = parseFloat(s1);
t1 = parseFloat(t1);
r2 = parseFloat(r2);
s2 = parseFloat(s2);
t2 = parseFloat(t2);
r3 = parseFloat(r3);
s3 = parseFloat(s3);
t3 = parseFloat(t3);
var a = (((t2 - t3) * (s1 - s2)) - ((t1 - t2) * (s2 - s3))) / (((r2 - r3) * (s1 - s2)) - ((r1 - r2) * (s2 - s3)));
var b = (((t2 - t3) * (r1 - r2)) - ((t1 - t2) * (r2 - r3))) / (((s2 - s3) * (r1 - r2)) - ((s1 - s2) * (r2 - r3)));
var c = t1 - (r1 * a) - (s1 * b);
return [a, b, c];
}
img.onload = function()
{
var c = document.getElementById('c').getContext('2d');
var w = document.getElementById('c').width;
var h = document.getElementById('c').height;
var x1 = 25 + Math.round(Math.random() * (w - 50));
var y1 = 25 + Math.round(Math.random() * (h - 50));
var x2 = 25 + Math.round(Math.random() * (w - 50));
var y2 = 25 + Math.round(Math.random() * (h - 50));
var x3 = 25 + Math.round(Math.random() * (w - 50));
var y3 = 25 + Math.round(Math.random() * (h - 50));
var x4 = 25 + Math.round(Math.random() * (w - 50));
var y4 = 25 + Math.round(Math.random() * (h - 50));
xm = linearSolution(0, 0, x1, this.width, 0, x2, 0, this.height, x3);
ym = linearSolution(0, 0, y1, this.width, 0, y2, 0, this.height, y3);
xn = linearSolution(this.width, this.height, x4, this.width, 0, x2, 0, this.height, x3);
yn = linearSolution(this.width, this.height, y4, this.width, 0, y2, 0, this.height, y3);
c.save();
// another matrix argument order bug?
c.setTransform(xm[0], ym[0], xm[1], ym[1], xm[2], ym[2]);
c.beginPath();
c.moveTo(0, 0);
c.lineTo(this.width, 0);
c.lineTo(0, this.height);
c.lineTo(0, 0);
c.closePath();
c.fill();
c.clip();
c.drawImage(this, 0, 0, this.width, this.height);
c.restore();
c.save();
// another matrix argument order bug?
c.setTransform(xn[0], yn[0], xn[1], yn[1], xn[2], yn[2]);
c.beginPath();
c.moveTo(this.width, this.height);
c.lineTo(this.width, 0);
c.lineTo(0, this.height);
c.lineTo(this.width, this.height);
c.closePath();
c.fill();
c.clip();
c.drawImage(this, 0, 0, this.width, this.height);
c.restore();
c.beginPath();
c.arc(x1, y1, 5, 0, Math.PI * 2, true);
c.closePath();
c.fillStyle = '#ff0000';
c.fill();
c.beginPath();
c.arc(x2, y2, 5, 0, Math.PI * 2, true);
c.closePath();
c.fillStyle = '#009900';
c.fill();
c.beginPath();
c.arc(x3, y3, 5, 0, Math.PI * 2, true);
c.closePath();
c.fillStyle = '#0000ff';
c.fill();
c.beginPath();
c.arc(x4, y4, 5, 0, Math.PI * 2, true);
c.closePath();
c.fillStyle = '#666666';
c.fill();
};
img.src = 'magical_forest.jpg';
//-->
</script>
</body>
</html>