如何繪制圓角環(huán)形圖形
發(fā)表時間:2023-12-25 來源:明輝站整理相關軟件相關文章人氣:
[摘要]在很多進度條的形狀上面的選擇,大家都會選擇環(huán)形圖。那么今天就來教大家怎么用canvas怎么繪制圓角環(huán)形圖,以及進度條模糊的解決方案,希望對你有所幫助* @param {type} radius 圓環(huán)半徑* @param {type} lineWidth 圓環(huán)寬度* @param {type} st...
在很多
進度條的形狀上面的選擇,大家都會選擇環(huán)形圖。那么今天就來教大家怎么用
canvas怎么繪制圓角環(huán)形圖,以及進度條模糊的解決方案,希望對你有所幫助
* @param {type} radius 圓環(huán)半徑
* @param {type} lineWidth 圓環(huán)寬度
* @param {type} strokeStyle 默認背景
* @param {type} fillStyleArray 數組,圓環(huán)色塊顏色
* @param {type} capType 類型:round是圓角,square正方形頂帽,butt是正常
* @param {type} percentArray ,數字,每個占據的百分比
* @param {type} startAngle 開始的角度
* @param {type} criclex,cricley 圓心坐標,一般是canvas的一半,例如:canvas給的寬度是250,高度是250,那么criclex是125
使用方法
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var ring = new Ring("80", "25", "#ccc", ["#a1b91d", "#e9636a", "#e7ba21"], "round");
ring.drawRing(ctx, 2 * Math.PI / 3, [20, 50, 30],125,125);//占據的百分比分別是20%,50%,30%
源代碼
源代碼很簡單,歡迎大家擴展!
function Circle(radius, lineWidth, strokeStyle, fillStyleArray, capType) {
this.radius = radius; // 圓環(huán)半徑
this.lineWidth = lineWidth; // 圓環(huán)邊的寬度
this.strokeStyle = strokeStyle; //邊的顏色
this.fillStyle = fillStyleArray; //填充色
this.lineCap = capType;}Circle.prototype.draw = function (ctx,criclex,cricley) {
ctx.beginPath();
ctx.arc(criclex, cricley, this.radius, 0, Math.PI * 2, true); // 坐標為90的圓,這里起始角度是0,結束角度是Math.PI*2
ctx.lineWidth = this.lineWidth;
ctx.strokeStyle = this.strokeStyle;
ctx.stroke(); // 這里用stroke畫一個空心圓,想填充顏色的童鞋可以用fill方法};function Ring(radius, lineWidth, strokeStyle, fillStyleArray, capType) {
Circle.call(this, radius, lineWidth, strokeStyle, fillStyleArray, capType);}Ring.prototype = Object.create(Circle.prototype);Ring.prototype.drawRing = function (ctx, startAngle, percentArray ,criclex,cricley) {
startAngle = startAngle 3 * Math.PI / 2;
percentArray = percentArray [];
this.draw(ctx,criclex,cricley); // 調用Circle的draw方法畫圈圈
var _this = this;
// angle
percentArray.forEach(function (item, index) {
ctx.beginPath();
var anglePerSec = 2 * Math.PI / (100 / item); // 藍色的弧度
ctx.arc(criclex, cricley, _this.radius, startAngle, startAngle + anglePerSec, false); //這里的圓心坐標要和cirle的保持一致
startAngle = startAngle + anglePerSec;
ctx.strokeStyle = _this.fillStyle[index];
ctx.lineCap = _this.lineCap;
ctx.stroke();
ctx.closePath();
})
//小圓圈覆蓋
ctx.beginPath();
ctx.arc(criclex, cricley, _this.radius, startAngle, startAngle, false); //這里的圓心坐標要和cirle的保持一致
ctx.strokeStyle = _this.fillStyle[0];
ctx.lineCap = _this.lineCap;
ctx.stroke();
ctx.closePath();}
相信看了這些案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
相關閱讀:
CSS的編碼怎么轉換
CSS3怎么制作蝴蝶飛舞的動畫
css3怎么實現圖片封面展示的動畫
以上就是怎么繪制圓角環(huán)形圖形的詳細內容,更多請關注php中文網其它相關文章!
網站建設是一個廣義的術語,涵蓋了許多不同的技能和學科中所使用的生產和維護的網站。