【关键点】
采用线性渐变色,使上深下浅的圆有凹下效果,使上浅下深的圆有凸起效果,两者结合就有立体圆钮的感觉。
【图例】
【代码】
<!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="10px" height="10px" style="border:1px dotted black;">
如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试.
</canvas>
</div>
</body>
</html>
<script type="text/javascript">
<!--
/*****************************************************************
* 将全体代码(ctrl+A)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/
// canvas的绘图环境
var ctx;
// 边长
const LENGTH=512;
// 舞台对象
var stage;
//-------------------------------
// 初始化
//-------------------------------
function init(){
// 获得canvas对象
var canvas=document.getElementById('myCanvas');
canvas.width=LENGTH;
canvas.height=LENGTH;
// 初始化canvas的绘图环境
ctx=canvas.getContext('2d');
ctx.translate(LENGTH/2,LENGTH/2);// 原点平移到画布中央
// 准备
stage=new Stage();
stage.init();
// 开幕
animate();
}
// 播放动画
function animate(){
stage.update();
stage.paintBg(ctx);
stage.paintFg(ctx);
// 循环
if(true){
window.requestAnimationFrame(animate);
}
}
// 舞台类
function Stage(){
// 初始化
this.init=function(){
}
// 更新
this.update=function(){
}
// 画背景
this.paintBg=function(ctx){
ctx.clearRect(-LENGTH/2,-LENGTH/2,LENGTH,LENGTH);// 清屏
// 黑底
ctx.beginPath();
ctx.arc(0,0,250,0,2*Math.PI,true);
ctx.closePath();
ctx.fillStyle="rgb(0,0,7)";
ctx.fill();
// 灰蓝外圈
ctx.strokeStyle="rgb(67,113,220)";
ctx.lineWidth=2;
ctx.beginPath();
ctx.arc(0,0,250,0,2*Math.PI,true);
ctx.closePath();
ctx.stroke();
// 用上面的圆去剪切下面的图形
ctx.clip();
// 背景网格
for(var i=0;i<105;i++){
const step=18;
ctx.beginPath();
ctx.moveTo(-LENGTH/2,-LENGTH/2+i*step);
ctx.lineTo(-LENGTH/2+i*step,-LENGTH/2);
ctx.closePath();
ctx.lineWidth=1;
ctx.strokeStyle="rgba(35,91,178,0.3)";
ctx.stroke();
ctx.beginPath();
ctx.moveTo(LENGTH/2-i*step,-LENGTH/2);
ctx.lineTo(LENGTH/2,-LENGTH/2+i*step);
ctx.closePath();
ctx.lineWidth=1;
ctx.strokeStyle="rgba(35,91,178,0.3)";
ctx.stroke();
}
// 画辐射状半透明蒙版
var rgnt=ctx.createRadialGradient(0,0,240,0,0,250);
rgnt.addColorStop(0,"rgba(35,91,178,0.3)");
rgnt.addColorStop(1,"rgba(255,255,255,1)");
ctx.fillStyle=rgnt;
ctx.beginPath();
ctx.arc(0,0,250,0,2*Math.PI,true);
ctx.closePath();
ctx.fill();
// 右四分之一圆
ctx.beginPath();
ctx.arc(0,0,245,-Math.PI/4,Math.PI/4,false);
ctx.lineWidth=10;
ctx.strokeStyle="rgb(173,205,243)";
ctx.stroke();
// 左四分之一圆
ctx.beginPath();
ctx.arc(0,0,245,Math.PI*3/4,Math.PI*5/4,false);
ctx.lineWidth=10;
ctx.strokeStyle="rgb(173,205,243)";
ctx.stroke();
// 红圈
ctx.beginPath();
ctx.arc(0,0,230,0,Math.PI*2,false);
ctx.closePath();
ctx.lineWidth=4;
ctx.strokeStyle="rgb(252,66,28)";
ctx.stroke();
// 蓝白圈
ctx.beginPath();
ctx.arc(0,0,220,0,Math.PI*2,false);
ctx.closePath();
ctx.lineWidth=2;
ctx.strokeStyle="rgb(98,168,255)";
ctx.stroke();
// 内部光弧
var rgnt=ctx.createRadialGradient(0,0,210,0,0,220);
rgnt.addColorStop(0,"rgba(98,168,255,0.1)");
rgnt.addColorStop(1,"rgba(255,255,255,1)");
ctx.fillStyle=rgnt;
ctx.beginPath();
ctx.arc(0,0,220,0,2*Math.PI,true);
ctx.closePath();
ctx.fill();
// 缺口蓝白圈
ctx.beginPath();
ctx.arc(0,0,160,Math.PI*3/4,Math.PI/4,false);
ctx.lineWidth=2;
ctx.strokeStyle="rgb(190,216,241)";
ctx.stroke();
// 画刻度
for(var i=0;i<=120;i++){
var theta=Math.PI/80*i+Math.PI*3/4;
var x=140*Math.cos(theta);
var y=140*Math.sin(theta);
if((i % 2)==0){
// 画刻度
var x1=160*Math.cos(theta);
var y1=160*Math.sin(theta);
if((i % 10)==0){
var x4=134*Math.cos(theta);
var y4=134*Math.sin(theta);
ctx.lineWidth=6;
ctx.strokeStyle="rgb(190,216,241)";
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x4,y4);
ctx.closePath();
ctx.stroke();
// 写数字
var x3=180*Math.cos(theta);
var y3=180*Math.sin(theta);
ctx.fillStyle="white";
ctx.font="20px Bahnschrift Condensed";
ctx.textAlign="center";
ctx.textBaseLine="Middle";
ctx.fillText(i*2,x3,y3+8);
}else{
var x2=140*Math.cos(theta);
var y2=140*Math.sin(theta);
ctx.lineWidth=2;
ctx.strokeStyle="rgb(190,216,241)";
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x2,y2);
ctx.closePath();
ctx.stroke();
}
}
}
}
// 画前景
this.paintFg=function(ctx){
var angle=Math.PI*11/6+Math.random()*Math.PI/120;
//
ctx.save();
ctx.lineWidth=0.5;
ctx.fillStyle = "rgb(250,53,21)";
ctx.beginPath();
ctx.rotate(angle);
ctx.moveTo(200,0);
ctx.lineTo(-50,0);
ctx.lineTo(-40,5);
ctx.lineTo(190,2);
ctx.closePath();
ctx.fill();
ctx.restore();
ctx.save();
ctx.lineWidth=0.5;
ctx.fillStyle = "red";
ctx.beginPath();
ctx.rotate(angle);
ctx.moveTo(200,0);
ctx.lineTo(-50,0);
ctx.lineTo(-40,-5);
ctx.lineTo(190,-2);
ctx.closePath();
ctx.fill();
ctx.restore();
// 外圈
var gnt=ctx.createLinearGradient(0,0,0,80);
gnt.addColorStop(0,"navy");
gnt.addColorStop(0.3,"rgb(6,22,45)");
gnt.addColorStop(1,"black");
ctx.beginPath();
ctx.arc(0,0,40,0,Math.PI*2,false);
ctx.closePath();
ctx.fillStyle=gnt;
ctx.fill();
// 内圈
var gnt=ctx.createLinearGradient(0,0,0,60);
gnt.addColorStop(0,"rgb(6,22,45)");
gnt.addColorStop(0.5,"navy");
gnt.addColorStop(1,"blue");
ctx.beginPath();
ctx.arc(0,0,30,0,Math.PI*2,false);
ctx.closePath();
ctx.fillStyle=gnt;
ctx.fill();
ctx.fillStyle="rgb(39,172,231)";
ctx.font="30px Bahnschrift Condensed";
ctx.textAlign="center";
ctx.textBaseLine="Middle";
ctx.fillText("173.2 km/h",0,150);
}
}
/*-------------------------------------
人是气死的,花是浇死的。
凡是大病,必与情志有关,长期情志不遂,
必会滋生大病。
养生并不是一味地讲究吃什么喝什么,
养生养的其实是人生格局,人生情怀。
-------------------------------------*/
//-->
</script>
END