-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
366 lines (298 loc) · 8.85 KB
/
index.html
File metadata and controls
366 lines (298 loc) · 8.85 KB
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
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<link type="text/css" rel="stylesheet" href="styles.css"/>
<title>plot-reverse</title>
<section>
Upload image:<input type='file' onchange="readURL(this);" />
</section>
<section>
<ul>
<li>x1:<input id="x1" type="text"/></li>
<li>x2:<input id="x2" type="text"/></li>
<li>y1:<input id="y1" type="text"></li>
<li>y2:<input id="y2" type="text"/></li>
</ul>
</section>
<section>
<div id="map"></div>
<textarea readonly id="output"></textarea>
</section>
<ol>
<li>Upload image.</li>
<li>Enter reference values at top.</li>
<li>Drag reference handles (x1, x2, y1, y2) in place using mouse.</li>
<li>Add points by alt+click.</li>
</ol>
<ul>
<li>Pan and zoom using mouse.</li>
<li>Move points using mouse.</li>
<li>Remove points by shift+click.</li>
</ul>
<br>
<br>
<p>
Created by Martin Stålberg, using mainly <a href="https://d3js.org/">D3.js</a>.
<br>
Source code available on <a href="https://github.com/mast4461/plot-reverse">GitHub</a>.
</p>
<script>
////////////////////////////////////////////////////////////////////////////
// Functions for loading an image
function readURL(input) {
// http://jsbin.com/uboqu3/1/edit?html,js,output
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = onload;
reader.readAsDataURL(input.files[0]);
}
};
function onload(file) {
var image = new Image();
image.onload = function() {
// imageGroup.append("image")
imageElement
.attr("width", this.width + "px")
.attr("height", this.height + "px")
.attr("x", 0 + "px")
.attr("y", 0 + "px")
.attr("xlink:href", file.target.result)
;
setScale(Math.min(
div.node().clientWidth/this.width,
div.node().clientHeight/this.height
));
resetHandles();
};
image.src = file.target.result
};
////////////////////////////////////////////////////////////////////////////
// Creating some elements and objects and storing references in variables
var zoomHandler = d3.behavior.zoom()
.scaleExtent([0.1, 8])
.on("zoom", zoom)
;
var outputElement = d3.select("textarea#output");
var inpuxX1 = d3.select("input#x1");
var inputX2 = d3.select("input#x2");
var inputY1 = d3.select("input#y1");
var inputY2 = d3.select("input#y2");
// Let the output update when the input changes
d3.selectAll('input[type=text]').on('input', updateOutput);
var div = d3.select("div#map");
var svg = div.append('svg');
var zoomGroup = svg
.style("width", "100%")
.style("height", "100%")
.call(zoomHandler)
.append("g");
;
var imageGroup = zoomGroup.append('g');
var imageElement = imageGroup.append('image');
var overlayGroup = svg.append('g');
var xScale = d3.scale.linear();
var yScale = d3.scale.linear();
zoomHandler.x(xScale)
zoomHandler.y(yScale)
function zoom() {
// Credit to http://bl.ocks.org/pbogden/7363519
zoomHandler.scale(d3.event.scale);
zoomHandler.translate(d3.event.translate);
scheduleUpdate();
};
function setScale(scale) {
zoomHandler.scale(scale)
scheduleUpdate();
};
function updateZoom() {
zoomGroup.attr(
'transform',
'translate(' + zoomHandler.translate() + ')scale(' + zoomHandler.scale() + ')'
);
};
////////////////////////////////////////////////////////////////////////////
// Functions for updating the graphics, only on frame draw.
// This approach is taken to avoid unneccessary computations,
// the screen only refreshes every so often so we don't need to update
// the graphics more than once per animation frame.
// Keeping track of the current animation frame id
var animationFrameId;
// Update the graphics
function update() {
updateHandles();
updatePoints();
updateOutput();
updateZoom();
animationFrameId = undefined;
};
// Schecule update of the graphics
function scheduleUpdate() {
if (animationFrameId) return;
animationFrameId = requestAnimationFrame(update);
};
////////////////////////////////////////////////////////////////////////////
// Graphics update functions for separate element categories
// Helper functions
function xFunc(d) { return xScale(d.x); };
function yFunc(d) { return yScale(d.y); };
function updateHandles() {
handles
.attr('x', xFunc)
.attr('y', yFunc)
;
};
function updatePoints() {
var pointMarkers = overlayGroup.selectAll('.point').data(points);
pointMarkers
.enter()
.append('circle')
.classed('point', true)
.call(dragPoint)
.on('click', removePoint)
;
pointMarkers
.attr('cx', xFunc)
.attr('cy', yFunc)
.attr('r', 5)
;
pointMarkers.exit().remove();
};
function updateOutput() {
var getValue = function(input) {
return parseFloat(input.node().value);
};
var xScale = d3.scale.linear()
.domain([handlesData[0].x, handlesData[1].x])
.range([getValue(inpuxX1), getValue(inputX2)])
;
var yScale = d3.scale.linear()
.domain([handlesData[2].y, handlesData[3].y])
.range([getValue(inputY1), getValue(inputY2)])
;
var pointsSorted = points.sort(function(a,b) {
return a.x - b.x;
});
var rows = pointsSorted.map(function(point) {
return [xScale(point.x), yScale(point.y)].join(', ');
});
outputElement.node().value = rows.join('\n');
};
////////////////////////////////////////////////////////////////////////////
// User interactions
// Adding and removing points
var points = [];
svg.on('click', function() {
console.log(d3.event);
if (d3.event.altKey) {
points.push({
x: xScale.invert(d3.event.offsetX),
y: yScale.invert(d3.event.offsetY)
});
scheduleUpdate();
}
});
function removePoint(d, i) {
if (d3.event.shiftKey) {
// Remove point
points.splice(i, 1);
scheduleUpdate();
}
};
// Removing and dragging points
var dragPoint = d3.behavior.drag()
.on('dragstart', function(d, i) {
d3.event.sourceEvent.stopPropagation();
})
.on('drag', function(d) {
d.x = xScale.invert(d3.event.x);
d.y = yScale.invert(d3.event.y);
scheduleUpdate();
})
;
// Dragging reference handles
var dragHandle = d3.behavior.drag()
.on('dragstart', function() {
d3.event.sourceEvent.stopPropagation();
// Create ruler lines, visible during dragging
this.lineX = svg.append('line')
.attr('x1', "0%")
.attr('x2', "100%")
.classed('ruler-line', true)
;
this.lineY = svg.append('line')
.attr('y1', "0%")
.attr('y2', "100%")
.classed('ruler-line', true)
;
})
.on('drag', function(d) {
// Update data for handles and ruler lines
d.x = xScale.invert(d3.event.x);
d.y = yScale.invert(d3.event.y);
this.lineX
.attr('y1', d3.event.y)
.attr('y2', d3.event.y)
;
this.lineY
.attr('x1', d3.event.x)
.attr('x2', d3.event.x)
;
scheduleUpdate();
})
.on('dragend', function() {
// Remove ruler lines
this.lineX.remove();
this.lineY.remove();
})
;
////////////////////////////////////////////////////////////////////////////
// Initialization and other things related to reference handles,
var handlesData = [
{x0: '0.20', y0: '0.98', label: "x1"},
{x0: '0.98', y0: '0.98', label: "x2"},
{x0: '0.10', y0: '0.88', label: "y1"},
{x0: '0.10', y0: '0.10', label: "y2"}
];
var handles = overlayGroup.selectAll('.ref-handle').data(handlesData);
handles
.enter()
.append('text')
.classed('ref-handle', true)
.text(function(d) {return d.label;})
.call(dragHandle)
;
function resetHandles() {
handles
.attr('x', function(d) {
// Get the initial value in relative units
var x0 = d.x0;
// Get the screen position in pixel units
var xScreen = x0*svg.node().clientWidth;
// Set the position in data units
d.x = xScale.invert(xScreen);
// Return screen position
return xScreen;
})
.attr('y', function(d) {
var y0 = d.y0;
var yScreen = y0*svg.node().clientHeight;
d.y = xScale.invert(yScreen);
return yScreen;
})
;
}
resetHandles();
////////////////////////////////////////////////////////////////////////////
// Load example image and set reference values for it, used during development
onload({
target: {
result: "/plot-reverse/example-plot.svg"
}
});
inpuxX1.node().value = 0;
inputX2.node().value = 10;
inputY1.node().value = 0;
inputY2.node().value = 1;
</script>
</body>