From 65c59af08ff3fa38a2a4d40ca6c4d83d51c9c6c9 Mon Sep 17 00:00:00 2001 From: hdx Date: Wed, 3 Jul 2024 10:50:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(canvas):=20=E5=A2=9E=E5=8A=A0=20toBlob/toD?= =?UTF-8?q?ataURL=20=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/component/canvas/canvas.test.js | 20 ++++++++++++++++++++ pages/component/canvas/canvas.uvue | 27 ++++++++++++++++++--------- 2 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 pages/component/canvas/canvas.test.js diff --git a/pages/component/canvas/canvas.test.js b/pages/component/canvas/canvas.test.js new file mode 100644 index 00000000..d0f31240 --- /dev/null +++ b/pages/component/canvas/canvas.test.js @@ -0,0 +1,20 @@ +let page + +beforeAll(async () => { + page = await program.reLaunch('/pages/component/canvas/canvas') + await page.waitFor(2000) +}) + +describe('Canvas.uvue', () => { + it('toBlob', async () => { + if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('web')) { + const { + testToBlobResult, + testToDataURLResult + } = await page.data() + + expect(testToBlobResult).toBe(true) + expect(testToDataURLResult).toBe(true) + } + }) +}) diff --git a/pages/component/canvas/canvas.uvue b/pages/component/canvas/canvas.uvue index 34e44e9d..1c177d0a 100644 --- a/pages/component/canvas/canvas.uvue +++ b/pages/component/canvas/canvas.uvue @@ -33,6 +33,9 @@ canvasWidth: 0, canvasHeight: 0, image: null as Image | null, + // 仅测试 + testToBlobResult: false, + testToDataURLResult: false } }, onReady() { @@ -41,6 +44,13 @@ hidpi(canvas); this.canvasWidth = this.canvasContext!.canvas.width; this.canvasHeight = this.canvasContext!.canvas.height; + + // #ifdef WEB + canvas.toBlob((blob : Blob) => { + this.testToBlobResult = (blob.size > 0 && blob.type == 'image/jpeg') + }, 'image/jpeg', 0.95) + this.testToDataURLResult = canvas.toDataURL().startsWith('data:image/png;base64') + // #endif }, methods: { handleCanvasButton(name : string) { @@ -630,15 +640,14 @@ context.restore() }, restore() { - const context = this.canvasContext! - - ;[3, 2, 1].forEach((item) => { - context.save() - context.beginPath() - context.scale(item, item) - context.rect(10, 10, 100, 100) - context.stroke() - context.restore() + const context = this.canvasContext!; + [3, 2, 1].forEach((item) => { + context.save() + context.beginPath() + context.scale(item, item) + context.rect(10, 10, 100, 100) + context.stroke() + context.restore() }) }, drawImageLocal() { -- GitLab