小編給大家分享一下怎么通過(guò)HTML5 Canvas實(shí)現(xiàn)圖片的平移及旋轉(zhuǎn)變化,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
目前創(chuàng)新互聯(lián)公司已為上1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、承留網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
平移變換translate()
平移變換,故名思議,就是一般的圖形位移。比如這里我想將位于(100,100)的矩形平移至(200,200)點(diǎn)。那么我只要在繪制矩形之前加上context.translate(100,100)即可。
這里的translate()只傳入兩個(gè)參數(shù),其實(shí)就是新畫(huà)布坐標(biāo)系原點(diǎn)的坐標(biāo)。下面結(jié)合代碼來(lái)看看效果。
XML/HTML Code復(fù)制內(nèi)容到剪貼板
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>平移變換</title> <style> body { background: url("./images/bg3.jpg") repeat; } #canvas { border: 1px solid #aaaaaa; display: block; margin: 50px auto; } </style> </head> <body> <div id="canvas-warp"> <canvas id="canvas"> 你的瀏覽器居然不支持Canvas?!趕快換一個(gè)吧!! </canvas> </div> <script> window.onload = function(){ var canvas = document.getElementById("canvas"); canvas.width = 800; canvas.height = 600; var context = canvas.getContext("2d"); context.fillStyle = "#FFF"; context.fillRect(0,0,800,600); context.fillStyle = "#00AAAA"; context.fillRect(100,100,200,100); context.fillStyle = "red"; context.translate(100,100); context.fillRect(100,100,200,100); }; </script> </body> </html>
運(yùn)行結(jié)果:
這里的藍(lán)色矩形,是矩形原來(lái)的位置,然后調(diào)用translate()方法,將矩形位移至(200,200),即紅色矩形的位置。我們來(lái)用一張圖看看,它是怎么做到平移變換的。
沒(méi)錯(cuò),其實(shí)這里的平移變換實(shí)質(zhì)就是在平移坐標(biāo)系,而對(duì)translate()傳入的參數(shù),實(shí)質(zhì)就是新坐標(biāo)系相對(duì)于舊坐標(biāo)系的原點(diǎn)。這使得我們依舊是在(100,100)繪制的紅色矩形,在平移坐標(biāo)系之后,變到了(200,200)處。
注意使用狀態(tài)保存:
其實(shí)這里有一個(gè)坑,我們?nèi)绻氚丫匦纹揭浦粒?00,300)怎么辦呢?或許我們會(huì)想,直接調(diào)用context.translate(200,200)就可以了。好,我們看看效果。
JavaScript Code復(fù)制內(nèi)容到剪貼板
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>平移變換</title> <style> body { background: url("./images/bg3.jpg") repeat; } #canvas { border: 1px solid #aaaaaa; display: block; margin: 50px auto; } </style> </head> <body> <div id="canvas-warp"> <canvas id="canvas"> 你的瀏覽器居然不支持Canvas?!趕快換一個(gè)吧!! </canvas> </div> <script> window.onload = function(){ var canvas = document.getElementById("canvas"); canvas.width = 800; canvas.height = 600; var context = canvas.getContext("2d"); context.fillStyle = "#FFF"; context.fillRect(0,0,800,600); context.fillStyle = "#00AAAA"; context.fillRect(100,100,200,100); context.fillStyle = "red"; context.translate(100,100); context.fillRect(100,100,200,100); context.fillStyle = "green"; context.translate(200,200); context.fillRect(100,100,200,100); }; </script> </body> </html>
運(yùn)行結(jié)果:
這里的綠色矩形并沒(méi)有如我們所愿在(300,300)位置處,而是跑到了(400,400)這里。為什么呢?想必大家已經(jīng)知道了答案——Canvas是基于狀態(tài)的繪制。在我們第一次平移之后,坐標(biāo)系已經(jīng)在(100,100)處了,所以如果繼續(xù)平移,這個(gè)再基于新坐標(biāo)系繼續(xù)平移坐標(biāo)系。那么要怎么去解決呢?很簡(jiǎn)單,有兩個(gè)方法。
第一,在每次使用完變換之后,記得將坐標(biāo)系平移回原點(diǎn),即調(diào)用translate(-x,-y)。
第二,在每次平移之前使用context.save(),在每次繪制之后,使用context.restore()。
切記,千萬(wàn)不要再想著我繼續(xù)緊接著第一次平移之后再平移translate(100,100)不就行了,這樣你自己的坐標(biāo)系就會(huì)亂套,根本找不到自己的坐標(biāo)系原點(diǎn)在哪,在多次變換或者封裝函數(shù)之后,會(huì)坑死你。所以一定要以最初狀態(tài)為最根本的參照物,這是原則性問(wèn)題。這里我建議使用第二種方法,而且在涉及所有圖形變換的時(shí)候,都要這么處理,不僅僅是平移變換。
具體使用如下。
JavaScript Code復(fù)制內(nèi)容到剪貼板
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>平移變換</title> <style> body { background: url("./images/bg3.jpg") repeat; } #canvas { border: 1px solid #aaaaaa; display: block; margin: 50px auto; } </style> </head> <body> <div id="canvas-warp"> <canvas id="canvas"> 你的瀏覽器居然不支持Canvas?!趕快換一個(gè)吧!! </canvas> </div> <script> window.onload = function(){ var canvas = document.getElementById("canvas"); canvas.width = 800; canvas.height = 600; var context = canvas.getContext("2d"); context.fillStyle = "#FFF"; context.fillRect(0,0,800,600); context.fillStyle = "#00AAAA"; context.fillRect(100,100,200,100); context.save(); context.fillStyle = "red"; context.translate(100,100); context.fillRect(100,100,200,100); context.restore(); context.save(); context.fillStyle = "green"; context.translate(200,200); context.fillRect(100,100,200,100); context.restore(); }; </script> </body> </html>
運(yùn)行結(jié)果:
因此,在使用圖形變換的時(shí)候,要記得結(jié)合使用狀態(tài)保存。
旋轉(zhuǎn)變換rotate()
同畫(huà)圓弧一樣,這里的rotate(deg)傳入的參數(shù)是弧度,不是角度。同時(shí)需要注意的是,這個(gè)的旋轉(zhuǎn)是以坐標(biāo)系的原點(diǎn)(0,0)為圓心進(jìn)行的順時(shí)針旋轉(zhuǎn)。所以,在使用rotate()之前,通常需要配合使用translate()平移坐標(biāo)系,確定旋轉(zhuǎn)的圓心。即,旋轉(zhuǎn)變換通常搭配平移變換使用的。
最后一點(diǎn)需要注意的是,Canvas是基于狀態(tài)的繪制,所以每次旋轉(zhuǎn)都是接著上次旋轉(zhuǎn)的基礎(chǔ)上繼續(xù)旋轉(zhuǎn),所以在使用圖形變換的時(shí)候必須搭配save()與restore()方法,一方面重置旋轉(zhuǎn)角度,另一方面重置坐標(biāo)系原點(diǎn)。
JavaScript Code復(fù)制內(nèi)容到剪貼板
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>旋轉(zhuǎn)變換</title> <style> body { background: url("./images/bg3.jpg") repeat; } #canvas { border: 1px solid #aaaaaa; display: block; margin: 50px auto; } </style> </head> <body> <div id="canvas-warp"> <canvas id="canvas"> 你的瀏覽器居然不支持Canvas?!趕快換一個(gè)吧!! </canvas> </div> <script> window.onload = function(){ var canvas = document.getElementById("canvas"); canvas.width = 800; canvas.height = 600; var context = canvas.getContext("2d"); context.fillStyle = "#FFF"; context.fillRect(0,0,800,600); for(var i = 0; i <= 12; i++){ context.save(); context.translate(70 + i * 50, 50 + i * 40); context.fillStyle = "#00AAAA"; context.fillRect(0,0,20,20); context.restore(); context.save(); context.translate(70 + i * 50, 50 + i * 40); context.rotate(i * 30 * Math.PI / 180); context.fillStyle = "red"; context.fillRect(0,0,20,20); context.restore(); } }; </script> </body> </html>
運(yùn)行結(jié)果:
這里用for循環(huán)繪制了14對(duì)正方形,其中藍(lán)色是旋轉(zhuǎn)前的正方形,紅色是旋轉(zhuǎn)后的正方形。每次旋轉(zhuǎn)都以正方形左上角頂點(diǎn)為原點(diǎn)進(jìn)行旋轉(zhuǎn)。每次繪制都被save()與restore()包裹起來(lái),每次旋轉(zhuǎn)前都移動(dòng)了坐標(biāo)系。童鞋們可以自己動(dòng)動(dòng)手,實(shí)踐一下,就能體會(huì)到旋轉(zhuǎn)變換的奧妙了。
以上是“怎么通過(guò)HTML5 Canvas實(shí)現(xiàn)圖片的平移及旋轉(zhuǎn)變化”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁(yè)題目:怎么通過(guò)HTML5Canvas實(shí)現(xiàn)圖片的平移及旋轉(zhuǎn)變化
文章源于:http://www.chinadenli.net/article38/gisgpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站制作、小程序開(kāi)發(fā)、自適應(yīng)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)