js canvas仿支付寶芝麻信用分儀表盤
這是一個(gè)仿支付寶芝麻信用分的一個(gè)canvas,其實(shí)就是一個(gè)動(dòng)畫儀表盤。
首先, 上原圖:

這個(gè)是在下支付寶上的截圖,分低各位見笑了。然后看下我用canvas實(shí)現(xiàn)的效果圖:
<canvas id="canvas" width="400" height="700" data-score='724'></canvas> <!-- 設(shè)置data-score,分?jǐn)?shù)區(qū)間[400, 900] -->

唉,總感覺不像。這個(gè)是GIF圖,可能在網(wǎng)頁上打開的效果會(huì)好一點(diǎn)(當(dāng)然可能就是這樣)。大家可以點(diǎn)擊底部預(yù)覽codepen上的演示。有兩個(gè)不完美的地方,一個(gè)是實(shí)際上芝麻信用表盤上的的刻度是不均勻的,我這為了簡單的實(shí)現(xiàn)就采取相同的刻度;二是表盤上運(yùn)動(dòng)的點(diǎn)是有模糊的效果,還沒解決。唉,下次再說吧。
接下來還是來說說怎么實(shí)現(xiàn)的吧。第一步,國際慣例,創(chuàng)建畫布:
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
cWidth = canvas.width,
cHeight = canvas.height;
然后繪制表盤,雖說不是處女座,但也要盡可能做到跟原圖上的一樣,那就是這個(gè)環(huán)形開口的角度是多少呢?請上ps來測一下:

嗯,136°,這個(gè)角度確實(shí)刁鉆,為了方便接下來的計(jì)算,那就約等于140°。那么一個(gè)分?jǐn)?shù)段的弧度就是:
var deg1 = Math.PI * 11 / 45
先把中間半透明的刻度層畫好:
ctx.save(); //中間刻度層 ctx.beginPath(); ctx.strokeStyle = 'rgba(255, 255, 255, .2)'; ctx.lineWidth = 10; ctx.arc(0, 0, 135, 0, 11 * deg0, false); ctx.stroke(); ctx.restore();
接著,畫6條刻度線,用for循環(huán)來實(shí)現(xiàn):
ctx.save(); // 刻度線
for (var i = 0; i < 6; i++) {
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = 'rgba(255, 255, 255, .3)';
ctx.moveTo(140, 0);
ctx.lineTo(130, 0);
ctx.stroke();
ctx.rotate(deg1);
}
ctx.restore();
同理,再把大刻度細(xì)分為5個(gè)小刻度:
ctx.save(); // 細(xì)分刻度線
for (i = 0; i < 25; i++) {
if (i % 5 !== 0){
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = 'rgba(255, 255, 255, .1)';
ctx.moveTo(140, 0);
ctx.lineTo(133, 0);
ctx.stroke();
}
ctx.rotate(deg1 / 5);
}
ctx.restore();
刻度到這里就ok了,還需要給刻度標(biāo)上文字和每個(gè)分?jǐn)?shù)段的信用級(jí)別,具體的參見代碼,因?yàn)楦潭葘?shí)現(xiàn)的原理差不多,就不啰嗦了?,F(xiàn)在最關(guān)鍵就是實(shí)現(xiàn)表盤上那個(gè)運(yùn)動(dòng)的點(diǎn)(不知道怎么稱呼,下文就叫它動(dòng)點(diǎn)),我們可以這樣想,它是個(gè)半徑很小的圓,只不過是畫在最外層環(huán)形軌道上圓,而圓在canvas上的實(shí)現(xiàn)方法是:
ctx.arc(x, y, radius, sAngle, eAngle, false);
我們只要控制x, y就能讓它動(dòng)起來,實(shí)現(xiàn)我們想要的效果。so,創(chuàng)建一個(gè)動(dòng)點(diǎn)對(duì)象:
function Dot() {
this.x = 0;
this.y = 0;
this.draw = function (ctx) {
ctx.save();
ctx.beginPath();
ctx.fillStyle = 'rgba(255, 255, 255, .7)';
ctx.arc(this.x, this.y, 3, 0, Math.PI * 2, false);
ctx.fill();
ctx.restore();
};
}
var dot = new Dot(),
dotSpeed = 0.03, //控制動(dòng)點(diǎn)的速度
angle = 0, //這個(gè)很關(guān)鍵,用來得到動(dòng)點(diǎn)的坐標(biāo)x, y
credit = 400; //信用最低分?jǐn)?shù)
如何得到dot的坐標(biāo)x, y呢?那就要用到傳說中三角函數(shù)了。

