forked from migurski/canvas-warp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarp-simple.html
288 lines (231 loc) · 12.1 KB
/
warp-simple.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
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
<!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 Simple</title>
<script src="modestmaps.js" type="text/javascript" language="javascript1.5"></script>
<script src="follower.js" type="text/javascript" language="javascript1.5"></script>
<script src="zoomify.js" type="text/javascript" language="javascript1.5"></script>
<style type="text/css" title="text/css">
<!--
body { background-color: #bbb; }
.map
{
border: 1px solid black;
background-color: #ccc;
width: 550px;
height: 390px;
margin: 0 1em 1em 0;
float: left;
}
-->
</style>
</head>
<body>
<div id="picture" class="map"></div>
<!--<div id="basemap" class="map"></div>-->
<div id="combine" class="map"></div>
<script type="text/javascript" language="javascript1.5">
<!--
var mm = com.modestmaps;
var Marker = function(location, point)
{
this.location = location;
this.point = point;
}
var zoomify = new ZoomifyProvider('http://localhost/~migurski/Canvas%20Warp/pages-12-13_img', 11946, 5670);
var picture = new mm.Map('picture', zoomify, new mm.Point(550, 390))
picture.setCenterZoom(new mm.Location(559, 2563), 5);
picture.draw();
/*
var basemap = new mm.Map('basemap', new mm.MapProvider(function(c) { return 'http://tile.openstreetmap.org/'+c.zoom+'/'+c.column+'/'+c.row+'.png'; }), new mm.Point(550, 390))
basemap.setCenterZoom(new mm.Location(37.805, -122.263), 15);
basemap.draw();
*/
var combine = new mm.Map('combine', new mm.MapProvider(function(c) { return 'http://tile.openstreetmap.org/'+c.zoom+'/'+c.column+'/'+c.row+'.png'; }), new mm.Point(550, 390))
combine.setCenterZoom(new mm.Location(37.805, -122.263), 15);
combine.revision = 1;
combine.addCallback('zoomed', function(m, a) { return onMapChanged(); });
combine.addCallback('centered', function(m, a) { return onMapChanged(); });
combine.addCallback('extentset', function(m, a) { return onMapChanged(); });
combine.addCallback('panned', function(m, a) { return onMapChanged(); });
combine.draw();
var canvas = document.createElement('canvas');
canvas.style.position = 'absolute';
canvas.width = 550;
canvas.height = 390;
combine.parent.appendChild(canvas);
var m1 = new Marker(new mm.Location(37.808055, -122.264507), new mm.Point(2296, 465)); // 20th & Harrison
var m2 = new Marker(new mm.Location(37.802036, -122.269700), new mm.Point(2480, 982)); // 12th & Webster
var m3 = new Marker(new mm.Location(37.799730, -122.263756), new mm.Point(2812, 813)); // 12th & Oak
var m4 = new Marker(new mm.Location(37.803715, -122.261197), new mm.Point(2654, 506)); // 17th & Oak
var m5 = new Marker(new mm.Location(37.810174, -122.273176), new mm.Point(1868, 773)); // 20th & San Pablo
function warpTo(zoomifyProvider, destinationMap, canvas, m1, m2, m3, revision)
{
// pixel positions of triangle corners
var p1 = destinationMap.locationPoint(m1.location);
var p2 = destinationMap.locationPoint(m2.location);
var p3 = destinationMap.locationPoint(m3.location);
// coordinates of triangle corners
var c1 = new mm.Coordinate(m1.point.y, m1.point.x, zoomifyProvider.bottomright.zoom);
var c2 = new mm.Coordinate(m2.point.y, m2.point.x, zoomifyProvider.bottomright.zoom);
var c3 = new mm.Coordinate(m3.point.y, m3.point.x, zoomifyProvider.bottomright.zoom);
// transformation from image pixel space to html pixel space
var t = mm.Transformation.prototype.deriveTransformation(c1.column, c1.row, p1.x, p1.y, c2.column, c2.row, p2.x, p2.y, c3.column, c3.row, p3.x, p3.y);
//alert([t.ax, t.bx, t.cx, t.ay, t.by, t.cy]);
// get the best resampled zoom level from the image pyramid
var sx = Math.sqrt(Math.pow(t.ax, 2) + Math.pow(t.bx, 2));
var sy = Math.sqrt(Math.pow(t.ay, 2) + Math.pow(t.by, 2));
var s = Math.max(sx, sy); //(sx + sy) / 2;
var z = zoomifyProvider.bottomright.zoom + Math.min(0, Math.round(Math.log(s) / Math.LN2));
//alert(['native zoom level', z, Math.min(0, Math.round(Math.log(s) / Math.LN2)), zoomifyProvider.bottomright.zoom].join('\n'));
// pixel space zoom-corrected coordinates of triangle corners
c1 = c1.zoomTo(z);
c2 = c2.zoomTo(z);
c3 = c3.zoomTo(z);
// do this again at the corrected zoom
var t = mm.Transformation.prototype.deriveTransformation(c1.column, c1.row, p1.x, p1.y, c2.column, c2.row, p2.x, p2.y, c3.column, c3.row, p3.x, p3.y);
//alert(['pixel space transform', t.ax, t.bx, t.cx, t.ay, t.by, t.cy].join('\n'));
// for later, in pixel space
var triangle = [c1.column, c1.row, c2.column, c2.row, c3.column, c3.row];
var transform = [t.ax, t.ay, t.bx, t.by, t.cx, t.cy];
// tile space zoom-corrected coordinates of triangle corners
c1 = c1.zoomBy(-8);
c2 = c2.zoomBy(-8);
c3 = c3.zoomBy(-8);
var minrow = Math.min(c1.row, c2.row, c3.row);
var maxrow = Math.max(c1.row, c2.row, c3.row);
var mincol = Math.min(c1.column, c2.column, c3.column);
var maxcol = Math.max(c1.column, c2.column, c3.column);
var ul = new mm.Coordinate(minrow, mincol, c1.zoom).container();
var br = new mm.Coordinate(maxrow, maxcol, c1.zoom).container();
//alert(['boundaries of triangle', minrow, mincol, maxrow, maxcol, ul, br].join('\n'));
var tiles = [];
for(var row = ul.row; row <= br.row; row += 1)
{
for(var col = ul.column; col <= br.column; col += 1)
{
var tile = new mm.Coordinate(row, col, ul.zoom);
var offset = new mm.Point(tile.zoomBy(8).column, tile.zoomBy(8).row);
var url = zoomifyProvider.getTileUrl(tile);
tiles.push({tile: tile, offset: offset, url: url, transform: transform, triangle: triangle});
}
}
//alert('Drawing ' + tiles.length + ' tiles');
drawTiles(canvas.getContext('2d'), tiles, destinationMap, revision);
}
function drawTiles(c, tiles, destinationMap, revision)
{
if(tiles.length)
{
var tile = tiles.shift();
var img = new Image();
img.onload = function()
{
if(destinationMap.revision != revision)
{
return;
}
c.save();
c.setTransform(tile.transform[0], tile.transform[1], tile.transform[2], tile.transform[3], tile.transform[4], tile.transform[5]);
c.beginPath();
c.moveTo(tile.triangle[0], tile.triangle[1]);
c.lineTo(tile.triangle[2], tile.triangle[3]);
c.lineTo(tile.triangle[4], tile.triangle[5]);
c.lineTo(tile.triangle[0], tile.triangle[1]);
c.closePath();
c.clip();
c.globalAlpha = .65;
c.drawImage(this, tile.offset.x, tile.offset.y, 256, 256);
c.restore();
drawTiles(c, tiles, destinationMap, revision);
}
img.src = tile.url;
}
}
function onMapChanged()
{
combine.revision += 1;
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
warpTo(zoomify, combine, canvas, m1, m2, m3, combine.revision);
warpTo(zoomify, combine, canvas, m1, m4, m3, combine.revision);
warpTo(zoomify, combine, canvas, m1, m2, m5, combine.revision);
// what follows is a potentially stupid way to do 2-dim linear regression
var markers = [m1, m2, m3, m4, m5];
var avglat = 0;
var avglon = 0;
var avgx = 0;
var avgy = 0;
var j = 0;
for(var i = 0; i < markers.length; i += 1)
{
avglat = ((j * avglat) + markers[i].location.lat) / (j + 1);
avglon = ((j * avglon) + markers[i].location.lon) / (j + 1);
avgx = ((j * avgx) + markers[i].point.x) / (j + 1);
avgy = ((j * avgy) + markers[i].point.y) / (j + 1);
j += 1;
}
//alert([avgx, avgy, avglat, avglon]);
var num = 0;
var den = 0;
for(var i in markers)
{
num += (markers[i].point.x - avgx) * (markers[i].location.lon - avglon);
den += (markers[i].point.x - avgx) * (markers[i].point.x - avgx);
}
var lonmx = num/den;
var lonbx = avglon - (lonmx * avgx);
//alert([lonmx, lonbx]);
var num = 0;
var den = 0;
for(var i in markers)
{
num += (markers[i].point.y - avgy) * (markers[i].location.lon - avglon);
den += (markers[i].point.y - avgy) * (markers[i].point.y - avgy);
}
var lonmy = num/den;
var lonby = avglon - (lonmy * avgy);
//alert([lonmy, lonby]);
var estlon = function(x, y) { return lonmx * x + lonmy * y + (lonbx + lonby) / 2; }
var num = 0;
var den = 0;
for(var i in markers)
{
num += (markers[i].point.x - avgx) * (markers[i].location.lat - avglat);
den += (markers[i].point.x - avgx) * (markers[i].point.x - avgx);
}
var latmx = num/den;
var latbx = avglat - (latmx * avgx);
//alert([latmx, latbx]);
var num = 0;
var den = 0;
for(var i in markers)
{
num += (markers[i].point.y - avgy) * (markers[i].location.lat - avglat);
den += (markers[i].point.y - avgy) * (markers[i].point.y - avgy);
}
var latmy = num/den;
var latby = avglat - (latmy * avgy);
//alert([latmy, latby]);
var estlat = function(x, y) { return latmx * x + latmy * y + (latbx + latby) / 2; }
var c = canvas.getContext('2d');
var ul = new mm.Location(estlat(0, 0), estlon(0, 0));
var bl = new mm.Location(estlat(0, zoomify.bottomright.row), estlon(0, zoomify.bottomright.row));
var ur = new mm.Location(estlat(zoomify.bottomright.column, 0), estlon(zoomify.bottomright.column, 0));
var br = new mm.Location(estlat(zoomify.bottomright.column, zoomify.bottomright.row), estlon(zoomify.bottomright.column, zoomify.bottomright.row));
c.save();
c.setTransform();
c.beginPath();
c.moveTo(combine.locationPoint(bl).x, combine.locationPoint(bl).y);
c.lineTo(combine.locationPoint(ul).x, combine.locationPoint(ul).y);
c.lineTo(combine.locationPoint(ur).x, combine.locationPoint(ur).y);
c.lineTo(combine.locationPoint(br).x, combine.locationPoint(br).y);
c.stroke();
c.restore();
}
onMapChanged();
//-->
</script>
</body>
</html>