校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > canvas環(huán)形倒計時組件的示例代碼

canvas環(huán)形倒計時組件的示例代碼

熱門標簽:四川點撥外呼系統(tǒng) 鎮(zhèn)江智能外呼系統(tǒng)有效果嗎 黃島區(qū)地圖標注 當涂高德地圖標注 電銷機器人電話用什么卡 南寧點撥外呼系統(tǒng)哪家公司做的好 云南大理400電話申請官方 成都智能外呼系統(tǒng)平臺 江蘇智能電銷機器人哪家好

本文介紹了canvas環(huán)形倒計時組件的示例代碼,分享給大家,具體如下:

效果如下圖一:

Canvas環(huán)形倒計時組件

Canvas環(huán)形倒計時是基于Canvas實現(xiàn)的倒計時,建議于移動端使用

Canvas環(huán)形倒計時 下載地址

一、如何使用

1. html代碼

ID屬性可隨意取名

<canvas id="canvas"></canvas>

2. 引入process.js文件

頁面引用

<script src="js/process.js"></script>

3. 初始化參數

實例化即可

<script>
    window.onload = function () {
        let ctd = new Countdown();
        ctd.init();
    };

</script>

二、settings參數說明

以下參數非必選項,可根據具體需求配置

window.onload = function () {
        let ctd = new Countdown();
        ctd.init({
            id: "canvas",         // ID,canvas一定要有ID屬性
            size: 130,            // 繪制圓形的最大尺寸,寬=高
            borderWidth: 4,       // 邊框寬度
            borderColor:"#fff",   // 邊框顏色
            outerColor:"#fff",    // 最外層底圓顏色
            scheduleColor:"#fff", // 進度條動畫顏色
            fontColor: "#fff",    // 字體顏色
            ringColor: "#ffc720", // 進度條環(huán)形顏色
            innerColor: "#4e84e5",// 最內圓底色
            fontSize: 50,
            time: 5
        });
    };

三、示例代碼

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body {
            background: #c2c1ce;
        }
        .container {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            width: 130px;
            height: 130px;
            text-align: center;
        }
    </style>
</head>
<body>
<div class="container">
    <canvas class="canvas" id="canvas"></canvas>
</div>
<script src="js/process.js"></script>
<script>
    window.onload = function () {
        let ctd = new Countdown();
        ctd.init();
    };
</script>
</body>
</html>

js

/**
 * Created by 譚瞎 on 2018/3/15.
 */

function Countdown() {
    // 設置默認參數
    this.settings = {
        id: "canvas",         // ID,canvas一定要有ID屬性
        size: 130,            // 繪制圓形的最大尺寸,寬=高
        borderWidth: 4,       // 邊框寬度
        borderColor:"#fff",   // 邊框顏色
        outerColor:"#fff",    // 最外層底圓顏色
        scheduleColor:"#fff", // 進度條動畫顏色
        fontColor: "#fff",    // 字體顏色
        ringColor: "#ffc720", // 進度條環(huán)形顏色
        innerColor: "#4e84e5",// 最內圓底色
        fontSize: 50,
        time: 5
    }
}

Countdown.prototype.init = function (opt) {
    this.obj = document.getElementById(this.settings.id);
    this.obj.width = this.settings.size;
    this.obj.height = this.settings.size;
    this.ctx = this.obj.getContext("2d");
    extend(this.settings, opt);
    this.countdown();
};

// 繪制底色
Countdown.prototype.drawBackground = function () {
    this.drawCircle(0, 360, 0, this.settings.outerColor);
};
// 繪制進度條動畫背景
Countdown.prototype.drawProcess = function () {
    this.drawCircle(0, 360, 4, this.settings.ringColor);
};

// 繪制倒計時
Countdown.prototype.drawInner = function () {
    this.drawCircle(0, 360, 23, this.settings.innerColor);
    this.strokeBorder(this.settings.borderWidth);
};

