【Canvas与艺术】绘制圆形biohazrad蒙版

【关键点】

制作一个半圆形不透明蒙版来造成左右两边相异的明暗效果。

【成果图】

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>使用HTML5/Canvas绘制圆形biozhazrad蒙版</title>
     <style type="text/css">
     .centerlize{
        margin:0 auto;
        width:1200px;
     }
     </style>
     </head>

     <body οnlοad="init();">
        <div class="centerlize">
            <canvas id="myCanvas" width="12px" height="12px" style="border:1px dotted black;">
                如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试.
            </canvas>
        </div>
     </body>
</html>
<script type="text/javascript">
<!--
/*****************************************************************
* 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/

// canvas的绘图环境
var ctx;

// 高宽
const WIDTH=512;
const HEIGHT=512;

// 舞台对象
var stage;

//-------------------------------
// 初始化
//-------------------------------
function init(){
    // 获得canvas对象
    var canvas=document.getElementById('myCanvas');  
    canvas.width=WIDTH;
    canvas.height=HEIGHT;

    // 初始化canvas的绘图环境
    ctx=canvas.getContext('2d');  
    ctx.translate(WIDTH/2,HEIGHT/2);// 原点平移到画布中央

    // 准备
    stage=new Stage();    
    stage.init();

    // 开幕
    animate();
}

// 播放动画
function animate(){    
    stage.update();    
    stage.paintBg(ctx);
    stage.paintFg(ctx);     

    // 循环
    if(true){
        //sleep(100);
        window.requestAnimationFrame(animate);   
    }
}

// 舞台类
function Stage(){

    // 初始化
    this.init=function(){
        
    }

    // 更新
    this.update=function(){
        
    }

    // 画背景
    this.paintBg=function(ctx){
        ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏            
        writeText(ctx,WIDTH/2-30,HEIGHT/2-6,"逆火原创","8px consolas","black");// 版权
    }

    // 画前景
    this.paintFg=function(ctx){
        // 黄底
        ctx.beginPath();
        ctx.arc(0,0,200,0,Math.PI*2,false);
        ctx.closePath();        
        ctx.fillStyle="rgb(254,212,3)";
        ctx.fill();

        // 黑圈
        ctx.beginPath();
        ctx.arc(0,0,180,0,Math.PI*2,false);
        ctx.closePath();    
        ctx.lineWidth=20;
        ctx.strokeStyle="rgb(81,75,79)";
        ctx.stroke();

        //--------------下面开始绘制多刺爪
        ctx.save();
        ctx.scale(1.5,1.5);
        
        // 三个黑实心圆
        var arr=createRegTriArr(0,0,50);
        for(var i=0;i<arr.length;i++){
            var pt=arr[i];

            ctx.beginPath();
            ctx.arc(pt.x,pt.y,56,0,Math.PI*2,false);
            ctx.closePath();
            ctx.fillStyle="rgb(81,75,79)";
            ctx.fill();
        }

        // 三个偏心的黄色实心圆
        var arr=createRegTriArr(0,0,62);
        for(var i=0;i<arr.length;i++){
            var pt=arr[i];

            ctx.beginPath();
            ctx.arc(pt.x,pt.y,45,0,Math.PI*2,false);
            ctx.closePath();
            ctx.fillStyle="rgb(254,212,3)";
            ctx.fill();
        }

        // 三段式黑圈
        for(var i=0;i<3;i++){
            var startAngle=i*Math.PI*2/3-Math.PI/28;
            var endAngle=startAngle+Math.PI/5*2;

            ctx.beginPath();
            ctx.arc(0,0,40,startAngle,endAngle,false);
            ctx.lineWidth=12;
            ctx.strokeStyle="rgb(81,75,79)";
            ctx.stroke();
        }

        // 三小黄叉
        var arr=createRegTriArr(0,0,18);
        for(var i=0;i<arr.length;i++){
            var pt=arr[i];

            ctx.beginPath();
            ctx.moveTo(0,0);
            ctx.lineTo(pt.x,pt.y);
            ctx.lineWidth=6;
            ctx.strokeStyle="rgb(254,212,3)";
            ctx.stroke();
        }

        // 中心黄点
        ctx.beginPath();
        ctx.arc(0,0,9,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fillStyle="rgb(254,212,3)";
        ctx.fill();

        ctx.restore();
        ctx.restore();

        // 半圆形蒙版
        ctx.beginPath();
        ctx.moveTo(0,-200);
        ctx.arc(0,0,200,-Math.PI/2,Math.PI/2,false);
        ctx.closePath();
        ctx.fillStyle="rgba(81,75,79,0.2)";
        ctx.fill();
    }
}

/*----------------------------------------------------------
函数:创建一个以x,y为中心,r为半径的正三角形数组
ctx:绘图上下文
x:三角形中心横坐标
y:三角形中心纵坐标
r:三角形中心到顶点的长度
arr[0]为右下,arr[1]为左下,arr[2]为正上。
----------------------------------------------------------*/
function createRegTriArr(x,y,r){
    var arr=new Array();

    for(var i=0;i<3;i++){
        var theta=Math.PI*2/3*i+Math.PI/6;
        var pt=createPt(r*Math.cos(theta)+x,r*Math.sin(theta)+y);
        arr.push(pt);
    }

    return arr;
}

/*----------------------------------------------------------
函数:创建一个二维坐标点
x:横坐标
y:纵坐标
Pt即Point
----------------------------------------------------------*/
function createPt(x,y){
    var retval={};
    retval.x=x;
    retval.y=y;
    return retval;
}

/*----------------------------------------------------------
函数:延时若干毫秒
milliseconds:毫秒数
----------------------------------------------------------*/
function sleep(milliSeconds) {
    const date = Date.now();
    let currDate = null;
    while (currDate - date < milliSeconds) {
        currDate = Date.now();
    } 
}

/*----------------------------------------------------------
函数:书写文字
ctx:绘图上下文
x:横坐标
y:纵坐标
text:文字
font:字体
color:颜色
----------------------------------------------------------*/
function writeText(ctx,x,y,text,font,color){
    ctx.save();
    ctx.textBaseline="bottom";
    ctx.textAlign="center";
    ctx.font = font;
    ctx.fillStyle=color;
    ctx.fillText(text,x,y);
    ctx.restore();
}

/*-------------------------------------------------------------
滨城的出租车

远了找你要放空费,近了说不划算,表超过一分就多要一块,半路不管你
同不同意随时拉人,快到终点告诉你掉头麻烦让你走几百米进去,一边
开车一便和对讲机里聊天,天天盼着达沃斯之类活动管制私家车。
--------------------------------------------------------------*/
//-->
</script>

END

  • 18
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值