微信小程序canvas动态时钟
生活随笔
收集整理的這篇文章主要介紹了
微信小程序canvas动态时钟
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
canvas時鐘效果圖:
代碼:
wxml:
<view style='width:100%;height:{{canvasHeight}}px' catchtap='goCountdown'catchlongtap='touchstart' catchtouchend='touchend'><canvas canvas-id='clock' style='width:100%;height:{{canvasHeight}}px'></canvas> </view>JS
Page({data: {width: 0,height: 0,showMask: false},onLoad: function (options) {var that = this//獲取系統信息 wx.getSystemInfo({//獲取系統信息成功,將系統窗口的寬高賦給頁面的寬高 success: function (res) {that.width = res.windowWidththat.height = res.windowHeightthat.setData({canvasWidth: res.windowWidth * 0.9 * 0.52,canvasHeight: res.windowWidth * 0.9 * 0.52 * 0.9819,rightWidth: res.windowWidth * 0.9 * 0.47})}})},onReady: function () {this.drawClock();// 每40ms執行一次drawClock(),this.interval = setInterval(this.drawClock, 40);},// 所有的canvas屬性以及Math.sin,Math.cos()等涉及角度的參數都是用弧度表示// 時鐘drawClock: function () {let _this = this;const ctx = wx.createCanvasContext('clock');var height = this.height;var width = this.width;// 設置文字對應的半徑var R = this.data.canvasWidth / 5;ctx.save();// 把原點的位置移動到屏幕中間,及寬的一半,高的一半ctx.translate(this.data.canvasWidth / 1.83, this.data.canvasHeight / 1.83);// 畫外框function drawBackground() {ctx.setStrokeStyle('#4BB5C3');// 設置線條的粗細,單位pxctx.setLineWidth(8);// 開始路徑ctx.beginPath();// 運動一個圓的路徑// arc(x,y,半徑,起始位置,結束位置,false為順時針運動)ctx.arc(0, 0, R * 1.7, 0, 2 * Math.PI, false);ctx.closePath();// 描出點的路徑ctx.stroke();};// 畫時鐘數function drawHoursNum() {ctx.setFontSize(20);// 圓的起始位置是從3開始的,所以我們從3開始填充數字var hours = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2];hours.forEach(function (hour, i) {var rad = (2 * Math.PI / 12) * i;var x = R * Math.cos(rad);var y = R * Math.sin(rad);if (hour == 12) {ctx.fillText(hour, x - 11, y + 6);} else if (hour == 6) {ctx.fillText(hour, x - 5, y + 10);} else if (hour == 3) {ctx.fillText(hour, x, y + 8);} else if (hour == 9) {ctx.fillText(hour, x - 10, y + 8);}else {//ctx.fillText(hour, x - 6, y + 6);}})};// 畫數字對應的點function drawdots() {for (let i = 0; i < 60; i++) {var rad = 2 * Math.PI / 60 * i;var x = (R + 15) * Math.cos(rad);var y = (R + 15) * Math.sin(rad);ctx.beginPath();// 每5個點一個比較大if (i % 5 == 0) {ctx.arc(x, y, 2, 0, 2 * Math.PI, false);} else {// ctx.arc(x, y, 1, 0, 2 * Math.PI, false);}ctx.setFillStyle('black');ctx.fill();}ctx.closePath();}// 畫時針function drawHour(hour, minute) {ctx.setStrokeStyle('#000000');// 保存畫之前的狀態ctx.save();ctx.beginPath();// 根據小時數確定大的偏移var rad = 2 * Math.PI / 12 * hour;// 根據分鐘數確定小的偏移var mrad = 2 * Math.PI / 12 / 60 * minute;// 做旋轉ctx.rotate(rad + mrad);ctx.setLineWidth(4);// 設置線條結束樣式為圓ctx.setLineCap('round');// 時針向后延伸8個px;ctx.moveTo(0, 8);// 一開始的位置指向12點的方向,長度為R/2ctx.lineTo(0, -R / 2);ctx.stroke();ctx.closePath();// 返回畫之前的狀態ctx.restore();}// 畫分針function drawMinute(minute, second) {ctx.save();ctx.beginPath();// 根據分鐘數確定大的偏移var rad = 2 * Math.PI / 60 * minute;// 根據秒數確定小的偏移var mrad = 2 * Math.PI / 60 / 60 * second;ctx.rotate(rad + mrad);// 分針比時針細ctx.setLineWidth(3);ctx.setLineCap('round');ctx.moveTo(0, 10);// 一開始的位置指向12點的方向,長度為3 * R / 4ctx.lineTo(0, -3 * R / 4);ctx.stroke();ctx.closePath();ctx.restore();}// 畫秒針function drawSecond(second, msecond) {ctx.save();ctx.beginPath();// 根據秒數確定大的偏移var rad = 2 * Math.PI / 60 * second;// 1000ms=1s所以這里多除個1000var mrad = 2 * Math.PI / 60 / 1000 * msecond;ctx.rotate(rad + mrad);ctx.setLineWidth(2);ctx.setStrokeStyle('#4BB5C3');ctx.setLineCap('round');ctx.moveTo(0, 12);ctx.lineTo(0, -R);ctx.stroke();ctx.closePath();ctx.restore();}//畫出中間那個灰色的圓function drawDot() {ctx.beginPath();ctx.arc(0, 0, 4, 0, 2 * Math.PI, false);ctx.setFillStyle('#FFF9E6');ctx.setLineWidth(6);ctx.setStrokeStyle('#000000');ctx.stroke();ctx.fill();ctx.closePath();}//畫蒙層function drawMask() {ctx.restore();ctx.rect(0, 0, width * 0.5, _this.data.canvasHeight);ctx.setFillStyle('rgba(0,0,0,.2)')ctx.fill()}function Clock() {// 實時獲取各個參數var now = new Date();var hour = now.getHours();var minute = now.getMinutes()var second = now.getSeconds();var msecond = now.getMilliseconds();if (_this.data.showMask) {ctx.scale(0.98,0.98)}// 依次執行各個方法drawBackground();drawHoursNum();drawdots();drawSecond(second, msecond);drawHour(hour, minute);drawMinute(minute, second);drawDot();if (_this.data.showMask) {drawMask();}ctx.draw();}Clock();},goCountdown() {let _this = this;_this.setData({showMask: true})setTimeout(function () {_this.setData({showMask: false})wx.navigateTo({url: '/pages/countdown/countdown',})}, 200)},touchstart: function (e) {console.log(e)this.setData({showMask: true})},touchend: function (e) {this.setData({showMask: false})} })總結
以上是生活随笔為你收集整理的微信小程序canvas动态时钟的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三保障的内容是什么
- 下一篇: 鲁滨逊漂流记读后感100字