// 繪制進度條動畫
Countdown.prototype.drawAnimate = function () {
    // 旋轉的角度
    let deg = Math.PI / 180;
    let v = schedule * 360,
        startAng = -90,
        endAng = -90 + v;

    this.ctx.beginPath();
    this.ctx.moveTo(this.settings.size / 2, this.settings.size / 2);
    this.ctx.arc(this.settings.size / 2, this.settings.size / 2, this.settings.size / 2 -3, startAng * deg, endAng * deg, false);
    this.ctx.fillStyle = this.settings.scheduleColor;
    this.ctx.fill();
    this.ctx.closePath();

};
// 繪制邊框
Countdown.prototype.strokeBorder = function (borderWidth) {
    this.ctx.lineWidth = borderWidth;
    this.ctx.strokeStyle = this.settings.borderColor;
    this.ctx.stroke();
};
// 繪制文字
Countdown.prototype.strokeText = function (text) {
    this.ctx.textAlign = "center";
    this.ctx.textBaseline = "middle";
    this.ctx.font = this.settings.fontSize+"px"+ " microsoft yahei";
    this.ctx.fillStyle = this.settings.fontColor;
    this.ctx.fillText(text, this.settings.size / 2, this.settings.size / 2);
};
// 繪制圓
Countdown.prototype.drawCircle = function (startAng, endAng, border, fillColor) {
    let deg = Math.PI / 180;
    this.ctx.beginPath();
    this.ctx.arc(this.settings.size / 2, this.settings.size / 2, this.settings.size / 2 -border, startAng * deg, endAng * deg, false);
    this.ctx.fillStyle = fillColor;
    this.ctx.fill();
    this.ctx.closePath();
};
// 進度條動畫
Countdown.prototype.countdown = function () {
    let oldTime = +new Date();
    timer = setInterval(() => {
        let allMs = this.settings.time * 1000,// 如30*1000=30 000ms
            currentTime = +new Date();
        // 步長=(當前的時間-過去的時間)/總秒數
        schedule = (currentTime - oldTime) / allMs;
        this.schedule = schedule;

        this.drawAll(schedule);
        if (currentTime - oldTime >= allMs) {
            // 重繪
            this.drawBackground();
            this.drawProcess();
            this.drawAnimate();
            this.drawInner();
            this.strokeText(0);
            clearInterval(timer);
        }
    }, 100);
};

// 繪制所有
Countdown.prototype.drawAll = function (schedule) {
    schedule = schedule >= 1 ? 1 : schedule;
    let text = parseInt(this.settings.time * (1 - schedule)) + 1;
    // 清除畫布
    this.ctx.clearRect(0, 0, this.settings.size, this.settings.size);
    this.drawBackground();
    this.drawProcess();
    this.drawAnimate();
    this.drawInner();
    this.strokeText(text);
};

// 對象拷貝
function extend(obj1,obj2){
    for(let attr in obj2){
        obj1[attr] = obj2[attr];
    }
}

四、附加——canvas準備工作

canvas其實沒有那么玄乎,它不外乎是一個H5的標簽,跟其它HTML標簽如出一轍:

<canvas id="canvas"></canvas>  

注意最好在一開始的時候就給canvas設置好其寬高(若不設定寬高,瀏覽器會默認設置canvas大小為寬300、高100像素),而且不能使用css來設置(會被拉伸),建議直接寫于canvas標簽內部:

<canvas id="canvas" width="130" height="130"></canvas>

canvas本身沒有任何的繪圖能力,所有的繪圖工作都是通過js來實現(xiàn)的。通常我們在js通過getElementById來獲取要操作的canvas(這意味著得給canvas設個id):

var c = document.getElementById("canvas");
var ctx = c.getContext("2d");

1.準備好畫筆之后就可以開始繪圖了,環(huán)形其實就是半徑不同的同心圓,圓心坐標是(size/2,size/2), 先畫一個最大的白色背景底圓,半徑是size/2。

let deg = Math.PI / 180;
// beginPath()可以做到隔離路徑繪制效果的作用,防止之前的效果被污染。
ctx.beginPath();

// tcx.arc(圓心X,圓心Y,半徑,起始角度,結束角度,順逆時針);
ctx.arc(size / 2, size / 2, size / 2, 0* deg, 360 * deg, false);
ctx.fillStyle = "#fff";
ctx.fill();
ctx.closePath();

2.開始畫第二個黃色打底圓,圓心也是(size/2,size/2),只是半徑比白色底圓小4px,所以黃色底圓的半徑是(size/2-4)

let deg = Math.PI / 180;
// beginPath()可以做到隔離路徑繪制效果的作用,防止之前的效果被污染。
ctx.beginPath();

// tcx.arc(圓心X,圓心Y,半徑,起始角度,結束角度,順逆時針);
ctx.arc(size / 2, size / 2, size / 2-4, 0* deg, 360 * deg, false);
ctx.fillStyle = "#fff";
ctx.fill();
ctx.closePath();

