[js高手之路] html5 canvas系列教程 - 掌握画直线图形的常用API

我们接着上文[js高手之路] html5 canvas系列教程 - 认识canvas以及基本使用方法继续.

一、直线的绘制

cxt.moveTo( x1, y1 ): 将画笔移动到x1, y1这个点

cxt.lineTo( x2, y2 ):将画笔从起点开始画直线,一直画到终点坐标( x2, y2 )

cxt.stroke();用画笔连线,moveTo,lineTo并不会产生实际的线条

x1,y1,x2,y2是点的坐标,canvas的坐标原点在canvas的左上角.

画一根直线:

 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         oGc.moveTo( 50, 50 );
14         oGc.lineTo( 250, 50 );
15         oGc.stroke();
16     }
17 </script>
18 </head>
19 <body>
20 <canvas id="canvas"></canvas>
21 </body>

如果把stroke注释了,是不会出现线条的,stoke的作用就是用来将点连起来

通过2个实例来区分,moveTo与lineTo的区别

 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         oGc.moveTo( 50, 50 );
14         oGc.lineTo( 250, 50 );
15         oGc.moveTo( 50, 200 );
16         oGc.lineTo( 250, 200 );
17         oGc.stroke();
18 
19         oGc.moveTo( 300, 50 );
20         oGc.lineTo( 500, 50 );
21         oGc.lineTo( 300, 200 );
22         oGc.lineTo( 500, 200 );
23         oGc.stroke();
24     }
25 </script>
26 </head>
27 <body>
28 <canvas id="canvas" width="600" height="400"></canvas>
29 </body>

左右两边的线形图,代码就一点区别,左边图形是第二个点用了lineTo, 第三个点用了moveTo, 右边图形第二个点用了lineTo,第三个点还是lineTo,从图中你应该能感受到这两个方法的区别吧?

 画三角形

 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( 450, 50 ); 
16         oGc.lineTo( 450, 300 );
17         oGc.lineTo( 50, 50 );
18         oGc.stroke();
19     }
20 </script>
21 </head>
22 <body>
23 <canvas id="canvas" width="600" height="400"></canvas>
24 </body>

把上面的代码,稍微修改下,就能画出一个矩形了

 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( 450, 50 ); 
16         oGc.lineTo( 450, 300 );
17         oGc.lineTo( 50, 300 );
18         oGc.lineTo( 50, 50 );
19         oGc.stroke();
20     }
21 </script>
22 </head>
23 <body>
24 <canvas id="canvas" width="600" height="400"></canvas>
25 </body>

二,canvas提供了画矩形的API

 通过线条我们也能拼接出一个矩形,但是代码太多,每个点都要把握,显得比较麻烦,canvas为我们提供了画矩形的API,有两种,一种是描边矩形,一种是填充矩形.

cxt.strokeStyle = 属性值

cxt.strokeRect( x, y, width, height )

strokeStyle后面的属性是为了修饰线条的,主要包括( 颜色值,渐变色,图案 ),颜色支持英文单词,十六进制,RGB, RGBA格式的颜色设置.

strokeRect: x, y为矩形的左上角坐标,width和height为矩形的宽度和高度

 1     <script>
 2         window.onload = function(){
 3             var oCanvas = document.querySelector( "#canvas" ),
 4                 oGc = oCanvas.getContext( '2d' );
 5     
 6             oGc.strokeStyle = '#09f';
 7             oGc.strokeRect( 50, 50, 500, 300 );
 8         }
 9     </script>
10     </head>
11     <body>
12     <canvas id="canvas" width="600" height="400"></canvas>
13     </body>

注意:oGc.strokeStyle = '#09f'; 如果把这句代码放在oGc.strokeRect( 50, 50, 500, 300 );的后面,那么设置的线条样式将不会生效,strokeStyle一定要在画图之前设置,否则是不会应用到的

 填充矩形API

 cxt.fillStyle = 属性值;

cxt.fillRect( x, y, width, height );

跟上面是一样的,只是把stoke换成了fill,fill就是填充的意思

画一个带有透明度的矩形:

 1 <script>
 2     window.onload = function(){
 3         var oCanvas = document.querySelector( "#canvas" ),
 4             oGc = oCanvas.getContext( '2d' );
 5 
 6         oGc.fillStyle = 'rgba( 255, 0, 0, 0.3 )';
 7         oGc.fillRect( 50, 50, 500, 300 );
 8     }
 9 </script>
