You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
1.8 KiB
97 lines
1.8 KiB
1 year ago
|
export const uniContext = (canvasId, context) => {
|
||
|
let ctx = uni.createCanvasContext(canvasId, context)
|
||
|
if (!ctx.uniDrawImage) {
|
||
|
ctx.uniDrawImage = ctx.drawImage
|
||
|
ctx.drawImage = (image, ...agrs) => {
|
||
|
ctx.uniDrawImage(image.src, ...agrs)
|
||
|
}
|
||
|
}
|
||
|
if (!ctx.getImageData) {
|
||
|
ctx.getImageData = (x, y, width, height) => {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
// #ifdef MP || VUE2
|
||
|
if (context.proxy) context = context.proxy
|
||
|
// #endif
|
||
|
uni.canvasGetImageData({
|
||
|
canvasId,
|
||
|
x,
|
||
|
y,
|
||
|
width,
|
||
|
height,
|
||
|
success(res) {
|
||
|
resolve(res)
|
||
|
},
|
||
|
fail(error) {
|
||
|
reject(error)
|
||
|
}
|
||
|
}, context)
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return ctx
|
||
|
}
|
||
|
|
||
|
class Image {
|
||
|
constructor() {
|
||
|
this.currentSrc = null
|
||
|
this.naturalHeight = 0
|
||
|
this.naturalWidth = 0
|
||
|
this.width = 0
|
||
|
this.height = 0
|
||
|
this.tagName = 'IMG'
|
||
|
}
|
||
|
set src(src) {
|
||
|
this.currentSrc = src
|
||
|
uni.getImageInfo({
|
||
|
src,
|
||
|
success: (res) => {
|
||
|
this.naturalWidth = this.width = res.width
|
||
|
this.naturalHeight = this.height = res.height
|
||
|
this.onload()
|
||
|
},
|
||
|
fail: () => {
|
||
|
this.onerror()
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
get src() {
|
||
|
return this.currentSrc
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export const createImage = () => {
|
||
|
return new Image()
|
||
|
}
|
||
|
export function useCurrentPage() {
|
||
|
const pages = getCurrentPages();
|
||
|
return pages[pages.length - 1];
|
||
|
}
|
||
|
export const toDataURL = (canvasId, context, options = {}) => {
|
||
|
// #ifdef MP-QQ
|
||
|
// context = context.$scope
|
||
|
// #endif
|
||
|
// #ifdef MP-ALIPAY
|
||
|
context = ''
|
||
|
// #endif
|
||
|
return new Promise((resolve, reject) => {
|
||
|
const {canavs} = options
|
||
|
const params = {
|
||
|
...options,
|
||
|
canvasId,
|
||
|
// canvas,
|
||
|
success: (res) => {
|
||
|
resolve(res.tempFilePath)
|
||
|
},
|
||
|
fail: (err) => {
|
||
|
reject(err)
|
||
|
}
|
||
|
}
|
||
|
if(canavs && canavs.toTempFilePath) {
|
||
|
canavs.toTempFilePath(params)
|
||
|
} else {
|
||
|
uni.canvasToTempFilePath(params, context)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
}
|