提交 404139f5 编写于 作者: d-u-a's avatar d-u-a

update: H5 高清canvas canvasToTempFilePath模糊问题.

上级 d04fabd4
const pixelRatio = (function () { const pixelRatio = (function () {
const canvas = document.createElement('canvas') const canvas = document.createElement('canvas')
const context = canvas.getContext('2d') const context = canvas.getContext('2d')
const backingStore = context.backingStorePixelRatio || const backingStore = context.backingStorePixelRatio ||
context.webkitBackingStorePixelRatio || context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio || context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio || context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio || context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 1 context.backingStorePixelRatio || 1
return (window.devicePixelRatio || 1) / backingStore return (window.devicePixelRatio || 1) / backingStore
})() })()
const forEach = function (obj, func) { const forEach = function (obj, func) {
for (let key in obj) { for (let key in obj) {
if (obj.hasOwnProperty(key)) { if (obj.hasOwnProperty(key)) {
func(obj[key], key) func(obj[key], key)
} }
} }
} }
const ratioArgs = { const ratioArgs = {
'fillRect': 'all', 'fillRect': 'all',
'clearRect': 'all', 'clearRect': 'all',
'strokeRect': 'all', 'strokeRect': 'all',
'moveTo': 'all', 'moveTo': 'all',
'lineTo': 'all', 'lineTo': 'all',
'arc': [0, 1, 2], 'arc': [0, 1, 2],
'arcTo': 'all', 'arcTo': 'all',
'bezierCurveTo': 'all', 'bezierCurveTo': 'all',
'isPointinPath': 'all', 'isPointinPath': 'all',
'isPointinStroke': 'all', 'isPointinStroke': 'all',
'quadraticCurveTo': 'all', 'quadraticCurveTo': 'all',
'rect': 'all', 'rect': 'all',
'translate': 'all', 'translate': 'all',
'createRadialGradient': 'all', 'createRadialGradient': 'all',
'createLinearGradient': 'all', 'createLinearGradient': 'all',
'setTransform': [4, 5] 'setTransform': [4, 5]
} }
if (pixelRatio !== 1) { if (pixelRatio !== 1) {
const proto = CanvasRenderingContext2D.prototype const proto = CanvasRenderingContext2D.prototype
forEach(ratioArgs, function (value, key) { forEach(ratioArgs, function (value, key) {
proto[key] = (function (_super) { proto[key] = (function (_super) {
return function () { return function () {
if (this.__ignore__) { if (this.__ignore__) {
return _super.apply(this, arguments) return _super.apply(this, arguments)
} }
let args = Array.prototype.slice.call(arguments) let args = Array.prototype.slice.call(arguments)
if (value === 'all') { if (value === 'all') {
args = args.map(function (a) { args = args.map(function (a) {
return a * pixelRatio return a * pixelRatio
}) })
} else if (Array.isArray(value)) { } else if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) { for (let i = 0; i < value.length; i++) {
args[value[i]] *= pixelRatio args[value[i]] *= pixelRatio
} }
} }
return _super.apply(this, args) return _super.apply(this, args)
} }
})(proto[key]) })(proto[key])
}) })
proto.stroke = (function (_super) { proto.stroke = (function (_super) {
return function () { return function () {
if (this.__ignore__) { if (this.__ignore__) {
return _super.apply(this, arguments) return _super.apply(this, arguments)
} }
this.lineWidth *= pixelRatio this.lineWidth *= pixelRatio
_super.apply(this, arguments) _super.apply(this, arguments)
this.lineWidth /= pixelRatio this.lineWidth /= pixelRatio
} }
})(proto.stroke) })(proto.stroke)
proto.fillText = (function (_super) { proto.fillText = (function (_super) {
return function () { return function () {
if (this.__ignore__) { if (this.__ignore__) {
return _super.apply(this, arguments) return _super.apply(this, arguments)
} }
const args = Array.prototype.slice.call(arguments) const args = Array.prototype.slice.call(arguments)
args[1] *= pixelRatio args[1] *= pixelRatio
args[2] *= pixelRatio args[2] *= pixelRatio
this.font = this.font.replace( this.font = this.font.replace(
/(\d+)(px|em|rem|pt)/g, /(\d+)(px|em|rem|pt)/g,
function (w, m, u) { function (w, m, u) {
return (m * pixelRatio) + u return (m * pixelRatio) + u
} }
) )
_super.apply(this, args) _super.apply(this, args)
this.font = this.font.replace( this.font = this.font.replace(
/(\d+)(px|em|rem|pt)/g, /(\d+)(px|em|rem|pt)/g,
function (w, m, u) { function (w, m, u) {
return (m / pixelRatio) + u return (m / pixelRatio) + u
} }
) )
} }
})(proto.fillText) })(proto.fillText)
proto.strokeText = (function (_super) { proto.strokeText = (function (_super) {
return function () { return function () {
if (this.__ignore__) { if (this.__ignore__) {
return _super.apply(this, arguments) return _super.apply(this, arguments)
} }
var args = Array.prototype.slice.call(arguments) var args = Array.prototype.slice.call(arguments)
args[1] *= pixelRatio // x args[1] *= pixelRatio // x
args[2] *= pixelRatio // y args[2] *= pixelRatio // y
this.font = this.font.replace( this.font = this.font.replace(
/(\d+)(px|em|rem|pt)/g, /(\d+)(px|em|rem|pt)/g,
function (w, m, u) { function (w, m, u) {
return (m * pixelRatio) + u return (m * pixelRatio) + u
} }
) )
_super.apply(this, args) _super.apply(this, args)
this.font = this.font.replace( this.font = this.font.replace(
/(\d+)(px|em|rem|pt)/g, /(\d+)(px|em|rem|pt)/g,
function (w, m, u) { function (w, m, u) {
return (m / pixelRatio) + u return (m / pixelRatio) + u
} }
) )
} }
})(proto.strokeText) })(proto.strokeText)
proto.drawImageByCanvas = (function (_super) { proto.drawImageByCanvas = (function (_super) {
return function (canvas, srcx, srcy, srcw, srch, desx, desy, desw, desh, isScale) { return function (canvas, srcx, srcy, srcw, srch, desx, desy, desw, desh, isScale) {
if (this.__ignore__) { if (this.__ignore__) {
return _super.apply(this, arguments) return _super.apply(this, arguments)
} }
srcx *= pixelRatio srcx *= pixelRatio
srcy *= pixelRatio srcy *= pixelRatio
srcw *= pixelRatio srcw *= pixelRatio
srch *= pixelRatio srch *= pixelRatio
desx *= pixelRatio desx *= pixelRatio
desy *= pixelRatio desy *= pixelRatio
desw = isScale ? desw * pixelRatio : desw desw = isScale ? desw * pixelRatio : desw
desh = isScale ? desh * pixelRatio : desh desh = isScale ? desh * pixelRatio : desh
_super.call(this, canvas, srcx, srcy, srcw, srch, desx, desy, desw, desh) _super.call(this, canvas, srcx, srcy, srcw, srch, desx, desy, desw, desh)
} }
})(proto.drawImage) })(proto.drawImage)
proto.drawImage = (function (_super) { proto.drawImage = (function (_super) {
return function () { return function () {
if (this.__ignore__) { if (this.__ignore__) {
return _super.apply(this, arguments) return _super.apply(this, arguments)
} }
this.scale(pixelRatio, pixelRatio) this.scale(pixelRatio, pixelRatio)
_super.apply(this, arguments) _super.apply(this, arguments)
this.scale(1 / pixelRatio, 1 / pixelRatio) this.scale(1 / pixelRatio, 1 / pixelRatio)
} }
})(proto.drawImage) })(proto.drawImage)
} }
export function wrapper (canvas) { export function wrapper (canvas) {
canvas.style.height = canvas.height + 'px' canvas.style.height = canvas.height + 'px'
canvas.style.width = canvas.width + 'px' canvas.style.width = canvas.width + 'px'
canvas.width *= pixelRatio canvas.width *= pixelRatio
canvas.height *= pixelRatio canvas.height *= pixelRatio
console.log(canvas.width) console.log(canvas.width)
console.log(canvas.height) console.log(canvas.height)
} }
import createCallbacks from 'uni-helpers/callbacks' import createCallbacks from 'uni-helpers/callbacks'
import { wrapper } from 'uni-helpers/hidpi'
const canvasEventCallbacks = createCallbacks('canvasEvent') const canvasEventCallbacks = createCallbacks('canvasEvent')
...@@ -257,6 +258,7 @@ var tempCanvas ...@@ -257,6 +258,7 @@ var tempCanvas
function getTempCanvas () { function getTempCanvas () {
if (!tempCanvas) { if (!tempCanvas) {
tempCanvas = document.createElement('canvas') tempCanvas = document.createElement('canvas')
wrapper(tempCanvas)
} }
return tempCanvas return tempCanvas
} }
...@@ -816,7 +818,7 @@ export function canvasToTempFilePath ({ ...@@ -816,7 +818,7 @@ export function canvasToTempFilePath ({
pageId = app.$route.params.__id__ pageId = app.$route.params.__id__
} else { } else {
invoke(callbackId, { invoke(callbackId, {
errMsg: 'canvasPutImageData:fail' errMsg: 'canvasToTempFilePath:fail'
}) })
return return
} }
...@@ -840,20 +842,24 @@ export function canvasToTempFilePath ({ ...@@ -840,20 +842,24 @@ export function canvasToTempFilePath ({
canvas.width = data.width canvas.width = data.width
canvas.height = data.height canvas.height = data.height
var c2d = canvas.getContext('2d') var c2d = canvas.getContext('2d')
c2d.putImageData(imgData, 0, 0) c2d.putImageData(imgData, 0, 0, 0, 0, destWidth || imgData.width, destHeight || imgData.height)
var base64 = canvas.toDataURL('image/png') var base64 = canvas.toDataURL(`image/${fileType.toLowerCase()}`, qualit)
var img = new Image() invoke(callbackId, {
img.onload = function () { errMsg: 'canvasToTempFilePath:ok',
canvas.width = destWidth || imgData.width tempFilePath: base64
canvas.height = destHeight || imgData.height })
c2d.drawImage(img, 0, 0) // var img = new Image()
base64 = canvas.toDataURL(`image/${fileType.toLowerCase()}`, qualit) // img.onload = function () {
invoke(callbackId, { // canvas.width = destWidth || imgData.width
errMsg: 'canvasToTempFilePath:ok', // canvas.height = destHeight || imgData.height
tempFilePath: base64 // c2d.drawImage(img, 0, 0)
}) // base64 = canvas.toDataURL(`image/${fileType.toLowerCase()}`, qualit)
} // invoke(callbackId, {
img.src = base64 // errMsg: 'canvasToTempFilePath:ok',
// tempFilePath: base64
// })
// }
// img.src = base64
}) })
operateCanvas(canvasId, pageId, 'getImageData', { operateCanvas(canvasId, pageId, 'getImageData', {
x, x,
......
<template> <template>
<uni-canvas <uni-canvas
:canvas-id="canvasId" :canvas-id="canvasId"
:disable-scroll="disableScroll" :disable-scroll="disableScroll"
v-on="_listeners"> v-on="_listeners">
<canvas <canvas
ref="canvas" ref="canvas"
width="300" width="300"
height="150" /> height="150" />
<div style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: hidden;"> <div style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: hidden;">
<slot /> <slot />
...@@ -19,7 +19,7 @@ import { ...@@ -19,7 +19,7 @@ import {
import { import {
wrapper wrapper
} from './hidpi' } from 'uni-helpers/hidpi'
function resolveColor (color) { function resolveColor (color) {
color = color.slice(0) color = color.slice(0)
...@@ -459,4 +459,4 @@ export default { ...@@ -459,4 +459,4 @@ export default {
width: 100%; width: 100%;
height: 100%; height: 100%;
} }
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册