10 </head>
11 <body>
12 <canvas id="canvas" width="600" height="400"></canvas>
13 </body>

另一种绘制矩形的API:cxt.rect( x, y, width, height );

他与strokeRect和fillRect有什么区别呢?

1,共同点:参数的意思相同

2,不同点,调用strokeRect和fillRect会立即绘制出矩形,而rect并不会,他需要调用stoke()或者fill()方法,才能把矩形绘制出来

 1 <script>
 2     window.onload = function(){
 3         var oCanvas = document.querySelector( "#canvas" ),
 4             oGc = oCanvas.getContext( '2d' );
 5 
 6         oGc.fillStyle = 'rgba( 255, 0, 0, 0.3 )';
 7         oGc.rect( 50, 50, 500, 300 );
 8         // oGc.stroke();
 9         oGc.fill(); 
10     }
11 </script>
12 </head>
13 <body>
14 <canvas id="canvas" width="600" height="400"></canvas>
15 </body>

清空矩形API:cxt.clearRect( x, y, width, height ); 参数跟strokeRect,fillRect意思一样

 1 <script>
 2     window.onload = function(){
 3         var oCanvas = document.querySelector( "#canvas" ),
 4             oGc = oCanvas.getContext( '2d' );
 5 
 6         oGc.fillStyle = 'rgba( 255, 0, 0, 0.3 )';
 7         oGc.fillRect( 50, 50, 500, 300 );
 8 
 9         oGc.clearRect( 100, 100, 200, 200 );
10     }
11 </script>
12 </head>
13 <body>
14 <canvas id="canvas" width="600" height="400"></canvas>
15 </body>

 

用fillRect和clearRect画一个加号,当然你可以用moveTo和lineTo,不过代码应该比这种方法多了不少.

 1 <script>
 2     window.onload = function(){
 3         var oCanvas = document.querySelector( "#canvas" ),
 4             oGc = oCanvas.getContext( '2d' );
 5 
 6         oGc.fillStyle = 'rgba( 255, 0, 0, 0.3 )';
 7         oGc.fillRect( 100, 100, 200, 200 );
 8         oGc.clearRect( 100, 100, 50, 50 );
 9         oGc.clearRect( 250, 100, 50, 50 );
10         oGc.clearRect( 250, 250, 50, 50 );
11         oGc.clearRect( 100, 250, 50, 50 );
12     }
13 </script>
14 </head>
15 <body>
16 <canvas id="canvas" width="400" height="400"></canvas>
17 </body>

绘制一个调色板:

 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             aColor = [ '00', '33', '66', '99', 'cc', 'ff' ],
14             aMiddle = [ 'ff', 'cc', '99', '66', '33', '00' ], count = 0;
15         for( var i = 0; i < 12; i++ ){
16             for( var j = 0; j < 18; j++ ){
17                 count++;
18                 if ( i < 6 && count < 6 && j < 6 )
19                     oGc.fillStyle = `#${aColor[i]}${aMiddle[0]}${aColor[j]}`;
20                 else if( i < 6 && count < 12 && j < 12 )
21                     oGc.fillStyle = `#${aColor[i]}${aMiddle[1]}${aColor[j-6]}`;
22                 else if ( i < 6 && count < 18 && j < 18 )
23                     oGc.fillStyle = `#${aColor[i]}${aMiddle[2]}${aColor[j-12]}`;
24                 else if ( count < 6 && j < 6 )
25                     oGc.fillStyle = `#${aColor[i-6]}${aMiddle[3]}${aColor[j]}`;
26                 else if ( count < 12 && j < 12 )
27                     oGc.fillStyle = `#${aColor[i-6]}${aMiddle[4]}${aColor[j-6]}`;
28                 else if ( count < 18 && j < 18 )
29                     oGc.fillStyle = `#${aColor[i-6]}${aMiddle[5]}${aColor[j-12]}`;
30                 oGc.fillRect( j * 40, i * 40, 40, 40 );
31             }
32             count = 0;
33         }
34     }
35 </script>
36 </head>
37 <body>
38 <canvas id="canvas" width="720" height="720"></canvas>
39 </body>

