-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas_zoom.html
More file actions
323 lines (271 loc) · 9.09 KB
/
canvas_zoom.html
File metadata and controls
323 lines (271 loc) · 9.09 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
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<style>
.scatter-container {
margin: auto;
width: 900px;
height: 500px;
}
.svg-plot, .canvas-plot {
position: absolute;
}
.tools {
position: absolute;
left: calc(50% + 400px);
visibility: hidden;
z-index:10;
}
.tools button {
background-color: #e7e7e7;
border: none;
color: #000000;
cursor: pointer;
display: block;
margin-bottom: 5px;
padding: 5px 10px;
outline: 0;
}
.tools button.active {
background-color: #13a613;
}
</style>
<body>
<div class="scatter-container">
<div class="tools">
<button id="reset">Reset</button>
<button id="zoom" class="active">Zoom</button>
<button id="brush">Brush</button>
</div>
</div>
<script>
let dataExample = [];
for (let i = 0; i < 1000000; i++) {
const x = Math.floor(Math.random() * 999999) + 1;
const y = Math.floor(Math.random() * 999999) + 1;
dataExample.push([x, y]);
}
const pointColor = '#3585ff'
const margin = { top: 20, right: 15, bottom: 60, left: 70 };
const outerWidth = 1700;
const outerHeight = 1200;
const width = outerWidth - margin.left - margin.right;
const height = outerHeight - margin.top - margin.bottom;
const container = d3.select('.scatter-container');
let lastTransform = null;
// Init SVG
const svgChart = container.append('svg:svg')
.attr('width', outerWidth)
.attr('height', outerHeight)
.attr('class', 'svg-plot')
.append('g')
.attr('transform', `translate(${margin.left}, ${margin.top})`);
// Init Canvas
const canvasChart = container.append('canvas')
.attr('width', width)
.attr('height', height)
.style('margin-left', margin.left + 'px')
.style('margin-top', margin.top + 'px')
.attr('class', 'canvas-plot');
// Prepare buttons
const toolsList = container.select('.tools')
.style('margin-top', margin.top + 'px')
.style('visibility', 'visible');
toolsList.select('#reset').on('click', () => {
const t = d3.zoomIdentity.translate(0, 0).scale(1);
canvasChart.transition()
.duration(200)
.ease(d3.easeLinear)
.call(zoom_function.transform, t)
});
const context = canvasChart.node().getContext('2d');
// Init Scales
const x = d3.scaleLinear().domain([0, d3.max(dataExample, (d) => d[0])]).range([0, width]).nice();
const y = d3.scaleLinear().domain([0, d3.max(dataExample, (d) => d[1])]).range([height, 0]).nice();
// // Init Axis
// const xAxis = d3.axisBottom(x);
// const yAxis = d3.axisLeft(y);
// // Add Axis
// const gxAxis = svgChart.append('g')
// .attr('transform', `translate(0, ${height})`)
// .call(xAxis);
//
// const gyAxis = svgChart.append('g')
// .call(yAxis);
//
// // Add labels
// svgChart.append('text')
// .attr('x', `-${height / 2}`)
// .attr('dy', '-3.5em')
// .attr('transform', 'rotate(-90)')
// .text('Axis Y');
// svgChart.append('text')
// .attr('x', `${width / 2}`)
// .attr('y', `${height + 40}`)
// .text('Axis X');
// Draw plot on canvas
function draw(transform) {
lastTransform = transform;
const scaleX = transform.rescaleX(x);
const scaleY = transform.rescaleY(y);
// gxAxis.call(xAxis.scale(scaleX));
// gyAxis.call(yAxis.scale(scaleY));
context.clearRect(0, 0, width, height);
dataExample.forEach(point => {
drawPoint(scaleX, scaleY, point, transform.k);
});
}
// Initial draw made with no zoom
draw(d3.zoomIdentity)
function drawPoint(scaleX, scaleY, point, k) {
context.beginPath();
context.fillStyle = pointColor;
const px = scaleX(point[0]);
const py = scaleY(point[1]);
context.arc(px, py, .5 * k, 0, 2 * Math.PI, true);
context.fill();
}
// Zoom/Drag handler
const zoom_function = d3.zoom().scaleExtent([1, 1000])
.on('zoom', () => {
const transform = d3.event.transform;
context.save();
draw(transform);
context.restore();
});
canvasChart.call(zoom_function);
//Box Zoom
const svgChartParent = d3.select('svg');
const zoomButton = toolsList.select('#zoom').on('click', () => {
toolsList.selectAll('.active').classed('active', false);
zoomButton.classed('active', true);
canvasChart.style('z-index', 1);
svgChartParent.style('z-index', 0);
});
const brushButton = toolsList.select('#brush').on('click', () => {
toolsList.selectAll('.active').classed('active', false);
brushButton.classed('active', true);
canvasChart.style('z-index', 0);
svgChartParent.style('z-index', 1);
});
const brush = d3.brush().extent([[0, 0], [width, height]])
.on("start", () => { brush_startEvent(); })
.on("brush", () => { brush_brushEvent(); })
.on("end", () => { brush_endEvent(); })
.on("start.nokey", function() {
d3.select(window).on("keydown.brush keyup.brush", null);
});
const brushSvg = svgChart
.append("g")
.attr("class", "brush")
.call(brush);
let brushStartPoint = null;
function brush_startEvent() {
const sourceEvent = d3.event.sourceEvent;
const selection = d3.event.selection;
if (sourceEvent.type === 'mousedown') {
brushStartPoint = {
mouse: {
x: sourceEvent.screenX,
y: sourceEvent.screenY
},
x: selection[0][0],
y: selection[0][1]
}
} else {
brushStartPoint = null;
}
}
function brush_brushEvent() {
if (brushStartPoint !== null) {
const scale = width / height;
const sourceEvent = d3.event.sourceEvent;
const mouse = {
x: sourceEvent.screenX,
y: sourceEvent.screenY
};
if (mouse.x < 0) { mouse.x = 0; }
if (mouse.y < 0) { mouse.y = 0; }
let distance = mouse.y - brushStartPoint.mouse.y;
let yPosition = brushStartPoint.y + distance;
let xCorMulti = 1;
if ((distance < 0 && mouse.x > brushStartPoint.mouse.x) || (distance > 0 && mouse.x < brushStartPoint.mouse.x)) {
xCorMulti = -1;
}
if (yPosition > height) {
distance = height - brushStartPoint.y;
yPosition = height;
} else if (yPosition < 0) {
distance = -brushStartPoint.y;
yPosition = 0;
}
let xPosition = brushStartPoint.x + distance * scale * xCorMulti;
const oldDistance = distance;
if (xPosition > width) {
distance = (width - brushStartPoint.x) / scale;
xPosition = width;
} else if (xPosition < 0) {
distance = brushStartPoint.x / scale;
xPosition = 0;
}
if (oldDistance !== distance) {
distance *= (oldDistance < 0) ? -1 : 1;
yPosition = brushStartPoint.y + distance;
}
const selection = svgChart.select(".selection");
const posValue = Math.abs(distance);
selection.attr('width', posValue * scale).attr('height', posValue);
if (xPosition < brushStartPoint.x) {
selection.attr('x', xPosition);
}
if (yPosition < brushStartPoint.y) {
selection.attr('y', yPosition);
}
const minX = Math.min(brushStartPoint.x, xPosition);
const maxX = Math.max(brushStartPoint.x, xPosition);
const minY = Math.min(brushStartPoint.y, yPosition);
const maxY = Math.max(brushStartPoint.y, yPosition);
lastSelection = { x1: minX, x2: maxX, y1: minY, y2: maxY };
}
}
function brush_endEvent() {
const s = d3.event.selection;
if (!s && lastSelection !== null) {
// Re-scale axis for the last transformation
let zx = lastTransform.rescaleX(x);
let zy = lastTransform.rescaleY(y);
// Calc distance on Axis-X to use in scale
let totalX = Math.abs(lastSelection.x2 - lastSelection.x1);
// Get current point [x,y] on canvas
const originalPoint = [zx.invert(lastSelection.x1), zy.invert(lastSelection.y1)];
// Calc scale mapping distance AxisX in width * k
// Example: Scale 1, width: 830, totalX: 415
// Result in a zoom of 2
const t = d3.zoomIdentity.scale(((width * lastTransform.k) / totalX));
// Re-scale axis for the new transformation
zx = t.rescaleX(x);
zy = t.rescaleY(y);
// Call zoomFunction with a new transformation from the new scale and brush position.
// To calculate the brush position we use the originalPoint in the new Axis Scale.
// originalPoint it's always positive (because we're sure it's within the canvas).
// We need to translate this originalPoint to [0,0]. So, we do (0 - position) or (position * -1)
canvasChart
.transition()
.duration(200)
.ease(d3.easeLinear)
.call(zoom_function.transform,
d3.zoomIdentity
.translate(zx(originalPoint[0]) * -1, zy(originalPoint[1]) * -1)
.scale(t.k));
lastSelection = null;
} else {
brushSvg.call(brush.move, null);
}
}
</script>
</body>