3.開始畫藍色內圓,同理圓心為(size/2,size/2),半徑為(size-23),再給它加上4px的白色邊框。

let deg = Math.PI / 180;
// beginPath()可以做到隔離路徑繪制效果的作用,防止之前的效果被污染。
ctx.beginPath();

// tcx.arc(圓心X,圓心Y,半徑,起始角度,結束角度,順逆時針);
ctx.arc(size / 2, size / 2, size / 2-23, 0* deg, 360 * deg, false);
ctx.fillStyle = "#fff";
ctx.fill();
ctx.closePath();

// 白色邊框
ctx.lineWidth = 4;
ctx.strokeStyle = #fff;
ctx.stroke();

4.繪制文字,垂直居中

ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#fff";
// ctx.fillText(文字,相對畫布的X坐標,相對畫布的Y坐標)
ctx.fillText(30, size / 2, size / 2);

5.如何制作動畫?其實也是畫白色圓的過程,慢慢的覆蓋黃色進度條的過程,那么先把白色的圓畫出來出來,這個時候藍圓就會被白色的動畫圓給蓋住,這個時候最后畫藍圓就好了。

let deg = Math.PI / 180;
ctx.beginPath();
// tcx.arc(圓心X,圓心Y,半徑,起始角度,結束角度,順逆時針);
ctx.arc(size / 2, size / 2, size / 2-4, 0* deg, 360 * deg, false);
ctx.fillStyle = "#fff";
ctx.fill();
ctx.closePath();

6.比較簡單的繪畫過程完成了,接下來要將動畫和數字關聯(lián)起來,利用當前的最新時間-最開始的時間,再除總的時間可以得到一個關鍵的百分比,這個百分比決定數字的變化,以及白色動畫圓繪制的角度。

Countdown.prototype.countdown = function () {
    let oldTime = +new Date();// 過去的時間:1522136419291
    timer = setInterval(() => {
        let currentTime = +new Date();// 現(xiàn)在的時間:1522136419393
        let allMs = this.settings.time * 1000;// 總時間豪秒數:如30*1000=30 000ms
        schedule = (currentTime - oldTime) / allMs;// 繪制百分比:(1522136419393-1522136419291)/30000=0.0204
        this.schedule = schedule;
        this.drawAll(schedule);
        if (currentTime - oldTime >= allMs) {
            // 重繪
            this.drawBackground();
            this.drawProcess();
            this.drawAnimate();
            this.drawInner();
            this.strokeText(0);
            clearInterval(timer);
        }
    }, 10);
};

// 繪制所有
Countdown.prototype.drawAll = function (schedule) {
    schedule = schedule >= 1 ? 1 : schedule;
    let text = parseInt(this.settings.time * (1 - schedule)) + 1;
    // 清除畫布
    this.ctx.clearRect(0, 0, this.settings.size, this.settings.size);
    this.drawBackground();
    this.drawProcess();
    this.drawAnimate();
    this.drawInner();
    this.strokeText(text);
};

// 繪制進度條動畫
Countdown.prototype.drawAnimate = function () {
    // 旋轉的角度
    let deg = Math.PI / 180;
    let v = schedule * 360,
        startAng = -90,// 開始角度
        endAng = -90 + v;// 結束角度

    this.ctx.beginPath();
    this.ctx.moveTo(this.settings.size / 2, this.settings.size / 2);
    this.ctx.arc(this.settings.size / 2, this.settings.size / 2, this.settings.size / 2 - 3, startAng * deg, endAng * deg, false);
    this.ctx.fillStyle = this.settings.scheduleColor;
    this.ctx.fill();
    this.ctx.closePath();
};

面向過程版本

