Skip to content

Instantly share code, notes, and snippets.

@michaschwab
Last active April 20, 2020 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaschwab/a05187daaaa652de0e903365d1f0d327 to your computer and use it in GitHub Desktop.
Save michaschwab/a05187daaaa652de0e903365d1f0d327 to your computer and use it in GitHub Desktop.
VisConnect Collaborative Drag Example

Collaborative Dragging

This block demonstrates a minimal example for collaborative interaction on a visualization using VisConnect. The block is based on https://observablehq.com/@d3/circle-dragging-i. This block simply adds VisConnect and replaces Math.random() with vc.random, and d3.drag with vc.drag.

<svg width=500 height=400 id="vis"></svg>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://unpkg.com/peerjs@1.0.4/dist/peerjs.min.js"></script>
<script src="https://michaschwab.github.io/VisConnect/visconnect-bundle.js"></script>
<script>
const svg = d3.select("#vis");
const circles = d3.range(20).map(() => ({x: vc.random() * 500, y: vc.random() * 400}));
const drag = vc.drag().on("drag", function(d) {
d3.select(this).attr("cx", d.x = d3.event.x).attr("cy", d.y = d3.event.y);
});
svg.selectAll("circle")
.data(circles)
.join("circle")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", 20)
.attr("fill", (d, i) => d3.schemeCategory10[i % 10])
.call(drag);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment