-
Notifications
You must be signed in to change notification settings - Fork 5
/
osd.js
547 lines (485 loc) · 15.2 KB
/
osd.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
import * as d3 from "d3"
import { round4 } from "./render"
import { greenOrWhite } from "./render"
var lasso_draw_counter = 0;
// Draw a point of a lasso-style polygon
var lasso_draw = function(event){
lasso_draw_counter ++;
if (lasso_draw_counter % 5 != 1) {
return
}
const viewer = this.viewer;
// add points to polygon and (re)draw
var webPoint = event.position;
var viewportPoint = viewer.viewport.pointFromPixel(webPoint);
this.hashstate.state.p.push({"x":viewportPoint.x,"y":viewportPoint.y});
this.newView(false);
}
// Change the spring stiffness for navigation
const changeSprings = function(viewer, seconds, stiffness) {
const springs = [
'centerSpringX', 'centerSpringY', 'zoomSpring'
];
springs.forEach(function(spring) {
const s = viewer.viewport[spring];
s.animationTime = seconds;
s.springStiffness = stiffness;
s.springTo(s.target.value);
});
};
// Render openseadragon from given hash state
export const RenderOSD = function(hashstate, viewer, eventHandler) {
this.svg_overlay = d3.select(viewer.svgOverlay().node());
this.hashstate = hashstate;
this.viewer = viewer;
this.mouseEvent = {};
this.trackers = [];
this.eventHandler = eventHandler;
}
RenderOSD.prototype = {
// Handle mouse position
get mouseXY() {
const e = this.mouseEvent;
const pos = OpenSeadragon.getMousePosition(e);
return this.normalize(pos);
},
set mouseXY(e) {
this.mouseEvent = e;
},
// Initialize connection to openseadragon
init: function () {
const viewer = this.viewer;
const HS = this.hashstate;
const THIS = this;
const isRendered = (n) => {
return HS.isRendered(n);
}
// Track mouse drag for lasso polygon drawing
var mouse_drag = new OpenSeadragon.MouseTracker({
element: viewer.canvas,
dragHandler: function(event) {
if (HS.drawType == "lasso" && HS.drawing) {
viewer.setMouseNavEnabled(false);
lasso_draw.bind(THIS)(event);
}
}
})
// Track mouse drag end for lasso polygon drawing
var mouse_up = new OpenSeadragon.MouseTracker({
element: viewer.canvas,
dragEndHandler: function(event) {
if (HS.drawType == "lasso" && HS.drawing) {
HS.finishDrawing();
}
viewer.setMouseNavEnabled(true);
}
})
// Track mouse drag for box overlay drawing
this.viewer.addHandler('canvas-drag', function(e) {
const THIS = e.userData;
const HS = THIS.hashstate;
if (HS.drawType != "box") {
return;
}
const position = THIS.normalize(e.position);
if (HS.drawing == 1) {
HS.drawing = 2;
e.preventDefaultAction = true;
THIS.drawLowerBounds(position);
}
else if (HS.drawing == 2) {
e.preventDefaultAction = true;
THIS.drawUpperBounds(position);
}
}, this);
// Track mouse drag end for box overlay drawing
this.viewer.addHandler('canvas-drag-end', function(e) {
const THIS = e.userData;
const HS = THIS.hashstate;
if (HS.drawType != "box") {
return;
}
const position = THIS.normalize(e.position);
if (HS.drawing == 2) {
e.preventDefaultAction = true;
THIS.drawUpperBounds(position);
HS.finishDrawing();
HS.pushState();
THIS.newView(false);
}
}, this);
// Track mouse click for arrow and box drawing
this.viewer.addHandler('canvas-click', function(e) {
const THIS = e.userData;
const HS = THIS.hashstate;
const position = THIS.normalize(e.position);
//trigger event
var imageCoordinates = THIS.viewer.viewport.viewportToImageCoordinates(position.x, position.y);
THIS.eventHandler.trigger(THIS.eventHandler.events.osdClickEvent, {'x': imageCoordinates.x, "y" : imageCoordinates.y});
if (HS.drawType == "lasso") {
return;
}
if (HS.drawType == "arrow") {
// const position = THIS.normalize(e.position);
if (HS.drawing == 1) {
HS.a = [position.x, position.y];
HS.finishDrawing();
THIS.viewer.setMouseNavEnabled(true);
HS.pushState();
THIS.newView(false);
}
return;
}
// Dragging the lower bounds of the box
if (HS.drawing == 1) {
HS.drawing = 2;
e.preventDefaultAction = true;
THIS.drawLowerBounds(position);
}
// Dragging the upper bounds of the box
else if (HS.drawing == 2) {
e.preventDefaultAction = true;
THIS.drawUpperBounds(position);
HS.finishDrawing();
THIS.viewer.setMouseNavEnabled(true);
HS.pushState();
THIS.newView(false);
}
}, this);
// Track mouse movement for box overlay drawing
$(this.viewer.element).mousemove(this, function(e) {
const THIS = e.data;
const HS = THIS.hashstate;
if (HS.drawType == "lasso") {
return;
}
THIS.mouseXY = e;
if (HS.drawing == 2) {
THIS.drawUpperBounds(THIS.mouseXY);
}
});
// Transfer animated viewport into hash state
this.viewer.addHandler('animation', function(e) {
const THIS = e.userData;
const HS = THIS.hashstate;
HS.gl_state.redrawLensTiles();
const scale = THIS.viewer.viewport.getZoom();
const pan = THIS.viewer.viewport.getCenter();
HS.v = [
round4(scale),
round4(pan.x),
round4(pan.y)
];
}, this);
// Transfer animated viewport into hash state and url
this.viewer.addHandler('animation-finish', function(e) {
const THIS = e.userData;
const HS = THIS.hashstate;
const scale = THIS.viewer.viewport.getZoom();
const pan = THIS.viewer.viewport.getCenter();
HS.v = [
round4(scale),
round4(pan.x),
round4(pan.y)
];
HS.pushState();
THIS.newView(false);
THIS.faster();
}, this);
// Display viewer
this.finishAnimation();
this.viewer.setVisible(true);
},
// Immediately set viewport
finishAnimation: function() {
const target = this.viewer.viewport.getBounds();
this.viewer.viewport.fitBounds(target, true);
},
// Make springs faster
faster: function() {
changeSprings(this.viewer, 1.2, 6.4);
},
// Make springs slower
slower: function() {
changeSprings(this.viewer, 3.2, 6.4);
},
// Normalize pixel coordinates to openseadragon viewport coordinates
normalize: function(pixels) {
const vp = this.viewer.viewport;
const norm = vp.viewerElementToViewportCoordinates;
return norm.call(vp, pixels);
},
// Draw lower bounds of a box overlay
drawLowerBounds: function(position) {
const HS = this.hashstate;
const wh = [0, 0];
const new_xy = [
position.x, position.y
];
HS.o = new_xy.concat(wh);
this.newView(false);
},
// Compute new bounds in x or y
computeBounds: function(value, start, len) {
const center = start + (len / 2);
const end = start + len;
// Below center
if (value < center) {
return {
start: value,
range: end - value,
};
}
// Above center
return {
start: start,
range: value - start,
};
},
// Draw upper bounds of a box overlay
drawUpperBounds: function(position) {
const HS = this.hashstate;
const xy = HS.o.slice(0, 2);
const wh = HS.o.slice(2);
// Set actual bounds
const x = this.computeBounds(position.x, xy[0], wh[0]);
const y = this.computeBounds(position.y, xy[1], wh[1]);
const o = [x.start, y.start, x.range, y.range];
HS.o = o.map(round4);
this.newView(false);
},
// update openseadragon optionally redrawing
newView: function(redraw) {
const HS = this.hashstate;
this.trackers.forEach(t => t.destroy());
this.trackers = [];
this.addPolygon(HS.id+"-selection", HS.state.p);
// Update the box overlays
HS.allOverlays.forEach(function(indices) {
const [prefix, s, w, o] = indices;
var overlay = HS.overlay;
if (prefix == 'waypoint-overlay') {
overlay = HS.stories[s].Waypoints[w].Overlays[o];
}
var el = 'minerva-'+ HS.id + '-' + indices.join('-');
this.addOverlay(overlay, el, s, w);
}, this)
// Update the arrow overlays
const THIS = this;
$.each($(HS.el).find('.minerva-arrow-overlay'), function(id, el) {
const current = THIS.viewer.getOverlayById(el.id);
const xy = new OpenSeadragon.Point(-100, -100);
if (current) {
current.update({
location: xy,
});
}
});
HS.allArrows.forEach(function(indices) {
this.addArrow(indices);
}, this);
// Redraw design
if(redraw) {
// Update OpenSeadragon
HS.gl_state.reloadTiles();
this.activateViewport();
HS.newMasks(this.viewer)
}
},
// add a lasso polygon to svg overlay
addPolygon: function(id, polygon) {
var svg_overlay = this.svg_overlay;
d3.select('#' + id).remove();
var selPoly = svg_overlay.selectAll(id).data([polygon]);
selPoly.enter().append("polygon")
.attr('id', id)
.attr("points",function(d) {
return d.map(function(d) { return [d.x,d.y].join(","); }).join(" ");
});
},
// add an arrow overlay to openseadragon
addArrow: function(indices) {
// prefix: waypoint arrow or user arrow
// s_i: story index, w_i: waypoint index, a_i: arrow index
const [prefix, s_i, w_i, a_i] = indices;
const HS = this.hashstate;
var a = {
Point: HS.a,
Text: ''
}
// Take waypoint arrows from waypoint
if (prefix == 'waypoint-arrow') {
a = Object.assign({}, HS.stories[s_i].Waypoints[w_i].Arrows[a_i])
}
// Set default angle of 60 degrees
if (a.Angle == undefined) {
a.Angle = 60;
}
const text_class = "minerva-arrow-text";
const arrow_class = a.Arrowhead? "minerva-arrowhead-image" : "minerva-arrow-image";
const text_el = "minerva-arrow-text-" + HS.id + '-' + indices.join('-');
const el = "minerva-arrow-image-" + HS.id + '-' + indices.join('-');
// Hide arrows not equal to current story and waypoint
if (s_i != HS.s || w_i != HS.w) {
a.Point = [-100, -100];
}
const current = this.viewer.getOverlayById(el);
const xy = new OpenSeadragon.Point(a.Point[0], a.Point[1]);
// Update existing arrows
if (current) {
current.update({
location: xy,
});
}
// Create new arrows
else {
const proto_element = HS.el.getElementsByClassName(arrow_class)[0];
const element = proto_element.cloneNode(true);
element.id = el;
document.body.appendChild(element);
this.viewer.addOverlay({
x: a.Point[0],
y: a.Point[1],
element: el,
placement: OpenSeadragon.Placement.CENTER
});
}
const current_text = this.viewer.getOverlayById(text_el);
const xy_text = new OpenSeadragon.Point(a.Point[0], a.Point[1]);
// Update existing arrow text
if (current_text) {
current_text.update({
location: xy_text,
});
}
// Create new arrow text
else {
const proto_text_element = HS.el.getElementsByClassName(text_class)[0];
const text_element = proto_text_element.cloneNode(true);
text_element.id = text_el;
document.body.appendChild(text_element);
this.viewer.addOverlay({
x: a.Point[0],
y: a.Point[1],
element: text_el,
placement: OpenSeadragon.Placement.CENTER
});
}
// Create specific ids for each arrow subelement
const a_text_el = $('#'+text_el);
const a_image_el = document.querySelector('#'+el);
const a_svg_el = document.querySelector('#'+el+' svg');
const a_label_el = $('#'+text_el+' .minerva-arrow-label');
const a_radius = a_svg_el.getAttribute('width') / 2;
const a_y = a_radius * Math.sin(a.Angle * Math.PI /180);
const a_x = a_radius * Math.cos(a.Angle * Math.PI /180);
// Enable hidden (text-only) arrows
if (a.HideArrow == true) {
$(a_image_el).addClass('d-none');
}
else {
$(a_image_el).removeClass('d-none');
const x_px = Math.round(a_x)+'px'
const y_px = Math.round(a_y)+'px'
const deg = Math.round(a.Angle)+'deg';
a_svg_el.style.transform = `translate(${x_px},${y_px}) rotate(${deg})`;
a_svg_el.style['transform-origin'] = 'center';
a_label_el.css('top', '100px');
}
const a_text = a.Text;
// Position text to endpoint of arrow
if (a_text) {
const t_w = a_text_el.width();
const t_h = a_text_el.height();
var t_x = 2 * a_x + t_w * Math.sign(Math.round(a_x)) / 2;
var t_y = 2 * a_y + t_h * Math.sign(Math.round(a_y)) / 2;
if (a.HideArrow == true) {
t_x = 0;
t_y = 0;
}
a_label_el.css('transform',
'translate('+t_x+'px, '+t_y+'px)');
a_label_el.addClass('p-3');
a_label_el.text(a_text);
}
else {
a_label_el.removeClass('p-3');
a_label_el.text('');
}
},
// add a box overlay to openseadragon
addOverlay: function(overlay, el, s, w) {
const current = this.viewer.getOverlayById(el);
const HS = this.hashstate;
const not_outline = (HS.waypoint.Mode != 'outline');
const not_current = (HS.s != s || HS.w != w);
// Hide if not part of the outline and not current story/waypoint
if (not_outline && not_current) {
if (current) {
const xy = new OpenSeadragon.Point(-100, -100);
current.update({
location: xy,
width: 1,
height: 1,
});
}
return;
}
var div = document.getElementById(el);
// Create a new overlay if needed
if (!div) {
div = document.createElement("div");
div.className = "minerva-white minerva-overlay";
div.id = el;
HS.el.getElementsByClassName('minerva-all-overlays')[0].appendChild(div);
}
const xy = new OpenSeadragon.Point(overlay.x, overlay.y);
const is_green = HS.drawing && HS.drawType == "box";
greenOrWhite('#' + el, is_green);
// Update existing overlays
if (current) {
current.update({
location: xy,
width: overlay.width,
height: overlay.height
});
}
// Add new overlays
else {
this.viewer.addOverlay({
x: overlay.x,
y: overlay.y,
width: overlay.width,
height: overlay.height,
element: el
});
}
const THIS = this;
// Allow interactive box overlays if in outline mode
if (HS.waypoint.Mode == 'outline') {
const tracker = new OpenSeadragon.MouseTracker({
element: document.getElementById(el),
moveHandler: function(event) {
$(div).css('cursor', 'pointer');
},
clickHandler: (function(event) {
const [s, w] = el.split('-').slice(-3,-1);
event.preventDefaultAction = false;
HS.s = s;
HS.w = w;
HS.pushState();
window.onpopstate();
}).bind(this)
});
this.trackers.push(tracker);
}
},
// Pan to viewport given by hash state
activateViewport: function() {
const HS = this.hashstate;
const viewport = this.viewer.viewport;
viewport.panTo(HS.viewport.pan);
viewport.zoomTo(HS.viewport.scale);
viewport.applyConstraints(true);
}
}