小程序canvas實(shí)現(xiàn)畫布半圓環(huán)
本文實(shí)例為大家分享了小程序canvas實(shí)現(xiàn)畫布半圓環(huán)的具體代碼,供大家參考,具體內(nèi)容如下
1、效果圖:

2、wxml:
<view class="fragrance_item">
? <view class="fragPos">
? ? <view class="fragNum">{{perfumePercent}}%</view>
? ? <view class="fragText">香水余量</view>
? </view>
? <view class="fragPsoReset">
? ? <view bindtap="resetTab" class="fragReset" hover-class="fragModel_hover">
? ? ? <image class="fragReset_icon" src="/static/image/reset.png" mode="widthFix"> </image>
? ? ? <view>復(fù)位</view>
? ? </view>
? </view>
? <view class="fragRing">
? ? <canvas id="perfumeBalance" type="2d" style="width: 220px; height: 120px;" ></canvas>
? </view>
</view>3、wxss:
.fragModel_hover{box-shadow: 0.2rem 0 0.3rem #8fd0e0, -0.2rem 0 0.3rem #58bfd1;}
.fragrance_item{position: relative;margin-left: 15px;margin-right: 15px;}
.fragPos{position: absolute;left: 50%; top: 50%;transform: translateX(-50%);text-align: center;}
.fragPos .fragNum{color:#32CD32;}
.fragPsoReset{position: absolute;right: 3px;top: 36px;text-align: center;}
.fragPsoReset .fragReset{display:inline-block;padding:8px 13px;background-color: rgba(50,80,80.68);border-radius: 15px;transition: all .3s ease-in;font-size: 12px;}
.fragPsoReset .fragReset_icon{width:22px;height:22px;}
.fragRing{width: 220px;height:120px;margin: auto;}4、js:
let perfumeNum = 1.5;
Page({
? data: {
? ? perfumePercent: 0, // 香水余量
? },
?
? /**
? ?* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
? ?*/
? onLoad: function (options) {
? ? let that = this;
? ? // 香水余量圓環(huán)
? ? const query = wx.createSelectorQuery()
? ? query.select('#perfumeBalance') .fields({ node: true, size: true }).exec((res) => {
? ? ? that.initRing(res);
? ? })
? },
? // b、香水余量
? initRing(res) {
? ? let that = this;
? ? const canvas = res[0].node
? ? const ctx = canvas.getContext('2d');
? ? const dpr = wx.getSystemInfoSync().pixelRatio // 設(shè)備像素比
? ? canvas.width = res[0].width * dpr
? ? canvas.height = res[0].height * dpr
? ? ctx.scale(dpr, dpr);
? ? var circleR = 220 / 2; // 畫布的一半,用來(lái)找中心點(diǎn)和半徑
? ? ctx.translate(0, 0); ? // 定義起始點(diǎn)
? ? function draw() {
? ? ? ctx.clearRect(0, 0, canvas.width, canvas.height);
? ? ? // 取色圓環(huán)
? ? ? ctx.beginPath();
? ? ? ctx.strokeStyle = '#fff';//transparent
? ? ? ctx.lineWidth = 12;
? ? ? ctx.lineCap='round' // 設(shè)置圓環(huán)端點(diǎn)的形狀
? ? ? ctx.arc(circleR, circleR-10, circleR - 35, Math.PI, 2 * Math.PI, false);
? ? ? ctx.stroke();
? ? ? ctx.closePath();
? ? ? console.log("香水余量")
? ? ? // 進(jìn)度_香水余量
? ? ? ctx.beginPath(); // 起始一條路徑,或重置當(dāng)前路徑
? ? ? ctx.lineWidth = 6; ?// 線條寬度
? ? ? ctx.lineCap='round' // 線條的結(jié)束端點(diǎn)樣式
? ? ? ctx.arc(circleR, circleR-10, circleR - 35, Math.PI,perfumeNum * Math.PI, false);
? ? ? ctx.strokeStyle = "#00CED1";//transparent
? ? ? ctx.stroke();
? ? ? canvas.requestAnimationFrame(draw);
? ? }
? ? draw();
? },
?
? // 復(fù)位
? resetTab(){
? ? let that = this;
? ? perfumeNum = 2; // 香水余量為100%
? ? that.setData({
? ? ? perfumePercent: 100,
? ? })
? },
})當(dāng)然,你弄一個(gè)定時(shí)器,讓 perfumeNum 的值一直 在 0 ~ 2 的范圍內(nèi)循環(huán)遞增或遞減,這個(gè)圓環(huán)的進(jìn)度條就動(dòng)起來(lái)了!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序使用picker實(shí)現(xiàn)時(shí)間和日期選擇框功能【附源碼下載】
這篇文章主要介紹了微信小程序使用picker實(shí)現(xiàn)時(shí)間和日期選擇框功能,結(jié)合實(shí)例形式分析了微信小程序picker組件進(jìn)行日期與時(shí)間選擇的相關(guān)操作技巧,并附帶源碼供讀者下載參考,需要的朋友可以參考下2017-12-12
js實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的幾種方法小結(jié)
下面小編就為大家?guī)?lái)一篇js實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的幾種方法小結(jié)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧2016-05-05
JavaScript 設(shè)計(jì)模式學(xué)習(xí) Factory
通過(guò)接口實(shí)現(xiàn)工廠,這是通過(guò)List方式顯示RSS 等實(shí)現(xiàn)代碼。2009-07-07
window.setInterval()方法的定義和用法及offsetLeft與style.left的區(qū)別
window.setInterval()方法可以按照指定的周期執(zhí)行來(lái)執(zhí)行一段程序。周期是以毫秒為單位的,本文給大家介紹window.setInterval()方法的定義和用法,感興趣的朋友參考下2015-11-11
Bootstrap基本樣式學(xué)習(xí)筆記之表格(2)
Bootstrap提供了一個(gè)清晰的創(chuàng)建表格的布局,這篇文章主要介紹了Bootstrap學(xué)習(xí)筆記之表格基本樣式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
RequireJS入門一之實(shí)現(xiàn)第一個(gè)例子
RequireJS由James Burke創(chuàng)建,他也是AMD規(guī)范的創(chuàng)始人。大家知道我們?yōu)槭裁匆獙W(xué)習(xí)requirejs嗎?帶著這個(gè)疑問(wèn)來(lái)學(xué)習(xí)本篇文章吧,感興趣的朋友一起學(xué)習(xí)RequireJS入門一之實(shí)現(xiàn)第一個(gè)例子吧2015-09-09

