js和canvas繪制圓形金屬質(zhì)感特效
前言
在JavaScript中,可以使用HTML5提供的Canvas元素來進(jìn)行繪圖操作,要使用canvas元素,瀏覽器必須支持html5。Canvas是一個(gè)HTML元素,可以通過JavaScript來操作和繪制圖形。以下是一些常用的Canvas操作方法:
getContext():通過Canvas的getContext()方法可以獲取一個(gè)繪圖上下文(context),可以是2D或3D的。常見的繪圖上下文是2D上下文,可以使用getContext('2d')來獲取。
繪制基本形狀:Canvas提供了一系列的繪制方法,如繪制矩形(rect)、圓形(arc)、直線(lineTo)、路徑(path)等,可以使用這些方法來繪制基本的形狀。
繪制文本:Canvas提供了繪制文本的方法,如fillText和strokeText,可以設(shè)置文本的字體、大小、顏色等樣式。
繪制圖像:Canvas可以繪制圖像,可以使用drawImage方法將圖片繪制到Canvas上。
漸變和陰影:Canvas支持漸變和陰影效果,可以使用createLinearGradient或createRadialGradient方法創(chuàng)建漸變對(duì)象,使用shadowOffsetX、shadowOffsetY、shadowBlur和shadowColor屬性設(shè)置陰影效果。
動(dòng)畫效果:通過使用定時(shí)器(如setInterval或requestAnimationFrame)和更新畫布上的圖形,可以實(shí)現(xiàn)Canvas的動(dòng)畫效果。
事件處理:Canvas可以處理鼠標(biāo)點(diǎn)擊、移動(dòng)等事件,可以使用addEventListener方法來添加事件監(jiān)聽器,并在事件觸發(fā)時(shí)執(zhí)行相應(yīng)的操作。
繪制基礎(chǔ)
繪制矩形
context.fillStyle = "red"; context.fillRect(100, 100, 200, 150);
上述代碼會(huì)在畫布上繪制一個(gè)紅色的矩形,起始點(diǎn)坐標(biāo)為(100, 100),寬度為200,高度為150。
繪制圓形
context.beginPath(); context.arc(250, 250, 100, 0, Math.PI * 2, false); context.fillStyle = "green"; context.fill();
上述代碼繪制了一個(gè)中心坐標(biāo)為(250, 250),半徑為100的綠色圓形。
繪制直線
context.beginPath(); context.moveTo(100, 100); context.lineTo(300, 300); context.lineWidth = 5; context.strokeStyle = "blue"; context.stroke();
上述代碼繪制了一條起始點(diǎn)坐標(biāo)為(100, 100),終點(diǎn)坐標(biāo)為(300, 300),線寬為5,顏色為藍(lán)色的直線。
【成圖】

