From ad81ca681329ce2f1cb262be2cc01e0f909e92b6 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Mon, 7 Feb 2022 17:16:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20canvas=20=E6=94=AF=E6=8C=81=20hidpi=20?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E6=8E=A7=E5=88=B6=E6=98=AF=E5=90=A6=E5=90=AF?= =?UTF-8?q?=E7=94=A8=E9=AB=98=E6=B8=85=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/helpers/hidpi.js | 12 +++++++----- src/core/view/components/canvas/index.vue | 17 ++++++++++++----- .../service/api/device/add-phone-contact.js | 6 +++--- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/core/helpers/hidpi.js b/src/core/helpers/hidpi.js index ca8e0084b..09fecb39c 100644 --- a/src/core/helpers/hidpi.js +++ b/src/core/helpers/hidpi.js @@ -105,6 +105,7 @@ if (pixelRatio !== 1) { args[1] *= pixelRatio args[2] *= pixelRatio + args[3] *= pixelRatio // Safari 重新设置部分属性会导致其他值恢复默认,需获取原始值 var font = this.__font__ || this.font @@ -130,6 +131,7 @@ if (pixelRatio !== 1) { args[1] *= pixelRatio // x args[2] *= pixelRatio // y + args[3] *= pixelRatio // y // Safari 重新设置部分属性会导致其他值恢复默认,需获取原始值 var font = this.__font__ || this.font @@ -157,11 +159,11 @@ if (pixelRatio !== 1) { })(proto.drawImage) } -export function wrapper (canvas) { - canvas.width = canvas.offsetWidth * pixelRatio - canvas.height = canvas.offsetHeight * pixelRatio - canvas.__hidpi__ = true +export function wrapper (canvas, hidpi = true) { + canvas.width = canvas.offsetWidth * (hidpi ? pixelRatio : 1) + canvas.height = canvas.offsetHeight * (hidpi ? pixelRatio : 1) + canvas.__hidpi__ = hidpi // 避免低版本安卓上 context 实例被回收 canvas.__context2d__ = canvas.getContext('2d') - canvas.__context2d__.__hidpi__ = true + canvas.__context2d__.__hidpi__ = hidpi } diff --git a/src/core/view/components/canvas/index.vue b/src/core/view/components/canvas/index.vue index 786a83cd9..4ca7ae210 100644 --- a/src/core/view/components/canvas/index.vue +++ b/src/core/view/components/canvas/index.vue @@ -78,6 +78,10 @@ export default { disableScroll: { type: [Boolean, String], default: false + }, + hidpi: { + type: Boolean, + default: true } }, data () { @@ -110,6 +114,9 @@ export default { $listeners[event] = eventHandler }) return $listeners + }, + pixelRatio () { + return this.hidpi ? pixelRatio : 1 } }, created () { @@ -135,15 +142,15 @@ export default { }, _resize (size) { var canvas = this.$refs.canvas - var hasChanged = !size || (canvas.width !== Math.floor(size.width * pixelRatio) || canvas.height !== Math.floor(size.height * pixelRatio)) + var hasChanged = !size || (canvas.width !== Math.floor(size.width * this.pixelRatio) || canvas.height !== Math.floor(size.height * this.pixelRatio)) if (!hasChanged) return if (canvas.width > 0 && canvas.height > 0) { var context = canvas.getContext('2d') var imageData = context.getImageData(0, 0, canvas.width, canvas.height) - wrapper(canvas) + wrapper(canvas, this.hidpi) context.putImageData(imageData, 0, 0) } else { - wrapper(canvas) + wrapper(canvas, this.hidpi) } }, _touchmove (event) { @@ -377,8 +384,8 @@ export default { height = height ? Math.min(height, maxHeight) : maxHeight if (!hidpi) { if (!destWidth && !destHeight) { - destWidth = Math.round(width * pixelRatio) - destHeight = Math.round(height * pixelRatio) + destWidth = Math.round(width * this.pixelRatio) + destHeight = Math.round(height * this.pixelRatio) } else if (!destWidth) { destWidth = Math.round(width / height * destHeight) } else if (!destHeight) { diff --git a/src/platforms/app-plus/service/api/device/add-phone-contact.js b/src/platforms/app-plus/service/api/device/add-phone-contact.js index 0f55b9eed..3c06ef075 100644 --- a/src/platforms/app-plus/service/api/device/add-phone-contact.js +++ b/src/platforms/app-plus/service/api/device/add-phone-contact.js @@ -6,7 +6,7 @@ const schema = { name: { givenName: 'firstName', middleName: 'middleName', - familyName: 'lastName', + familyName: 'lastName' }, nickname: 'nickName', photos: { @@ -84,7 +84,7 @@ const schema = { const keepFields = ['type', 'preferred'] -function buildContact(contact, data, schema) { +function buildContact (contact, data, schema) { let hasValue = 0 Object.keys(schema).forEach(contactKey => { const dataKey = schema[contactKey] @@ -127,7 +127,7 @@ function buildContact(contact, data, schema) { return hasValue } -export function addPhoneContact(data, callbackId) { +export function addPhoneContact (data, callbackId) { plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, (addressbook) => { !data.photoFilePath && (data.photoFilePath = '') const contact = addressbook.create() -- GitLab