画布Canvas的使用方法

   今天来个画布的讲解,对于画布你们了解多少呢。

  Canvas他是使用 JavaScript 来绘制图像的,canvas 元素本身是没有绘图能力的。所有的绘制工作必须在 JavaScript 内部完成。

  在画布的流程中大致是这样:

  1、’先获取画布canvas;

  2、创建2d画布;

  3、开始          X.beginPath(); 

  4、开始坐标        X.moveTo(X,Y);

  5、结束坐标  X.lineTo(X,Y)

  4、过渡    X.stroke();

  5、结束    X.closePath();

首先来个简单的表格的制作:
 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8 <canvas id="ca" width="300px" height="300px" style="width:300px;height:300px;border: 1px solid black"></canvas><!--canvas【画布】==========宽高必须在style外设*/-->
 9 <script>//=====相当于笔
10 //    1、获取
11     var ca=document.querySelector("#ca");
12     w=ca.width;//画布的宽
13     h=ca.height;//画布的高
14 var X=ca.getContext("2d");
15 for(var i=0;i<7;i++){
16     X.beginPath();
17     X.moveTo(h/6*i,0);
18     X.lineTo(h/6*i,w);
19     X.stroke();
20     X.strokeStyle='red';
21     X.closePath();
22 
23     X.beginPath();
24     X.moveTo(0,h/6*i);
25     X.lineTo(w,h/6*i);
26     X.stroke();
27     X.strokeStyle="yellow";
28     X.closePath();
29 }
30 X.beginPath();
31 X.fillStyle="red";
32 x.strokeStyle='red';
33 X.moveTo(10,10);
34 X.lineTo(100,100);
35 X.lineTo(10,100);
36 X.lineTo(10,10);
37 X.fill();
38 X.stroke();
39 X.closePath();
40 
41 
42 
43 
44 
45 </script>
46 </body>
47 </html>

再看看我画的太极吧:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<canvas width="300px" height="300px" style="border: 1px solid"></canvas>
<script>
    var ca=document.querySelector("canvas");
    var x=ca.getContext("2d");
    x.beginPath();
//    两个大圆相交
    x.fillStyle="white";
    x.arc(150,150,150,0,180*Math.PI/180,false);
    x.fill();
    x.stroke();
    x.closePath();

    x.beginPath();
    x.fillStyle="black";
    x.arc(150,150,150,0,180*Math.PI/180,true);
    x.fill();
    x.stroke();
    x.closePath();
    //两个小圆
    x.beginPath();
    x.restore(90*Math.PI/180);
    x.fillStyle="black";
    x.arc(76,150,75,0,180*Math.PI/180);
    x.fill();
    x.stroke();
    x.closePath();
    x.beginPath();
    x.restore(90*Math.PI/180);
    x.fillStyle="white";
    x.arc(76,150,20,0,360*Math.PI/180);
    x.fill();
    x.stroke();
    x.closePath();

    x.beginPath();
    x.fillStyle="white";
    x.arc(227,150,75,0,180*Math.PI/180,true);
    x.fill();
    x.stroke();
    x.closePath();
    x.beginPath();
    x.fillStyle="black";
    x.arc(227,150,20,0,360*Math.PI/180);
    x.fill();
    x.stroke();
    x.closePath();
</script>
</body>
</html>

 

转载于:https://www.cnblogs.com/yhyanjin/p/7100885.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值