Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
5a195061
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
6018
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看板
提交
5a195061
编写于
6月 27, 2024
作者:
H
hdx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(canvas): Web 平台 新增 canvas/ball 示例
上级
51cdc316
变更
4
展开全部
显示空白变更内容
内联
并排
Showing
4 changed file
with
1114 addition
and
158 deletion
+1114
-158
pages.json
pages.json
+9
-2
pages/component/canvas/ball.uvue
pages/component/canvas/ball.uvue
+184
-0
pages/component/canvas/canvas.uvue
pages/component/canvas/canvas.uvue
+914
-154
pages/tabBar/component.uvue
pages/tabBar/component.uvue
+7
-2
未找到文件。
pages.json
浏览文件 @
5a195061
...
@@ -422,12 +422,19 @@
...
@@ -422,12 +422,19 @@
"enablePullDownRefresh"
:
false
"enablePullDownRefresh"
:
false
}
}
},
},
//
#ifdef
APP-ANDROID
//
#ifdef
WEB
{
{
"path"
:
"pages/component/canvas/canvas"
,
"path"
:
"pages/component/canvas/canvas"
,
"style"
:
"style"
:
{
{
"navigationBarTitleText"
:
""
"navigationBarTitleText"
:
"canvas"
}
},
{
"path"
:
"pages/component/canvas/ball"
,
"style"
:
{
"navigationBarTitleText"
:
"ball"
}
}
},
},
//
#endif
//
#endif
...
...
pages/component/canvas/ball.uvue
0 → 100644
浏览文件 @
5a195061
<template>
<view class="page-body">
<canvas id="canvas" class="canvas"></canvas>
</view>
</template>
<script setup>
class Ball {
private width : number
private height : number
public x : number
public y : number
public vx : number
public vy : number
public radius : number = 5
constructor(w : number, h : number, x : number, y : number, vx : number, vy : number) {
this.width = w
this.height = h
this.x = x
this.y = y
this.vx = vx
this.vy = vy
}
move() {
this.x += this.vx
this.y += this.vy
// 边框反弹
if (this.x < this.radius) {
this.vx = Math.abs(this.vx)
return
}
if (this.x > this.width - this.radius) {
this.vx = -Math.abs(this.vx)
}
if (this.y < this.radius) {
this.vy = Math.abs(this.vy)
return
}
if (this.y > this.width - this.radius) {
this.vy = -Math.abs(this.vy)
}
}
}
class BallAnimation {
private ctx : CanvasRenderingContext2D
private ballList : Array<Ball> = []
private speed = 3
private layer = 3
private ballInlayer = 20
private interval : number = 0
private runningFlag : boolean = false
// #ifdef WEB
private _bindAnimate: Function = null
// #endif
constructor(ctx : CanvasRenderingContext2D) {
this.ctx = ctx
this.initBall()
this.ctx.fillStyle = '#007AFF'
// #ifdef WEB
this._bindAnimate = this.animate.bind(this)
// #endif
}
private getDistance(x : number, y : number) : number {
return Math.pow((Math.pow(x, 2) + Math.pow(y, 2)), 0.5)
}
private initBall() {
const canvasWidth = this.ctx.canvas.offsetWidth;
const canvasHeight = this.ctx.canvas.offsetHeight;
for (let i = 0; i < this.layer; i++) {
let radius = this.getDistance(canvasWidth / 2, canvasHeight / 2) / this.layer * i
for (let j = 0; j < this.ballInlayer; j++) {
let deg = j * 2 * Math.PI / this.ballInlayer,
sin = Math.sin(deg),
cos = Math.cos(deg),
x = radius * cos + canvasWidth / 2,
y = radius * sin + canvasHeight / 2,
vx = this.speed * cos,
vy = this.speed * sin
this.ballList.push(new Ball(canvasWidth, canvasHeight, x, y, vx, vy))
}
}
}
public animate() {
this.ctx.clearRect(0, 0, this.ctx.canvas.offsetWidth, this.ctx.canvas.offsetHeight)
this.ballList.forEach((item) => {
item.move()
this.ctx.beginPath()
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.fill()
})
// #ifdef APP
this.ctx.draw()
// #endif
// #ifdef WEB
if (!this.runningFlag) {
return
}
requestAnimationFrame(this._bindAnimate)
// #endif
}
start() {
// #ifdef WEB
cancelAnimationFrame(this._bindAnimate)
this.runningFlag = true
this.animate()
// #endif
// #ifdef APP
//Todo.. requestAnimationFrame
clearInterval(this.interval)
this.interval = setInterval(() => {
this.animate()
}, 17)
// #endif
}
stop() {
// #ifdef WEB
this.runningFlag = false
cancelAnimationFrame(this._bindAnimate)
// #endif
// #ifdef APP
//Todo.. requestAnimationFrame
clearInterval(this.interval)
// #endif
}
}
let animation : BallAnimation | null = null
onReady(() => {
let canvas = uni.getElementById("canvas") as UniCanvasElement
let canvasContext = canvas.getContext("2d");
if (canvasContext != null) {
const dpr = uni.getSystemInfoSync().pixelRatio
canvas.width = canvas.offsetWidth * dpr
canvas.height = canvas.offsetHeight * dpr
canvasContext.scale(dpr, dpr)
animation = new BallAnimation(canvasContext)
animation?.start()
} else {
console.log("canvas.getContext error!!")
}
})
onUnload(() => {
animation?.stop()
animation = null
})
onPageShow(() => {
animation?.start()
})
onPageHide(() => {
animation?.stop()
})
</script>
<style>
.page-body-wrapper {
text-align: center;
}
.canvas {
width: 300px;
height: 300px;
margin: auto;
background-color: #fff;
}
</style>
pages/component/canvas/canvas.uvue
浏览文件 @
5a195061
此差异已折叠。
点击以展开。
pages/tabBar/component.uvue
浏览文件 @
5a195061
...
@@ -194,13 +194,18 @@
...
@@ -194,13 +194,18 @@
] as Page[]
] as Page[]
},
},
// #endif
// #endif
// #ifdef
APP-ANDROID
// #ifdef
WEB
{
{
id: 'canvas',
id: 'canvas',
name: '画布',
name: '画布',
pages: [
pages: [
{
{
name: 'canvas'
name: 'canvas',
url: '/pages/component/canvas/canvas',
},
{
name: 'ball',
url: '/pages/component/canvas/ball',
}
}
] as Page[]
] as Page[]
},
},
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录