這篇文章主要介紹如何使用HTML5中的Canvas繪制陰影效果,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)專注于平樂(lè)企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城開發(fā)。平樂(lè)網(wǎng)站建設(shè)公司,為平樂(lè)等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)創(chuàng)建陰影效果需要操作以下4個(gè)屬性:
1.context.shadowColor:陰影顏色。
2.context.shadowOffsetX:陰影x軸位移。正值向右,負(fù)值向左。
3.context.shadowOffsetY:陰影y軸位移。正值向下,負(fù)值向上。
4.context.shadowBlur:陰影模糊濾鏡。數(shù)據(jù)越大,擴(kuò)散程度越大。
這四個(gè)屬性只要設(shè)置了第一個(gè)和剩下三個(gè)中的任意一個(gè)就有陰影效果。不過(guò)通常情況下,四個(gè)屬性都要設(shè)置。
例如,創(chuàng)建一個(gè)向右下方位移各5px的紅色陰影,模糊2px,可以這樣寫。
context.shadowColor = "red"; context.shadowOffsetX = 5; context.shadowOffsetY = 5; context.shadowBlur= 2;
需要注意的是,這里的陰影同其他屬性設(shè)置一樣,都是基于狀態(tài)的設(shè)置。因此,如果只想為某一個(gè)對(duì)象應(yīng)用陰影而不是全局陰影,需要在下次繪制前重置陰影的這四個(gè)屬性。
運(yùn)行結(jié)果:
陰影文字:
只要設(shè)置shadowOffsetX與shadowOffsetY的值,當(dāng)值都正數(shù)時(shí),陰影相對(duì)文字的右下
方偏移。當(dāng)值都為負(fù)數(shù)時(shí),陰影相對(duì)文字的左上方偏移。
3D拉影效果:
在同一位置不斷的重復(fù)繪制文字同時(shí)改變shadowOffsetX、shadowOffsetY、shadowBlur
的值,從小到大不斷偏移不斷增加,透明度也不斷增加。就得到了拉影效果文字。
邊緣模糊效果文字:
在3D拉影效果的基礎(chǔ)上在四個(gè)方向重復(fù),就得到了邊緣羽化的文字效果。
運(yùn)行效果:
程序代碼:
<!DOCTYPE html> <html> <head> <meta http-equiv="X-UA-Compatible" content="chrome=IE8"> <meta http-equiv="Content-type" content="text/html;charset=UTF-8"> <title>Canvas Clip Demo</title> <link href="default.css" rel="stylesheet" /> <script> var ctx = null; // global variable 2d context var imageTexture = null; window.onload = function() { var canvas = document.getElementById("text_canvas"); console.log(canvas.parentNode.clientWidth); canvas.width = canvas.parentNode.clientWidth; canvas.height = canvas.parentNode.clientHeight; if (!canvas.getContext) { console.log("Canvas not supported. Please install a HTML5 compatible browser."); return; } var context = canvas.getContext('2d'); // section one - shadow and blur context.fillStyle="black"; context.fillRect(0, 0, canvas.width, canvas.height/4); context.font = '60pt Calibri'; context.shadowColor = "white"; context.shadowOffsetX = 0; context.shadowOffsetY = 0; context.shadowBlur = 20; context.fillText("Blur Canvas", 40, 80); context.strokeStyle = "RGBA(0, 255, 0, 1)"; context.lineWidth = 2; context.strokeText("Blur Canvas", 40, 80); // section two - shadow font var hh = canvas.height/4; context.fillStyle="white"; context.fillRect(0, hh, canvas.width, canvas.height/4); context.font = '60pt Calibri'; context.shadowColor = "RGBA(127,127,127,1)"; context.shadowOffsetX = 3; context.shadowOffsetY = 3; context.shadowBlur = 0; context.fillStyle = "RGBA(0, 0, 0, 0.8)"; context.fillText("Blur Canvas", 40, 80+hh); // section three - down shadow effect var hh = canvas.height/4 + hh; context.fillStyle="black"; context.fillRect(0, hh, canvas.width, canvas.height/4); for(var i = 0; i < 10; i++) { context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")"; context.shadowOffsetX = i*2; context.shadowOffsetY = i*2; context.shadowBlur = i*2; context.fillStyle = "RGBA(127, 127, 127, 1)"; context.fillText("Blur Canvas", 40, 80+hh); } // section four - fade effect var hh = canvas.height/4 + hh; context.fillStyle="green"; context.fillRect(0, hh, canvas.width, canvas.height/4); for(var i = 0; i < 10; i++) { context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")"; context.shadowOffsetX = 0; context.shadowOffsetY = -i*2; context.shadowBlur = i*2; context.fillStyle = "RGBA(127, 127, 127, 1)"; context.fillText("Blur Canvas", 40, 80+hh); } for(var i = 0; i < 10; i++) { context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")"; context.shadowOffsetX = 0; context.shadowOffsetY = i*2; context.shadowBlur = i*2; context.fillStyle = "RGBA(127, 127, 127, 1)"; context.fillText("Blur Canvas", 40, 80+hh); } for(var i = 0; i < 10; i++) { context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")"; context.shadowOffsetX = i*2; context.shadowOffsetY = 0; context.shadowBlur = i*2; context.fillStyle = "RGBA(127, 127, 127, 1)"; context.fillText("Blur Canvas", 40, 80+hh); } for(var i = 0; i < 10; i++) { context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")"; context.shadowOffsetX = -i*2; context.shadowOffsetY = 0; context.shadowBlur = i*2; context.fillStyle = "RGBA(127, 127, 127, 1)"; context.fillText("Blur Canvas", 40, 80+hh); } } </script> </head> <body> <h2>HTML5 Canvas Clip Demo - By Gloomy Fish</h2> <pre>Fill And Stroke Clip</pre> <div id="my_painter"> <canvas id="text_canvas"></canvas> </div> </body> </html>
以上是“如何使用HTML5中的Canvas繪制陰影效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文名稱:如何使用HTML5中的Canvas繪制陰影效果-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://aaarwkj.com/article4/jsjie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、響應(yīng)式網(wǎng)站、App開發(fā)、微信公眾號(hào)、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
營(yíng)銷型網(wǎng)站建設(shè)知識(shí)