/**
    * 進度條動畫
    */
    countdown: function () {
        this.getSystemInfo().then(v => {
            // 自適應
            let width = v.windowWidth,
                size = width >= 414 ? 66 : 400 / 414 * 66;
            size = parseInt(size);
            size = size % 2 ? size + 1 : size;

            let maxtime =30,
                sTime = +new Date,

                temp = setInterval(() => {
                    let time = maxtime * 1000,
                        currentTime = +new Date,
                        schedule = (currentTime - sTime) / time;

                    this.drew(schedule, maxtime, size);

                    if (currentTime - sTime >= time) {
                        // 繪制文字
                        this.setData({
                            schedule: 0
                        });
                        clearInterval(temp);
                    };
                }, 100);

        });
    },

    /**
     * 繪制
     */
    drew: function (schedule, val, size) {
        size = size || 66;
        const _ts = this;
        schedule = schedule >= 1 ? 1 : schedule;

        let text = parseInt(val - val * schedule),
            r = size / 2,
            deg = Math.PI / 180;

        _ts.setData({
            width: size,
            height: size,
            schedule: text + 1
        });

        // 清除畫布
        ctx.clearRect(0, 0, size, size);

        // 繪制白色底
        ctx.beginPath();
        ctx.arc(r, r, r, 0 * deg, 360 * deg);
        ctx.fillStyle = 'rgba(255,255,255,1)';
        ctx.closePath();
        ctx.fill();

        // 繪制橙色
        ctx.beginPath();
        ctx.arc(r, r, r - 2, 0 * deg, 360 * deg);
        ctx.fillStyle = 'rgba(248,200,80,1)';
        ctx.closePath();
        ctx.fill();

        // 繪制白色進度條
        let v = schedule * 360;

        ctx.beginPath();
        ctx.moveTo(r, r);
        ctx.arc(r, r, r, -90 * deg, (-90 + v) * deg);

        ctx.fillStyle = 'rgba(255,255,255,1)';
        ctx.closePath();
        ctx.fill();

        // 中心藍色底
        ctx.beginPath();
        ctx.arc(r, r, r - 12, 0 * deg, 360 * deg);
        ctx.fillStyle = 'rgba(90,140,220,1)';
        ctx.closePath();
        ctx.fill();

        // 繪制文字
        ctx.strokeText();
        
        // 統(tǒng)一畫
        ctx.draw();
            
    },

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

標簽:廣西 淮安 酒泉 西寧 十堰 佳木斯 咸寧 南京

