提交 937b99f0 编写于 作者: H hdx

feat(ball): 调整调用方式为 createCanvasContextAsync

上级 3fd8c09d
...@@ -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.
先完成此消息的编辑!
想要评论请 注册