通過上圖我們可以得到
x = r * cos(angle), y = r * sin(angle)
在JavaScript中,dot的中心坐標(biāo)就變成了:
dot.x = radius * Math.cos(angle); //radius為最外層軌道的半徑值 dot.y = radius * Math.sin(angle);
接下來我們只要得到這個(gè)angle。這個(gè)通過弧度與分?jǐn)?shù)的比例關(guān)系就可以得到:
var aim = (score - 400) * deg1 / 100;
if (angle < aim) {
angle += dotSpeed;
}
dot.draw(ctx);
然后讓中間的信用分?jǐn)?shù)也能隨動(dòng)點(diǎn)的轉(zhuǎn)動(dòng)而變化,創(chuàng)建一個(gè)text(),為了使數(shù)字變化能和動(dòng)點(diǎn)保持一致,要根據(jù)動(dòng)點(diǎn)的速率來計(jì)算數(shù)字變化:
function text(process) {
ctx.save();
ctx.rotate(10 * deg0);
ctx.fillStyle = '#000';
ctx.font = '80px Microsoft yahei';
ctx.textAlign = 'center';
ctx.textBaseLine = 'top';
ctx.fillText(process, 0 ,10);
ctx.restore();
}
var textSpeed = Math.round(dotSpeed * 100 / deg1),
if (credit < score - textSpeed) {
credit += textSpeed;
} else if (credit >= score - textSpeed && credit < score) {
credit += 1; // 這里確保信用分?jǐn)?shù)最后停下來是我們輸入的分?jǐn)?shù)
}
text(credit);
最后這一切都逃不過讓window.requestAnimationFrame()來控制繪制動(dòng)畫和用ctx.clearRect(0, 0, cWidth, cHeight)來清除畫布。
寫的不好,大家將就著看,我相信大家理解代碼的能力一定強(qiáng)于理解我這些我自己都不知道說什么的文字。
好了,以上。
code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>芝麻信用儀表盤</title>
<style type="text/css">
html,
body {
width: 100%;
height: 100%;
margin: 0;
}
canvas {
border: 1px solid #eee;
position: relative;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: -webkit-linear-gradient(top, #0e83f5 0%, #21bdf6 100%);
background: -ms-linear-gradient(top, #0e83f5 0%, #21bdf6 100%);
background: -moz-linear-gradient(top, #0e83f5 0%, #21bdf6 100%);
background: linear-gradient(top, #0e83f5 0%, #21bdf6 100%);
}
</style>
<script type="text/javascript">
window.onload = function() {
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
cWidth = canvas.width,
cHeight = canvas.height,
score = canvas.attributes['data-score'].value,
stage = ['較差', '中等', '良好', '優(yōu)秀', '極好'],
radius = 150,
deg0 = Math.PI / 9,
deg1 = Math.PI * 11 / 45;
if(score < 400 || score > 900) {
alert('信用分?jǐn)?shù)區(qū)間:400~900');
} else {
var dot = new Dot(),
dotSpeed = 0.03,
textSpeed = Math.round(dotSpeed * 100 / deg1),
angle = 0,
credit = 400;
(function drawFrame() {
ctx.save();
ctx.clearRect(0, 0, cWidth, cHeight);
ctx.translate(cWidth / 2, cHeight / 2);
ctx.rotate(8 * deg0);
dot.x = radius * Math.cos(angle);
dot.y = radius * Math.sin(angle);
var aim = (score - 400) * deg1 / 100;
if(angle < aim) {
angle += dotSpeed;
}
dot.draw(ctx);
if(credit < score - textSpeed) {
credit += textSpeed;
} else if(credit >= score - textSpeed && credit < score) {
credit += 1;
}
text(credit);
ctx.save();
ctx.beginPath();
ctx.lineWidth = 3;
ctx.strokeStyle = 'rgba(255, 255, 255, .5)';
ctx.arc(0, 0, radius, 0, angle, false);
ctx.stroke();
ctx.restore();
window.requestAnimationFrame(drawFrame);
ctx.save(); //中間刻度層
ctx.beginPath();
ctx.strokeStyle = 'rgba(255, 255, 255, .2)';
ctx.lineWidth = 10;
ctx.arc(0, 0, 135, 0, 11 * deg0, false);
ctx.stroke();
ctx.restore();
ctx.save(); // 刻度線
for(var i = 0; i < 6; i++) {
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = 'rgba(255, 255, 255, .3)';
ctx.moveTo(140, 0);
ctx.lineTo(130, 0);
ctx.stroke();
ctx.rotate(deg1);
}
ctx.restore();
ctx.save(); // 細(xì)分刻度線
for(i = 0; i < 25; i++) {
if(i % 5 !== 0) {
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = 'rgba(255, 255, 255, .1)';
ctx.moveTo(140, 0);
ctx.lineTo(133, 0);
ctx.stroke();
}
ctx.rotate(deg1 / 5);
}
ctx.restore();
ctx.save(); //信用分?jǐn)?shù)
ctx.rotate(Math.PI / 2);
for(i = 0; i < 6; i++) {
ctx.fillStyle = 'rgba(255, 255, 255, .4)';
ctx.font = '10px Microsoft yahei';
ctx.textAlign = 'center';
ctx.fillText(400 + 100 * i, 0, -115);
ctx.rotate(deg1);
}
ctx.restore();
ctx.save(); //分?jǐn)?shù)段
ctx.rotate(Math.PI / 2 + deg0);
for(i = 0; i < 5; i++) {
ctx.fillStyle = 'rgba(255, 255, 255, .4)';
ctx.font = '10px Microsoft yahei';
ctx.textAlign = 'center';
ctx.fillText(stage[i], 5, -115);
ctx.rotate(deg1);
}
ctx.restore();
ctx.save(); //信用階段及評(píng)估時(shí)間文字
ctx.rotate(10 * deg0);
ctx.fillStyle = '#fff';
ctx.font = '28px Microsoft yahei';
ctx.textAlign = 'center';
if(score < 500) {
ctx.fillText('信用較差', 0, 40);
} else if(score < 600 && score >= 500) {
ctx.fillText('信用中等', 0, 40);
} else if(score < 700 && score >= 600) {
ctx.fillText('信用良好', 0, 40);
} else if(score < 800 && score >= 700) {
ctx.fillText('信用優(yōu)秀', 0, 40);
} else if(score <= 900 && score >= 800) {
ctx.fillText('信用極好', 0, 40);
}
ctx.fillStyle = '#80cbfa';
ctx.font = '14px Microsoft yahei';
ctx.fillText('評(píng)估時(shí)間:2016.11.06', 0, 60);
ctx.fillStyle = '#7ec5f9';
ctx.font = '14px Microsoft yahei';
ctx.fillText('BETA', 0, -60);
ctx.restore();
// ctx.save(); //最外層軌道
ctx.beginPath();
ctx.strokeStyle = 'rgba(255, 255, 255, .4)';
ctx.lineWidth = 3;
ctx.arc(0, 0, radius, 0, 11 * deg0, false);
ctx.stroke();
ctx.restore();
})();
}
function Dot() {
this.x = 0;
this.y = 0;
this.draw = function(ctx) {
ctx.save();
ctx.beginPath();
ctx.fillStyle = 'rgba(255, 255, 255, .7)';
ctx.arc(this.x, this.y, 3, 0, Math.PI * 2, false);
ctx.fill();
ctx.restore();
};
}
function text(process) {
ctx.save();
ctx.rotate(10 * deg0);
ctx.fillStyle = '#000';
ctx.font = '80px Microsoft yahei';
ctx.textAlign = 'center';
ctx.textBaseLine = 'top';
ctx.fillText(process, 0, 10);
ctx.restore();
}
};
</script>
</head>
<body>
<canvas id="canvas" width="400" height="700" data-score='724'></canvas>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript 動(dòng)態(tài)改變onclick事件觸發(fā)函數(shù)代碼
javascript 動(dòng)態(tài)改變onclick事件觸發(fā)函數(shù)代碼,需要的朋友可以參考下。2011-08-08
js+html獲取系統(tǒng)當(dāng)前時(shí)間
這篇文章主要為大家詳細(xì)介紹了javascript html獲取系統(tǒng)當(dāng)前時(shí)間,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
使用Object.defineProperty如何巧妙找到修改某個(gè)變量的準(zhǔn)確代碼位置
Object.defineProperty() 方法直接在一個(gè)對(duì)象上定義一個(gè)新屬性,或者修改一個(gè)已經(jīng)存在的屬性, 并返回這個(gè)對(duì)象。下面這篇文章主要給大家介紹了關(guān)于使用Object.defineProperty如何巧妙找到修改某個(gè)變量的準(zhǔn)確代碼位置的相關(guān)資料,需要的朋友可以參考下2018-11-11
JavaScript獲取移動(dòng)設(shè)備型號(hào)的實(shí)現(xiàn)代碼(JS獲取手機(jī)型號(hào)和系統(tǒng))
這篇文章主要介紹了JavaScript獲取移動(dòng)設(shè)備型號(hào)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-03-03
javascript解決小數(shù)的加減乘除精度丟失的方案
這篇文章主要介紹了javascript解決小數(shù)的加減乘除精度丟失的方案的相關(guān)資料以及JavaScript中關(guān)于丟失數(shù)字精度的問題的探討,非常的詳細(xì),需要的朋友可以參考下2016-05-05
Avalonjs雙向數(shù)據(jù)綁定與監(jiān)聽的實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了Avalonjs雙向數(shù)據(jù)綁定與監(jiān)聽的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-06-06

