A demonstration of handling multitouch events using D3. Requires a touch-supporting browser, such as iOS Safari.
forked from mbostock's block: Multitouch
| license: gpl-3.0 |
A demonstration of handling multitouch events using D3. Requires a touch-supporting browser, such as iOS Safari.
forked from mbostock's block: Multitouch
| <!DOCTYPE html> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="initial-scale=1,maximum-scale=1"> | |
| <style> | |
| html,body {height: 100%;} | |
| body {margin: 0;} | |
| svg { | |
| display: block; | |
| overflow: hidden; | |
| width: 100%; | |
| height: 100%; | |
| } | |
| </style> | |
| <body> | |
| <script src="//d3js.org/d3.v3.min.js"></script> | |
| <script> | |
| var svg = d3.select("body").append("svg").attr("width",innerWidth).attr("height", innerHeight) | |
| .on("touchstart",touch).on("touchmove",touch).on("touchend",touch);; | |
| var color = d3.scale.linear().domain([0,1,2]).range(['red','green','blue']); | |
| var drawPhase="clear"; | |
| var nodes=[]; // 1 node = elm, x, y, name, [{id,relation}] | |
| var circle = svg.selectAll("circle.node"); | |
| function touch() { | |
| d3.event.preventDefault(); | |
| /* | |
| clear | |
| touch0 (and back up) : if eventOnNode get i in nodes array, otherwise i=nodes.length; Node[i].x=event.x; Node[i].y=event.y; | |
| reading nodeNameA : changing nodes[i].name | |
| touch1 (and back up) / touch0up==>touch0=touch1; : if eventOnNode get i in nodes array, otherwise i=nodes.length; Node[i].x=event.x; Node[i].y=event.y; / | |
| reading relationName | |
| touch2 (and back up) | |
| reading nodeNameB | |
| */ | |
| switch (drawPhase) { | |
| case "clear": | |
| if (event.target.class=="node"){ | |
| // for what A nodes[A]==event.target ? if no A exists than { | |
| A=nodes.length; | |
| nodes[A].x=event.x | |
| nodes[A].y=event.y | |
| } | |
| break; | |
| case "namingA": | |
| nodes[A].name=""; // give a reference to a different event handler to keep adding to this | |
| break; | |
| case "namingRelation": | |
| break; | |
| case "namingB": | |
| break; | |
| default: | |
| console.log("this code should never be reached"); | |
| break; | |
| } | |
| circle = circle | |
| .data(d3.touches(svg.node()), function(d) { return d.identifier; }); | |
| circle.enter().append("circle") | |
| .attr("class", "node") | |
| .attr("r", 36) | |
| .style("fill", function(d) { return color(d.identifier); }) | |
| .transition() | |
| .duration(500) | |
| .ease("elastic") | |
| .attr("r", 48); | |
| circle | |
| .attr("cx", function(d) { return d[0]; }) | |
| .attr("cy", function(d) { return d[1]; }) | |
| .on("touchstart",touch).on("touchmove",touch).on("touchend",touch); | |
| } | |
| </script> |