学习HTML5 canvas遇到的问题

1. 非零环绕原则(nonzZero rule)

  • 非零环绕原则是canvas在进行填充的时候是否要进行填充的判断依据。
  • 在判断填充的区域拉一条线出来,拉到图形的外面,这条拉出来的线就是辅助线。判断绘制的线是否是从辅助线的左边穿过到辅助线的右边,此时这种穿过的方式记录为+1;如果是从辅助线的右边穿到辅助线的左边,就记做-1.最后将所有记录的数字进行求和,如果求和的结果为0,代表这块区域不要填充,否则,必须填充
  • 上面的原理较难理解,可以这样理解,当在大矩形中绘制小矩形,大矩形的绘制方向与小矩形的绘制方向相同时,填充颜色后,大小矩形都填充相同颜色;大矩形的绘制方向与小矩形的绘制方向相反时,填充颜色后,小矩形不会填充颜色,大矩形与小矩形之间的区域会填充颜色。
  • 大矩形的绘制方向与小矩形的绘制方向相同时的代码
  •  <!DOCTYPE html>
    <html lang="en"> <head>
    <meta charset="UTF-8">
    <title>非零环绕原则</title>
    </head> <body>
    <canvas id="canvas" style="margin:0 auto;border:1px #666 solid" width="800" height="600">
    </canvas>
    <script>
    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');
    ctx.moveTo(100, 100);
    ctx.lineTo(100, 400);
    ctx.lineTo(400, 400);
    ctx.lineTo(400, 100);
    ctx.lineTo(100, 100); ctx.moveTo(200, 200);
    ctx.lineTo(300, 300);
    ctx.lineTo(300, 300);
    ctx.lineTo(300, 200);
    ctx.lineTo(200, 200);
    ctx.fill();
    </script>
    </body> </html>
  • 大矩形的绘制方向与小矩形的绘制方向相同时的效果图
  • 大矩形的绘制方向与小矩形的绘制方向相反时的代码
  •  <!DOCTYPE html>
    <html lang="en"> <head>
    <meta charset="UTF-8">
    <title>非零环绕原则</title>
    </head> <body>
    <canvas id="canvas" style="margin:0 auto;border:1px #666 solid" width="800" height="600">
    </canvas>
    <script>
    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');
    ctx.moveTo(100, 100);
    ctx.lineTo(100, 400);
    ctx.lineTo(400, 400);
    ctx.lineTo(400, 100);
    ctx.lineTo(100, 100); ctx.moveTo(200, 200);
    ctx.lineTo(300, 200);
    ctx.lineTo(300, 300);
    ctx.lineTo(200, 300);
    ctx.lineTo(200, 200);
    ctx.fill();
    </script>
    </body> </html>
  • 大矩形的绘制方向与小矩形的绘制方向相反时效果图

2. closePath() 与 lineTo()的区别

  • closePath与lineTo闭合是有区别的,closePath闭合自然,lineTo闭合会有锯齿,仅在闭合的连接处会有区别
  • 效果图
  •  <!DOCTYPE html>
    <html lang="en"> <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    canvas {
    display: block;
    margin: 100px auto;
    border: 1px solid #000;
    }
    </style>
    </head> <body>
    <canvas id="myCanvas" width="600px" height="400px"></canvas>
    <script>
    var myCanvas = document.getElementById("myCanvas");
    var ctx = myCanvas.getContext('2d');
    ctx.lineWidth = 20;
    ctx.moveTo(100, 100);
    ctx.lineTo(100, 100 + 100);
    ctx.lineTo(100 + 100, 100 + 100);
    ctx.lineTo(100, 100); ctx.moveTo(300, 100);
    ctx.lineTo(300, 100 + 100);
    ctx.lineTo(300 + 100, 100 + 100);
    ctx.closePath();
    ctx.stroke();
    </script>
    </body>
    </html>

3. arc绘图的注意事项

  • 使用 arc 绘图的时候, 如果没有设置 moveTo ,那么会从开始绘弧的地方作为起始点,连线到圆弧的起点.
  • 如果使用 stroke 方法, 那么会连线到圆弧的起始位置. 如果是 fill 方法, 会自动闭合路径填充.
  •  <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    canvas{
    display: block;
    margin: 0 auto;
    border: 1px solid #666;
    }
    </style>
    </head>
    <body>
    <canvas id="myCanvas" width="800" height="300"></canvas>
    <script>
    var myCanvas = document.getElementById("myCanvas");
    var ctx = myCanvas.getContext('2d');
    ctx.moveTo(50,100);
    ctx.lineTo(100,100);
    ctx.arc(150,150,50,0,Math.PI);
    ctx.stroke(); ctx.moveTo(200,100);
    ctx.lineTo(300,100);
    ctx.arc(300,150,50,0,Math.PI*1.2);
    ctx.stroke(); ctx.beginPath();
    ctx.moveTo(400,100);
    ctx.lineTo(500,100);
    ctx.arc(500,150,50,0,Math.PI*1.2);
    ctx.fill(); ctx.beginPath();
    ctx.moveTo(600,50);
    ctx.lineTo(700,100);
    ctx.arc(700,150,50,0,Math.PI*1.2);
    ctx.fill();
    </script>
    </body>
    </html>
  • 效果图

