This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ism.js
415 lines (364 loc) · 11.9 KB
/
ism.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
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
var ism = {};
(function (ism) {
"use strict";
ism.drag = function (element, onDrag) {
var drag = {},
lastEvt = null;
element.addEventListener("mousedown", drag, false);
// subscribe to all mouse up events to prevent a "sticky" pointer
window.addEventListener("mouseup", drag, false);
drag.handleEvent = function (evt) {
if (evt.type === "mousemove") {
evt.preventDefault();
onDrag({
"x" : evt.clientX - lastEvt.clientX,
"y" : evt.clientY - lastEvt.clientY
});
lastEvt = evt;
} else if (evt.type === "mousedown") {
evt.preventDefault();
lastEvt = evt;
window.addEventListener("mousemove", drag, false);
element.setAttribute("cursor", "move");
} else if (evt.type === "mouseup") {
window.removeEventListener("mousemove", drag, false);
element.setAttribute("cursor", "default");
}
};
return drag;
};
return ism;
}(ism));
(function (ism) {
"use strict";
ism.event = function (that) {
var types = {};
that.on = function (type, handler) {
if (types.hasOwnProperty(type)) {
types[type].push(handler);
} else {
types[type] = [handler];
}
};
that.off = function (type, handler) {
if (types.hasOwnProperty(type)) {
// Create new type array without given handler
types[type] = types[type].filter(function (x) {
return (x === handler ? false : true);
});
}
};
that.trigger = function (type) {
if (types.hasOwnProperty(type)) {
// fire all functions in types[type]
types[type].map(function (x) {
x();
});
}
};
that.types = types;
};
return ism;
}(ism));
(function (ism) {
"use strict";
ism.image = function (spec) {
var image = {},
width = spec.width || 256,
height = spec.height || 256,
x = spec.x || 0,
// negate spec.y to make the y axis point up
y = -spec.y || 0,
url = spec.url,
clazz = spec.class || "",
element = ism.svg("image"),
map;
element.setAttribute("width", width);
element.setAttribute("height", height);
element.setAttribute("x", x - (width / 2));
element.setAttribute("y", y - (height / 2));
element.setAttribute("class", clazz);
element.setAttributeNS("http://www.w3.org/1999/xlink",
"href", url);
image.map = function (m) {
if (arguments.length === 0) {
return map;
}
m.layers().appendChild(element);
map = m;
return image;
};
return image;
};
return ism;
}(ism));
(function (ism) {
"use strict";
ism.map = function (spec) {
var map = {},
container = spec.container,
width = spec.width,
height = spec.height,
zoomRange = spec.zoomRange || [0, 10],
angle = 0,
x = 0,
y = 0,
zoom = 0,
mag = function () {
return Math.pow(2, zoom);
},
apply = function () {
var t = "scale(" + (mag()) + ") " +
"rotate(" + (angle) + ") " +
"translate(" + (-x) +
" " + y + ")";
layers.setAttribute("transform", t);
},
layers;
// add event handling methods
ism.event(map);
map.add = function (object) {
object.map(map);
return map;
};
map.angle = function (deg) {
if (typeof(deg) !== "number") {
return angle;
} else {
angle = deg;
apply();
}
};
map.container = function () {
return container;
};
map.layers = function () {
return layers;
};
map.center = function (coordinates) {
if (!coordinates) {
return {
"x" : x,
"y" : y
};
} else {
x = coordinates.x;
y = coordinates.y;
apply();
return map;
}
};
map.panBy = function (delta) {
// Get the inversed transformation matrix of the layer element
// to transform between the layers and screen coordinate systems.
var p = ism.point(delta),
m = layers.getCTM().inverse();
// Set translation to 0
m.e = 0;
m.f = 0;
// transform p with m
p = p.matrixTransform(m);
// Recenter map
map.center({
"x" : map.center().x - (p.x),
"y" : map.center().y + (p.y)
});
};
map.zoom = function (level) {
if (typeof(level) !== "number") {
return zoom;
}
//Keep level inside the zoomRange
level = level < zoomRange[0] ? zoomRange[0] : level;
level = level > zoomRange[1] ? zoomRange[1] : level;
zoom = level;
map.trigger("zoom");
apply();
return map;
};
map.zoomBy = function (delta, point) {
var z = zoom,
panDist;
map.zoom(zoom + delta);
// Pan around point, if defined
if (typeof(point) === "object") {
// Recalculate delta, in case map.zoom hit a constraint
delta = zoom - z;
panDist = function (a) {
return -(a * (Math.pow(2, delta) - 1));
};
map.panBy({"x": panDist(point.x), "y": panDist(point.y)});
}
return map;
};
// Initialize
container.setAttribute("width", width);
container.setAttribute("height", height);
container.setAttribute("viewBox",
(-width / 2) + " " + (-height / 2) + " " + width + " " + height);
layers = ism.svg("g");
container.appendChild(layers);
apply();
return map;
};
return ism;
}(ism));
(function (ism) {
"use strict";
ism.tiled = function (spec) {
var tiled = {},
width = spec.width || 256,
height = spec.height || 256,
x = spec.x || 0,
// negate spec.y to make the y axis point up
y = -spec.y || 0,
clazz = spec.class || "",
element = ism.svg("svg"),
tiles = [],
draw = function () {
var divs = Math.pow(2, zoom),
line = "",
size = width,
r,
x,
y;
// delete old tiles
tiles.map(function (t) {
t.parentNode.removeChild(t);
});
tiles = [];
// draw
for (y = 0; y < divs; y += 1) {
for (x = 0; x < divs; x += 1) {
r = ism.svg("rect");
r.setAttribute("x", x * size / divs);
r.setAttribute("y", y * size / divs);
r.setAttribute("width", (size - 1) / divs);
r.setAttribute("height", (size - 1) / divs);
element.appendChild(r);
tiles.push(r);
}
}
},
map,
zoom;
element.setAttribute("width", width);
element.setAttribute("height", height);
element.setAttribute("x", x - (width / 2));
element.setAttribute("y", y - (height / 2));
element.setAttribute("class", clazz);
tiled.map = function (m) {
if (arguments.length === 0) {
return map;
}
m.layers().appendChild(element);
map = m;
map.on("zoom", tiled.update);
tiled.update();
return tiled;
};
tiled.update = function () {
var z = Math.round(map.zoom());
if (z !== zoom) {
zoom = z;
draw();
}
return tiled;
};
return tiled;
};
return ism;
}(ism));
(function (ism) {
"use strict";
ism.svg = function (tag) {
return document.createElementNS("http://www.w3.org/2000/svg", tag);
};
ism.point = function (coordinates) {
var p = ism.svg("svg").createSVGPoint();
p.x = coordinates.x;
p.y = coordinates.y;
return p;
};
return ism;
}(ism));
(function (ism) {
"use strict";
ism.vector = function (spec) {
var width = spec.width || 256,
height = spec.height || 256,
x = spec.x || 0,
// negate spec.y to make the y axis point up
y = -spec.y || 0,
url = spec.url,
clazz = spec.class || "",
element = ism.svg("svg"),
vector = {},
(responseElement) {
responseElement.setAttribute("x", x - (width / 2));
responseElement.setAttribute("y", y - (height / 2));
responseElement.setAttribute("width", width);
responseElement.setAttribute("height", height);
responseElement.setAttribute("class", clazz);
element.parentNode.replaceChild(responseElement, element);
},
callback,
map,
request;
// FIXME: AJAX code was stuffed here,
// though it belongs into it's own function
// check if XMLHttpRequest() is available
if (window.XMLHttpRequest) {
// this closure is used to make the requests thread safe
callback = function () {
// when the transaction is complete, call onComplete
if (request.readyState === 4 && request.status === 200) {
onComplete(request.responseXML.documentElement);
}
};
request = new XMLHttpRequest();
request.open("GET", url, true);
request.>
request.send(null);
}
vector.map = function (m) {
if (arguments.length === 0) {
return map;
}
m.layers().appendChild(element);
map = m;
return vector;
};
return vector;
};
return ism;
}(ism));
(function (ism) {
"use strict";
ism.wheel = function (element, onChange) {
var wheel = {};
// for w3c
element.addEventListener("mousewheel", wheel, false);
// for Firefox
element.addEventListener("DOMMouseScroll", wheel, false);
wheel.handleEvent = function (evt) {
if (evt.type === "mousewheel" || evt.type === "DOMMouseScroll") {
evt.preventDefault();
var delta = 0;
// Normalize the delta
if (evt.wheelDelta) {
delta = evt.wheelDelta / 120;
} else if (evt.detail) {
delta = -evt.detail / 3;
}
// Return delta and mouse position
onChange(delta, {
"x": evt.pageX - element.getScreenCTM().e,
"y": evt.pageY - element.getScreenCTM().f
});
}
};
return wheel;
};
return ism;
}(ism));