-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.js
68 lines (55 loc) · 1.29 KB
/
render.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
var canvas
function render() {
var dataset = JSON.parse(document.getElementById("JSONInput").value)
console.log(dataset)
//calculate width and height based on input polygons
var maxX = 0
var maxY = 0
for(i = 0; i<dataset.length; ++i) {
for(j = 0; j<dataset[i].coords.length; ++j) {
curX = dataset[i].coords[j][0]
curY = dataset[i].coords[j][1]
if(curX > maxX) {
maxX = curX
}
if(curY > maxY) {
maxY = curY
}
}
}
canvas = d3.select("body").append("svg")
.attr("width", maxX+20)
.attr("height", maxY+20);
canvas.selectAll("polygon")
.data(dataset)
.enter()
.append("polygon")
.attr("points",function(d) {
return d.coords.join(" ")
})
.attr("stroke","black")
.attr("stroke-width",2)
.attr("fill", "white")
.on("mouseover", handleMouseOver)
.on("mouseout", handleMouseOut);
}
function handleMouseOut(d, i) {
window.speechSynthesis.cancel();
}
function handleMouseOver(d, i) {
var msg = new SpeechSynthesisUtterance(d.text);
window.speechSynthesis.speak(msg);
/*
d3.select(this).attr({
fill: "orange"
});
*/
canvas.append("text").attr({
id: "t" + d.x + "-" + d.y + "-" + i,
x: function() { return d.coords[0][0]; },
y: function() { return d.coords[0][1]; }
})
.text(function() {
return d.text; // Value of the text
});
}