提交 69dd2179 编写于 作者: Q qiang

Merge branch 'fix-canvas' into dev

...@@ -158,9 +158,7 @@ if (pixelRatio !== 1) { ...@@ -158,9 +158,7 @@ if (pixelRatio !== 1) {
} }
export function wrapper (canvas) { export function wrapper (canvas) {
canvas.style.height = canvas.height + 'px' canvas.width = canvas.offsetWidth * pixelRatio
canvas.style.width = canvas.width + 'px' canvas.height = canvas.offsetHeight * pixelRatio
canvas.width *= pixelRatio
canvas.height *= pixelRatio
canvas.getContext('2d').__hidpi__ = true canvas.getContext('2d').__hidpi__ = true
} }
...@@ -21,6 +21,7 @@ import { ...@@ -21,6 +21,7 @@ import {
} from 'uni-mixins' } from 'uni-mixins'
import { import {
pixelRatio,
wrapper wrapper
} from 'uni-helpers/hidpi' } from 'uni-helpers/hidpi'
...@@ -106,16 +107,8 @@ export default { ...@@ -106,16 +107,8 @@ export default {
method(data) method(data)
} }
}, },
_resize ({ _resize () {
width, wrapper(this.$refs.canvas)
height
}) {
var canvas = this.$refs.canvas
if (canvas.style.width !== (width + 'px') || canvas.style.height !== (height + 'px')) {
canvas.width = width
canvas.height = height
wrapper(canvas)
}
}, },
_touchmove (event) { _touchmove (event) {
event.preventDefault() event.preventDefault()
...@@ -381,22 +374,44 @@ export default { ...@@ -381,22 +374,44 @@ export default {
} }
}, },
getImageData ({ getImageData ({
x, x = 0,
y, y = 0,
width, width,
height, height,
destWidth,
destHeight,
hidpi = true,
callbackId callbackId
}) { }) {
var imgData var imgData
var canvas = this.$refs.canvas var canvas = this.$refs.canvas
if (!width) { if (!width) {
width = canvas.width width = canvas.offsetWidth - x
} }
if (!height) { if (!height) {
height = canvas.height height = canvas.offsetHeight - y
} }
try { try {
imgData = canvas.getContext('2d').getImageData(x, y, width, height) const newCanvas = document.createElement('canvas')
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, destWidth, destHeight, false)
imgData = context.getImageData(0, 0, destWidth, destHeight)
} catch (error) { } catch (error) {
UniViewJSBridge.publishHandler('onCanvasMethodCallback', { UniViewJSBridge.publishHandler('onCanvasMethodCallback', {
callbackId, callbackId,
...@@ -411,8 +426,8 @@ export default { ...@@ -411,8 +426,8 @@ export default {
data: { data: {
errMsg: 'canvasGetImageData:ok', errMsg: 'canvasGetImageData:ok',
data: [...imgData.data], data: [...imgData.data],
width, width: destWidth,
height height: destHeight
} }
}, this.$page.id) }, this.$page.id)
}, },
...@@ -428,8 +443,12 @@ export default { ...@@ -428,8 +443,12 @@ export default {
if (!height) { if (!height) {
height = Math.round(data.length / 4 / width) height = Math.round(data.length / 4 / width)
} }
this.$refs.canvas.getContext('2d').putImageData(new ImageData(new Uint8ClampedArray(data), width, const canvas = document.createElement('canvas')
height), x, y) canvas.width = width
canvas.height = height
const context = canvas.getContext('2d')
context.putImageData(new ImageData(new Uint8ClampedArray(data), width, height), 0, 0)
this.$refs.canvas.getContext('2d').drawImage(canvas, x, y, width, height)
} catch (error) { } catch (error) {
UniViewJSBridge.publishHandler('onCanvasMethodCallback', { UniViewJSBridge.publishHandler('onCanvasMethodCallback', {
callbackId, callbackId,
......
import createCallbacks from 'uni-helpers/callbacks' import createCallbacks from 'uni-helpers/callbacks'
import { pixelRatio } from 'uni-helpers/hidpi'
const canvasEventCallbacks = createCallbacks('canvasEvent') const canvasEventCallbacks = createCallbacks('canvasEvent')
...@@ -808,8 +807,8 @@ export function canvasPutImageData ({ ...@@ -808,8 +807,8 @@ export function canvasPutImageData ({
} }
export function canvasToTempFilePath ({ export function canvasToTempFilePath ({
x, x = 0,
y, y = 0,
width, width,
height, height,
destWidth, destWidth,
...@@ -818,13 +817,7 @@ export function canvasToTempFilePath ({ ...@@ -818,13 +817,7 @@ export function canvasToTempFilePath ({
fileType, fileType,
qualit qualit
}, callbackId) { }, callbackId) {
if (typeof width !== 'undefined') { let pageId
width *= pixelRatio
}
if (typeof height !== 'undefined') {
height *= pixelRatio
}
var pageId
const app = getApp() const app = getApp()
if (app.$route && app.$route.params.__id__) { if (app.$route && app.$route.params.__id__) {
pageId = app.$route.params.__id__ pageId = app.$route.params.__id__
...@@ -834,37 +827,39 @@ export function canvasToTempFilePath ({ ...@@ -834,37 +827,39 @@ export function canvasToTempFilePath ({
}) })
return return
} }
var cId = canvasEventCallbacks.push(function (data) { const cId = canvasEventCallbacks.push(function ({
var imgData = data.data data,
if (!imgData || !imgData.length) { width,
height
}) {
if (!data || !data.length) {
invoke(callbackId, { invoke(callbackId, {
errMsg: 'canvasToTempFilePath:fail' errMsg: 'canvasToTempFilePath:fail'
}) })
return return
} }
let imgData
try { try {
imgData = new ImageData(new Uint8ClampedArray(imgData), data.width, data.height) imgData = new ImageData(new Uint8ClampedArray(data), width, height)
} catch (error) { } catch (error) {
invoke(callbackId, { invoke(callbackId, {
errMsg: 'canvasToTempFilePath:fail' errMsg: 'canvasToTempFilePath:fail'
}) })
return return
} }
var canvas = getTempCanvas() const canvas = getTempCanvas()
canvas.width = data.width canvas.width = width
canvas.height = data.height canvas.height = height
var c2d = canvas.getContext('2d') const c2d = canvas.getContext('2d')
c2d.putImageData(imgData, 0, 0) c2d.putImageData(imgData, 0, 0)
var base64 = canvas.toDataURL('image/png') let base64 = canvas.toDataURL('image/png')
var img = new Image() const img = new Image()
img.onload = function () { 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') { if (fileType === 'jpeg') {
c2d.fillStyle = '#fff' 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) base64 = canvas.toDataURL(`image/${fileType}`, qualit)
invoke(callbackId, { invoke(callbackId, {
errMsg: 'canvasToTempFilePath:ok', errMsg: 'canvasToTempFilePath:ok',
...@@ -878,6 +873,9 @@ export function canvasToTempFilePath ({ ...@@ -878,6 +873,9 @@ export function canvasToTempFilePath ({
y, y,
width, width,
height, height,
destWidth,
destHeight,
hidpi: false,
callbackId: cId callbackId: cId
}) })
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册