diff --git a/pages/component/canvas/canvas.test.js b/pages/component/canvas/canvas.test.js index 890fb8368832b61f71bc141fb8b3a5ff4f74d1ed..6b06cef96c7a70110bc121f1e8f32fc6e18a0d0c 100644 --- a/pages/component/canvas/canvas.test.js +++ b/pages/component/canvas/canvas.test.js @@ -30,13 +30,19 @@ describe('Canvas.uvue', () => { }) it("测试同步创建canvas上下文", async () => { - await page.callMethod('useAsync'); - const element = await page.$('#testCanvasContext') - expect(await element.text()).toBe('true') + await page.callMethod('useAsync'); + const element = await page.$('#testCanvasContext') + expect(await element.text()).toBe('true') }) it('测试 canvasToDataURL', async () => { await page.callMethod('canvasToDataURL'); const element = await page.$('#testToDataURLResult') expect(await element.text()).toBe('true') }) + it('测试 createImage', async () => { + await page.callMethod('drawImageAPI'); + await page.waitFor(500) // 加载图片 + const element = await page.$('#testCreateImage') + expect(await element.text()).toBe('true') + }) }) diff --git a/pages/component/canvas/canvas.uvue b/pages/component/canvas/canvas.uvue index 5a889a86cdd214127dcf1c4014454a96eb233108..b67035674fbe9d427cd757f2f7ea896f9ae87a3f 100644 --- a/pages/component/canvas/canvas.uvue +++ b/pages/component/canvas/canvas.uvue @@ -16,6 +16,12 @@ 测试 ToDataURL 结果: {{testToDataURLResult}} + + + 测试 createImage 结果: + {{testCreateImage}} + + @@ -27,6 +33,7 @@ canvasToDataURL: {{dataBase64.slice(0,22)}}... + CanvasContext API 演示 @@ -70,6 +77,7 @@ image: null as Image | null, // 仅测试 testCreateCanvasContextAsyncSuccess: false, + testCreateImage: false } }, onReady() { @@ -86,6 +94,25 @@ canvasToDataURL() { this.dataBase64 = this.canvas!.toDataURL() }, + drawImageAPI() { + uni.createCanvasContextAsync({ + id: 'canvas', + success: (res : CanvasContext) => { + this.context = res.getContext('2d'); + this.clearCanvasRect() + // hidpi(this.context.canvas); + let image = res.createImage(); + image.src = "../../static/logo.png" + image.onload = () => { + this.testCreateImage = true + this.context?.drawImage(image, 0, 0, 100, 100); + } + }, + fail: (err : UniError) => { + console.log(err); + } + } as CreateCanvasContextAsyncOptions) + }, useAsync() { // HBuilderX 4.25+