score:1
Updated to still include the bounding client rectangle, but iterate through any number of rectangles that exist. New Fiddle here.
Here's my solution to the problem. I used a great little "moveToBack" helper function seen here to move the rect to the back without changing the order in which it appears.
To get the positions of the circle and rectangle, I made heavy use of the vanilla js getBoundingClientRect()
method. You can see all this together in this JS Fiddle.
var width = window.innerWidth,
height = window.innerHeight;
var drag = d3.behavior.drag()
.on("dragstart", dragstarted)
.on("drag", dragged)
.on("dragend", dragended);
//create circle and space evenly
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
var circle = d3.select("svg")
.append("circle")
.attr("r", 15)
.attr("transform", "translate(50,30)")
.style("stroke", "black")
.attr("id", "circle")
.call(drag);
d3.selection.prototype.moveToBack = function() {
return this.each(function() {
var firstChild = this.parentNode.firstChild;
if (firstChild) {
this.parentNode.insertBefore(this, firstChild);
}
});
};
var rect = svg.append("rect")
.attr("x", 150)
.attr("y", 50)
.attr("width", 50)
.attr("height", 140)
.attr("fill", "green")
.attr("id", "rect")
.moveToBack();
var rect2 = svg.append("rect")
.attr("x", 350)
.attr("y", 50)
.attr("width", 50)
.attr("height", 140)
.attr("fill", "green")
.attr("id", "rect")
.moveToBack();
function dragstarted(d) {
d3.event.sourceEvent.stopPropagation();
}
function dragged(d) {
d3.select(this).attr("transform", "translate(" + d3.event.x + "," + d3.event.y + ")");
}
function dragended(d) {
// Define boundary
var rects = document.querySelectorAll("rect");
for (var i = 0; i < rects.length; i++) {
var rectDimensions = rects[i].getBoundingClientRect();
var xmin = rectDimensions.x;
var ymin = rectDimensions.y;
var xmax = rectDimensions.x + rectDimensions.width;
var ymax = rectDimensions.y + rectDimensions.height;
// Get circle position
var circlePos = document.getElementById("circle").getBoundingClientRect();
var x1 = circlePos.x;
var y1 = circlePos.y;
var x2 = circlePos.x + circlePos.width;
var y2 = circlePos.y + circlePos.height;
if(x2 >= xmin && x1 <= xmax && y2 >= ymin && y1 <= ymax) {
rects[i].setAttribute("fill", "red");
} else {
rects[i].setAttribute("fill", "green");
}
}
d3.event.sourceEvent.stopPropagation();
}
Source: stackoverflow.com
Related Query
- D3js drag&drop an object and detect where it is dropped.
- D3: How do I parse a CSV string into an object where keys are column names and values are arrays of data?
- how to reference d3js object on hover and click
- D3.js binding an object to data and appending for each key
- D3 force directed graph with drag and drop support to make selected node position fixed when dropped
- Adding and Removing Nodes in D3js Force Graph
- d3js scale, transform and translate
- D3js - Creating and easily updating a multi-line chart
- d3js force directed - On hover to node, highlight/colourup linked nodes and links?
- D3js force layout destroy and reset
- Stop Force Layout on d3js and start free dragging nodes
- Where can I get the .geojson file for India and not separate files for each state/territory or any other distinction?
- D3JS scaling and transition performance
- d3js axis dates start and end value only
- How to replace a d3js nest function with group and rollup
- D3JS Selection and class
- Find the maximum value in an object where the keys are arrays?
- Dynamic url as data-attribute of pseudo-element in css and D3JS
- Tree graph, in D3.js Where does x and y property came from?
- Create bode plot with minor and major ticks d3js
- Trying to link object and text when dragging in v4
- How to add a force drag event in D3 and make the node stay where i leave it?
- d3js pie graph from jquery ajax - correlating json keys and values on the chart
- Removing node and its children from d3js tree layout
- d3js returning "[object%20Object]" and "Uncaught TypeError: Cannot read property: nodes of undefined"
- How to detect current element's position and cancel an animation in d3.js?
- How to attach nodes and links to simulation force in d3js version 4?
- d3js v4: How can I apply force to nodes onclick and make it look like a tween?
- Nested JSON array and D3JS
- Where to store remote data in Svelte for responsive data vizualisations with Svelte and D3 (best practices)
More Query from same tag
- d3 + create pie chart with different formats
- draw multiple lines in line chart dynamically
- Creating a dropdown/select list from csv with d3 nest
- Return value from JSON, not the children count
- D3 map - excluding a polygon from hovering
- Iterating over object properties in Ember
- rotate and center globe on click
- How to have marker end enable to end of path in a d3 tree
- Using d3js to calculate midpoints and curve path for quadratic SVG connectors
- d3 choropleth queue and fill issues
- Replicating a chropleth in D3.js using d3.tsv
- How to create one group with multiple dimension on dc.js
- change class in javascript by d3 select and filter
- What is the purpose of `constant.js` in d3-drag library?
- d3 v4 error this.setAttribute is not a function
- D3 horizontal bar chart
- Setting up links in d3 force layout
- JavaScript D3 bar chart data will not update when new source is selected from drop down filter
- TypeError: d3.scaleLinear is not a function
- Removing event listener that invokes an event listener
- d3 adding custom marker to d3 map
- D3: Label layout on scatterplots
- Can't access data in D3 within on click event?
- Focused part shows wrong chart (NVD3)
- D3.js line chart axis
- D3.js moving from tsv to json with nested array
- D3 positive negative updating data
- How to show rating using row chart in dc js
- How to use d3+react?
- D3.js: Code not working