-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzoom.js
More file actions
53 lines (45 loc) · 1.11 KB
/
zoom.js
File metadata and controls
53 lines (45 loc) · 1.11 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
var canvas = d3.select("canvas"),
context = canvas.node().getContext("2d"),
width = canvas.property("width"),
height = canvas.property("height"),
radius = 2.5;
var points = d3.range(2000).map(phyllotaxis(10)),
fx = points[0][0],
fy = points[0][1];
var zoom = d3.zoom()
.scaleExtent([1 / 2, 96])
.on("zoom", zoomed);
canvas
.call(zoom)
.call(zoom.transform, d3.zoomIdentity);
function zoomed() {
var t = d3.zoomIdentity
.translate(width / 2, height / 2)
.scale(d3.event.transform.k)
.translate(-fx, -fy);
context.save();
context.clearRect(0, 0, width, height);
context.translate(t.x, t.y);
context.scale(t.k, t.k);
drawPoints();
context.restore();
}
function drawPoints() {
context.beginPath();
points.forEach(drawPoint);
context.fill();
}
function drawPoint(point) {
context.moveTo(point[0] + radius, point[1]);
context.arc(point[0], point[1], radius, 0, 2 * Math.PI);
}
function phyllotaxis(radius) {
var theta = Math.PI * (3 - Math.sqrt(5));
return function(i) {
var r = radius * Math.sqrt(i), a = theta * i;
return [
width / 2 + r * Math.cos(a),
height / 2 + r * Math.sin(a)
];
};
}