Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to blur an image on mouseover using D3?

Tags:

css

svg

d3.js

I'm using D3, and I want to blur an image on mouseover.

I've placed a rect element placed directly behind the image, and set pointer-events: none on the image and pointer-events: all on the rect. This captures mouseover events fine, but I can't get the image to blur.

Here's my D3 code setting up the image and its rect element:

var newImages = images.enter()
   .append("g")
   .attr("class", "image");

newImages.append("svg:image")
  .attr('class', 'image-body')
  .attr('x', 0).attr('y', 20)
  .attr("xlink:href", function(d, i) {
    return "http://placekitten.com/150/200";
  });

newImages.append("rect")
  .attr('class', 'image-background')
  .attr('x', 0).attr('y', 20);

And CSS that doesn't work:

.image-background {
    stroke: black;
    stroke-width: 1px;
    fill: none;
    cursor: pointer;
    pointer-events: all;
    width: 150px;
    height: 200px;
}
.image-background:hover {
    -webkit-filter: blur(1px);
    -moz-filter: blur(1px);
    -o-filter: blur(1px);
    -ms-filter: blur(1px);
    filter: blur(1px);
}
.image-body {
    pointer-events:none;
    width: 150px;
    height: 200px;
}

If I add fill: blue to image-background: hover then that appears just fine. Is it possible to add a blur filter instead?

Here's my JSFiddle: https://jsfiddle.net/p4bfsvw9/2/

like image 837
Richard Avatar asked Mar 13 '23 02:03

Richard


1 Answers

You can use SVG filters.

Create filter:

 var filter = svg.append("defs")
      .append("filter")
      .attr("id", "blur")
      .append("feGaussianBlur")
      .attr("stdDeviation", 1); 

Use filter :

newImages.on("mouseover",function(){
        d3.select(this).select("image").attr("filter", "url(#blur)");
      })
      .on("mouseout",function(){
        d3.select(this).select("image").attr("filter", null);
      });

Reference: http://www.w3schools.com/svg/svg_fegaussianblur.asp

var data = [{
  id: 1
}];

var svg = d3.select(".container")
  .append("svg")
  .attr("width", 500)
  .attr("height", 500)
  .append("g")
  .attr("transform", "translate(10,10)");

var filter = svg.append("defs")
  .append("filter")
  .attr("id", "blur")
  .append("feGaussianBlur")
  .attr("stdDeviation", 1);

var images = svg.selectAll(".image")
  .data(data, function(d) {
    return d.id;
  });

var newImages = images.enter()
  .append("g")
  .attr("class", "image");

newImages.append("svg:image")
  .attr('class', 'image-body')
  .attr('x', 0)
  .attr('y', 20)
  .attr("xlink:href", function(d, i) {
    return "http://placekitten.com/150/200";
  })

newImages.on("mouseover",function(){
    console.log(this);
    d3.select(this).select("image").attr("filter", "url(#blur)");
  })
  .on("mouseout",function(){
    d3.select(this).select("image").attr("filter", null);
  });

newImages.append("rect")
  .attr('class', 'image-background')
  .attr('x', 0).attr('y', 20);

newImages.append("rect")
  .attr('class', 'image-text-background')
  .attr('x', 0).attr('y', 30);

newImages.append("text")
  .attr('class', 'image-text')
  .attr('x', 0).attr('y', 50)
  .text('hello, kitty');
.image-background {
  stroke: black;
  stroke-width: 1px;
  fill: none;
  cursor: pointer;
  pointer-events: all;
  width: 150px;
  height: 200px;
}
.image-body {
  pointer-events: none;
  width: 150px;
  height: 200px;
}
.image-text-background {
  fill: white;
  width: 75px;
  height: 30px;
  opacity: 0;
}
.image-text {
  opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div class="container">
</div>
like image 155
Gilsha Avatar answered Mar 15 '23 17:03

Gilsha