위 챗 애플 릿 은 canvas 로 시 계 를 그립 니 다.
아 날로 그 시계
canvas 를 이용 하여 시 계 를 그립 니 다.시 계 는 시간 과 시스템 시간 이 일치 하고 눈금 은 24 시간 제 를 12 시간 제로 바 꿉 니 다.중심 원,바깥쪽 큰 원,분침,시침,초침 을 각각 그 려 야 합 니 다.
효과 도 는 다음 과 같다.
코드 는 다음 과 같 습 니 다:
index.wxml
<canvas canvas-id="myCanvas" class="mycanvas"></canvas>
index.wxss
/**index.wxss**/
.mycanvas {
width: 100%;
height: 100%;
position: fixed;
}
index.js
Page({
width: 0, //
height: 0, //
onLoad: function () {
//
wx.getSystemInfo({
// ,
success: res => {
// console.log(res)
this.width = res.windowWidth
this.height = res.windowHeight
}
})
},
timer: null, //
onReady: function(){
// ctx
var ctx = wx.createCanvasContext('myCanvas')
// ,
// : = *Math.PI / 180
const D6 = 6 * Math.PI / 180
const D30 = 30 * Math.PI / 180
const D90 = 90 * Math.PI / 180
//
var width = this.width, height = this.height
// , 30px
var radius = width / 2 -30
//
draw()
this.timer = setInterval(draw, 1000)
//
function draw(){
//
ctx.translate(width / 2, height / 2)
//
drawClock(ctx,radius)
//
drawHand(ctx, radius)
//
ctx.draw()
}
//
function drawClock(ctx, radius){
//
// radius 2px
ctx.setLineWidth(2) //
ctx.beginPath() //
ctx.arc(0, 0, radius, 0, 2 * Math.PI, true)
ctx.stroke() //
//
// 8px 1px
ctx.setLineWidth(1) //
ctx.beginPath() //
ctx.arc(0, 0, 8, 0, 2 * Math.PI, true)
ctx.stroke() //
// 5px
ctx.setLineWidth(5)
for (var i = 0; i < 12; ++i){
// ( )
// 12 , 12 , 30
ctx.rotate(D30) // 360 / 12 = 30
ctx.beginPath()
ctx.moveTo(radius, 0)
ctx.moveTo(radius - 15, 0) // 15px
ctx.stroke()
}
// , 1px
ctx.setLineWidth(1)
for(var i = 0; i < 60; ++i){
// 60 , 60 60 , 6
ctx.rotate(D6)
ctx.beginPath()
ctx.moveTo(radius, 0)
ctx.lineTo(radius - 10, 0) // 10px
ctx.stroke()
}
//
ctx.setFontSize(20) //
ctx.textBaseline = 'middle' //
// r
var r = radius - 30
for(var i = 1; i <= 12; ++i){
//
var x = r * Math.cos(D30 * i - D90)
var y = r * Math.sin(D30 * i - D90)
if(i > 10){ // 11 12
// fillText( , x , y )
ctx.fillText(i, x - 12, y)
} else {
ctx.fillText(i, x-6, y)
}
}
}
//
function drawHand(ctx, radius){
var t = new Date() //
var h = t.getHours() //
var m = t.getMinutes() //
var s = t.getSeconds() //
h = h > 12 ? h -12 :h // 24 12
// , 90 , 12
ctx.rotate(-D90)
//
ctx.save() //
//
// 30 * h
// , h + m / 60 + s / 3600
ctx.rotate(D30 * (h + m / 60 + s / 3600))
ctx.setLineWidth(6)
ctx.beginPath()
ctx.moveTo(-20, 0)
ctx.lineTo(radius / 2.6, 0)
ctx.stroke()
ctx.restore()
//
ctx.save()
ctx.rotate(D6 * (m + s / 60))
ctx.setLineWidth(4)
ctx.beginPath()
ctx.moveTo(-20, 0)
ctx.lineTo(radius / 1.8, 0)
ctx.stroke()
ctx.restore()
//
ctx.save()
ctx.rotate(D6 * s)
ctx.setLineWidth(2)
ctx.beginPath()
ctx.moveTo(-20, 0)
ctx.lineTo(radius / 1.6, 0)
ctx.stroke()
ctx.restore()
}
}
})
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Jetpack Compose를 사용한 커스텀 컴포저블첫 번째 기사 시리즈에서는 Jetpack Compose에서 맞춤 보기를 만드는 방법에 대해 이야기하고 싶습니다. Labeled Ranged Slider의 예에서는 완전히 맞춤설정된 컴포저블을 만드는 데 필요한 단계를...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.