zoukankan      html  css  js  c++  java
  • canvas扩散圆环

    最近看了很多牛的动画,想想自己的canvas的确很菜。

    公式在那里,但是不是太会套。找demo发现都是很难的

    于是找了个简单的效果

    圆环从中间扩散的效果

    关键是 globalCompositeOperation合成操作,只留下重叠的部分

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>圆形扩散</title>
        <style>
            body {
                overflow: hidden;
                background: #000;
            }
            body,
            html {
                height: 100%;
                 100%;
                margin: 0;
                padding: 0;
            }
        </style>
    </head>
    <body>
        <canvas id="canvas"></canvas>
    </body>
    <script type="text/javascript">
    var oAnim=document.getElementById('canvas');
    var context = oAnim.getContext("2d");
    var radius=0
    
        function drawCircle(){
            context.beginPath();
            render(radius);
            context.arc(50,50,radius,0,Math.PI * 2);
            
            context.closePath();
    
            context.lineWidth=2;
            context.strokeStyle='rgba(250,250,50,1)';
    
            context.stroke();
            radius +=0.5;//每帧半径增加0.5
    
                if(radius > 20){
                    radius=0;
                }
        }
    
        function render(x) {
        //默认值为source-over,覆盖原图上面,效果像z-index:999
        var prev = context.globalCompositeOperation;

        //只显示canvas上原图像的重叠部分
        context.globalCompositeOperation = 'destination-in';

        //设置主canvas的绘制透明度,圆圈中间的浅黄色部分
        context.globalAlpha = 0.95;
     
        //这一步目的是将canvas上的图像变的透明
        context.fillRect(0,0,40*x,40*x);

        //在原图像上重叠新图像
        context.globalCompositeOperation = prev;
        //下面代用的drawcricle方法,圆圈覆盖在正方形上 }; //在主canvas上画新圆 setInterval(function(){ drawCircle(); },20); </script> </html>
  • 相关阅读:
    sass08 if while for each
    sass07 函数
    sass06 mixin
    sass05 数据类型,数据运算
    sass04 嵌套、继承、占位符
    批量导出docker images 的一个简单方法
    ARM 版本 瀚高 数据库的启动命令
    PHPStorm+Wamp+Xdebug+Windows7调试代码
    在Windows Server 2012 中安装 .NET 3.5 Framework
    Windows Server 2012 GUI与Core的切换
  • 原文地址:https://www.cnblogs.com/anxiaoyu/p/6672187.html
Copyright © 2011-2022 走看看