Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
937b99f0
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看板
提交
937b99f0
编写于
11月 02, 2024
作者:
H
hdx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(ball): 调整调用方式为 createCanvasContextAsync
上级
3fd8c09d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
25 addition
and
20 deletion
+25
-20
pages/component/canvas/canvas/ball.uvue
pages/component/canvas/canvas/ball.uvue
+25
-20
未找到文件。
pages/component/canvas/canvas/ball.uvue
浏览文件 @
937b99f0
...
@@ -70,7 +70,8 @@
...
@@ -70,7 +70,8 @@
}
}
class BallAnimation {
class BallAnimation {
private ctx : CanvasRenderingContext2D
private ctx : CanvasRenderingContext2D
private canvasContext : CanvasContext
private ballList : Array<Ball> = []
private ballList : Array<Ball> = []
private _speed = 3
private _speed = 3
private _layer = 3
private _layer = 3
...
@@ -81,8 +82,9 @@
...
@@ -81,8 +82,9 @@
private frameCount = 0
private frameCount = 0
private lastTime = 0
private lastTime = 0
constructor(ctx : CanvasRenderingContext2D) {
constructor(canvasContext : CanvasContext) {
this.ctx = ctx
this.canvasContext = canvasContext
this.ctx = canvasContext.getContext('2d') as CanvasRenderingContext2D
this.initBall()
this.initBall()
this.ctx.fillStyle = '#007AFF'
this.ctx.fillStyle = '#007AFF'
}
}
...
@@ -151,7 +153,7 @@
...
@@ -151,7 +153,7 @@
if (!this.runningFlag) {
if (!this.runningFlag) {
return
return
}
}
this._animateTaskId = requestAnimationFrame((timestamp : number) => {
this._animateTaskId =
this.canvasContext.
requestAnimationFrame((timestamp : number) => {
this.animate()
this.animate()
this.updateFPS(timestamp)
this.updateFPS(timestamp)
})
})
...
@@ -168,32 +170,35 @@
...
@@ -168,32 +170,35 @@
}
}
start() {
start() {
cancelAnimationFrame(this._animateTaskId)
this.canvasContext.
cancelAnimationFrame(this._animateTaskId)
this.runningFlag = true
this.runningFlag = true
this.animate()
this.animate()
}
}
stop() {
stop() {
this.runningFlag = false
this.runningFlag = false
cancelAnimationFrame(this._animateTaskId)
this.canvasContext.
cancelAnimationFrame(this._animateTaskId)
}
}
}
}
let animation : BallAnimation | null = null
let animation : BallAnimation | null = null
onReady(() => {
onReady(() => {
let canvas = uni.getElementById("canvas") as UniCanvasElement
uni.createCanvasContextAsync({
let canvasContext = canvas.getContext("2d");
id: 'canvas',
if (canvasContext != null) {
// component: this,
const dpr = uni.getDeviceInfo().devicePixelRatio ?? 1;
success: (context : CanvasContext) => {
canvas.width = canvas.offsetWidth * dpr
const renderingContext = context.getContext('2d')!;
canvas.height = canvas.offsetHeight * dpr
const canvas = renderingContext!.canvas;
canvasContext.scale(dpr, dpr)
const dpr = uni.getDeviceInfo().devicePixelRatio ?? 1;
animation = new BallAnimation(canvasContext)
canvas.width = canvas.offsetWidth * dpr
animation?.start()
canvas.height = canvas.offsetHeight * dpr
} else {
renderingContext.scale(dpr, dpr)
console.log("canvas.getContext error!!")
}
animation = new BallAnimation(context)
animation?.start()
}
})
})
})
let lessClick = (type : string) => {
let lessClick = (type : string) => {
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录