Canvas学习之基础一

绘制直线、图形

this.test1 = document.getElementById('test1');
this.test1.width = 1024;    // 必须如此设置宽高
this.test1.height = 900;
let ctx1;
if (this.test1.getContext('2d')) {
    ctx1 = this.test1.getContext('2d'); // 获取上下文
} else {
    alert('不支持canvas')
}
ctx1.beginPath();           // 清除之前的路径
ctx1.moveTo(100,200);       // 起始点
ctx1.lineTo(200,300);       // 移动端点
ctx1.lineTo(200,200);       // 移动端点
ctx1.fillStyle='red';       // 填充样式
ctx1.fill();                // 执行填充
ctx1.pathClose();           // 使路径封闭
ctx1.strokeStyle='black'    // 线条样式
ctx1.strokeWidth=3;         // 线条宽度
ctx1.stroke();              // 执行画线

圆弧的绘制

this.test2 = document.getElementById('test2');
this.test2.width = 1024;    // 必须如此设置宽高
this.test2.height = 900;
let ctx2;
if (this.test2.getContext('2d')) {
    ctx2 = this.test2.getContext('2d'); // 获取上下文
} else {
    alert('不支持canvas')
}
//ctx2.fillStyle = 'orange';
for (let i=1; i<= 10; i++) {
    ctx2.beginPath();
    ctx2.arc(50+100*(i-1), 500, 30, 0, 0.2*i*Math.PI, true);
    ctx2.closePath();
    ctx2.stroke();
//    ctx2.fill();
}
ctx2.fillStyle = 'orange';
for (let i=1; i<= 10; i++) {
    ctx2.beginPath();
    ctx2.arc(50+100*(i-1), 700, 30, 0, 0.2*i*Math.PI, true);
//                    ctx2.closePath();
    ctx2.stroke();
    ctx2.fill();// 不管有没有pathClose,执行fill则封闭
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值