[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.pswp.cn/news/540578.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540578.shtml
英文地址,請注明出處:http://en.pswp.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…