vue canvas 手繪進度條動畫

vue+canvas 手繪進度條動畫(不喜勿噴)

明確步驟

1.劃一個矩形碑定,加上文字效果

2.判斷新值和現(xiàn)有值大小比較纷纫,并算出一份占矩形的長度

3.畫圖

直接上代碼

<template>
    <div id="index" class="index">
        <canvas id="canvas"></canvas>
        <br />
        總值
        <el-input
            v-model="total"
            style="width:100px"
            placeholder="請輸入內容"
        ></el-input>
        輸入值
        <el-input
            v-model="now"
            :max="total"
            style="width:100px"
            placeholder="請輸入內容"
        ></el-input>
        <el-button type="primary" @click="btnClick">主要按鈕</el-button>
    </div>
</template>

<script>
export default {
    props: {
        // 父輩向子輩傳參
    },
    name: "index",
    data() {
        return {
            cvs: "", //畫布
            ctx: {},
            total: 200, //總數(shù)
            now: "", //現(xiàn)數(shù)
            oldNumber: 0, //老值
            newNumber: 0, //新值
            proportion: "", //占比,
        };
    },
    created() {
        // 實例被創(chuàng)建之后執(zhí)行代碼
    },
    computed: {
        // 計算屬性
    },
    components: {
        // 組件的引用
    },
    methods: {
        // 方法
        btnClick() {
            this.proportion =
                (Number(this.now) / Number(this.total)) * Number(this.cvs.width); //比例:一份占多長
            this.newNumber = this.now;
            if (this.newNumber > this.total) {
                console.warn("超了");
            } else {
                if (Number(this.newNumber) > Number(this.oldNumber)) {
                    this.drivingChange(
                        Number(this.oldNumber),
                        Number(this.newNumber),
                        Number(this.now) / Number(this.total),
                        "++"
                    );
                    //增加
                } else if (Number(this.newNumber) < Number(this.oldNumber)) {
                    //減少
                    this.drivingChange(
                        Number(this.oldNumber),
                        Number(this.newNumber),
                        Number(this.now) / Number(this.total),
                        "--"
                    );
                } else {
                    console.warn("無變化");
                }
            }
        },
        drivingChange(oldVal, newVal, proportion, type) {
            //驅動改變(舊值,新值,百分比蕉世,加還是減方式)
            // console.warn(oldVal, newVal);
            var ss = oldVal;
            var that = this;
            var timer = setInterval(function() {
                that.clearRect();
                if (type == "++") {
                    that.dram(ss++, proportion);
                    if (ss > newVal) {
                        clearInterval(timer);
                    }
                } else {
                    that.dram(ss--, proportion);
                    if (ss < newVal) {
                        clearInterval(timer);
                    }
                }
            }, 10);
            this.oldNumber = newVal;
        },
        dram(i, proportion) {
            //畫圖
            this.ctx.fillStyle = "#7fecc1";
            /*矩形填充方法:fillRect(x,y,w,h)*/
            this.ctx.fillRect(51, 51, i - 2, 58); //i-2是為了去除掉邊框空隙的
            this.ctx.fillStyle = "white";
            this.ctx.textAlign = "center";
            this.ctx.textBaseline = "middle";
            this.ctx.font = "26px 黑體";
            this.ctx.fillText(i, 120, 70);
            this.ctx.font = "16px 黑體";
            this.ctx.fillText("基礎**", 120, 95);
            this.ctx.font = "30px 黑體";
            this.ctx.fillText(proportion * 100 + "%", 200, 80);
        },
        clearRect() {
            /*清理矩形方法:clearRect(x,y,w,h)*/
            this.ctx.clearRect(50, 50, 200, 60);
        },
        initCanvas() {
            //加載并繪制模型
            this.cvs = document.getElementById("canvas");
            //設置長寬
            this.cvs.width = 400;
            this.cvs.height = 400;
            //獲取畫筆
            this.ctx = this.cvs.getContext("2d");
            //===============================填充矩形方法
            /*填充矩形方法:fillRect(x,y,w,h)*/
            this.ctx.fillStyle = "#7fecc1";
            //===============================填充矩形方法

            //===============================描邊矩形
            /*描邊矩形方法:strokeRect(x,y,w,h)*/
            this.ctx.strokeStyle = "#7fecc1";
            this.ctx.lineWidth = 5;
            this.ctx.strokeRect(50, 50, 200, 60);
            //===============================描邊矩形

            //===============================文字
            this.ctx.fillStyle = "white";
            this.ctx.textAlign = "center";
            this.ctx.textBaseline = "middle";
            this.ctx.font = "26px 黑體";
            this.ctx.fillText("00", 120, 70);
            this.ctx.font = "16px 黑體";
            this.ctx.fillText("基礎**", 120, 95);
            this.ctx.font = "30px 黑體";
            this.ctx.fillText("00%", 200, 80);
            //===============================文字
        },
    },
    mounted() {
        // 頁面進入時加載內容
        this.initCanvas();
    },
    watch: {
        // 監(jiān)測變化
    },
};
</script>
<style scoped lang="scss">
#canvas {
    background-color: antiquewhite;
}
</style>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末遍烦,一起剝皮案震驚了整個濱河市婿崭,隨后出現(xiàn)的幾起案子拨拓,更是在濱河造成了極大的恐慌,老刑警劉巖氓栈,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件渣磷,死亡現(xiàn)場離奇詭異,居然都是意外死亡授瘦,警方通過查閱死者的電腦和手機醋界,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來提完,“玉大人形纺,你說我怎么就攤上這事⊥叫溃” “怎么了逐样?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長打肝。 經(jīng)常有香客問我脂新,道長,這世上最難降的妖魔是什么粗梭? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任争便,我火速辦了婚禮,結果婚禮上断医,老公的妹妹穿的比我還像新娘滞乙。我一直安慰自己,他們只是感情好鉴嗤,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布斩启。 她就那樣靜靜地躺著,像睡著了一般躬窜。 火紅的嫁衣襯著肌膚如雪浇垦。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天荣挨,我揣著相機與錄音,去河邊找鬼朴摊。 笑死默垄,一個胖子當著我的面吹牛,可吹牛的內容都是我干的甚纲。 我是一名探鬼主播口锭,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了鹃操?” 一聲冷哼從身側響起韭寸,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎荆隘,沒想到半個月后恩伺,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡椰拒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年晶渠,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片燃观。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡褒脯,死狀恐怖,靈堂內的尸體忽然破棺而出缆毁,到底是詐尸還是另有隱情番川,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布脊框,位于F島的核電站颁督,受9級特大地震影響,放射性物質發(fā)生泄漏缚陷。R本人自食惡果不足惜适篙,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望箫爷。 院中可真熱鬧嚷节,春花似錦、人聲如沸虎锚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽窜护。三九已至效斑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間柱徙,已是汗流浹背缓屠。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留护侮,地道東北人敌完。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像羊初,于是被迫代替她去往敵國和親滨溉。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內容