Canvas的Path分享,又一个html5知识点哦!

我们先画一行

然后,上面的脚本,画了棕色的线。

<div><canvas id="Canvas2" width="600" height = "200" style="border:solid 1px #000000;"></canvas>
   <div>
     <button οnclick="Vertical_line();return true;">Click me to draw a brown vertical line</button>
   </div> 
</div>
<script>
   var c3 = document.getElementById("c3");
   var c3_context = c3.getContext("2d");

   function Vertical_line() {
     c3_context.moveTo(300, 10);
     c3_context.lineTo(300, 190);
     c3_context.strokeStyle = "brown";
     c3_context.stroke();
   }
</script>

真正重要的方法是“函数”,“画线”、“stroke”和“strokeStyle”。

上下文方法 描述
函数(x,y) 搬到起点,x和y坐标。
画线(x,y) 画一条线从起点这一点。再次x和y坐标。
strokestyle  css线的颜色
stroke的方法 实际上使javascript画一条线
beginPath 在你开始画一个新的和不同的颜色,你将不得不叫“beginPath”。

<div>
<canvas id="c4" width="600" height = "200" style="border:solid 1px #000000;"></canvas>
<div>
  <button οnclick="Vertical_2px_Red();return true;">Vertical 2px Red line</button>
  <button οnclick="Vertical_1px_Blue();return true;">Vertical 1px Blue line</button>
  <button οnclick="Horizontal_2px_Green();return true;">Horizontal 2px Green line</button>
  <button οnclick="Clear_line();return true;">Erase Everything</button>
</div> 
</div>

<script>
var c4 = document.getElementById("c4");
var c4_context = c4.getContext("2d");

function Vertical_2px_Red() {
c4_context.beginPath();
c4_context.moveTo(300, 10);
c4_context.lineTo(300, 190);
c4_context.strokeStyle = "Red";
c4_context.stroke();
}

function Vertical_1px_Blue() {
c4_context.beginPath();
c4_context.moveTo(350.5, 10);
c4_context.lineTo(350.5, 190);
c4_context.strokeStyle = "Blue";
c4_context.stroke();
}

function Horizontal_2px_Green() {
c4_context.beginPath();
c4_context.moveTo(100, 100);
c4_context.lineTo(500, 100);
c4_context.strokeStyle = "Green";
c4_context.stroke();
}

function Clear_line() {
c4_context.clearRect(1, 1, 600, 190);
}
</script>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值