Home > Web Front-end > JS Tutorial > body text

JS and Canves realize water ripple effect on click button

不言
Release: 2018-06-25 15:38:58
Original
2601 people have browsed it

This article will share with you the water ripple effect of clicking buttons based on js and canvas. The effect is very realistic. Friends who are interested in this can take a look.

I recently saw a good button click effect. When clicked It produces a water ripple effect, which is quite fun, so I simply implemented it (without considering the compatibility issues with lower version browsers)

Let’s take a look at the effect first, as shown below (the gif recording software is a bit scummy, it looks like Kaka...)

#This effect can be achieved by nesting canvases within the element or by css3.

Canves implementation

I picked up a code for canves implementation from the Internet, slightly removed some repeatedly defined styles and gave js comments. The code is as follows

html code

<a class="btn color-1 material-design" data-color="#2f5398">Press me!</a>
Copy after login

css code

* {
box-sizing: border-box;
outline: none;
}
body {
font-family: &#39;Open Sans&#39;;
font-size: 100%;
font-weight: 300;
line-height: 1.5em;
text-align: center;
}
.btn {
border: none;
display: inline-block;
color: white;
overflow: hidden;
margin: 1rem;
padding: 0;
width: 150px;
height: 40px;
text-align: center;
line-height: 40px;
border-radius: 5px;
}
.btn.color-1 {
background-color: #426fc5;
}
.btn-border.color-1 {
background-color: transparent;
border: 2px solid #426fc5;
color: #426fc5;
}
.material-design {
position: relative;
}
.material-design canvas {
opacity: 0.25;
position: absolute;
top: 0;
left: 0;
}
.container {
align-content: center;
align-items: flex-start;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
margin: 0 auto;
max-width: 46rem;
}
Copy after login

js code

var canvas = {},
centerX = 0,
centerY = 0,
color = &#39;&#39;,
containers = document.getElementsByClassName(&#39;material-design&#39;)
context = {},
element = {},
radius = 0,
// 根据callback生成requestAnimationFrame动画
requestAnimFrame = function () {
return (
window.requestAnimationFrame || 
window.mozRequestAnimationFrame || 
window.oRequestAnimationFrame || 
window.msRequestAnimationFrame || 
function (callback) {
window.setTimeout(callback, 1000 / 60);
}
);
} (),
// 为每个指定元素生成canves
init = function () {
containers = Array.prototype.slice.call(containers);
for (var i = 0; i < containers.length; i += 1) {
canvas = document.createElement(&#39;canvas&#39;);
canvas.addEventListener(&#39;click&#39;, press, false);
containers[i].appendChild(canvas);
canvas.style.width =&#39;100%&#39;;
canvas.style.height=&#39;100%&#39;;
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
}
},
// 点击并且获取需要的数据,如点击坐标、元素大小、颜色
press = function (event) {
color = event.toElement.parentElement.dataset.color;
element = event.toElement;
context = element.getContext(&#39;2d&#39;);
radius = 0;
centerX = event.offsetX;
centerY = event.offsetY;
context.clearRect(0, 0, element.width, element.height);
draw();
},
// 绘制圆形,并且执行动画
draw = function () {
context.beginPath();
context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
context.fillStyle = color;
context.fill();
radius += 2;
// 通过判断半径小于元素宽度,不断绘制 radius += 2 的圆形
if (radius < element.width) {
requestAnimFrame(draw);
}
};
init();
Copy after login

CSS3 implementation

The next step is to type the code by hand...I think it is more convenient to implement CSS3, maybe I am used to writing css...

html code

<a class="waves ts-btn">Press me!</a>
Copy after login

css code

.waves{
position:relative;
cursor:pointer;
display:inline-block;
overflow:hidden;
text-align: center;
-webkit-tap-highlight-color:transparent;
z-index:1;
}
.waves .waves-animation{
position:absolute;
border-radius:50%;
width:25px;
height:25px;
opacity:0;
background:rgba(255,255,255,0.3);
transition:all 0.7s ease-out;
transition-property:transform, opacity, -webkit-transform;
-webkit-transform:scale(0);
transform:scale(0);
pointer-events:none
}
.ts-btn{
width: 200px;
height: 56px;
line-height: 56px;
background: #f57035;
color: #fff;
border-radius: 5px;
}
Copy after login

js code

document.addEventListener(&#39;DOMContentLoaded&#39;,function(){
var duration = 750;
// 样式string拼凑
var forStyle = function(position){
var cssStr = &#39;&#39;;
for( var key in position){
if(position.hasOwnProperty(key)) cssStr += key+&#39;:&#39;+position[key]+&#39;;&#39;;
};
return cssStr;
}
// 获取鼠标点击位置
var forRect = function(target){
var position = {
top:0,
left:0
}, ele = document.documentElement;
&#39;undefined&#39; != typeof target.getBoundingClientRect && (position = target.getBoundingClientRect());
return {
top: position.top + window.pageYOffset - ele.clientTop,
left: position.left + window.pageXOffset - ele.clientLeft
}
}
var show = function(event){
var pp = event.target,
cp = document.createElement(&#39;p&#39;);
pp.appendChild(cp);
var rectObj = forRect(pp),
_height = event.pageY - rectObj.top,
_left = event.pageX - rectObj.left,
_scale = &#39;scale(&#39; + pp.clientWidth / 100 * 10 + &#39;)&#39;;
var position = {
top: _height+&#39;px&#39;,
left: _left+&#39;px&#39;
};
cp.className = cp.className + " waves-animation",
cp.setAttribute("style", forStyle(position)),
position["-webkit-transform"] = _scale,
position["-moz-transform"] = _scale,
position["-ms-transform"] = _scale,
position["-o-transform"] = _scale,
position.transform = _scale,
position.opacity = "1",
position["-webkit-transition-duration"] = duration + "ms",
position["-moz-transition-duration"] = duration + "ms",
position["-o-transition-duration"] = duration + "ms",
position["transition-duration"] = duration + "ms",
position["-webkit-transition-timing-function"] = "cubic-bezier(0.250, 0.460, 0.450, 0.940)",
position["-moz-transition-timing-function"] = "cubic-bezier(0.250, 0.460, 0.450, 0.940)",
position["-o-transition-timing-function"] = "cubic-bezier(0.250, 0.460, 0.450, 0.940)",
position["transition-timing-function"] = "cubic-bezier(0.250, 0.460, 0.450, 0.940)",
cp.setAttribute("style", forStyle(position));
var finishStyle = {
opacity: 0,
"-webkit-transition-duration": duration + "ms", // 过渡时间
"-moz-transition-duration": duration + "ms",
"-o-transition-duration": duration + "ms",
"transition-duration": duration + "ms",
"-webkit-transform" : _scale,
"-moz-transform" : _scale,
"-ms-transform" : _scale,
"-o-transform" : _scale,
top: _height + "px",
left: _left + "px",
};
setTimeout(function(){
cp.setAttribute("style", forStyle(finishStyle));
setTimeout(function(){
pp.removeChild(cp);
},duration);
},100)
}
document.querySelector(&#39;.waves&#39;).addEventListener(&#39;click&#39;,function(e){
show(e);
},!1);
},!1);
Copy after login

Okay, that’s all, by the way, happy Mid-Autumn Festival ~

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

jQuery and CSS3 folding card drop-down list box to achieve the effect

##About the JS component Bootstrap navigation bar How to use

The above is the detailed content of JS and Canves realize water ripple effect on click button. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!