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方法创建渐变对象,使用shadowOffsetX、shadowOffsetY、shadowBlur和shadowColor属性设置阴影效果。
动画效果:通过使用定时器(如setInterval或requestAnimationFrame)和更新画布上的图形,可以实现Canvas的动画效果。
事件处理:Canvas可以处理鼠标点击、移动等事件,可以使用addEventListener方法来添加事件监听器,并在事件触发时执行相应的操作。
绘制基础
绘制矩形
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浏览器打开,就能看到实现效果。
******************************************************************/
// canvas的绘图环境
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的绘图环境
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);
// 循环
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();
// 斜向渐变内圈
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();
// 斜向渐变内圈
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();
// 斜向渐变内圈
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,"侠客行节选",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");// 版权
}
}
/*----------------------------------------------------------
函数:用于绘制实心圆,用途是标记点以辅助作图
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();
}
/*----------------------------------------------------------
函数:创建一个二维坐标点
x:横坐标
y:纵坐标
Pt即Point
----------------------------------------------------------*/
function createPt(x,y){
var retval={};
retval.x=x;
retval.y=y;
return retval;
}
/*----------------------------------------------------------
函数:延时若干毫秒
milliseconds:毫秒数
----------------------------------------------------------*/
function sleep(milliSeconds) {
const date = Date.now();
let currDate = null;
while (currDate - date < milliSeconds) {
currDate = Date.now();
}
}
/*----------------------------------------------------------
函数:书写文字
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来操作和绘制图形,本文示例实现js和canvas绘制圆形金属质感的诗词高级排版特效。
到此这篇关于js和canvas绘制圆形金属质感特效的文章就介绍到这了,更多相关js和canvas绘制金属特效内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