【代碼】
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<title>俠客行</title>
<style type="text/css">
.centerlize{
margin:0 auto;
width:1200px;
}
</style>
</head>
<body onload="init();">
<div class="centerlize">
<canvas id="myCanvas" width="12px" height="12px" style="border:1px dotted black;">
如果看到這段文字說您的瀏覽器尚不支持HTML5 Canvas,請(qǐng)更換瀏覽器再試.
</canvas>
</div>
</body>
</html>
<script type="text/javascript">
/*****************************************************************
* 將全體代碼(從<!DOCTYPE到script>)拷貝下來,粘貼到文本編輯器中,
* 另存為.html文件,再用chrome瀏覽器打開,就能看到實(shí)現(xiàn)效果。
******************************************************************/
// canvas的繪圖環(huán)境
var ctx;
// 高寬
const WIDTH=512;
const HEIGHT=512;
// 舞臺(tái)對(duì)象
var stage;
//-------------------------------
// 初始化
//-------------------------------
function init(){
// 獲得canvas對(duì)象
var canvas=document.getElementById('myCanvas');
canvas.width=WIDTH;
canvas.height=HEIGHT;
// 初始化canvas的繪圖環(huán)境
ctx=canvas.getContext('2d');
ctx.translate(WIDTH/2,HEIGHT/2);// 原點(diǎn)平移
// 準(zhǔn)備
stage=new Stage();
stage.init();
// 開幕
animate();
}
// 播放動(dòng)畫
function animate(){
stage.update();
stage.paintBg(ctx);
stage.paintFg(ctx);
// 循環(huán)
if(true){
//sleep(100);
window.requestAnimationFrame(animate);
}
}
// 舞臺(tái)類
function Stage(){
// 初始化
this.init=function(){
}
// 更新
this.update=function(){
}
// 畫背景
this.paintBg=function(ctx){
ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏
}
// 畫前景
this.paintFg=function(ctx){
// 底色
ctx.save();
ctx.fillStyle = "white";
ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
ctx.restore();
const R=180;// 基準(zhǔn)尺寸
// 豎向漸變外框
ctx.save();
var r=R*1.20;
var gnt=ctx.createLinearGradient(0,-r,0,r);
gnt.addColorStop(0, "rgb(237,198,119)");
gnt.addColorStop(0.2,"rgb(252,244,172)");
gnt.addColorStop(0.4,"rgb(147,95,36)");
gnt.addColorStop(0.5,"rgb(188,138,69)");
gnt.addColorStop(0.6,"rgb(147,95,36)");
gnt.addColorStop(0.8,"rgb(252,244,172)");
gnt.addColorStop(1, "rgb(237,198,119)");
ctx.fillStyle=gnt;
ctx.beginPath();
ctx.arc(0,0,r,0,Math.PI*2,false);
ctx.closePath();
ctx.fill();
ctx.restore();
// 上半圈漸變外框
ctx.save();
var ro=R*1.16;// outer radius
var ri=R*1.04;// inner radius
var gnt=ctx.createLinearGradient(0,-ro,0,0);
gnt.addColorStop(0, "rgb(253,247,242)");
gnt.addColorStop(0.5,"rgb(251,212,125)");
gnt.addColorStop(0.95,"rgb(245,225,190)");
gnt.addColorStop(1, "rgb(76,38,17)");
ctx.fillStyle=gnt;
ctx.beginPath();
ctx.moveTo(ri,0);
ctx.lineTo(ro,0);
ctx.arc(0,0,ro,0,-Math.PI,true);
ctx.lineTo(-ri,0);
ctx.arc(0,0,ri,-Math.PI,0,false);
ctx.closePath();
ctx.fill();
ctx.restore();
// 下半圈漸變外框
ctx.save();
var ro=R*1.16;// outer radius
var ri=R*1.04;// inner radius
var gnt=ctx.createLinearGradient(0,0,0,ro);
gnt.addColorStop(0, "rgb(76,38,17)");
gnt.addColorStop(1,"rgb(234,199,133)");
ctx.fillStyle=gnt;
ctx.beginPath();
ctx.moveTo(ri,0);
ctx.lineTo(ro,0);
ctx.arc(0,0,ro,0,Math.PI,false);
ctx.lineTo(-ri,0);
ctx.arc(0,0,ri,Math.PI,0,true);
ctx.closePath();
ctx.fill();
ctx.restore();
// 斜向漸變外框
ctx.save();
var r=R*1.04;
var gnt=ctx.createLinearGradient(-r,-r,r,r);
gnt.addColorStop(0, "rgb(186,132,46)");
gnt.addColorStop(0.5,"rgb(240,232,155)");
gnt.addColorStop(1, "rgb(176,118,64)");
ctx.fillStyle=gnt;
ctx.beginPath();
ctx.arc(0,0,r,0,Math.PI*2,false);
ctx.closePath();
ctx.fill();
ctx.restore();
// 斜向漸變內(nèi)圈
ctx.save();
var r=R*1.00;
var gnt=ctx.createLinearGradient(-r,-r,r,r);
gnt.addColorStop(0, "black");
gnt.addColorStop(1, "rgb(47,101,114)");
ctx.fillStyle=gnt;
ctx.beginPath();
ctx.arc(0,0,r,0,Math.PI*2,false);
ctx.closePath();
ctx.fill();
ctx.restore();
// 斜向漸變內(nèi)圈
ctx.save();
var r=R*0.98;
ctx.fillStyle="white";
ctx.beginPath();
ctx.arc(0,0,r,0,Math.PI*2,false);
ctx.closePath();
ctx.fill();
ctx.restore();
// slash蒙版
ctx.save();
r=R*0.98;
var gnt=ctx.createLinearGradient(-r,-r,r,r);
gnt.addColorStop(0, "rgba(255,255,255,0.25)");
gnt.addColorStop(0.4, "rgba(0,0,0,0.95)");
gnt.addColorStop(0.6, "rgba(0,0,0,0.95)");
gnt.addColorStop(1, "rgba(255,255,255,0.25)");
ctx.fillStyle=gnt;
ctx.beginPath();
ctx.arc(0,0,r,0,Math.PI*2,false);
ctx.closePath();
ctx.fill();
ctx.restore();
// backslash蒙版
ctx.save();
r=R*0.98;
var gnt=ctx.createLinearGradient(r,-r,-r,r);
gnt.addColorStop(0, "rgba(255,255,255,0.35)");
gnt.addColorStop(0.4, "rgba(0,0,0,0.75)");
gnt.addColorStop(0.6, "rgba(0,0,0,0.75)");
gnt.addColorStop(1, "rgba(255,255,255,0.35)");
ctx.fillStyle=gnt;
ctx.beginPath();
ctx.arc(0,0,r,0,Math.PI*2,false);
ctx.closePath();
ctx.fill();
ctx.restore();
// 斜向漸變內(nèi)圈
ctx.save();
var r=R*0.96;
var gnt=ctx.createLinearGradient(-r,-r,r,r);
gnt.addColorStop(0, "black");
gnt.addColorStop(1, "rgb(47,101,114)");
ctx.fillStyle=gnt;
ctx.beginPath();
ctx.arc(0,0,r,0,Math.PI*2,false);
ctx.closePath();
ctx.fill();
ctx.restore();
// 詩正文
var r=R*0.96;
var color="rgb(251,223,159)";
var sz=R/5.61;
writeText(ctx,0,-r/4*2.5,"俠客行節(jié)選",sz+"32px 方正宋刻本秀楷簡(jiǎn)體",color);
var sz=R/11.2;
writeText(ctx,0,-r/4*2.0,"李白",sz+"16px 方正宋刻本秀楷簡(jiǎn)體",color);
// 詩歌正文
var start=createPt(0,-r/3.5);
var gap=r/3.5;
var sz=R/6.6;
writeText(ctx,start.x,start.y,"趙客縵胡纓 吳鉤霜雪明",sz+"px 方正宋刻本秀楷簡(jiǎn)體",color);
writeText(ctx,start.x,start.y+gap,"銀鞍照白馬 颯沓如流星",sz+"px 方正宋刻本秀楷簡(jiǎn)體",color);
writeText(ctx,start.x,start.y+2*gap, "十步殺一人 千里不留行",sz+"px 方正宋刻本秀楷簡(jiǎn)體",color);
writeText(ctx,start.x,start.y+3*gap,"事了拂衣去 深藏身與名",sz+"px 方正宋刻本秀楷簡(jiǎn)體",color);
writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制圖","8px consolas","lightgrey");// 版權(quán)
}
}
/*----------------------------------------------------------
函數(shù):用于繪制實(shí)心圓,用途是標(biāo)記點(diǎn)以輔助作圖
ctx:繪圖上下文
x:矩形中心橫坐標(biāo)
y:矩形中心縱坐標(biāo)
r:圓半徑
color:填充圓的顏色
----------------------------------------------------------*/
function drawSolidCircle(ctx,x,y,r,color){
ctx.fillStyle=color;
ctx.beginPath();
ctx.arc(x,y,r,0,Math.PI*2,false);
ctx.closePath();
ctx.fill();
}
/*----------------------------------------------------------
函數(shù):創(chuàng)建一個(gè)二維坐標(biāo)點(diǎn)
x:橫坐標(biāo)
y:縱坐標(biāo)
Pt即Point
----------------------------------------------------------*/
function createPt(x,y){
var retval={};
retval.x=x;
retval.y=y;
return retval;
}
/*----------------------------------------------------------
函數(shù):延時(shí)若干毫秒
milliseconds:毫秒數(shù)
----------------------------------------------------------*/
function sleep(milliSeconds) {
const date = Date.now();
let currDate = null;
while (currDate - date < milliSeconds) {
currDate = Date.now();
}
}
/*----------------------------------------------------------
函數(shù):書寫文字
ctx:繪圖上下文
x:橫坐標(biāo)
y:縱坐標(biāo)
text:文字
font:字體
color:顏色
----------------------------------------------------------*/
function writeText(ctx,x,y,text,font,color){
ctx.save();
ctx.textBaseline="bottom";
ctx.textAlign="center";
ctx.font = font;
ctx.fillStyle=color;
ctx.fillText(text,x,y);
ctx.restore();
}
/*-------------------------------------------------------------
俠客行
唐·李白
趙客縵胡纓,吳鉤霜雪明。
銀鞍照白馬,颯沓如流星。
十步殺一人,千里不留行。
事了拂衣去,深藏身與名。
閑過信陵飲,脫劍膝前橫。
將炙啖朱亥,持觴勸侯嬴。
三杯吐然諾,五岳倒為輕。
眼花耳熱后,意氣素霓生。
--------------------------------------------------------------*/
</script>總結(jié)
在JavaScript中,可以使用HTML5提供的Canvas元素來進(jìn)行繪圖操作,要使用canvas元素,瀏覽器必須支持html5,Canvas是一個(gè)HTML元素,可以通過JavaScript來操作和繪制圖形,本文示例實(shí)現(xiàn)js和canvas繪制圓形金屬質(zhì)感的詩詞高級(jí)排版特效。
到此這篇關(guān)于js和canvas繪制圓形金屬質(zhì)感特效的文章就介紹到這了,更多相關(guān)js和canvas繪制金屬特效內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js給onclick事件賦值,動(dòng)態(tài)傳參數(shù)實(shí)例解說
js動(dòng)態(tài)給對(duì)象onclick事件賦值,動(dòng)態(tài)傳參數(shù)舉兩個(gè)例子一對(duì)一錯(cuò),感興趣的朋友可以對(duì)比下,希望可以從中發(fā)現(xiàn)不一樣之處2013-03-03
JS使用for循環(huán)遍歷Table的所有單元格內(nèi)容
JS遍歷Table的所有單元格內(nèi)容思路是遍歷Table的所有Row,遍歷Row中的每一列,獲取Table中單元格的內(nèi)容2014-08-08
JS獲取節(jié)點(diǎn)的兄弟,父級(jí),子級(jí)元素的方法
本篇文章主要是對(duì)JS獲取節(jié)點(diǎn)的兄弟,父級(jí),子級(jí)元素的方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01
JS判斷數(shù)組是否包含某元素實(shí)現(xiàn)方法匯總
這篇文章主要介紹了JS判斷數(shù)組是否包含某元素實(shí)現(xiàn)方法匯總,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
微信小程序?qū)崿F(xiàn)多選框全選與取消全選功能示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)多選框全選與取消全選功能,結(jié)合實(shí)例形式分析了微信小程序多選框功能實(shí)現(xiàn)、布局顯示及全選、取消全選相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
JS鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn)方法
程序開發(fā)人員可以使用一些簡(jiǎn)單的技術(shù)來改進(jìn)自己的代碼編寫工作。你可以寫一些函數(shù)來處理各種常見任務(wù),以節(jié)省時(shí)間;也可以改進(jìn)一下代碼的實(shí)現(xiàn)方式,比如你可以把方法的鏈?zhǔn)秸{(diào)用技術(shù)用到自己所寫的JS庫(kù)中,把自己喜歡的方法串起來調(diào)用。2013-03-03