巨人網絡通訊聲明:本文標題《canvas環(huán)形倒計時組件的示例代碼》,本文關鍵詞  canvas,環(huán)形,倒計時,組件,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《canvas環(huán)形倒計時組件的示例代碼》相關的同類信息!
  • 本頁收集關于canvas環(huán)形倒計時組件的示例代碼的相關信息資訊供網民參考!
  • 推薦文章
    校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃
    3atv一区二区三区| 国产一区二区三区四| 99久久免费精品高清特色大片| 精品区一区二区| 国产在线观看一区二区| 久久久久免费观看| 成人的网站免费观看| 亚洲精品久久久久久国产精华液| 99国产麻豆精品| 亚洲综合一区二区| 777精品伊人久久久久大香线蕉| 日本麻豆一区二区三区视频| 欧美成人一级视频| 国产成人精品亚洲日本在线桃色| 中文字幕在线免费不卡| 色婷婷激情久久| 男女男精品视频网| 国产日韩一级二级三级| 欧洲亚洲精品在线| 麻豆精品在线播放| 国产精品乱码一区二区三区软件 | 中文字幕一区二区三区不卡| 色视频欧美一区二区三区| 丝袜美腿亚洲一区| 国产亚洲欧美日韩日本| 色综合色综合色综合| 久久国产生活片100| 中文一区一区三区高中清不卡| 91成人国产精品| 国产激情视频一区二区在线观看| 一区二区三区不卡视频 | 久久精品一区二区三区不卡牛牛| 成人a免费在线看| 手机精品视频在线观看| 国产亚洲精品7777| 欧美视频在线一区二区三区 | 日本精品视频一区二区| 精品一区二区三区欧美| 夜夜爽夜夜爽精品视频| 久久久久久久久久久电影| 欧美三区在线观看| 国产成人亚洲综合a∨婷婷| 日韩国产高清影视| 国产精品理伦片| 欧美成人一级视频| 欧美三级视频在线观看| 国产91精品在线观看| 日韩精品视频网| 亚洲色图欧美激情| 久久在线观看免费| 欧美精品v国产精品v日韩精品| 成人国产精品视频| 狠狠色丁香婷综合久久| 丝袜诱惑制服诱惑色一区在线观看 | 久久―日本道色综合久久| 欧日韩精品视频| 99在线精品免费| 国产精品一区不卡| 麻豆精品精品国产自在97香蕉 | 午夜a成v人精品| 国产精品国产成人国产三级| 久久日韩粉嫩一区二区三区| 制服丝袜成人动漫| 欧美三级电影精品| 欧美日韩视频专区在线播放| 99久久99精品久久久久久| 国产一本一道久久香蕉| 美女网站色91| 日本免费新一区视频| 日日欢夜夜爽一区| 日韩—二三区免费观看av| 五月天亚洲婷婷| 婷婷一区二区三区| 日韩电影免费在线观看网站| 午夜精品免费在线观看| 亚洲成av人片| 亚洲18女电影在线观看| 午夜欧美视频在线观看| 亚洲福利一二三区| 日韩黄色小视频| 蜜臀av一区二区在线观看 | www国产成人免费观看视频 深夜成人网| 欧美三级电影网| 欧美肥妇bbw| 日韩免费观看高清完整版| 欧美一卡二卡三卡四卡| 欧美不卡视频一区| 亚洲精品一区二区在线观看| 久久久久高清精品| 中文字幕欧美激情| 亚洲色图一区二区| 午夜国产精品影院在线观看| 婷婷国产v国产偷v亚洲高清| 天涯成人国产亚洲精品一区av| 日韩电影在线一区二区| 精品一区二区三区免费观看| 国产福利一区二区三区视频在线 | 国产98色在线|日韩| va亚洲va日韩不卡在线观看| 色香蕉久久蜜桃| 欧美日韩成人激情| 日韩欧美一级在线播放| 国产无人区一区二区三区| 亚洲柠檬福利资源导航| 国产做a爰片久久毛片| 国产麻豆精品95视频| 91在线观看下载| 欧美日韩国产美女| xnxx国产精品| 亚洲欧美激情一区二区| 日本aⅴ亚洲精品中文乱码| 国产成人在线电影| 欧美性xxxxxxxx| xnxx国产精品| 亚洲电影一级片| 高清不卡在线观看av| 欧美日韩一区二区在线观看| 久久亚洲欧美国产精品乐播| 樱花影视一区二区| 狠狠久久亚洲欧美| 在线观看精品一区| 久久久美女艺术照精彩视频福利播放| 亚洲男同性恋视频| 国产乱子轮精品视频| 欧美综合天天夜夜久久| 久久久国产一区二区三区四区小说| 亚洲欧美日韩国产综合| 久久激情五月激情| 色婷婷精品久久二区二区蜜臂av| 精品国产91亚洲一区二区三区婷婷| 亚洲天堂中文字幕| 国产一区二区调教| 911精品国产一区二区在线| 国产精品福利一区| 国产一区美女在线| 91精品一区二区三区在线观看| 亚洲欧洲成人av每日更新| 激情av综合网| 在线不卡免费欧美| 亚洲欧美国产77777| 国产精品一区二区免费不卡| 51精品久久久久久久蜜臀| 亚洲精品欧美专区| 成人毛片在线观看| 精品国产91久久久久久久妲己| 亚洲成a天堂v人片| 色婷婷综合五月| 国产精品乱人伦一区二区| 国产麻豆精品视频| 2023国产精华国产精品| 日韩在线一区二区三区| 欧美视频一区在线| 一区二区三区欧美在线观看| 成人中文字幕电影| 中文字幕欧美激情| 风间由美中文字幕在线看视频国产欧美| 欧美一级日韩免费不卡| 亚洲电影一级片| 欧美老年两性高潮| 一区二区三区欧美久久| 一本一道综合狠狠老| 亚洲欧洲av在线| 成人黄色软件下载| 国产精品免费人成网站| 成人ar影院免费观看视频| 中文字幕国产一区二区| 国产成人一区在线| 国产精品毛片无遮挡高清| 成人激情文学综合网| 综合网在线视频| 欧洲av在线精品| 日韩高清不卡一区二区| 日韩精品一区二区三区蜜臀| 激情六月婷婷久久| 久久久久九九视频| 成人精品小蝌蚪| 亚洲伦在线观看| 欧美精品免费视频| 久久99久久久久| 国产欧美日韩视频一区二区| 99久久er热在这里只有精品66| 亚洲色图在线播放| 欧美精品日韩精品| 国产一区二区调教| 1024国产精品| 欧美精品日韩精品| 国产电影一区在线| 亚洲人成网站在线| 欧美日韩国产免费一区二区| 久久国产三级精品| 国产欧美日韩精品在线| 欧美亚洲动漫制服丝袜| 欧美aa在线视频| 国产精品无码永久免费888| 91丝袜呻吟高潮美腿白嫩在线观看| 亚洲三级视频在线观看| 欧美喷潮久久久xxxxx| 日本欧美大码aⅴ在线播放| 久久久久久久久久电影| 日本乱人伦aⅴ精品|