提交 cca496f3 编写于 作者: D DCloud_LXH

chore: canvas remove callbackId

上级 5bc78fa0
...@@ -29,11 +29,7 @@ import { hasOwn } from '@vue/shared' ...@@ -29,11 +29,7 @@ import { hasOwn } from '@vue/shared'
import { once, ON_ERROR } from '@dcloudio/uni-shared' import { once, ON_ERROR } from '@dcloudio/uni-shared'
import { import { getPageIdByVm, getCurrentPageVm } from '@dcloudio/uni-core'
getPageIdByVm,
getCurrentPageVm,
createCallbacks,
} from '@dcloudio/uni-core'
import { TEMP_PATH } from '@dcloudio/uni-platform' import { TEMP_PATH } from '@dcloudio/uni-platform'
...@@ -41,30 +37,23 @@ import { TEMP_PATH } from '@dcloudio/uni-platform' ...@@ -41,30 +37,23 @@ import { TEMP_PATH } from '@dcloudio/uni-platform'
//#endregion //#endregion
//#region UniServiceJSBridge //#region UniServiceJSBridge
const canvasEventCallbacks = createCallbacks('canvasEvent')
function operateCanvas( function operateCanvas(
canvasId: string, canvasId: string,
pageId: number, pageId: number,
type: unknown, type: unknown,
data: any data: any,
callback: Callback
) { ) {
UniServiceJSBridge.invokeViewMethod< try {
{}, } catch (error) {}
{ callbackId: number | string; data: any } UniServiceJSBridge.invokeViewMethod<{}, Record<string, any>>(
>(
`canvas.${canvasId}`, `canvas.${canvasId}`,
{ {
type, type,
data, data,
}, },
pageId, pageId,
({ callbackId, data }) => { (data) => callback(data)
const callback = canvasEventCallbacks.pop(callbackId)
if (callback) {
callback(data)
}
}
) )
} }
//#endregion //#endregion
...@@ -404,17 +393,17 @@ export class CanvasContext implements UniApp.CanvasContext { ...@@ -404,17 +393,17 @@ export class CanvasContext implements UniApp.CanvasContext {
var actions = [...this.actions] var actions = [...this.actions]
this.actions = [] this.actions = []
this.path = [] this.path = []
var callbackId
if (typeof callback === 'function') { operateCanvas(
callbackId = canvasEventCallbacks.push(callback) this.id,
} this.pageId,
'actionsChanged',
operateCanvas(this.id, this.pageId, 'actionsChanged', { {
actions, actions,
reserve, reserve,
callbackId, },
}) callback
)
} }
createLinearGradient(x0: number, y0: number, x1: number, y1: number) { createLinearGradient(x0: number, y0: number, x1: number, y1: number) {
...@@ -1048,13 +1037,18 @@ export const canvasGetImageData = ...@@ -1048,13 +1037,18 @@ export const canvasGetImageData =
reject() reject()
return return
} }
const cId = canvasEventCallbacks.push(function ( function callback(
data: UniApp.CanvasGetImageDataRes & { compressed?: boolean } data: UniApp.CanvasGetImageDataRes & { compressed?: boolean }
) { ) {
if (data.errMsg && data.errMsg.indexOf('fail') !== -1) {
reject('', data)
return
}
let imgData = data.data let imgData = data.data
if (imgData && imgData.length) { if (imgData && imgData.length) {
if (__PLATFORM__ === 'app' && data.compressed) { if (__PLATFORM__ === 'app' && data.compressed) {
return import('pako').then((pako) => { import('pako').then((pako) => {
imgData = pako.inflateRaw(imgData) as any imgData = pako.inflateRaw(imgData) as any
delete data.compressed delete data.compressed
data.data = new Uint8ClampedArray(imgData) as any data.data = new Uint8ClampedArray(imgData) as any
...@@ -1064,14 +1058,19 @@ export const canvasGetImageData = ...@@ -1064,14 +1058,19 @@ export const canvasGetImageData =
data.data = new Uint8ClampedArray(imgData) as any data.data = new Uint8ClampedArray(imgData) as any
} }
resolve(data) resolve(data)
}) }
operateCanvas(canvasId, pageId, 'getImageData', { operateCanvas(
x, canvasId,
y, pageId,
width, 'getImageData',
height, {
callbackId: cId, x,
}) y,
width,
height,
},
callback
)
}, },
CanvasGetImageDataProtocol, CanvasGetImageDataProtocol,
CanvasGetImageDataOptions CanvasGetImageDataOptions
...@@ -1087,22 +1086,28 @@ export const canvasPutImageData = ...@@ -1087,22 +1086,28 @@ export const canvasPutImageData =
reject() reject()
return return
} }
const cId = canvasEventCallbacks.push(function (
data: UniApp.CanvasGetImageDataRes
) {
resolve(data)
})
let compressed: boolean let compressed: boolean
const operate = () => { const operate = () => {
operateCanvas(canvasId, pageId, 'putImageData', { operateCanvas(
data, canvasId,
x, pageId,
y, 'putImageData',
width, {
height, data,
compressed, x,
callbackId: cId, y,
}) width,
height,
compressed,
},
(data: UniApp.CanvasGetImageDataRes) => {
if (data.errMsg && data.errMsg.indexOf('fail')) {
reject()
return
}
resolve(data)
}
)
} }
// iOS真机非调试模式压缩太慢暂时排除 // iOS真机非调试模式压缩太慢暂时排除
if ( if (
...@@ -1146,24 +1151,30 @@ export const canvasToTempFilePath = ...@@ -1146,24 +1151,30 @@ export const canvasToTempFilePath =
reject() reject()
return return
} }
const cId = canvasEventCallbacks.push(function (
res: UniApp.CanvasToTempFilePathRes
) {
resolve(res)
})
const dirname = `${TEMP_PATH}/canvas` const dirname = `${TEMP_PATH}/canvas`
operateCanvas(canvasId, pageId, 'toTempFilePath', { operateCanvas(
x, canvasId,
y, pageId,
width, 'toTempFilePath',
height, {
destWidth, x,
destHeight, y,
fileType, width,
quality, height,
dirname, destWidth,
callbackId: cId, destHeight,
}) fileType,
quality,
dirname,
},
(res: UniApp.CanvasToTempFilePathRes & { errMsg?: string }) => {
if (res.errMsg && res.errMsg.indexOf('fail')) {
reject('', res)
return
}
resolve(res)
}
)
}, },
CanvasToTempFilePathProtocol, CanvasToTempFilePathProtocol,
CanvasToTempFilePathOptions CanvasToTempFilePathOptions
......
...@@ -232,19 +232,17 @@ function useMethods( ...@@ -232,19 +232,17 @@ function useMethods(
{ {
actions, actions,
reserve, reserve,
callbackId,
}: { }: {
actions: Actions actions: Actions
reserve: boolean reserve: boolean
callbackId: number
}, },
resolve: (res: { callbackId: number; data: any }) => void resolve: (res: any) => void
) { ) {
if (!actions) { if (!actions) {
return return
} }
if (actionsWaiting.value) { if (actionsWaiting.value) {
_actionsDefer.push([actions, reserve, callbackId]) _actionsDefer.push([actions, reserve])
return return
} }
var canvas = canvasRef.value! var canvas = canvasRef.value!
...@@ -300,7 +298,6 @@ function useMethods( ...@@ -300,7 +298,6 @@ function useMethods(
const loaded = checkImageLoaded( const loaded = checkImageLoaded(
data[1] as string, data[1] as string,
actions.slice(index + 1), actions.slice(index + 1),
callbackId,
resolve, resolve,
function (image) { function (image) {
if (image) { if (image) {
...@@ -374,7 +371,6 @@ function useMethods( ...@@ -374,7 +371,6 @@ function useMethods(
checkImageLoaded( checkImageLoaded(
url, url,
actions.slice(index + 1), actions.slice(index + 1),
callbackId,
resolve, resolve,
function (image) { function (image) {
if (image) { if (image) {
...@@ -409,12 +405,9 @@ function useMethods( ...@@ -409,12 +405,9 @@ function useMethods(
} }
} }
} }
if (!actionsWaiting.value && callbackId) { if (!actionsWaiting.value) {
resolve({ resolve({
callbackId, errMsg: 'drawCanvas:ok',
data: {
errMsg: 'drawCanvas:ok',
},
}) })
} }
} }
...@@ -467,8 +460,7 @@ function useMethods( ...@@ -467,8 +460,7 @@ function useMethods(
function checkImageLoaded( function checkImageLoaded(
src: string, src: string,
actions: Actions, actions: Actions,
callbackId: number, resolve: (res: any) => void,
resolve: (res: { callbackId: number; data: any }) => void,
fn: (a: CanvasImageSource) => void fn: (a: CanvasImageSource) => void
) { ) {
var image = _images[src] var image = _images[src]
...@@ -487,7 +479,6 @@ function useMethods( ...@@ -487,7 +479,6 @@ function useMethods(
for (var action = actions.shift(); action; ) { for (var action = actions.shift(); action; ) {
actionsChanged( actionsChanged(
{ {
callbackId,
actions: action[0], actions: action[0],
reserve: action[1], reserve: action[1],
}, },
...@@ -511,7 +502,6 @@ function useMethods( ...@@ -511,7 +502,6 @@ function useMethods(
dataType, dataType,
quality = 1, quality = 1,
type = 'png', type = 'png',
callbackId,
}: { }: {
x: number x: number
y: number y: number
...@@ -523,9 +513,8 @@ function useMethods( ...@@ -523,9 +513,8 @@ function useMethods(
dataType: string dataType: string
quality: number quality: number
type: string type: string
callbackId?: number
}, },
resolve?: (res: { callbackId: number; data: any }) => void resolve?: (res: any) => void
) { ) {
const canvas = canvasRef.value! const canvas = canvasRef.value!
let data: string | number[] let data: string | number[]
...@@ -553,9 +542,9 @@ function useMethods( ...@@ -553,9 +542,9 @@ function useMethods(
context.fillStyle = '#fff' context.fillStyle = '#fff'
context.fillRect(0, 0, destWidth, destHeight) context.fillRect(0, 0, destWidth, destHeight)
} }
// @ts-ignore // @ts-expect-error
context.__hidpi__ = true context.__hidpi__ = true
// @ts-ignore // @ts-expect-error
context.drawImageByCanvas( context.drawImageByCanvas(
canvas, canvas,
x, x,
...@@ -585,7 +574,6 @@ function useMethods( ...@@ -585,7 +574,6 @@ function useMethods(
} }
} }
result = { result = {
errMsg: 'canvasGetImageData:ok',
data, data,
compressed, compressed,
width: destWidth, width: destWidth,
...@@ -597,16 +585,12 @@ function useMethods( ...@@ -597,16 +585,12 @@ function useMethods(
} }
} }
newCanvas.height = newCanvas.width = 0 newCanvas.height = newCanvas.width = 0
// @ts-ignore // @ts-expect-error
context.__hidpi__ = false context.__hidpi__ = false
if (!callbackId) { if (!resolve) {
return result return result
} else { } else {
resolve && resolve(result)
resolve({
callbackId,
data: result,
})
} }
} }
function putImageData( function putImageData(
...@@ -617,7 +601,6 @@ function useMethods( ...@@ -617,7 +601,6 @@ function useMethods(
width, width,
height, height,
compressed, compressed,
callbackId,
}: { }: {
data: Array<number> data: Array<number>
x: number x: number
...@@ -625,9 +608,8 @@ function useMethods( ...@@ -625,9 +608,8 @@ function useMethods(
width: number width: number
height: number height: number
compressed: boolean compressed: boolean
callbackId: number
}, },
resolve: (res: { callbackId: number; data: any }) => void resolve: (res: any) => void
) { ) {
try { try {
if (!height) { if (!height) {
...@@ -647,20 +629,10 @@ function useMethods( ...@@ -647,20 +629,10 @@ function useMethods(
canvasRef.value!.getContext('2d')!.drawImage(canvas, x, y, width, height) canvasRef.value!.getContext('2d')!.drawImage(canvas, x, y, width, height)
canvas.height = canvas.width = 0 canvas.height = canvas.width = 0
} catch (error) { } catch (error) {
resolve({ resolve({ errMsg: 'canvasPutImageData:fail' })
callbackId,
data: {
errMsg: 'canvasPutImageData:fail',
},
})
return return
} }
resolve({ resolve({ errMsg: 'canvasPutImageData:ok' })
callbackId,
data: {
errMsg: 'canvasPutImageData:ok',
},
})
} }
function toTempFilePath( function toTempFilePath(
{ {
...@@ -673,7 +645,6 @@ function useMethods( ...@@ -673,7 +645,6 @@ function useMethods(
fileType, fileType,
quality, quality,
dirname, dirname,
callbackId,
}: { }: {
x: number x: number
y: number y: number
...@@ -684,9 +655,8 @@ function useMethods( ...@@ -684,9 +655,8 @@ function useMethods(
fileType: string fileType: string
quality: number quality: number
dirname: string dirname: string
callbackId: number
}, },
resolve: (res: { callbackId: number; data: any }) => void resolve: (res: any) => void
) { ) {
const res = getImageData({ const res = getImageData({
x, x,
...@@ -702,10 +672,7 @@ function useMethods( ...@@ -702,10 +672,7 @@ function useMethods(
})! })!
if (!res.data || !res.data.length) { if (!res.data || !res.data.length) {
resolve({ resolve({
callbackId, errMsg: res!.errMsg!.replace('canvasPutImageData', 'toTempFilePath'),
data: {
errMsg: res!.errMsg.replace('canvasPutImageData', 'toTempFilePath'),
},
}) })
return return
} }
...@@ -714,13 +681,7 @@ function useMethods( ...@@ -714,13 +681,7 @@ function useMethods(
if (error) { if (error) {
errMsg += ` ${error.message}` errMsg += ` ${error.message}`
} }
resolve({ resolve({ errMsg, tempFilePath })
callbackId,
data: {
errMsg,
tempFilePath: tempFilePath,
},
})
}) })
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册