javascript - How to make D3js object changing it's properties on mouse move near the object? -


the idea make object reacting on mouse moves in vicinity of object.

that how far got on now:

//declaring canvas var canvas = d3.select("body")                .append("svg")                .attr("width", 100)                .attr("height", 100);  //create circle attributes , listeners          var circles = canvas.selectall("circle")                     .data([1])                     .enter()                     .append("circle")   .attr("cy", 50).attr("cx", 50).attr("r", 20)   .style({"fill": "grey"})         .on("mousemove", handlemousemove);  //listener on mouse move inside canvas function handlemousemove(){     var coordinates = [0, 0];       coordinates = d3.mouse(this);       var x = coordinates[0];       var y = coordinates[1];       console.log(x,y);       console.log(this.attributes); } 

see example on codepen

i can object it's properties when hovering on - see last console.log();. , stuck on it. please share ideas regarding solution if any.

maybe cleanest way, if can, putting circle bigger radius under existing circle fill of transparent:

var g = canvas.selectall("g")     .data([1])     .enter()     .append("g")     .on("mouseover", handlemouseover) // event handlers here applied     .on("mouseout", handlemouseout)   // both 'circle'  g.append('circle').classed('invisible', true) // goes before     .attr("cy", 50)                           // visible circle     .attr("cx", 50)     .attr("r", 40)     .style({"fill": "transparent"});  g.append('circle').classed('visible', true)     .attr("cy", 50)     .attr("cx", 50)     .attr("r", 20)     .style({"fill": "grey"});      function handlemouseover(d,i){         d3.select(this).select('.visible').transition()             .style({"fill": "red"});                 };      function handlemouseout(d,i){         d3.select(this).select('.visible').transition()             .style({"fill": "green"});               }; 

or if want use mouse positions:

var circles = canvas.selectall("circle")     .data([1])     .enter()     .append("circle")         .attr("cy", 50)         .attr("cx", 50)         .attr("r", 20)         .style({"fill": "grey"})                 .on("mousemove", handlemousemove);  function handlemousemove(){     var coordinates = [];     coordinates = d3.mouse(this);     var x = coordinates[0];     var y = coordinates[1];      if (x>10 && x<90 && y>10 && y<90) {     // example values; rectangle can use more complex shapes          d3.select('.visible').style({"fill": "red"});     }     else {         d3.select('.visible').style({"fill": "green"});     } 

here fiddle can see both versions.
hope helps...


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -