欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript canvas實現(xiàn)環(huán)形漸變進度條

 更新時間:2022年06月30日 10:11:17   作者:shuaizi96  
這篇文章主要為大家詳細介紹了JavaScript canvas實現(xiàn)環(huán)形漸變進度條,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近有個需求要做環(huán)形漸變色進度條,去網(wǎng)上找了半天沒找到合適的,大多數(shù)漸變色都是徑向漸變,所以自己用canvas寫了個環(huán)形漸變的圖:

這個漸變其實就是把圓環(huán)分成許多小塊分別繪制的,所以小塊分的越多,漸變色越均勻,但是當(dāng)圓環(huán)尺寸比較小的時候,邊緣特別毛糙,需要適當(dāng)減少份數(shù),代碼里是用unit 這個變量手動控制的,算是一個缺陷吧。

代碼在此:

<!DOCTYPE html>

<html>
? ? <head>
? ? ? ? <style>
? ? ? ? ? ? html,body,canvas{
? ? ? ? ? ? ? ? width:100%;
? ? ? ? ? ? ? ? height:100%;
? ? ? ? ? ? ? ? margin:0;
? ? ? ? ? ? }
? ? ? ? </style>
? ? </head>
? ? <body>
? ? ? ? <canvas id="canvas"></canvas>
? ? </body>
? ? <script>
? ? ? ? const canvas = document.getElementById('canvas');
? ? ? ? const W = document.body.offsetWidth;
? ? ? ? const H = document.body.offsetHeight;
? ? ? ? canvas.width = W;
? ? ? ? canvas.height = H;

? ? ? ? const ctx = canvas.getContext("2d");

? ? ? ? /*
? ? ? ? // percent:圓環(huán)展示的百分比(0~1)
? ? ? ? // startColor:開始顏色
? ? ? ? // endColor:結(jié)束顏色
? ? ? ? */
? ? ? ? function paint(percent,startColor,endColor){

? ? ? ? ? ? // 圓環(huán)起始位置,正下方
? ? ? ? ? ? let startAngle = Math.PI/2;
? ? ? ? ? ? // 圓環(huán)結(jié)束位置,一個整圓(Math.PI*2)乘以比例 + 起始位置
? ? ? ? ? ? let endAngle = ((Math.PI*2)*percent + startAngle);
? ? ? ? ? ? // 每次繪制的弧度單位,越小顏色分布越均勻,但圖形較小時邊緣越糙
? ? ? ? ? ? const unit = 0.2;
? ? ? ? ? ? // 根據(jù)最小弧度單位計算整個圓弧可以切成多少小份
? ? ? ? ? ? let division = parseInt((endAngle-startAngle)/unit,10);
? ? ? ? ? ? // 生成漸變色數(shù)組
? ? ? ? ? ? let gradient = new gradientColor(startColor,endColor,division);
? ? ? ? ? ??
? ? ? ? ? ? let start = startAngle;
? ? ? ? ? ? let end = start;
? ? ? ? ? ? for(let i=0; i<division; i++){
? ? ? ? ? ? ? ? ctx.beginPath();
? ? ? ? ? ? ? ? ctx.lineCap = "round";
? ? ? ? ? ? ? ? end = start+unit;
? ? ? ? ? ? ? ? ctx.lineWidth = 20;
? ? ? ? ? ? ? ? ctx.strokeStyle = gradient[i];
? ? ? ? ? ? ? ? ctx.arc(W/2,H/2,90,start,end);
? ? ? ? ? ? ? ? ctx.stroke();
? ? ? ? ? ? ? ? start+=unit;
? ? ? ? ? ? }
? ? ? ? }


? ? ? ? /*
? ? ? ? // startColor:開始顏色hex
? ? ? ? // endColor:結(jié)束顏色hex
? ? ? ? // step:幾個階級(幾步)
? ? ? ? */
? ? ? ? function gradientColor(startColor,endColor,step){
? ? ? ? ? ? startRGB = this.colorRgb(startColor);//轉(zhuǎn)換為rgb數(shù)組模式
? ? ? ? ? ? startR = startRGB[0];
? ? ? ? ? ? startG = startRGB[1];
? ? ? ? ? ? startB = startRGB[2];

? ? ? ? ? ? endRGB = this.colorRgb(endColor);
? ? ? ? ? ? endR = endRGB[0];
? ? ? ? ? ? endG = endRGB[1];
? ? ? ? ? ? endB = endRGB[2];

? ? ? ? ? ? sR = (endR-startR)/step;//總差值
? ? ? ? ? ? sG = (endG-startG)/step;
? ? ? ? ? ? sB = (endB-startB)/step;

? ? ? ? ? ? var colorArr = [];
? ? ? ? ? ? for(var i=0;i<step;i++){
? ? ? ? ? ? ? ? //計算每一步的hex值
? ? ? ? ? ? ? ? var hex = this.colorHex('rgb('+parseInt((sR*i+startR))+','+parseInt((sG*i+startG))+','+parseInt((sB*i+startB))+')');
? ? ? ? ? ? ? ? colorArr.push(hex);
? ? ? ? ? ? }
? ? ? ? ? ? return colorArr;
? ? ? ? }

? ? ? ? // 將hex表示方式轉(zhuǎn)換為rgb表示方式(這里返回rgb數(shù)組模式)
? ? ? ? gradientColor.prototype.colorRgb = function(sColor){
? ? ? ? ? ? var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
? ? ? ? ? ? var sColor = sColor.toLowerCase();
? ? ? ? ? ? if(sColor && reg.test(sColor)){
? ? ? ? ? ? ? ? if(sColor.length === 4){
? ? ? ? ? ? ? ? ? ? var sColorNew = "#";
? ? ? ? ? ? ? ? ? ? for(var i=1; i<4; i+=1){
? ? ? ? ? ? ? ? ? ? ? ? sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? sColor = sColorNew;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //處理六位的顏色值
? ? ? ? ? ? ? ? var sColorChange = [];
? ? ? ? ? ? ? ? for(var i=1; i<7; i+=2){
? ? ? ? ? ? ? ? ? ? sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return sColorChange;
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? return sColor;
? ? ? ? ? ? }
? ? ? ? };

? ? ? ? // 將rgb表示方式轉(zhuǎn)換為hex表示方式
? ? ? ? gradientColor.prototype.colorHex = function(rgb){
? ? ? ? ? ? var _this = rgb;
? ? ? ? ? ? var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
? ? ? ? ? ? if(/^(rgb|RGB)/.test(_this)){
? ? ? ? ? ? ? ? var aColor = _this.replace(/(?:(|)|rgb|RGB)*/g,"").split(",");
? ? ? ? ? ? ? ? var strHex = "#";
? ? ? ? ? ? ? ? for(var i=0; i<aColor.length; i++){
? ? ? ? ? ? ? ? ? ? var hex = Number(aColor[i]).toString(16);
? ? ? ? ? ? ? ? ? ? hex = hex<10 ? 0+''+hex :hex;// 保證每個rgb的值為2位
? ? ? ? ? ? ? ? ? ? if(hex === "0"){
? ? ? ? ? ? ? ? ? ? ? ? hex += hex;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? strHex += hex;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if(strHex.length !== 7){
? ? ? ? ? ? ? ? ? ? strHex = _this;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return strHex;
? ? ? ? ? ? }else if(reg.test(_this)){
? ? ? ? ? ? ? ? var aNum = _this.replace(/#/,"").split("");
? ? ? ? ? ? ? ? if(aNum.length === 6){
? ? ? ? ? ? ? ? ? ? return _this;
? ? ? ? ? ? ? ? }else if(aNum.length === 3){
? ? ? ? ? ? ? ? ? ? var numHex = "#";
? ? ? ? ? ? ? ? ? ? for(var i=0; i<aNum.length; i+=1){
? ? ? ? ? ? ? ? ? ? ? ? numHex += (aNum[i]+aNum[i]);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? return numHex;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? return _this;
? ? ? ? ? ? }
? ? ? ? }

? ? ? ??
? ? ? ? paint(0.9,'#ff6464','#3586ff');


? ? </script>
</html>

生成漸變色的代碼是網(wǎng)上找的,由于那個代碼好多地方都能找到,也弄不清誰是原創(chuàng),這里就不貼地址了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論