提交 30b082d9 编写于 作者: H hdx

feat(ball): 增加可调参数

上级 f8da9f2a
<template> <template>
<view class="page-body"> <view class="page-body">
<canvas id="canvas" class="canvas"></canvas> <canvas id="canvas" class="canvas"></canvas>
<text class="fps">FPS: {{fpsString}}</text> <text class="fps">FPS: {{fpsString}}</text>
<view class="item">
<text class="item-label">Speed</text>
<text class="item-value">{{ballSpeed}}</text>
<view class="item-fill"></view>
<button size="mini" @click="lessClick('speed')">-</button>
<button size="mini" @click="plusClick('speed')">+</button>
</view>
<view class="item">
<text class="item-label">Layer</text>
<text class="item-value">{{ballLayer}}</text>
<view class="item-fill"></view>
<button size="mini" @click="lessClick('layer')">-</button>
<button size="mini" @click="plusClick('layer')">+</button>
</view>
<view class="item">
<text class="item-label">Inlayer</text>
<text class="item-value">{{ballInlayer}}</text>
<view class="item-fill"></view>
<button size="mini" @click="lessClick('inLayer')">-</button>
<button size="mini" @click="plusClick('inLayer')">+</button>
</view>
</view> </view>
</template> </template>
<script setup> <script setup>
let fpsString = ref("-/-ms") let fpsString = ref("-/-ms")
let ballSpeed = ref(3)
let ballLayer = ref(3)
let ballInlayer = ref(20)
class Ball { class Ball {
private width : number private width : number
...@@ -51,18 +75,18 @@ ...@@ -51,18 +75,18 @@
class BallAnimation { class BallAnimation {
private ctx : CanvasRenderingContext2D private ctx : CanvasRenderingContext2D
private ballList : Array<Ball> = [] private ballList : Array<Ball> = []
private speed = 3 private _speed = 3
private layer = 3 private _layer = 3
private ballInlayer = 20 private _ballInlayer = 20
private runningFlag : boolean = false private runningFlag : boolean = false
private _animateTaskId: number = 0 private _animateTaskId : number = 0
private frameCount = 0 private frameCount = 0
private lastTime = 0 private lastTime = 0
constructor(ctx : CanvasRenderingContext2D) { constructor(ctx : CanvasRenderingContext2D) {
this.ctx = ctx this.ctx = ctx
this.initBall() this.initBall()
this.ctx.fillStyle = '#007AFF' this.ctx.fillStyle = '#007AFF'
} }
...@@ -73,10 +97,10 @@ ...@@ -73,10 +97,10 @@
private initBall() { private initBall() {
const canvasWidth = this.ctx.canvas.offsetWidth; const canvasWidth = this.ctx.canvas.offsetWidth;
const canvasHeight = this.ctx.canvas.offsetHeight; const canvasHeight = this.ctx.canvas.offsetHeight;
for (let i = 0; i < this.layer; i++) { for (let i = 0; i < this._layer; i++) {
let radius = this.getDistance(canvasWidth / 2, canvasHeight / 2) / this.layer * i let radius = this.getDistance(canvasWidth / 2, canvasHeight / 2) / this._layer * i
for (let j = 0; j < this.ballInlayer; j++) { for (let j = 0; j < this._ballInlayer; j++) {
let deg = j * 2 * Math.PI / this.ballInlayer, let deg = j * 2 * Math.PI / this._ballInlayer,
sin = Math.sin(deg), sin = Math.sin(deg),
cos = Math.cos(deg), cos = Math.cos(deg),
x = radius * cos + canvasWidth / 2, x = radius * cos + canvasWidth / 2,
...@@ -88,42 +112,71 @@ ...@@ -88,42 +112,71 @@
} }
} }
private reset() {
this.ballList.length = 0
this.initBall()
}
public get speed() : number {
return this._speed
}
public set speed(value : number) {
this._speed = value
this.reset()
}
public get layer() : number {
return this._layer
}
public set layer(value : number) {
this._layer = value
this.reset()
}
public get inLayer() : number {
return this._ballInlayer
}
public set inLayer(value : number) {
this._ballInlayer = value
this.reset()
}
public animate() { public animate() {
this.ctx.clearRect(0, 0, this.ctx.canvas.offsetWidth, this.ctx.canvas.offsetHeight) this.ctx.clearRect(0, 0, this.ctx.canvas.offsetWidth, this.ctx.canvas.offsetHeight)
this.ballList.forEach((item) => { this.ballList.forEach((item) => {
item.move() item.move()
this.ctx.beginPath() this.ctx.beginPath()
this.ctx.arc(item.x, item.y, item.radius, 0, 2 * Math.PI) this.ctx.arc(item.x, item.y, item.radius, 0, 2 * Math.PI)
// this.ctx.ellipse(item.x, item.y, item.radius, item.radius, 0, 0, Math.PI * 2) // this.ctx.ellipse(item.x, item.y, item.radius, item.radius, 0, 0, Math.PI * 2)
this.ctx.fill() this.ctx.fill()
}) })
if (!this.runningFlag) { if (!this.runningFlag) {
return return
} }
this._animateTaskId = requestAnimationFrame((timestamp: number) => { this._animateTaskId = requestAnimationFrame((timestamp : number) => {
this.animate() this.animate()
this.updateFPS(timestamp) this.updateFPS(timestamp)
}) })
} }
updateFPS(timestamp : number) { updateFPS(timestamp : number) {
this.frameCount++ this.frameCount++
if (timestamp - this.lastTime >= 1000) { if (timestamp - this.lastTime >= 1000) {
const timeOfFrame = (1000 / this.frameCount) const timeOfFrame = (1000 / this.frameCount)
fpsString.value = `${this.frameCount} / ${timeOfFrame.toFixed(3)}ms` fpsString.value = `${this.frameCount} / ${timeOfFrame.toFixed(3)}ms`
this.frameCount = 0 this.frameCount = 0
this.lastTime = timestamp this.lastTime = timestamp
} }
} }
start() { start() {
cancelAnimationFrame(this._animateTaskId) cancelAnimationFrame(this._animateTaskId)
this.runningFlag = true this.runningFlag = true
this.animate() this.animate()
} }
stop() { stop() {
this.runningFlag = false this.runningFlag = false
cancelAnimationFrame(this._animateTaskId) cancelAnimationFrame(this._animateTaskId)
} }
...@@ -146,6 +199,32 @@ ...@@ -146,6 +199,32 @@
} }
}) })
let lessClick = (type : string) => {
if (type == 'speed') {
animation!.speed--;
ballSpeed.value = animation!.speed
} else if (type == 'layer') {
animation!.layer--;
ballLayer.value = animation!.layer
} else if (type == 'inLayer') {
animation!.inLayer--;
ballInlayer.value = animation!.inLayer
}
}
let plusClick = (type : string) => {
if (type == 'speed') {
animation!.speed++;
ballSpeed.value = animation!.speed
} else if (type == 'layer') {
animation!.layer++;
ballLayer.value = animation!.layer
} else if (type == 'inLayer') {
animation!.inLayer++;
ballInlayer.value = animation!.inLayer
}
}
onUnload(() => { onUnload(() => {
animation?.stop() animation?.stop()
animation = null animation = null
...@@ -163,6 +242,10 @@ ...@@ -163,6 +242,10 @@
<style> <style>
.page-body-wrapper { .page-body-wrapper {
text-align: center; text-align: center;
}
.page-body {
padding: 15px;
} }
.canvas { .canvas {
...@@ -170,10 +253,30 @@ ...@@ -170,10 +253,30 @@
height: 300px; height: 300px;
margin: auto; margin: auto;
background-color: #fff; background-color: #fff;
}
.fps {
margin-top: 30px;
margin-bottom: 20px;
}
.item {
flex-direction: row;
align-items: center;
margin-top: 10px;
} }
.fps { .item-label {
margin-left: 15px; width: 120px;
margin-top: 30px; }
.item-value {
margin-left: 5px;
color: cornflowerblue;
width: 50px;
}
.item-fill {
flex: 1;
} }
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册