美文网首页
H5-canvas 入门

H5-canvas 入门

作者: 张Boy | 来源:发表于2017-10-17 22:43 被阅读22次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello</title>
</head>
<body>
    <canvas width="600px" height="600px" id="mycanvas"></canvas>
    <script type="text/javascript">
        var canvas=document.getElementById('mycanvas');
        if(canvas.getContext){
            var ctx=canvas.getContext('2d');
        }
        ////绘制线
        ctx.beginPath();
        ctx.moveTo(50,50);
        ctx.lineTo(100,100);
        ctx.lineTo(80,90);
        ctx.lineWidth=2;
        //线条颜色
        ctx.strokeStyle='#ff0000';
        ctx.stroke();
        ////绘制矩形(实心)
        ctx.clearRect(60,50,20,20);
        //清除指定矩形区域
        ctx.fillRect(160,150,70,80);
        ctx.strokeStyle='orange'
        ctx.strokeRect(170,160,50,50);
        ////绘制文本
        ctx.font='Bold 20px Arial';
        ctx.textAlign='left';
        ctx.fillStyle='brown';
        ctx.fillText('hello canvas',0,200);
        ////绘制空心文本
        ctx.strokeStyle='gray';
        ctx.strokeText('hello.canvas',0,260);
        ////绘制圆
        ctx.beginPath();
        ctx.arc(80,360,40,0,Math.PI*2,true);
        ctx.fillStyle='yellow';
        ctx.fill();
        ////绘制空心圆
        ctx.beginPath();
        ctx.arc(180,360,40,0,Math.PI*2,true);
        ctx.strokeStyle='green';
        ctx.lineWidth=1.0;
        ctx.stroke();
        ////绘制渐变色,填充矩形
        var myGradient=ctx.createLinearGradient(0,700,160,160);
        myGradient.addColorStop(0,'red');
        myGradient.addColorStop(1,'yellow');
        ctx.fillStyle=myGradient;
        //绘制阴影
        ctx.shadowOffsetX=2;
        ctx.shadowOffsetY=2;
        ctx.shadowBlur=3;
        ctx.shadowColor='rgba(22,22,22,.5)';
        ctx.fillRect(0,420,160,160);

    </script>
</body>
</html>

相关文章

网友评论

      本文标题:H5-canvas 入门

      本文链接:https://www.haomeiwen.com/subject/lmbjuxtx.html