-
Notifications
You must be signed in to change notification settings - Fork 3
/
webgl-distort.js
171 lines (131 loc) · 4.22 KB
/
webgl-distort.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
// http://stackoverflow.com/questions/4998908/convert-data-uri-to-file-then-append-to-formdata
function dataURItoBlob(dataURI) {
// convert base64/URLEncoded data component to raw binary data held in a string
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
// write the bytes of the string to a typed array
var ia = new Uint8Array(byteString.length);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ia], {type:mimeString});
}
function canvasToBlobUrl(canvas) {
var blob = dataURItoBlob(canvas.toDataURL('image/png'));
return window.URL.createObjectURL(blob);
}
function warpWebGl(id, matrix1, matrix2, download) {
// try to create a WebGL canvas (will fail if WebGL isn't supported)
try {
var canvas = fx.canvas(1500,1500);
} catch (e) {
alert(e);
return;
}
// convert the image to a texture
var imageEl = document.getElementById(id);
var image = new Image();
image.onload = function() {
var texture = canvas.texture(image);
var bbox1 = {
nw: {
x: matrix1[0],
y: matrix1[1]
},
ne: {
x: matrix1[2],
y: matrix1[3]
},
se: {
x: matrix1[4],
y: matrix1[5]
},
sw: {
x: matrix1[6],
y: matrix1[7]
}
},
bbox2 = {
nw: {
x: matrix2[0],
y: matrix2[1]
},
ne: {
x: matrix2[2],
y: matrix2[3]
},
se: {
x: matrix2[4],
y: matrix2[5]
},
sw: {
x: matrix2[6],
y: matrix2[7]
}
};
var matrix1Xs = [],
matrix1Ys = [];
for (var i = 0; i < matrix1.length; i += 2) {
matrix1Xs.push(matrix1[i])
}
for (var i = 1; i < matrix1.length; i += 2) {
matrix1Ys.push(matrix1[i])
}
var matrix1northmost = Math.min.apply(null, matrix1Ys)
matrix1southmost = Math.max.apply(null, matrix1Ys)
matrix1westmost = Math.min.apply(null, matrix1Xs)
matrix1eastmost = Math.max.apply(null, matrix1Xs);
var matrix2Xs = [],
matrix2Ys = [];
for (var i = 0; i < matrix2.length; i += 2) {
matrix2Xs.push(matrix2[i])
}
for (var i = 1; i < matrix2.length; i += 2) {
matrix2Ys.push(matrix2[i])
}
var matrix2northmost = Math.min.apply(null, matrix2Ys)
matrix2southmost = Math.max.apply(null, matrix2Ys)
matrix2westmost = Math.min.apply(null, matrix2Xs)
matrix2eastmost = Math.max.apply(null, matrix2Xs);
var offsetX = matrix2westmost - matrix1westmost;
var offsetY = matrix2northmost - matrix1northmost;
canvas.draw(texture,
image.width,// * ratio,
image.height// * ratio
);
var ratioY = (matrix2southmost - matrix2northmost)
/ (matrix1southmost - matrix1northmost);
var ratioX = (matrix2eastmost - matrix2westmost)
/ (matrix1eastmost - matrix1westmost);
var ratio = Math.max(ratioX, ratioY);
// stretch output matrix x to fix:
for (var i = 0; i < matrix2.length; i += 2) {
matrix2[i] -= offsetX;
matrix2[i] /= ratio;
}
// stretch output matrix y to fix:
for (var i = 1; i < matrix2.length; i += 2) {
matrix2[i] -= offsetY;
matrix2[i] /= ratio;
}
canvas.perspective(matrix1, matrix2).update();
// replace the image with the canvas
// image.parentNode.insertBefore(canvas, image);
// image.parentNode.removeChild(image);
var burl = canvasToBlobUrl(canvas);
if (download) {
window.open(burl);
} else { // replace the image
// keep non-blob version in case we have to fall back:
// image.src = canvas.toDataURL('image/png');
// window.location = canvas.toDataURL('image/png');
imageEl.src = burl;
}
}
image.src = imageEl.src;
}