From 6a53cfb44431912d0dd7e8c499c83e8e620ddcaa Mon Sep 17 00:00:00 2001 From: CPPAlien Date: Wed, 18 Jul 2018 18:26:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- painter.js | 53 +++++++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/painter.js b/painter.js index 1e6cd9a..974e18e 100644 --- a/painter.js +++ b/painter.js @@ -89,7 +89,7 @@ Component({ const ctx = wx.createCanvasContext('k-canvas', this); const pen = new Pen(ctx, palette); pen.paint(() => { - this.saveImg(); + this.saveImgToLocal(); }); }); }, @@ -140,34 +140,13 @@ Component({ }); }, - saveImg() { + saveImgToLocal() { const that = this; setTimeout(() => { wx.canvasToTempFilePath({ canvasId: 'k-canvas', success: function (res) { - wx.getImageInfo({ - src: res.tempFilePath, - success: (infoRes) => { - if (that.paintCount > MAX_PAINT_COUNT) { - const error = `The result is always fault, even we tried ${MAX_PAINT_COUNT} times`; - console.error(error); - that.triggerEvent('imgErr', { error: error }); - return; - } - // 比例相符时才证明绘制成功,否则进行强制重绘制 - if (Math.abs((infoRes.width * that.canvasHeightInPx - that.canvasWidthInPx * infoRes.height) / (infoRes.height * that.canvasHeightInPx)) < 0.01) { - that.triggerEvent('imgOK', { path: res.tempFilePath }); - } else { - that.startPaint(); - } - that.paintCount++; - }, - fail: (error) => { - console.error(`getImageInfo failed, ${JSON.stringify(error)}`); - that.triggerEvent('imgErr', { error: error }); - }, - }); + that.getImageInfo(res.tempFilePath); }, fail: function (error) { console.error(`canvasToTempFilePath failed, ${JSON.stringify(error)}`); @@ -176,6 +155,32 @@ Component({ }, this); }, 300); }, + + getImageInfo(filePath) { + const that = this; + wx.getImageInfo({ + src: filePath, + success: (infoRes) => { + if (that.paintCount > MAX_PAINT_COUNT) { + const error = `The result is always fault, even we tried ${MAX_PAINT_COUNT} times`; + console.error(error); + that.triggerEvent('imgErr', { error: error }); + return; + } + // 比例相符时才证明绘制成功,否则进行强制重绘制 + if (Math.abs((infoRes.width * that.canvasHeightInPx - that.canvasWidthInPx * infoRes.height) / (infoRes.height * that.canvasHeightInPx)) < 0.01) { + that.triggerEvent('imgOK', { path: filePath }); + } else { + that.startPaint(); + } + that.paintCount++; + }, + fail: (error) => { + console.error(`getImageInfo failed, ${JSON.stringify(error)}`); + that.triggerEvent('imgErr', { error: error }); + }, + }); + }, }, });