canval+js实现material按钮点击水纹效果
2016/07/13    标签: html5    material    canvas    web前端   

Meterial Design是谷歌出品的一个跨平台的,统一视觉,运动和交互设计的规范。

现通过HTML5的cavans实现按钮点击水纹效果

html:

<button class="btn material-design btn_reg" data-color="#fff">注册</button>

css:

.btn{display:inline-block;}
.btn_reg{border:0;background:#333;color:#fff;width:140px;height:40px;}
.material-design{position:relative;}

js:

var canvas = {},
    centerX = 0,
    centerY = 0,
    color = '',
    containers = document.getElementsByClassName('material-design')
    context = {},
    element = {},
    radius = 0,

    requestAnimFrame = function () {
      return (
        window.requestAnimationFrame       || 
        window.mozRequestAnimationFrame    || 
        window.oRequestAnimationFrame      || 
        window.msRequestAnimationFrame     || 
        function (callback) {
          window.setTimeout(callback, 1000 / 60);
        }
      );
    } (),
      
    init = function () {
      containers = Array.prototype.slice.call(containers);
      for (var i = 0; i < containers.length; i += 1) {
        canvas = document.createElement('canvas');
        canvas.addEventListener('click', press, false);
        containers[i].appendChild(canvas);
        canvas.style.width ='100%';
        canvas.style.height='100%';
        canvas.width  = canvas.offsetWidth;
        canvas.height = canvas.offsetHeight;
      }
    },
      
    press = function (event) {
      color = event.toElement.parentElement.dataset.color;
      element = event.toElement;
      context = element.getContext('2d');
      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;
      if (radius < element.width) {
        requestAnimFrame(draw);
      }
    };

init();

代码转自网络,╮(╯▽╰)╭,忘记是哪个地方了