Built with blockbuilder.org
Last active
March 27, 2019 14:40
-
-
Save interwebjill/fb9b0d648df769ed72aeb2755d3ff7d5 to your computer and use it in GitHub Desktop.
d3 drag reset with multiple groups
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v5.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
</style> | |
</head> | |
<body> | |
<script> | |
const circleRadius = 40; | |
const variables = ['one', 'two', 'three', 'four']; | |
const inZone = []; | |
// DOM elements | |
const svg = d3.select("body").append("svg") | |
.attr("width", 960) | |
.attr("height", 500) | |
const dragDockGroup = svg.append('g') | |
.attr('id', 'draggables-dock'); | |
const dock = dragDockGroup.selectAll('g') | |
.data(variables) | |
.enter().append("g") | |
.attr("id", (d, i) => `dock-${variables[i]}`); | |
dock.append("circle") | |
.attr("cx", (d, i) => circleRadius * (2 * i + 1)) | |
.attr("cy", circleRadius) | |
.attr("r", circleRadius) | |
.style("stroke", "none") | |
.style("fill", "palegoldenrod"); | |
dock.append("text") | |
.attr("x", (d, i) => circleRadius * (2 * i + 1)) | |
.attr("y", circleRadius) | |
.attr("text-anchor", "middle") | |
.style("fill", "white") | |
.text((d, i) => variables[i]); | |
const draggablesGroup = svg.append('g') | |
.attr('id', 'draggables'); | |
const draggables = draggablesGroup.selectAll('g') | |
.data(variables) | |
.enter().append("g") | |
.datum(function(d,i) { | |
return "translate("+[circleRadius * (2 * i + 1),circleRadius]+")"; | |
}) | |
.attr("transform", (d,i) => d) | |
.attr("id", (d, i) => variables[i]) | |
.call(d3.drag() | |
.on("start", dragStarted) | |
.on("drag", dragged) | |
.on("end", dragEnded)); | |
draggables.append('circle') | |
.attr("r", circleRadius) | |
.style("stroke", "orange") | |
.style("fill", "yellowgreen"); | |
draggables.append("text") | |
.attr("text-anchor", "middle") | |
.style("fill", "white") | |
.text((d, i) => variables[i]); | |
svg.append('rect') | |
.attr("x", 960/2) | |
.attr("y", 0) | |
.attr("width", 100) | |
.attr("height", 500/2) | |
.attr("fill-opacity", 0) | |
.style("stroke", "#848276") | |
.attr("id", "hitZone"); | |
// functions | |
function dragStarted() { | |
d3.select(this).raise(); | |
} | |
function dragged() { | |
d3.select(this).attr("transform","translate("+[d3.event.x,d3.event.y]+")") | |
} | |
function dragEnded() { | |
d3.select(this).lower(); | |
let hit = d3.select(document.elementFromPoint(d3.event.sourceEvent.clientX, d3.event.sourceEvent.clientY)).attr("id"); | |
if (hit == "hitZone") { | |
inZone.push(this.id); | |
if (inZone.length > 1) { | |
let resetVar = inZone.shift(); | |
resetCircle(resetVar); | |
} | |
} | |
d3.select(this).raise(); | |
} | |
function resetCircle(resetVar) { | |
d3.select(`#${resetVar}`) | |
.transition() | |
.duration(1000) | |
.style('opacity', 0) | |
.transition() | |
.duration(500) | |
.attr("transform", (d,i) => d) | |
.transition() | |
.duration(1000) | |
.style('opacity', 1); | |
} | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment