[js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解

本文主要是介绍[js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

路径在canvas绘图中,经常被用到,是一个非常重要的概念.

比如:我们要在canvas画出3条直线,要求用不同的颜色加以区分.

 1 <style>
 2 body {
 3     background: #000;
 4 }
 5 #canvas{
 6     background:white;
 7 }
 8 </style>
 9 <script>
10 window.onload = function(){
11     var oCanvas = document.querySelector( "#canvas" ),
12         oGc = oCanvas.getContext( '2d' );
13 
14         oGc.strokeStyle = 'red';
15         oGc.moveTo( 50, 50 );
16         oGc.lineTo( 500, 50 );
17         oGc.stroke();
18 
19         oGc.strokeStyle = 'orange';
20         oGc.moveTo( 50, 150 );
21         oGc.lineTo( 500, 150 );
22         oGc.stroke();
23 
24         oGc.strokeStyle = 'yellow';
25         oGc.moveTo( 50, 250 );
26         oGc.lineTo( 500, 250 );
27         oGc.stroke();
28 }
29 </script>
30 </head>
31 <body>
32 <canvas id="canvas" width="600" height="300"></canvas> 
33 </body>

在画每一条线之前,我都用storeStyle设置了线的颜色,但是,出来的结果却是3条黄色的线,并不是红、橙、黄三条颜色不同的线。为什么呢?

首先我们要搞清楚canvas渲染图形,它是基于状态的,所谓状态就是每一次用( stroke/fill )之类的API渲染图形的时候,canvas会检查整个程序定义的( strokeStyle, fillStyle, lineWidth等 )当一个状态值没有被改变时,canvas就一直用这个状态。如果被改变,这里就要注意了:

1,如果使用beginPath()开始一个新的路径,则不同路径使用当前路径的值

2,如果没有使用beginPath()开始一个新的路径,后面的会覆盖前面的.

而我们这个程序就是属于第2种情况,尽管strokeStyle被改变了,但是没有用beginPath()开启新路径,所以前面两个strokeStyle会被最后一个strokeStyle='yellow'覆盖。所以3条线都是黄色.

看完这段解释,你应该知道怎样修改了吧?

只需要把每条线设置在不同的路径中,就可以区分了

 1 <style>
 2 body {
 3     background: #000;
 4 }
 5 #canvas{
 6     background:white;
 7 }
 8 </style>
 9 <script>
10 window.onload = function(){
11     var oCanvas = document.querySelector( "#canvas" ),
12         oGc = oCanvas.getContext( '2d' );
13 
14         oGc.beginPath();
15         oGc.strokeStyle = 'red';
16         oGc.moveTo( 50, 50 );
17         oGc.lineTo( 500, 50 );
18         oGc.stroke();
19 
20         oGc.beginPath();
21         oGc.strokeStyle = 'orange';
22         oGc.moveTo( 50, 150 );
23         oGc.lineTo( 500, 150 );
24         oGc.stroke();
25 
26         oGc.beginPath();
27         oGc.strokeStyle = 'yellow';
28         oGc.moveTo( 50, 250 );
29         oGc.lineTo( 500, 250 );
30         oGc.stroke();
31 }
32 </script>
33 </head>
34 <body>
35 <canvas id="canvas" width="600" height="300"></canvas> 
36 </body>

closePath:关闭路径

所谓关闭路径就是:指的是将同一个路径中的起点与终点相连接.

比如,我们画个三角形,不使用路径的时候,我们这样做:

 1 <style>
 2 body {
 3     background: #000;
 4 }
 5 #canvas{
 6     background:white;
 7 }
 8 </style>
 9 <script>
10 window.onload = function(){
11     var oCanvas = document.querySelector( "#canvas" ),
12         oGc = oCanvas.getContext( '2d' );
13 
14     oGc.moveTo( 50, 50 );
15     oGc.lineTo( 250, 50 );
16     oGc.lineTo( 250, 150 );
17     oGc.lineTo( 50, 50 );
18     oGc.stroke();
19 }
20 </script>
21 </head>
22 <body>
23 <canvas id="canvas" width="600" height="300"></canvas> 
24 </body>

最后一次用lineTo( 50, 50 )连接到起点,如果我们使用closePath,就不需要这一步操作了.

 1 <style>
 2 body {
 3     background: #000;
 4 }
 5 #canvas{
 6     background:white;
 7 }
 8 </style>
 9 <script>
10 window.onload = function(){
11     var oCanvas = document.querySelector( "#canvas" ),
12         oGc = oCanvas.getContext( '2d' );
13 
14     oGc.moveTo( 50, 50 );
15     oGc.lineTo( 250, 50 );
16     oGc.lineTo( 250, 150 );
17     oGc.closePath();
18     oGc.stroke();
19 }
20 </script>
21 </head>
22 <body>
23 <canvas id="canvas" width="600" height="300"></canvas> 
24 </body>

在stroke之前,用closePath关闭路径,他就会把( 250, 150)这个点和起始点( 50, 50 )连接起来.

画2个三角形:

 1     var oCanvas = document.querySelector( "#canvas" ),
 2         oGc = oCanvas.getContext( '2d' );
 3 
 4     oGc.moveTo( 50, 50 );
 5     oGc.lineTo( 250, 50 );
 6     oGc.lineTo( 250, 150 );
 7     oGc.closePath();
 8     oGc.stroke();
 9 