javascript原生实现调色板:

 1         var aColor = [ '00', '33', '66', '99', 'cc', 'ff' ],
 2             aMiddle = [ 'ff', 'cc', '99', '66', '33','00' ];
 3 
 4         document.write( "<table>" );
 5         for( var i = 0; i < 12; i++ ){
 6             document.write( "<tr>" );
 7             for( var j = 0 ; j < 18; j++ ) {
 8                 if ( i < 6 && j < 6 ) //前6行,左6列
 9                     document.write( "<td style='background-color:#" + aColor[i]+ aMiddle[0] + aColor[j] + "'>&nbsp;</td>" );
10                 else if ( i < 6 && j < 12 ){ //前6行 中间6列
11                     document.write( "<td style='background-color:#" + aColor[i]+ aMiddle[1] + aColor[j-6] + "'>&nbsp;</td>" );
12                 }else if ( i < 6 && j < 18 ){ //前6行, 后面6列
13                     document.write( "<td style='background-color:#" + aColor[i]+ aMiddle[2] + aColor[j-12] + "'>&nbsp;</td>" );
14                 }else if ( i < 12 && j < 6 ){ //后6行, 左6列
15                     document.write( "<td style='background-color:#" + aColor[i-6]+ aMiddle[3] + aColor[j] + "'>&nbsp;</td>" );
16                 }else if ( i < 12 && j < 12 ){ //后6行, 中6列
17                     document.write( "<td style='background-color:#" + aColor[i-6]+ aMiddle[4] + aColor[j-6] + "'>&nbsp;</td>" );
18                 }else if ( i < 12 && j < 18 ){ //后6行, 后6列
19                     document.write( "<td style='background-color:#" + aColor[i-6]+ aMiddle[5] + aColor[j-12] + "'>&nbsp;</td>" );
20                 }
21             }
22             document.write( "</tr>" );
23         }
24         document.write( "</table>" );

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540578.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

金矿问题

Description: 描述&#xff1a; This is a standard interview problem featured in interview coding rounds of Amazon, Flipkart. 这是亚马逊Flipkart的采访编码回合中的标准采访问题。 Problem statement: 问题陈述&#xff1a; Given a gold mine of n*m dimensions, e…

[转载] python中的数组类型及特点

参考链接&#xff1a; Python中的Array | 数组2(简介和功能) 名称 表示方法示例 是否有序 函数方法&#xff08;增删等&#xff09; 特点 List 类型表示&#xff1a;L L [Adam, 95.5, Lisa, 85] 有序 增加&#xff1a;&#xff08;1&#xff09;L.append(Paul),增加…

puppet

Puppet前期环境&#xff08;网络、解析、yum源、NTP&#xff09;在上一章节已经准备就绪&#xff0c;接下来我们就开始安装Puppet了&#xff0c;安装Puppet其实很简单&#xff0c;官方已经提供了yum源&#xff0c;只需要自己将所需要的安装包下载下来然后做成本地yum源即可使用…

[转载] 【数学问题】利用python求解表达式

参考链接&#xff1a; Python 变量 &#xff5c;表达式 &#xff5c;条件和函数 有时候我们会遇到一些很复杂的表达式&#xff0c;或者想要求解某个表达式&#xff0c;但是手动计算的话不但耗时还费精力&#xff0c;我们能不能利用计算机来帮助我们进行计算呢&#xff1f; 1…

cesium广告牌_公路广告牌

cesium广告牌Description: 描述&#xff1a; This is a standard dynamic programing problem of finding maximum profits with some constraints. This can be featured in any interview coding rounds. 这是在某些约束条件下找到最大利润的标准动态编程问题。 这可以在任何…

你和大牛差了啥

mmp。无时无刻不在想和大牛差在哪里了。别人为什么可以那么牛逼而你tmd那么菜&#xff01;整个人顿时都颓废了。啥事儿不想干。后来想了想感情就是他比较黑吧。

[转载] python数组的使用

参考链接&#xff1a; Python中整数的最大可能值是多少&#xff1f; 原文地址为&#xff1a; python数组的使用 python数组的使用 python数组的使用 2010-07-28 17:17 1、Python的数组分三种类型&#xff1a; (1) list 普通的链表&#xff0c;初始化后可以通过特定方法…

scala中循环守卫_Scala中的循环

