author | Tero Marttila <terom@fixme.fi> |
Wed, 06 Jan 2010 18:35:49 +0200 | |
changeset 42 | a5bca7b0cd8a |
parent 40 | 5454d2e2f633 |
child 43 | fcd818eb5a71 |
permissions | -rw-r--r-- |
30 | 1 |
// A source of tile images of a specific width/height, zoom level range, and some other attributes |
2 |
var Source = Class.create({ |
|
3 |
initialize: function (path, tile_width, tile_height, zoom_min, zoom_max) { |
|
4 |
this.path = path; |
|
5 |
this.tile_width = tile_width; |
|
6 |
this.tile_height = tile_height; |
|
7 |
this.zoom_min = zoom_min; |
|
8 |
this.zoom_max = zoom_max; |
|
9 |
||
10 |
this.refresh = false; |
|
11 |
this.opt_key = this.opt_value = null; |
|
12 |
}, |
|
13 |
||
14 |
// build a URL for the given tile image |
|
15 |
build_url: function (col, row, zl, sw, sh) { |
|
16 |
// two-bit hash (0-4) based on the (col, row) |
|
17 |
var hash = ( (col % 2) << 1 | (row % 2) ) + 1; |
|
18 |
||
19 |
// the subdomain to use |
|
20 |
var subdomain = ""; |
|
21 |
||
22 |
if (0) |
|
23 |
subdomain = "tile" + hash + "."; |
|
24 |
||
25 |
// the (x, y) co-ordinates |
|
26 |
var x = col * this.tile_width; |
|
27 |
var y = row * this.tile_height; |
|
28 |
||
40 | 29 |
var url = this.path + "?x=" + x + "&y=" + y + "&zl=" + zl; // + "&sw=" + sw + "&sh=" + sh; |
30 | 30 |
|
31 |
if (this.refresh) |
|
32 |
url += "&ts=" + new Date().getTime(); |
|
33 |
||
34 |
if (this.opt_key && this.opt_value) |
|
35 |
url += "&" + this.opt_key + "=" + this.opt_value; |
|
36 |
||
37 |
return url; |
|
38 |
}, |
|
40 | 39 |
|
40 |
// build an URL for a full image |
|
41 |
build_image_url: function (cx, cy, w, h, zl) { |
|
42 |
return (this.path + "?cx=" + cx + "&cy=" + cy + "&w=" + w + "&h=" + h + "&zl=" + zl); |
|
43 |
} |
|
30 | 44 |
}); |
45 |
||
46 |
// a viewport that contains a substrate which contains several zoom layers which contain many tiles |
|
47 |
var Viewport = Class.create({ |
|
48 |
initialize: function (source, viewport_id) { |
|
49 |
this.source = source; |
|
50 |
||
51 |
this.id = viewport_id; |
|
52 |
this.div = $(viewport_id); |
|
53 |
this.substrate = this.div.down("div.substrate"); |
|
54 |
||
55 |
// the stack of zoom levels |
|
56 |
this.zoom_layers = []; |
|
57 |
||
58 |
// pre-populate the stack |
|
59 |
for (var zoom_level = source.zoom_min; zoom_level <= source.zoom_max; zoom_level++) { |
|
60 |
var zoom_layer = new ZoomLayer(source, zoom_level); |
|
61 |
||
62 |
this.substrate.appendChild(zoom_layer.div); |
|
63 |
this.zoom_layers[zoom_level] = zoom_layer; |
|
64 |
} |
|
65 |
||
66 |
// make the substrate draggable |
|
67 |
this.draggable = new Draggable(this.substrate, { |
|
68 |
onStart: this.on_scroll_start.bind(this), |
|
69 |
onDrag: this.on_scroll_move.bind(this), |
|
70 |
onEnd: this.on_scroll_end.bind(this), |
|
71 |
}); |
|
72 |
||
73 |
// event handlers |
|
74 |
Event.observe(this.substrate, "dblclick", this.on_dblclick.bindAsEventListener(this)); |
|
75 |
Event.observe(this.substrate, "mousewheel", this.on_mousewheel.bindAsEventListener(this)); |
|
76 |
Event.observe(this.substrate, "DOMMouseScroll", this.on_mousewheel.bindAsEventListener(this)); // mozilla |
|
77 |
Event.observe(document, "resize", this.on_resize.bindAsEventListener(this)); |
|
39 | 78 |
|
79 |
// zoom buttons |
|
80 |
this.btn_zoom_in = $("btn-zoom-in"); |
|
81 |
this.btn_zoom_out = $("btn-zoom-out"); |
|
82 |
||
83 |
if (this.btn_zoom_in) |
|
84 |
Event.observe(this.btn_zoom_in, "click", this.zoom_in.bindAsEventListener(this)); |
|
85 |
||
86 |
if (this.btn_zoom_out) |
|
87 |
Event.observe(this.btn_zoom_out, "click", this.zoom_out.bindAsEventListener(this)); |
|
32 | 88 |
|
89 |
// set viewport size |
|
30 | 90 |
this.update_size(); |
32 | 91 |
|
40 | 92 |
// this comes after update_size, since it must be updated once we have the size and zoom layer... |
93 |
this.image_link = $("lnk-image"); |
|
94 |
||
32 | 95 |
// initial location? |
96 |
if (document.location.hash) { |
|
97 |
// x:y:z tuple |
|
98 |
var pt = document.location.hash.substr(1).split(":"); |
|
99 |
||
100 |
// unpack |
|
101 |
var cx = 0, cy = 0, z = 0; |
|
102 |
||
103 |
if (pt.length) cx = parseInt(pt.shift()); |
|
104 |
if (pt.length) cy = parseInt(pt.shift()); |
|
105 |
if (pt.length) z = parseInt(pt.shift()); |
|
30 | 106 |
|
32 | 107 |
// initial view |
38
0abb7289d4b8
fix intial zoom to be unscaled
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
108 |
this.zoom_scaled( |
0abb7289d4b8
fix intial zoom to be unscaled
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
109 |
cx - this.center_offset_x, |
0abb7289d4b8
fix intial zoom to be unscaled
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
110 |
cy - this.center_offset_y, |
0abb7289d4b8
fix intial zoom to be unscaled
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
111 |
z |
0abb7289d4b8
fix intial zoom to be unscaled
Tero Marttila <terom@fixme.fi>
parents:
34
diff
changeset
|
112 |
); |
32 | 113 |
|
114 |
} else { |
|
115 |
// this sets the scroll offsets, zoom level, and loads the tiles |
|
116 |
this.zoom_to(0, 0, 0); |
|
117 |
} |
|
30 | 118 |
}, |
119 |
||
120 |
/* event handlers */ |
|
121 |
||
122 |
// window resized |
|
123 |
on_resize: function (ev) { |
|
124 |
this.update_size(); |
|
125 |
this.update_tiles(); |
|
126 |
}, |
|
127 |
||
128 |
// double-click handler |
|
129 |
on_dblclick: function (ev) { |
|
130 |
var offset = this.event_offset(ev); |
|
131 |
||
132 |
this.zoom_center_to( |
|
133 |
this.scroll_x + offset.x, |
|
134 |
this.scroll_y + offset.y, |
|
135 |
1 // zoom in |
|
136 |
); |
|
137 |
}, |
|
138 |
||
139 |
// mousewheel handler |
|
140 |
on_mousewheel: function (ev) { |
|
141 |
// this works in very weird ways, so it's based on code from http://adomas.org/javascript-mouse-wheel/ |
|
142 |
// (it didn't include any license, so this is written out manually) |
|
143 |
var delta; |
|
144 |
||
145 |
// this is browser-dependant... |
|
146 |
if (ev.wheelDelta) { |
|
147 |
// IE + Opera |
|
148 |
delta = ev.wheelDelta; |
|
149 |
||
150 |
if (window.opera) { |
|
151 |
// Opera, but apparently not newer versions? |
|
152 |
//delta = -delta; |
|
153 |
} |
|
154 |
||
155 |
} else if (ev.detail) { |
|
156 |
// Mozilla |
|
157 |
delta = -ev.detail; |
|
158 |
||
159 |
} else { |
|
160 |
// mousewheel not supported... |
|
161 |
return; |
|
162 |
||
163 |
} |
|
164 |
||
165 |
// don't scroll the page |
|
166 |
if (ev.preventDefault) |
|
167 |
ev.preventDefault(); |
|
168 |
||
169 |
// delta > 0 : scroll up, zoom in |
|
170 |
// delta < 0 : scroll down, zoom out |
|
171 |
delta = delta < 0 ? -1 : 1; |
|
172 |
||
173 |
// Firefox's DOMMouseEvent's pageX/Y attributes are broken. layerN is for mozilla, offsetN for IE, seems to work |
|
174 |
||
175 |
// absolute location of the cursor |
|
176 |
var x = parseInt(ev.target.style.left) + (ev.layerX ? ev.layerX : ev.offsetX); |
|
177 |
var y = parseInt(ev.target.style.top) + (ev.layerY ? ev.layerY : ev.offsetY); |
|
178 |
||
179 |
// zoom \o/ |
|
180 |
this.zoom_center_to(x, y, delta); |
|
181 |
}, |
|
182 |
||
183 |
// substrate scroll was started |
|
184 |
on_scroll_start: function (ev) { |
|
185 |
||
186 |
}, |
|
187 |
||
188 |
// substrate was scrolled, update scroll_{x,y}, and then update tiles after 100ms |
|
189 |
on_scroll_move: function (ev) { |
|
190 |
this.update_scroll(); |
|
191 |
this.update_after_timeout(); |
|
192 |
}, |
|
193 |
||
194 |
// substrate scroll was ended, update tiles now |
|
195 |
on_scroll_end: function (ev) { |
|
196 |
this.update_now(); |
|
197 |
}, |
|
198 |
||
199 |
/* get state */ |
|
200 |
||
201 |
// return the absolute (x, y) coords of the given event inside the viewport |
|
202 |
event_offset: function (ev) { |
|
203 |
var offset = this.div.cumulativeOffset(); |
|
204 |
||
205 |
return { |
|
206 |
x: ev.pointerX() - offset.left, |
|
207 |
y: ev.pointerY() - offset.top |
|
208 |
}; |
|
209 |
}, |
|
210 |
||
211 |
/* modify state */ |
|
212 |
||
213 |
// scroll the view to place the given absolute (x, y) co-ordinate at the top left |
|
214 |
scroll_to: function (x, y) { |
|
215 |
// update it via the style |
|
216 |
this.substrate.style.top = "-" + y + "px"; |
|
217 |
this.substrate.style.left = "-" + x + "px"; |
|
218 |
||
219 |
// update these as well |
|
220 |
this.scroll_x = x; |
|
221 |
this.scroll_y = y; |
|
222 |
}, |
|
223 |
||
224 |
// scroll the view to place the given absolute (x, y) co-ordinate at the center |
|
225 |
scroll_center_to: function (x, y) { |
|
226 |
return this.scroll_to( |
|
227 |
x - this.center_offset_x, |
|
228 |
y - this.center_offset_y |
|
229 |
); |
|
230 |
}, |
|
231 |
||
232 |
// zoom à la delta such that the given (zoomed) absolute (x, y) co-ordinates will be at the top left |
|
233 |
zoom_scaled: function (x, y, delta) { |
|
234 |
if (!this.update_zoom(delta)) |
|
235 |
return false; |
|
236 |
||
237 |
// scroll to the new position |
|
238 |
this.scroll_to(x, y); |
|
239 |
||
240 |
// update view |
|
241 |
// XXX: ... |
|
242 |
this.update_after_timeout(); |
|
243 |
||
244 |
return true; |
|
245 |
}, |
|
246 |
||
247 |
// zoom à la delta such that the given (current) absolute (x, y) co-ordinates will be at the top left |
|
248 |
zoom_to: function (x, y, delta) { |
|
249 |
return this.zoom_scaled( |
|
250 |
scaleByZoomDelta(x, delta), |
|
251 |
scaleByZoomDelta(y, delta), |
|
252 |
delta |
|
253 |
); |
|
254 |
}, |
|
255 |
||
256 |
// zoom à la delta such that the given (current) absolute (x, y) co-ordinates will be at the center |
|
257 |
zoom_center_to: function (x, y, delta) { |
|
258 |
return this.zoom_scaled( |
|
259 |
scaleByZoomDelta(x, delta) - this.center_offset_x, |
|
260 |
scaleByZoomDelta(y, delta) - this.center_offset_y, |
|
261 |
delta |
|
262 |
); |
|
263 |
}, |
|
264 |
||
39 | 265 |
// zoom à la delta, keeping the view centered |
266 |
zoom_centered: function (delta) { |
|
267 |
return this.zoom_center_to( |
|
268 |
this.scroll_x + this.center_offset_x, |
|
269 |
this.scroll_y + this.center_offset_y, |
|
270 |
delta |
|
271 |
); |
|
272 |
}, |
|
273 |
||
274 |
// zoom in one level, keeping the view centered |
|
275 |
zoom_in: function () { |
|
276 |
return this.zoom_centered(+1); |
|
277 |
}, |
|
278 |
||
279 |
// zoom out one leve, keeping the view centered |
|
280 |
zoom_out: function () { |
|
281 |
return this.zoom_centered(-1); |
|
282 |
}, |
|
30 | 283 |
|
284 |
/* update view/state to reflect reality */ |
|
285 |
||
286 |
// figure out the viewport dimensions |
|
287 |
update_size: function () { |
|
288 |
this.view_width = this.div.getWidth(); |
|
289 |
this.view_height = this.div.getHeight(); |
|
290 |
||
291 |
this.center_offset_x = Math.floor(this.view_width / 2); |
|
292 |
this.center_offset_y = Math.floor(this.view_height / 2); |
|
40 | 293 |
|
294 |
this.update_image_link(); |
|
30 | 295 |
}, |
296 |
||
297 |
// figure out the scroll offset as absolute pixel co-ordinates at the top left |
|
298 |
update_scroll: function() { |
|
299 |
this.scroll_x = -parseInt(this.substrate.style.left); |
|
300 |
this.scroll_y = -parseInt(this.substrate.style.top); |
|
301 |
}, |
|
302 |
||
303 |
// wiggle the ZoomLevels around to match the current zoom level |
|
304 |
update_zoom: function(delta) { |
|
305 |
if (!this.zoom_layer) { |
|
306 |
// first zoom operation |
|
307 |
||
308 |
// is the new zoom level valid? |
|
309 |
if (!this.zoom_layers[delta]) |
|
310 |
return false; |
|
311 |
||
312 |
// set the zoom layyer |
|
313 |
this.zoom_layer = this.zoom_layers[delta]; |
|
314 |
||
315 |
// enable it |
|
316 |
this.zoom_layer.enable(11); |
|
317 |
||
318 |
// no need to .update_tiles or anything like that |
|
319 |
||
320 |
} else { |
|
321 |
// is the new zoom level valid? |
|
322 |
if (!this.zoom_layers[this.zoom_layer.level + delta]) |
|
323 |
return false; |
|
324 |
||
325 |
var zoom_old = this.zoom_layer; |
|
326 |
var zoom_new = this.zoom_layers[this.zoom_layer.level + delta]; |
|
327 |
||
328 |
// XXX: ugly hack |
|
329 |
if (this.zoom_timer) { |
|
330 |
clearTimeout(this.zoom_timer); |
|
331 |
this.zoom_timer = null; |
|
332 |
} |
|
333 |
||
334 |
// get other zoom layers out of the way |
|
335 |
this.zoom_layers.each(function (zl) { |
|
336 |
zl.disable(); |
|
337 |
}); |
|
338 |
||
339 |
// update the zoom layer |
|
340 |
this.zoom_layer = zoom_new; |
|
341 |
||
342 |
// apply new z-indexes, preferr the current one over the new one |
|
343 |
zoom_new.enable(11); |
|
344 |
zoom_old.enable(10); |
|
345 |
||
346 |
// resize the tiles in the two zoom layers |
|
347 |
zoom_new.update_tiles(zoom_new.level); |
|
348 |
zoom_old.update_tiles(zoom_old.level); |
|
349 |
||
350 |
// XXX: ugly hack |
|
351 |
this.zoom_timer = setTimeout(function () { zoom_old.disable()}, 1000); |
|
352 |
} |
|
353 |
||
39 | 354 |
// update UI |
355 |
this.update_zoom_ui(); |
|
356 |
||
30 | 357 |
return true; |
358 |
}, |
|
39 | 359 |
|
360 |
// keep the zoom buttons, if any, updated |
|
361 |
update_zoom_ui: function () { |
|
362 |
if (this.btn_zoom_in) |
|
363 |
(this.zoom_layer.level >= this.source.zoom_max) ? this.btn_zoom_in.disable() : this.btn_zoom_in.enable(); |
|
364 |
||
365 |
if (this.btn_zoom_out) |
|
366 |
(this.zoom_layer.level <= this.source.zoom_min) ? this.btn_zoom_out.disable() : this.btn_zoom_out.enable(); |
|
40 | 367 |
|
368 |
this.update_image_link(); |
|
39 | 369 |
}, |
30 | 370 |
|
371 |
// ensure that all tiles that are currently visible are loaded |
|
372 |
update_tiles: function() { |
|
373 |
// short names for some vars... |
|
374 |
var x = this.scroll_x; |
|
375 |
var y = this.scroll_y; |
|
376 |
var sw = this.view_width; |
|
377 |
var sh = this.view_height; |
|
378 |
var tw = this.source.tile_width; |
|
379 |
var th = this.source.tile_height; |
|
380 |
var zl = this.zoom_layer.level; |
|
381 |
||
382 |
// figure out what set of columns are visible |
|
383 |
var start_col = Math.max(0, Math.floor(x / tw)); |
|
384 |
var start_row = Math.max(0, Math.floor(y / th)); |
|
385 |
var end_col = Math.floor((x + sw) / tw); |
|
386 |
var end_row = Math.floor((y + sh) / th); |
|
387 |
||
388 |
// loop through all those tiles |
|
389 |
for (var col = start_col; col <= end_col; col++) { |
|
390 |
for (var row = start_row; row <= end_row; row++) { |
|
391 |
// the tile's id |
|
392 |
var id = "tile_" + this.zoom_layer.level + "_" + col + "_" + row; |
|
393 |
||
394 |
// does the element exist already? |
|
395 |
var t = $(id); |
|
396 |
||
397 |
if (!t) { |
|
398 |
// build a new tile |
|
399 |
t = Builder.node("img", { |
|
400 |
src: this.source.build_url(col, row, zl, sw, sh), |
|
401 |
id: id, |
|
402 |
title: "(" + col + ", " + row + ")", |
|
403 |
// style set later |
|
404 |
} |
|
405 |
); |
|
406 |
||
407 |
// set the CSS style stuff |
|
408 |
t.style.top = th * row; |
|
409 |
t.style.left = tw * col; |
|
410 |
t.style.display = "none"; |
|
411 |
||
412 |
// wait for it to load |
|
413 |
Event.observe(t, "load", _tile_loaded.bindAsEventListener(t)); |
|
414 |
||
415 |
// store the col/row |
|
416 |
t.__col = col; |
|
417 |
t.__row = row; |
|
418 |
||
419 |
// add it to the zoom layer |
|
420 |
this.zoom_layer.add_tile(t); |
|
421 |
||
422 |
} else if (this.source.reload) { |
|
423 |
// force the tile to reload |
|
424 |
touch_tile(t, col, row); |
|
425 |
||
426 |
} |
|
427 |
} |
|
428 |
} |
|
40 | 429 |
|
430 |
this.update_scroll_ui(); |
|
431 |
}, |
|
432 |
||
433 |
// update scroll-dependant UI elements |
|
434 |
update_scroll_ui: function () { |
|
435 |
// update link-to-image |
|
436 |
this.update_image_link(); |
|
30 | 437 |
|
438 |
// update the link-to-this-page thing |
|
32 | 439 |
document.location.hash = "#" + (this.scroll_x + this.center_offset_x) + ":" + (this.scroll_y + this.center_offset_y) + ":" + this.zoom_layer.level; |
40 | 440 |
}, |
441 |
||
442 |
// update image link with size, zoom, pos |
|
443 |
update_image_link: function () { |
|
444 |
if (!this.image_link) |
|
445 |
return; |
|
446 |
||
447 |
this.image_link.href = this.source.build_image_url( |
|
448 |
this.scroll_x + this.center_offset_x, |
|
449 |
this.scroll_y + this.center_offset_y, |
|
450 |
this.view_width, this.view_height, |
|
451 |
this.zoom_layer.level |
|
452 |
); |
|
453 |
||
454 |
this.image_link.innerHTML = this.view_width + "x" + this.view_height + "@" + this.zoom_layer.level; |
|
455 |
}, |
|
30 | 456 |
|
457 |
// do update_tiles after 100ms, unless we are called again |
|
458 |
update_after_timeout: function () { |
|
459 |
this._update_idle = false; |
|
460 |
||
461 |
if (this._update_timeout) |
|
462 |
clearTimeout(this._update_timeout); |
|
463 |
||
464 |
this._update_timeout = setTimeout(this._update_timeout_trigger.bind(this), 100); |
|
465 |
}, |
|
466 |
||
467 |
_update_timeout_trigger: function () { |
|
468 |
this._update_idle = true; |
|
469 |
||
470 |
this.update_tiles(); |
|
471 |
}, |
|
472 |
||
473 |
// call update_tiles if it hasn't been called due to update_after_timeout |
|
474 |
update_now: function () { |
|
475 |
if (this._update_timeout) |
|
476 |
clearTimeout(this._update_timeout); |
|
477 |
||
478 |
if (!this._update_idle) |
|
479 |
this.update_tiles(); |
|
480 |
}, |
|
481 |
||
482 |
}); |
|
483 |
||
484 |
// used by Viewport.update_tiles to make a tile visible after it has loaded |
|
485 |
function _tile_loaded (ev) { |
|
486 |
this.style.display = "block"; |
|
487 |
} |
|
488 |
||
489 |
// a zoom layer containing the tiles for one zoom level |
|
490 |
var ZoomLayer = Class.create({ |
|
491 |
initialize: function (source, zoom_level) { |
|
492 |
this.source = source; |
|
493 |
this.level = zoom_level; |
|
494 |
this.div = Builder.node("div", { id: "zl_" + this.level, style: "position: relative; display: none;"}); |
|
495 |
||
496 |
// our tiles |
|
497 |
this.tiles = []; |
|
498 |
}, |
|
499 |
||
500 |
// add a tile to this zoom layer |
|
501 |
add_tile: function (tile) { |
|
502 |
this.div.appendChild(tile); |
|
503 |
this.tiles.push(tile); |
|
504 |
}, |
|
505 |
||
506 |
// make this zoom layer visible with the given z-index |
|
507 |
enable: function (z_index) { |
|
508 |
this.div.style.zIndex = z_index; |
|
509 |
this.div.show(); |
|
510 |
}, |
|
511 |
||
512 |
// hide this zoom layer |
|
513 |
disable: function (z_index) { |
|
514 |
this.div.hide(); |
|
515 |
}, |
|
516 |
||
517 |
// update the tiles in this zoom layer so that they are in the correct position and of the correct size when |
|
518 |
// viewed with the given zoom level |
|
519 |
update_tiles: function (zoom_level) { |
|
520 |
var zd = zoom_level - this.level; |
|
521 |
||
522 |
var tw = scaleByZoomDelta(this.source.tile_width, zd); |
|
523 |
var th = scaleByZoomDelta(this.source.tile_height, zd); |
|
524 |
||
525 |
var tiles = this.tiles; |
|
526 |
var tiles_len = tiles.length; |
|
527 |
var t, ts; |
|
528 |
||
529 |
for (var i = 0; i < tiles_len; i++) { |
|
530 |
t = tiles[i]; |
|
531 |
ts = t.style; |
|
532 |
||
533 |
ts.width = tw; |
|
534 |
ts.height = th; |
|
535 |
ts.top = th*t.__row; |
|
536 |
ts.left = tw*t.__col; |
|
537 |
} |
|
538 |
}, |
|
539 |
||
540 |
||
541 |
||
542 |
}); |
|
543 |
||
544 |
// scale the given co-ordinate by a zoom delta. If we zoom in (dz > 0), n will become larger, and if we zoom |
|
545 |
// out (dz < 0), n will become smaller. |
|
546 |
function scaleByZoomDelta (n, dz) { |
|
547 |
if (dz > 0) |
|
548 |
return n << dz; |
|
549 |
else |
|
550 |
return n >> -dz; |
|
551 |
} |
|
552 |