From 2079ada1a96f8ff53f13601d6020c69687facaf7 Mon Sep 17 00:00:00 2001 From: hdx Date: Mon, 12 Aug 2024 17:34:50 +0800 Subject: [PATCH] =?UTF-8?q?canvas:=20=E8=B0=83=E6=95=B4=E5=B8=83=E5=B1=80?= =?UTF-8?q?=EF=BC=8C=E5=B0=86=E6=B5=8B=E8=AF=95=E7=BB=93=E6=9E=9C=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E5=88=B0=E5=BA=95=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/component/canvas/canvas.uvue | 103 ++++++++++++++--------------- 1 file changed, 49 insertions(+), 54 deletions(-) diff --git a/pages/component/canvas/canvas.uvue b/pages/component/canvas/canvas.uvue index 084621b5..e0dfd791 100644 --- a/pages/component/canvas/canvas.uvue +++ b/pages/component/canvas/canvas.uvue @@ -1,11 +1,27 @@ @@ -67,9 +62,9 @@ renderingContext: null as CanvasRenderingContext2D | null, canvasWidth: 0, canvasHeight: 0, - dataBase64: '', - taskId: 0, - lastTime: 0, + dataBase64: '', + taskId: 0, + lastTime: 0, frameCount: 0, // 仅测试 testCanvasContext: false, @@ -94,7 +89,7 @@ hidpi(this.canvas!); this.canvasWidth = this.canvas!.width; - this.canvasHeight = this.canvas!.height; + this.canvasHeight = this.canvas!.height; // #ifdef WEB context.toBlob((blob : Blob) => { @@ -113,11 +108,11 @@ // this.canvas = canvas; // this.canvasWidth = canvas.width; // this.canvasHeight = canvas.height; - }, - onUnload() { - if (this.taskId > 0) { - this.stopAnimationFrame() - } + }, + onUnload() { + if (this.taskId > 0) { + this.stopAnimationFrame() + } }, methods: { // #ifdef WEB @@ -130,7 +125,7 @@ canvasToDataURL() { this.dataBase64 = this.canvasContext!.toDataURL() }, - onCreateImage() { + onCreateImage() { this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight) let image = this.canvasContext!.createImage(); image.src = "../../static/logo.png" @@ -159,27 +154,27 @@ path2D.bezierCurveTo(x2, y2, x3, y3, x4, y4); } context.stroke(path2D); - }, - startAnimationFrame() { - this.taskId = this.canvasContext!.requestAnimationFrame((timestamp : number) => { - this.testFrameCount++ - this.updateFPS(timestamp) - this.startAnimationFrame() - }) - }, - stopAnimationFrame() { - this.canvasContext!.cancelAnimationFrame(this.taskId) - this.taskId = 0 - }, - updateFPS(timestamp : number) { - this.frameCount++ - if (timestamp - this.lastTime >= 1000) { - const timeOfFrame = (1000 / this.frameCount) - this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight) - this.renderingContext!.fillText(`${this.frameCount} / ${timeOfFrame.toFixed(3)}ms`, 10, 18) - this.frameCount = 0 - this.lastTime = timestamp - } + }, + startAnimationFrame() { + this.taskId = this.canvasContext!.requestAnimationFrame((timestamp : number) => { + this.testFrameCount++ + this.updateFPS(timestamp) + this.startAnimationFrame() + }) + }, + stopAnimationFrame() { + this.canvasContext!.cancelAnimationFrame(this.taskId) + this.taskId = 0 + }, + updateFPS(timestamp : number) { + this.frameCount++ + if (timestamp - this.lastTime >= 1000) { + const timeOfFrame = (1000 / this.frameCount) + this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight) + this.renderingContext!.fillText(`${this.frameCount} / ${timeOfFrame.toFixed(3)}ms`, 10, 18) + this.frameCount = 0 + this.lastTime = timestamp + } } } } -- GitLab