From 8c6118d6b9f032e855ee0e7c86b08856e4f14b1f Mon Sep 17 00:00:00 2001 From: qiang Date: Tue, 24 Sep 2019 22:08:06 +0800 Subject: [PATCH] =?UTF-8?q?=20fix:=20=E4=BF=AE=E5=A4=8D=20uni.canvasToTemp?= =?UTF-8?q?FilePath=20=E5=8F=82=E6=95=B0=20x=E3=80=81y=20=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98=20fixed=20#746?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/view/components/canvas/index.vue | 33 +++++++++---- .../h5/service/api/context/canvas.js | 46 +++++++++---------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/core/view/components/canvas/index.vue b/src/core/view/components/canvas/index.vue index 2ec058ba..4f1ad7d4 100644 --- a/src/core/view/components/canvas/index.vue +++ b/src/core/view/components/canvas/index.vue @@ -21,6 +21,7 @@ import { } from 'uni-mixins' import { + pixelRatio, wrapper } from 'uni-helpers/hidpi' @@ -373,10 +374,13 @@ export default { } }, getImageData ({ - x, - y, + x = 0, + y = 0, width, height, + destWidth, + destHeight, + hidpi = true, callbackId }) { var imgData @@ -389,12 +393,25 @@ export default { } try { const newCanvas = document.createElement('canvas') - newCanvas.width = width - newCanvas.height = height + if (!hidpi) { + if (!destWidth && !destHeight) { + destWidth = Math.round(width * pixelRatio) + destHeight = Math.round(height * pixelRatio) + } else if (!destWidth) { + destWidth = Math.round(width / height * destHeight) + } else if (!destHeight) { + destHeight = Math.round(height / width * destWidth) + } + } else { + destWidth = width + destHeight = height + } + newCanvas.width = destWidth + newCanvas.height = destHeight const context = newCanvas.getContext('2d') context.__hidpi__ = true - context.drawImageByCanvas(canvas, x, y, width, height, 0, 0, width, height, false) - imgData = context.getImageData(0, 0, width, height) + context.drawImageByCanvas(canvas, x, y, width, height, 0, 0, destWidth, destHeight, false) + imgData = context.getImageData(0, 0, destWidth, destHeight) } catch (error) { UniViewJSBridge.publishHandler('onCanvasMethodCallback', { callbackId, @@ -409,8 +426,8 @@ export default { data: { errMsg: 'canvasGetImageData:ok', data: [...imgData.data], - width, - height + width: destWidth, + height: destHeight } }, this.$page.id) }, diff --git a/src/platforms/h5/service/api/context/canvas.js b/src/platforms/h5/service/api/context/canvas.js index 914708d7..03265b51 100644 --- a/src/platforms/h5/service/api/context/canvas.js +++ b/src/platforms/h5/service/api/context/canvas.js @@ -1,5 +1,4 @@ import createCallbacks from 'uni-helpers/callbacks' -import { pixelRatio } from 'uni-helpers/hidpi' const canvasEventCallbacks = createCallbacks('canvasEvent') @@ -808,8 +807,8 @@ export function canvasPutImageData ({ } export function canvasToTempFilePath ({ - x, - y, + x = 0, + y = 0, width, height, destWidth, @@ -818,13 +817,7 @@ export function canvasToTempFilePath ({ fileType, qualit }, callbackId) { - if (typeof width !== 'undefined') { - width *= pixelRatio - } - if (typeof height !== 'undefined') { - height *= pixelRatio - } - var pageId + let pageId const app = getApp() if (app.$route && app.$route.params.__id__) { pageId = app.$route.params.__id__ @@ -834,37 +827,39 @@ export function canvasToTempFilePath ({ }) return } - var cId = canvasEventCallbacks.push(function (data) { - var imgData = data.data - if (!imgData || !imgData.length) { + const cId = canvasEventCallbacks.push(function ({ + data, + width, + height + }) { + if (!data || !data.length) { invoke(callbackId, { errMsg: 'canvasToTempFilePath:fail' }) return } + let imgData try { - imgData = new ImageData(new Uint8ClampedArray(imgData), data.width, data.height) + imgData = new ImageData(new Uint8ClampedArray(data), width, height) } catch (error) { invoke(callbackId, { errMsg: 'canvasToTempFilePath:fail' }) return } - var canvas = getTempCanvas() - canvas.width = data.width - canvas.height = data.height - var c2d = canvas.getContext('2d') + const canvas = getTempCanvas() + canvas.width = width + canvas.height = height + const c2d = canvas.getContext('2d') c2d.putImageData(imgData, 0, 0) - var base64 = canvas.toDataURL('image/png') - var img = new Image() + let base64 = canvas.toDataURL('image/png') + const img = new Image() img.onload = function () { - var width = canvas.width = typeof destWidth === 'number' ? destWidth : imgData.width * pixelRatio - var height = canvas.height = typeof destHeight === 'number' ? destHeight : imgData.height * pixelRatio if (fileType === 'jpeg') { c2d.fillStyle = '#fff' - c2d.fillRect(0, 0, width, height) + c2d.fillRect(0, 0, width, width) } - c2d.drawImage(img, 0, 0, img.width, img.height, 0, 0, width, height) + c2d.drawImage(img, 0, 0) base64 = canvas.toDataURL(`image/${fileType}`, qualit) invoke(callbackId, { errMsg: 'canvasToTempFilePath:ok', @@ -878,6 +873,9 @@ export function canvasToTempFilePath ({ y, width, height, + destWidth, + destHeight, + hidpi: false, callbackId: cId }) } -- GitLab