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

【代碼】
<!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,請更換瀏覽器再試.
</canvas>
</div>
</body>
</html>
<script type="text/javascript">
/*****************************************************************
* 將全體代碼(從<!DOCTYPE到script>)拷貝下來,粘貼到文本編輯器中,
* 另存為.html文件,再用chrome瀏覽器打開,就能看到實現(xiàn)效果。
******************************************************************/
// canvas的繪圖環(huán)境
var ctx;
// 高寬
const WIDTH=512;
const HEIGHT=512;
// 舞臺對象
var stage;
//-------------------------------
// 初始化
//-------------------------------
function init(){
// 獲得canvas對象
var canvas=document.getElementById('myCanvas');
canvas.width=WIDTH;
canvas.height=HEIGHT;
// 初始化canvas的繪圖環(huán)境
ctx=canvas.getContext('2d');
ctx.translate(WIDTH/2,HEIGHT/2);// 原點平移
// 準備
stage=new Stage();
stage.init();
// 開幕
animate();
}
// 播放動畫
function animate(){
stage.update();
stage.paintBg(ctx);
stage.paintFg(ctx);
// 循環(huán)
if(true){
//sleep(100);
window.requestAnimationFrame(animate);
}
}
// 舞臺類
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;// 基準尺寸
// 豎向漸變外框
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 方正宋刻本秀楷簡體",color);
var sz=R/11.2;
writeText(ctx,0,-r/4*2.0,"李白",sz+"16px 方正宋刻本秀楷簡體",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 方正宋刻本秀楷簡體",color);
writeText(ctx,start.x,start.y+gap,"銀鞍照白馬 颯沓如流星",sz+"px 方正宋刻本秀楷簡體",color);
writeText(ctx,start.x,start.y+2*gap, "十步殺一人 千里不留行",sz+"px 方正宋刻本秀楷簡體",color);
writeText(ctx,start.x,start.y+3*gap,"事了拂衣去 深藏身與名",sz+"px 方正宋刻本秀楷簡體",color);
writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制圖","8px consolas","lightgrey");// 版權
}
}
/*----------------------------------------------------------
函數(shù):用于繪制實心圓,用途是標記點以輔助作圖
ctx:繪圖上下文
x:矩形中心橫坐標
y:矩形中心縱坐標
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)建一個二維坐標點
x:橫坐標
y:縱坐標
Pt即Point
----------------------------------------------------------*/
function createPt(x,y){
var retval={};
retval.x=x;
retval.y=y;
return retval;
}
/*----------------------------------------------------------
函數(shù):延時若干毫秒
milliseconds:毫秒數(shù)
----------------------------------------------------------*/
function sleep(milliSeconds) {
const date = Date.now();
let currDate = null;
while (currDate - date < milliSeconds) {
currDate = Date.now();
}
}
/*----------------------------------------------------------
函數(shù):書寫文字
ctx:繪圖上下文
x:橫坐標
y:縱坐標
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>總結
在JavaScript中,可以使用HTML5提供的Canvas元素來進行繪圖操作,要使用canvas元素,瀏覽器必須支持html5,Canvas是一個HTML元素,可以通過JavaScript來操作和繪制圖形,本文示例實現(xiàn)js和canvas繪制圓形金屬質感的詩詞高級排版特效。
到此這篇關于js和canvas繪制圓形金屬質感特效的文章就介紹到這了,更多相關js和canvas繪制金屬特效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue-router路由懶加載的實現(xiàn)(解決vue項目首次加載慢)
這篇文章主要介紹了vue-router路由懶加載的實現(xiàn)(解決vue項目首次加載慢),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
前端內(nèi)網(wǎng)開發(fā)npm安裝的幾種方法小結
這篇文章主要介紹了如何在不聯(lián)網(wǎng)或離線環(huán)境下安裝npm依賴,文中通過代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考借鑒價值,需要的朋友可以參考下2025-01-01

