1. Web Design
  2. HTML/CSS
  3. SVG

Recreating the Touch Ripple Effect as Seen on Google Design

Scroll to top
Final product imageFinal product imageFinal product image
What You'll Be Creating

Google's new design efforts are vast and justify a significant amount of conversation. But instead of talking about the theory of Material Design and the implications of Google's newest efforts, we're going to focus on an interesting technique Google has employed on their Design landing page. 

When the user clicks on one of the blocks, an SVG circle expands from the point of the user's click to fill most of the box. Google refer to this as the touch ripple. We're going to recreate this effect using a few lines of jQuery, some simple HTML and CSS.

Let's get started!

The Basic Grid

Before we start, we need to set up a basic grid. We are going to build the grid elements without using a framework, but this technique would work with a framework perfectly fine.

1
<div class="row">
2
    <div class="col col-1-3 blue"></div>
3
	<div class="col col-1-3 orange"></div>
4
	<div class="col col-1-3 green"></div>
5
</div>
6
<div class="row">
7
	<div class="col col-1-2 gray"></div>
8
	<div class="col col-1-2 blue"></div>
9
</div>
10
<div class="row">
11
	<div class="col col-1-4 orange"></div>
12
	<div class="col col-1-2 green"></div>
13
	<div class="col col-1-4 blue"></div>
14
</div>

The column classes map to fractions, so that "col-1-3" means 1/3 the width of the containing element.

Basic CSS

Next, we will set up our column classes. We're using LESS, which allows us to nest rules and utilize the &operator. We won't get into the specifics of LESS, but for the sake of this tutorial, we'll explain how the & operator works. But first, here is the LESS for the columns.

1
.col {
2
  position: relative;
3
  display: block;
4
  float: left;
5
  margin: 1.25%;
6
  background-color: #444;
7
  color: #fff;
8
  padding: 100px;
9
  box-sizing: border-box;
10
  &.orange {
11
    background-color: #EF8130;
12
  }
13
  &.blue {
14
    background-color: #00ADE2;
15
  }
16
  &.gray {
17
    background-color: #444;
18
  }
19
  &.green {
20
    background-color: #76CE51;
21
  }
22
  &-1- {
23
    &2 {
24
      width: 47.5%;
25
    }
26
    &3 {
27
      width: 30.8333%;
28
    }
29
    &4 {
30
      width: 22.5%;
31
    }
32
  }
33
}

Notice the & rules. The & operator appends the string following it to the parent item. In other words, this LESS:

1
.col {
2
    &-1 {
3
		&-3 {
4
			color: #fff;
5
		}
6
	}
7
}

Would result in this CSS:

1
.col-1-3 { color: #fff; }

And this LESS:

1
&.col {
2
    &.orange {
3
		background-color: #EF8130;
4
	}
5
}

Would result in this CSS:

1
.col.orange {
2
    background-color: #EF8130;
3
}

If you'd like to learn more about LESS, take a look at these tutorials here on Tuts+:

How the SVG Will Work

Next, let's plan how the click will work, and how the SVG will be placed inside each of the boxes.

When the user clicks on any of the boxes, we will calculate the offset of the mouse position from the corner of that box. We'll then use those coordinates to place the circle. We will also absolutely position the SVG element inside the boxes, and the boxes themselves will be positioned relative. We'll utilize SVG's native <circle> element, along with a custom jQuery animation function.

First, let's set up the CSS for the SVG elements.

1
svg {
2
  position: absolute;
3
  top: 0;
4
  left: 0;
5
  width: 100%;
6
  height: 100%;
7
}
8
circle {
9
  fill: rgba(255,255,255,0.1);
10
}

The fill utilizes RGBa, which in effect fills the circle element with white at 10%.

The JavaScript

First, we will set up a click listener on the .col elements, and grab the mouse's position relative to the document (this disregards the scroll position). 

Mouse Position

The position is relative to the box itself; the top left corner of the box is fetched using $(this).offset().

1
$(".col").on("click", function(e){
2
    	var x = e.pageX;
3
		var y = e.pageY;
4
		var clickY = y - $(this).offset().top;
5
		var clickX = x - $(this).offset().left;
6
    var box = this;
7
    // ...

8
});

Note: we are using jQuery for this example.

Next, we will convert the clickX and clickY variables to integers, as they show up as floats in some browsers. This makes sure that we avoid any rendering issues resulting from subpixel aliasing. Note, however, that this wouldn't necessarily be required for the effect to work.

1
    var setX = parseInt(clickX);
2
    var setY = parseInt(clickY);

Remove Existing SVG Elements

Next, we will remove any existing SVG elements from our clicked box. If you plan to add an SVG to the content of the box, be certain that you use something like jQuery's.not() in combination with a class to avoid removing your content.

1
    $(this).find("svg").remove();

Append New SVG

Next, we append our SVG, which we are creating by passing text into the jQuery function.

1
$(this).append('<svg><circle cx="'+setX+'" cy="'+setY+'" r="'+0+'"></circle></svg>');

The setX and setY position the center of the circle at the point of the click that we derived earlier.

Animate Circle Radius

Next, we animate the r property (which sets the radius) using jQuery's animate function. The animate function doesn't support animating properties, so we use the step option, which is called after every step of the animation itself.

1
var c = $(box).find("circle");
2
c.animate(
3
  {
4
    "r" : $(box).outerWidth()
5
  },
6
  {
7
    easing: "easeOutQuad",
8
    duration: 400,
9
       step : function(val){
10
			c.attr("r", val);
11
		}
12
  }
13
);

Remember that box is defined earlier as the box that was clicked. We are also utilizing jquery.easing, which is what allows us to define "easeOutQuad" for the easing property.

The JavaScript, All Together

The final JavaScript will look like this:

1
$(".col").on("click", function(e){
2
    var x = e.pageX;
3
	var y = e.pageY;
4
	var clickY = y - $(this).offset().top;
5
	var clickX = x - $(this).offset().left;
6
  var box = this;
7
  
8
  var setX = parseInt(clickX);
9
  var setY = parseInt(clickY);
10
  $(this).find("svg").remove();
11
  $(this).append('<svg><circle cx="'+setX+'" cy="'+setY+'" r="'+0+'"></circle></svg>');
12
  
13
14
  var c = $(box).find("circle");
15
  c.animate(
16
    {
17
      "r" : $(box).outerWidth()
18
    },
19
    {
20
      easing: "easeOutQuad",
21
      duration: 400,
22
     	step : function(val){
23
				c.attr("r", val);
24
			}
25
    }
26
  );
27
});

Conclusion

This simple effect can be used in a number of ways beyond our example. Imagine, for instance, identifying where in an image a person clicked, and creating a popover to comment on that portion of the image, and subsequently saving the coordinates. What uses can you find for this effect?

In the Wild

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.