学习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>
  • 效果图

原文链接:http://www.cnblogs.com/stevexu/p/7196081.html

转载《学习HTML5 canvas遇到的问题》的更多相关文章

  1. 转载《Android LayoutInflater详解》

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  2. Android LayoutInflater详解

      在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且 ...

  3. Android LayoutInflater详解(转)

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  4. Android LayoutInflater详解 (转)

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  5. Android——LayoutInflater详解

    在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater. LayoutInflater在Android中是"扩展& ...

  6. <转> Android LayoutInflater详解

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  7. [ 转载 ] Android设计模式详解

    从Android再来认识23种设计模式 ReadyShow 关注  0.2 2018.01.06 23:18* 字数 3855 阅读 2584评论 0喜欢 20 概况来看本文章的内容 创建型:5个 单 ...

  8. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  9. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  10. Android ActionBar详解

    Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar   目录(?)[+]   第4 ...

随机推荐

  1. P2561 [AHOI2002]黑白瓷砖

    $ \color{#0066ff}{ 题目描述 }$ \(\color{#0066ff}{输入格式}\) 文件中以一行的形式存放一个正整数 n , n ≤ 20 . \(\color{#0066ff} ...

  2. C# winform间窗体传值简单Demo

    form1是用来接收值的 using System; using System.Collections.Generic; using System.ComponentModel; using Syst ...

  3. js 获取滚动条事件

    function getScroll() { return { left: window.pageXOffset || document.documentElement.scrollLeft || d ...

  4. java 加载properties

    /** * 取得属性值,无值时返回默认值 * @param name String 属性名称 * @param defaultValue String 属性名称 * @return String 属性 ...

  5. LeetCode154.寻找旋转排序数组中的最小值 II

    154.寻找旋转排序数组中的最小值 II 描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). ...

  6. sharepoint_study_2

    描述:向SharePoint中批量添加用户 解决:原文地址:http://bbs.winos.cn/thread-89236-1-1.html 一般情况下,要想登录SharePoint server ...

  7. hdu6183 Color it 线段树动态开点+查询减枝

    题目传送门 题目大意: 有多次操作.操作0是清空二维平面的点,操作1是往二维平面(x,y)上放一个颜色为c的点,操作2是查询一个贴着y轴的矩形内有几种颜色的点,操作3退出程序. 思路: 由于查询的矩形 ...

  8. [八分之一的男人]POJ - 1743 后缀数组 height分组 带详解

    题意:求最长不可重叠的相同差值子串的长度 这道题算是拖了好几个月,现在花了点时间应该搞懂了不少,尝试分析一下 我们首先来解决一个退化的版本,求最长不可重叠的相同子串(差值为0) 比如\(aabaaba ...

  9. HTML嵌套php

    1.  <?php echo 'if you want to serve XHTML or XML documents, do it like this'; ?> 2.  <scri ...

  10. 实现Java程序跨平台运行十二个注意事项

    [转自] http://blog.chinaunix.net/uid-20550186-id-1927257.html 使用Java语言编写应用程序最大的优点在于"一次编译,处处运行&quo ...