-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02.html
109 lines (89 loc) · 2.62 KB
/
02.html
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
<html>
<meta charset="utf-8">
<head>
<script src="d3/d3.v3.js" type="text/javascript"></script>
<script src="keybinding.js"></script>
</head>
<body>
<script>
var gridSize = 25;
//Width and height
var w = 20;
var h = 12;
var grid = [];
var drag = false;
var mx = w / 2;
var my = h / 2;
for (var row = 0; row < h; row++) {
for (var col = 0; col < w; col++) {
grid[row * w + col] = {
"x": col,
"y": row
};
}
}
var svg = d3.select("body")
.append("svg")
.attr("width", w * gridSize)
.attr("height", h * gridSize);
var circles = svg.selectAll("circle")
.data(grid)
.enter()
.append("circle");
circles.attr("cx", function(d) {
return (d["x"] + 0.5) * gridSize;
})
.attr("cy", function(d) {
return (d["y"] + 0.5) * gridSize;
})
.attr("opacity", "0.5");
updateGrid();
d3.select("svg").on('mousedown', function() {
drag = true;
mx = Math.floor(d3.mouse(this)[0] / gridSize);
my = Math.floor(d3.mouse(this)[1] / gridSize);
updateGrid();
});
d3.select("svg").on('mouseup', function() {
drag = false;
var a = 'red'; //d3.rgb(255,0,0);
var b = 'blue'; // d3.rgb(0,0,255);
circles.attr("fill", d3.interpolateRgb(a,b)(Math.random()));
updateGrid();
});
d3.select("svg").on('mousemove', function() {
mx = Math.floor(d3.mouse(this)[0] / gridSize);
my = Math.floor(d3.mouse(this)[1] / gridSize);
updateGrid();
});
d3.select("body").call(d3.keybinding()
.on('←', moveFocus(-1, 0))
.on('↑', moveFocus(0, -1))
.on('→', moveFocus(1, 0))
.on('↓', moveFocus(0, 1)));
function moveFocus(x, y) {
return function(event) {
event.preventDefault();
mx = Math.min(w, Math.max(0, mx + x));
my = Math.min(h, Math.max(0, my + y));
updateGrid();
};
}
function updateGrid() {
if (drag) {
circles.attr("r", function(d) {
var dx = (mx - d["x"]);
var dy = (my - d["y"]);
return 0.5 * gridSize * Math.sqrt(dx * dx + dy * dy) / Math.sqrt(w * w + h * h);
});
} else {
circles.attr("r", function(d) {
var dx = (mx - d["x"]);
var dy = (my - d["y"]);
return 0.5 * gridSize * (1 - Math.sqrt(dx * dx + dy * dy) / Math.sqrt(w * w + h * h));
});
}
}
</script>
</body>
</html>