小程序canvas實現畫佈半圓環
本文實例為大傢分享瞭小程序canvas實現畫佈半圓環的具體代碼,供大傢參考,具體內容如下
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>復位</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, // 香水餘量 }, /** * 生命周期函數--監聽頁面加載 */ onLoad: function (options) { let that = this; // 香水餘量圓環 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 // 設備像素比 canvas.width = res[0].width * dpr canvas.height = res[0].height * dpr ctx.scale(dpr, dpr); var circleR = 220 / 2; // 畫佈的一半,用來找中心點和半徑 ctx.translate(0, 0); // 定義起始點 function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); // 取色圓環 ctx.beginPath(); ctx.strokeStyle = '#fff';//transparent ctx.lineWidth = 12; ctx.lineCap='round' // 設置圓環端點的形狀 ctx.arc(circleR, circleR-10, circleR - 35, Math.PI, 2 * Math.PI, false); ctx.stroke(); ctx.closePath(); console.log("香水餘量") // 進度_香水餘量 ctx.beginPath(); // 起始一條路徑,或重置當前路徑 ctx.lineWidth = 6; // 線條寬度 ctx.lineCap='round' // 線條的結束端點樣式 ctx.arc(circleR, circleR-10, circleR - 35, Math.PI,perfumeNum * Math.PI, false); ctx.strokeStyle = "#00CED1";//transparent ctx.stroke(); canvas.requestAnimationFrame(draw); } draw(); }, // 復位 resetTab(){ let that = this; perfumeNum = 2; // 香水餘量為100% that.setData({ perfumePercent: 100, }) }, })
當然,你弄一個定時器,讓 perfumeNum 的值一直 在 0 ~ 2 的范圍內循環遞增或遞減,這個圓環的進度條就動起來瞭!
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- JavaScript+Canvas實現帶跳動效果的粒子動畫
- JavaScript canvas實現刮刮樂案例
- 微信小程序使用canvas繪制鐘表
- 微信小程序canvas實現環形漸變
- 微信小程序實現滑動驗證拼圖