From 30526f51a99600fed6de01be863b6f9a0c5230b0 Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Tue, 12 Jun 2012 01:02:49 +0200 Subject: [PATCH] pacman button --- html/force.js | 6 +++ html/nodes.html | 1 + html/nodes2.html | 27 ----------- html/pacman.js | 116 +++++++++++++++++++++++------------------------ 4 files changed, 65 insertions(+), 85 deletions(-) delete mode 100644 html/nodes2.html diff --git a/html/force.js b/html/force.js index f7e856a..6b9b741 100644 --- a/html/force.js +++ b/html/force.js @@ -54,6 +54,12 @@ var style_btn = cp.append("button") .text("Farbwechsler") .on("click", next_style) +cp.append("button") + .attr("class", "btn") + .attr("value", "reload") + .text("Click me!") + .on("click", pacman) + var btns = cp.append("div") .attr("class", "btn-group") diff --git a/html/nodes.html b/html/nodes.html index e713366..82ce6e5 100644 --- a/html/nodes.html +++ b/html/nodes.html @@ -9,6 +9,7 @@ + diff --git a/html/nodes2.html b/html/nodes2.html deleted file mode 100644 index 9a17574..0000000 --- a/html/nodes2.html +++ /dev/null @@ -1,27 +0,0 @@ - - - - - Freifunk Lübeck - Knotengraph - - - - - - - - - - -
-

luebeck.freifunk.net

- -
-
- - - - diff --git a/html/pacman.js b/html/pacman.js index f374ac2..fec688d 100644 --- a/html/pacman.js +++ b/html/pacman.js @@ -1,72 +1,72 @@ -var angle = d3.scale.linear() - .domain([0, 1, 2, 3]) - .range([0.01, Math.PI/4, 0.01, Math.PI/4]) - -d3.timer(pacman) -var a = 0 - -var p = {x: 0, y: 0} -var pm = vis.append("path") - .style("fill", "#ff0") - .style("stroke", "#000") - .style("stroke-width", 2.5) - .style("stroke-linejoin", "round") - function pacman() { - var size = force.size() - var nodes = force.nodes() - var n = nodes.length - if (n == 0) - return - - a = (a + 0.10)%2 + var angle = d3.scale.linear() + .domain([0, 1, 2, 3]) + .range([0.01, Math.PI/4, 0.01, Math.PI/4]) - pm.attr("d", d3.svg.arc().innerRadius(0) - .outerRadius(24).endAngle(-angle(a) + Math.PI/2 + 2*Math.PI).startAngle(angle(a) + Math.PI/2)) + d3.timer(pacman_animate) + var a = 0 - var closest = null - var dd = Infinity; - for (i = 0; i < n; i++) { - var o = nodes[i] + var p = {x: 0, y: 0} + var pm = vis.append("path") + .style("fill", "#ff0") + .style("stroke", "#000") + .style("stroke-width", 2.5) + .style("stroke-linejoin", "round") - var d = Math.pow((o.x - p.x),2) + Math.pow( o.y - p.y, 2) - if (d < dd) { - dd = d - closest = o + function pacman_animate() { + var size = force.size() + var nodes = force.nodes() + var n = nodes.length + if (n == 0) + return + + a = (a + 0.10)%2 + + pm.attr("d", d3.svg.arc().innerRadius(0) + .outerRadius(24).endAngle(-angle(a) + Math.PI/2 + 2*Math.PI).startAngle(angle(a) + Math.PI/2)) + + var closest = null + var dd = Infinity; + for (i = 0; i < n; i++) { + var o = nodes[i] + + var d = Math.pow((o.x - p.x),2) + Math.pow( o.y - p.y, 2) + if (d < dd) { + dd = d + closest = o + } } - } - var dx = closest.x - p.x - var dy = closest.y - p.y + var dx = closest.x - p.x + var dy = closest.y - p.y - var d = Math.sqrt(Math.pow(dx,2) + Math.pow(dy,2)) + var d = Math.sqrt(Math.pow(dx,2) + Math.pow(dy,2)) - dx = dx/d - dy = dy/d + dx = dx/d + dy = dy/d - if (d>1) { - - p.x += dx - p.y += dy - } else { - var snd; - if (closest.flags.client) { - snd = new Audio("pacman_eatfruit.wav") + if (d>8) { + p.x += dx * 2 + p.y += dy * 6 } else { - snd = new Audio("pacman_eatghost.wav") + var snd; + if (closest.flags.client) { + snd = new Audio("pacman_eatfruit.wav") + } else { + snd = new Audio("pacman_eatghost.wav") + } + snd.play() + + data.nodes = data.nodes.filter(function (d) { + return d.id != closest.id + }) + + data.links = data.links.filter(function (d) { + return d.target.id != closest.id && d.source.id != closest.id + }) + update() } - snd.play() - data.nodes = data.nodes.filter(function (d) { - return d.id != closest.id - }) - - data.links = data.links.filter(function (d) { - return d.target.id != closest.id && d.source.id != closest.id - }) - update() + pm.attr("transform", "matrix(" + [dx, dy, -dy, dx, p.x, p.y].join(",") + ")") } - - pm.attr("transform", "matrix(" + [dx, dy, -dy, dx, p.x, p.y].join(",") + ")") } -