10     oGc.moveTo( 50, 150 );
11     oGc.lineTo( 250, 150 );
12     oGc.lineTo( 250, 250 );
13     oGc.closePath();
14     oGc.stroke();

使用路径,绘制两个不同颜色的三角形:

 1 <style>
 2 body {
 3     background: #000;
 4 }
 5 #canvas{
 6     background:white;
 7 }
 8 </style>
 9 <script>
10 window.onload = function(){
11     var oCanvas = document.querySelector( "#canvas" ),
12         oGc = oCanvas.getContext( '2d' );
13 
14     //这段oGc.beginPath可有可无,不会影响结果,但是建议加上,代码可读性比较好一点
15     oGc.beginPath();
16     oGc.strokeStyle = 'red';
17     oGc.moveTo( 50, 50 );
18     oGc.lineTo( 250, 50 );
19     oGc.lineTo( 250, 150 );
20     oGc.closePath();
21     oGc.stroke();
22 
23     oGc.beginPath();
24     oGc.strokeStyle = '#09f';
25     oGc.moveTo( 50, 150 );
26     oGc.lineTo( 250, 150 );
27     oGc.lineTo( 250, 250 );
28     oGc.closePath();
29     oGc.stroke();
30 }
31 </script>
32 </head>
33 <body>
34 <canvas id="canvas" width="600" height="300"></canvas> 
35 </body>

这篇关于[js高手之路] html5 canvas系列教程 - 开始路径beginPath与关闭路径closePath详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1076909

相关文章

随想录 Day 69 并查集 107. 寻找存在的路径

随想录 Day 69 并查集 107. 寻找存在的路径 理论基础 int n = 1005; // n根据题目中节点数量而定,一般比节点数量大一点就好vector<int> father = vector<int> (n, 0); // C++里的一种数组结构// 并查集初始化void init() {for (int i = 0; i < n; ++i) {father[i] = i;}

如何开启和关闭3GB模式

https://jingyan.baidu.com/article/4d58d5414dfc2f9dd4e9c082.html

vue, 左右布局宽,可拖动改变

1:建立一个draggableMixin.js  混入的方式使用 2:代码如下draggableMixin.js  export default {data() {return {leftWidth: 330,isDragging: false,startX: 0,startWidth: 0,};},methods: {startDragging(e) {this.isDragging = tr

十四、观察者模式与访问者模式详解

21.观察者模式 21.1.课程目标 1、 掌握观察者模式和访问者模式的应用场景。 2、 掌握观察者模式在具体业务场景中的应用。 3、 了解访问者模式的双分派。 4、 观察者模式和访问者模式的优、缺点。 21.2.内容定位 1、 有 Swing开发经验的人群更容易理解观察者模式。 2、 访问者模式被称为最复杂的设计模式。 21.3.观察者模式 观 察 者 模 式 ( Obser

【操作系统】信号Signal超详解|捕捉函数

🔥博客主页: 我要成为C++领域大神🎥系列专栏:【C++核心编程】 【计算机网络】 【Linux编程】 【操作系统】 ❤️感谢大家点赞👍收藏⭐评论✍️ 本博客致力于知识分享,与更多的人进行学习交流 ​ 如何触发信号 信号是Linux下的经典技术,一般操作系统利用信号杀死违规进程,典型进程干预手段,信号除了杀死进程外也可以挂起进程 kill -l 查看系统支持的信号

问题-windows-VPN不正确关闭导致网页打不开

为什么会发生这类事情呢? 主要原因是关机之前vpn没有关掉导致的。 至于为什么没关掉vpn会导致网页打不开,我猜测是因为vpn建立的链接没被更改。 正确关掉vpn的时候,会把ip链接断掉,如果你不正确关掉,ip链接没有断掉,此时你vpn又是没启动的,没有域名解析,所以就打不开网站。 你可以在打不开网页的时候,把vpn打开,你会发现网络又可以登录了。 方法一 注意:方法一虽然方便,但是可能会有

vue项目集成CanvasEditor实现Word在线编辑器

CanvasEditor实现Word在线编辑器 官网文档:https://hufe.club/canvas-editor-docs/guide/schema.html 源码地址:https://github.com/Hufe921/canvas-editor 前提声明: 由于CanvasEditor目前不支持vue、react 等框架开箱即用版,所以需要我们去Git下载源码,拿到其中两个主

React+TS前台项目实战(十七)-- 全局常用组件Dropdown封装

文章目录 前言Dropdown组件1. 功能分析2. 代码+详细注释3. 使用方式4. 效果展示 总结 前言 今天这篇主要讲全局Dropdown组件封装,可根据UI设计师要求自定义修改。 Dropdown组件 1. 功能分析 (1)通过position属性,可以控制下拉选项的位置 (2)通过传入width属性, 可以自定义下拉选项的宽度 (3)通过传入classN

Jitter Injection详解

一、定义与作用 Jitter Injection,即抖动注入,是一种在通信系统中人为地添加抖动的技术。该技术通过在发送端对数据包进行延迟和抖动调整,以实现对整个通信系统的时延和抖动的控制。其主要作用包括: 改善传输质量:通过调整数据包的时延和抖动,可以有效地降低误码率,提高数据传输的可靠性。均衡网络负载:通过对不同的数据流进行不同程度的抖动注入,可以实现网络资源的合理分配,提高整体传输效率。增

js+css二级导航

效果 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con