3.1 解决方法一:使用beginPath(),开启新的路径,两次绘制的图形就不会相互产生影响

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
canvas{
display: block;
margin: 0 auto;
border: 1px solid #666;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="800" height="300"></canvas>
<script>
var myCanvas = document.getElementById("myCanvas");
var ctx = myCanvas.getContext('2d');
ctx.moveTo(50,100);
ctx.lineTo(100,100);
//使用beginPath(),多添加的两句代码
ctx.stroke();
ctx.beginPath();
ctx.arc(150,150,50,0,Math.PI);
ctx.stroke();
</script>
</body>
</html>

效果图

3.2 解决方法一:使用moveTo(),将上一个图形的终点移动到下一个即将绘制的图形上,就可以解决问题,效果与上面的解决方法相同。但是,该方法只需要使用一次stroke().

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
canvas{
display: block;
margin: 0 auto;
border: 1px solid #666;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="800" height="300"></canvas>
<script>
var myCanvas = document.getElementById("myCanvas");
var ctx = myCanvas.getContext('2d');
ctx.moveTo(50,100);
ctx.lineTo(100,100);
//添加moveTO()这一句代码即可
ctx.moveTo(200,150);
ctx.arc(150,150,50,0,Math.PI);
ctx.stroke();
</script>
</body>
</html>

3.3  arc的一个小应用,绘制圆环进度条,使用了lineWidth

 <!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>Document</title>
<style>
canvas {
display: block;
margin: 0 auto;
border: 1px solid #666;
}
</style>
</head> <body>
<canvas id="myCanvas" width="400" height="400"></canvas>
<script>
var myCanvas = document.getElementById("myCanvas");
var ctx = myCanvas.getContext('2d'); function toRad(d) {
return d * Math.PI / 180;
}
var x = 200,
y = 200,
angle = 0,
percent = 0;
var timeId = setInterval(function() {
ctx.clearRect(0,0,myCanvas.width,myCanvas.height);
ctx.beginPath();
ctx.arc(x, y, 120, 0, toRad(angle));
ctx.strokeStyle = '#00f';
ctx.lineWidth = 40;
ctx.stroke(); ctx.fillStyle = '#f00';
ctx.font = '700 30px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
percent = Math.floor(angle /360*100);
ctx.fillText(percent + '%', x, y);
if (percent >= 100) {
clearInterval(timeId)
}
else{
angle++;
}
}, 20);
</script>
</body> </html>

效果图

4. arcTo()的使用

  • arcTo绘制圆角,需要线端点,矩形顶点以及另一线段的端点三个参考点
  •  <!DOCTYPE html>
    <html lang="en"> <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    canvas {
    display: block;
    margin: 0 auto;
    border: 1px solid #666;
    }
    </style>
    </head> <body>
    <canvas id="myCanvas" width="600" height="460"></canvas>
    <script>
    var myCanvas = document.getElementById("myCanvas");
    var ctx = myCanvas.getContext('2d'); function toRad(d) {
    return d * Math.PI / 180;
    } function circleRect(x, y, width, height, r, color) {
    //保存之前的绘图状态
    ctx.save();
    ctx.beginPath();
    //绘制四条边
    ctx.moveTo(x + r, y);
    ctx.lineTo(x + width - r, y); ctx.moveTo(x + r, y + height);
    ctx.lineTo(x + width - r, y + height); ctx.moveTo(x, y + r);
    ctx.lineTo(x, y + height - r); ctx.moveTo(x + width, y + r);
    ctx.lineTo(x + width, y + height - r); ctx.moveTo(x + r, y);
    ctx.arcTo(x, y, x, y + r, r); ctx.moveTo(x + width - r, y);
    ctx.arcTo(x + width, y, x + width, y + r, r); ctx.moveTo(x, y + height - r);
    ctx.arcTo(x, y + height, x + r, y + height, r); ctx.moveTo(x + width - r, y + height);
    ctx.arcTo(x + width, y + height, x + width, y + height - r, r);
    //传入颜色,则使用传入的颜色;否则使用默认黑色
    ctx.strokeStyle = color || '#000';
    ctx.stroke();
    //恢复之前的绘图状态
    ctx.restore();
    } circleRect(100, 100, 200, 200, 50, 'red');
    circleRect(300, 300, 100, 100, 25);
    </script>
    </body> </html>
  • 效果图

 

学习HTML5 canvas遇到的问题的更多相关文章

  1. 转载《学习HTML5 canvas遇到的问题》

    学习HTML5 canvas遇到的问题 1. 非零环绕原则(nonzZero rule) 非零环绕原则是canvas在进行填充的时候是否要进行填充的判断依据. 在判断填充的区域拉一条线出来,拉到图形的 ...

  2. 学习HTML5, Canvas及简单动画的使用

    通过在CSDN中的一些简单课程学习,跟随老师联系,做了如下的月亮,太阳和地球的运动效果.纪录在文章中,用于下次使用. 准备工作如下: 1. 使用三张背景图片 太阳 月亮 地球 2. 在HTML页面中定 ...

  3. (转)学习HTML5 Canvas这一篇文章就够了

    作者:做人要厚道2013 原文:https://blog.csdn.net/u012468376/article/details/73350998

  4. 赠书:HTML5 Canvas 2d 编程必读的两本经典

    赠书:HTML5 Canvas 2d 编程必读的两本经典 这两年多一直在和HTML5 Canvas 打交道,也带领团队开发了世界首款基于HTML5 Canvas 的演示文档工具---AxeSlide( ...

  5. html5 canvas画图之图形随拖动而复制(有操作指示)

    学习html5 canvas,写了一个小练习来加深理解,可以实现图形随拖动而复制. <!DOCTYPE html> <html> <head> <meta c ...

  6. [js高手之路] html5 canvas系列教程 - 状态详解(save与restore)

    本文内容与路径([js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解)是canvas中比较重要的概念.掌握理解他们是做出复杂canvas动 ...

  7. [js高手之路] html5 canvas系列教程 - 线形渐变,径向渐变与阴影设置

    接着上文[js高手之路] html5 canvas系列教程 - 像素操作(反色,黑白,亮度,复古,蒙版,透明)继续. 一.线形渐变 线形渐变指的是一条直线上发生的渐变. 用法: var linear ...

  8. HTML5学习总结——canvas绘制象棋(canvas绘图)

    一.HTML5学习总结——canvas绘制象棋 1.第一次:canvas绘制象棋(笨方法)示例代码: <!DOCTYPE html> <html> <head> & ...

  9. 学习笔记:HTML5 Canvas绘制简单图形

    HTML5 Canvas绘制简单图形 1.添加Canvas标签,添加id供js操作. <canvas id="mycanvas" height="700" ...

随机推荐

  1. Angular 任务列表页

    新建一个任务Module $ ng g m task 功能:项目列表显示,增加,修改,删除项目.邀请其它人员加入项目. 单一性原则:希望搭建多个组件,每个组件负责自己的功能. 一.task相关组件 $ ...

  2. python 对任意文件(jpg,png,mp3,mp4)base64的编码解码

    程序是事件驱动的,写博客是什么驱动的?事件? 时间?no,我承认我很懒,甚至不愿意记录总结.哪是什么驱动的? 对! 问题驱动的.遇到了问题解决了问题突然想起来搬到blog上,让遇到相同问题的可以参考下 ...

  3. 记忆(缓存)函数返回值:Python 实现

    对于经常调用的函数,特别是递归函数或计算密集的函数,记忆(缓存)返回值可以显着提高性能.而在 Python 里,可以使用字典来完成. 例子:斐波那契数列 下面这个计算斐波那契数列的函数 fib() 具 ...

  4. CyclicBarrier介绍

    应用场景 在某种需求中,比如一个大型的任务,常常需要分配好多子任务去执行,只有当所有子任务都执行完成时候,才能执行主任务,这时候,就可以选择CyclicBarrier了. 实例分析 我们需要统计全国的 ...

  5. 我的Python笔记补充:入门知识拾遗

    声明:本文整理借鉴金角大王的Python之路,Day1 - Python基础1,仅供本人学习使用!!! 入门知识拾遗 一.bytes类型 二.三元运算 1 result = 值1 if 条件 else ...

  6. Windows10远程报错:由于CredSSP加密Oracle修正导致远程失败

    解决方案:Windows 10 家庭版,没有 gpedit.msc,只能修改本地电脑的注册表,在本地“运行”输入: regedit   按以下目录进入:HKEY_LOCAL_MACHINE\Softw ...

  7. 在Eclipse中使用git把项目导入到git中--转载

    [转载出处注明:http://www.zhangxiaofu.cn/java/commonTools/2015/0607/764.html] 一.原有项目:  项目名为TestGit 二.在osc@g ...

  8. docker报错Service 'pwn_deploy_chroot' failed to build: Get https://registry-1.docker.io/v2/library/ubuntu/manifests/16.04:net/http: request canceled

    这几天碰到师傅让我帮忙做pwn题环境,结果遇到了坑 第一种方法是:https://blog.csdn.net/zhaoyayua/article/details/60141660 解决办法是执行 vi ...

  9. vue获取当前对象

    <li v-for="img in willLoadImg" @click="selectImg($event)"> <img class=& ...

  10. A - A Secret -扩展KMP

    题目大意: 给你两个字符串A,B,现在要你求B串的后缀在A串中出现的次数和后缀长度的乘积和为多少. 题解: 扩展KMP模板题,将A和B串都逆序以后就变成了求前缀的问题了,扩展KMP求处从i位置开始的最 ...