diff --git a/pages/component/canvas/canvas.test.js b/pages/component/canvas/canvas.test.js new file mode 100644 index 0000000000000000000000000000000000000000..d0f31240c2d61c9183b472ddad20afc586266f79 --- /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 34e44e9de9cc8c5b52952d2a854f4821fac20eb9..1c177d0aa77d0f8522bbe7c809f1d9cbd59a8674 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() {