Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
30b082d9
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
6005
Star
91
Fork
164
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
18
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
18
Issue
18
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
30b082d9
编写于
8月 22, 2024
作者:
H
hdx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(ball): 增加可调参数
上级
f8da9f2a
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
146 addition
and
43 deletion
+146
-43
pages/component/canvas/canvas/ball.uvue
pages/component/canvas/canvas/ball.uvue
+146
-43
未找到文件。
pages/component/canvas/canvas/ball.uvue
浏览文件 @
30b082d9
<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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录