scala中循环守卫Scala中的循环 (Loops in Scala) In programming, many times a condition comes when we need to execute the same statement or block of code more than one time. It could be difficult to write the same code multiple times, so programing language d…

50个必备基础命令

1.tar创建一个新的tar文件$ tar cvf archive_name.tar dirname/解压tar文件$ tar xvf archive_name.tar查看tar文件$ tar tvf archive_name.tar2. grep在文件中查找字符串(不区分大小写)$ grep -i "the" demo_file输出成功匹配的行&#xff0c;以及该行之后的三行$ g…

NM的完整形式是什么?

NM&#xff1a;无消息 (NM: No Message) NM is an abbreviation of "No Message". NM是“无消息”的缩写。 It is an expression, which is commonly used in the Gmail platform. It is also written as N/M or n/m or *n/m*. It is written in the subject of the…

[转载] python中全局变量和局部变量解析

参考链接&#xff1a; Python中的全局变量和局部变量 python函数中可以访问全局变量但是不能给全局变量赋值&#xff0c;除非进行显式声明global a 比如定义了全局变量 a 在函数my_fun()中可以直接访问a的值&#xff0c;而不需要global全局变量申明。下图为上面代码运行输出 …

【iCore4 双核心板_FPGA】例程十六:基于双口RAM的ARM+FPGA数据存取实验

实验现象&#xff1a; 核心代码&#xff1a; int main(void) {/* USER CODE BEGIN 1 */int i;int address,data;char error_flag 0;char receive_data[50];char buffer[8];char *p;/* USER CODE END 1 *//* MCU Configuration-----------------------------------------------…

[转载] Python中TFTP的理解

参考链接&#xff1a; Python中的打包pack和拆包unpack参数 Num01–>TFTP协议介绍 TFTP&#xff08;Trivial File Transfer Protocol,简单文件传输协议&#xff09; 是TCP/IP协议族中的一个用来在客户端与服务器之间进行简单文件传输的协议 特点&#xff1a; 1,简单 2…

gn fast-gn_GN的完整形式是什么?

gn fast-gnGN&#xff1a;晚安 (GN: Good Night) GN is an abbreviation of "Good Night". GN是“ Good Night”的缩写 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, Yahoo Messenge…

从零开始编写自己的C#框架(27)——什么是开发框架

前言 做为一个程序员&#xff0c;在开发的过程中会发现&#xff0c;有框架同无框架&#xff0c;做起事来是完全不同的概念&#xff0c;关系到开发的效率、程序的健壮、性能、团队协作、后续功能维护、扩展......等方方面面的事情。很多朋友在学习搭建自己的框架&#xff0c;很多…

[转载] Python 递归 深入理解递归 Python递归剖析,绝对让你看懂!

参考链接&#xff1a; Python | print()中的结束参数 目录 递归剖析 递归的两个过程 return 返回值 详解 递归思路二分法和递归尾递归递归练习题 递归剖析 递归真的很重要&#xff0c;之前学的时候&#xff0c;学的一知半解&#xff0c;以为真正了解&#xff0c;每次想到递归…

laravel 项目迁移_在Laravel迁移

laravel 项目迁移Before moving forward we need to know some facts about it, 在继续前进之前&#xff0c;我们需要了解一些事实&#xff0c; Resources: In these directories, we have already a js, lang, sass and view page. Where, sass and js file holf their uncom…

Python之list对应元素求和

本次分享将讲述如何在Python中对多个list的对应元素求和&#xff0c;前提是每个list的长度一样。比如&#xff1a;a[1,2,3], b[2,3,4], c[3,4,5], 对a,b,c的对应元素求和&#xff0c;输出应为[6,9,12].    方法一&#xff1a;   直接求解&#xff0c;按照对应元素相加的…

[转载] Python中str跟int的转换

参考链接&#xff1a; Python中的类型转换 字符串str转换成int: int_value int(str_value) int转换成字符串str: str_value str(int_value) a100 b666 #int转str类型 print(int转str类型) print(int转str&#xff1a; str(a)) #str转int类型 print(str转int类型…

ot协议是什么_OT的完整形式是什么?

ot协议是什么OT&#xff1a;主题外 (OT: Off Topic) OT is an abbreviation of "Off Topic". OT是“ Off Topic”的缩写 。 It is an expression, which is commonly used in Gmail or messaging platform. It shows that the email that has been sent is irrelev…