|
@@ -17,9 +17,58 @@ export interface SetImgBgOption {
|
|
|
|
|
|
interface InitOption {
|
|
interface InitOption {
|
|
image: string | HTMLImageElement
|
|
image: string | HTMLImageElement
|
|
|
|
+ frontColor?: Ref<string>
|
|
|
|
+ setFrontColor?: any
|
|
}
|
|
}
|
|
|
|
|
|
-export const initImage = async ({ image }: InitOption) => {
|
|
|
|
|
|
+function getRgba(canvas: any, that: any) {
|
|
|
|
+ const imgWidth = that.width
|
|
|
|
+ const imgHeight = that.height
|
|
|
|
+
|
|
|
|
+ canvas.width = imgWidth
|
|
|
|
+ canvas.height = imgHeight
|
|
|
|
+
|
|
|
|
+ const context = canvas.getContext('2d')
|
|
|
|
+
|
|
|
|
+ context.drawImage(that, 0, 0, imgWidth, imgHeight)
|
|
|
|
+ const imgdatas = context.getImageData(0, 0, imgWidth, imgHeight)
|
|
|
|
+ const imgdata = imgdatas.data
|
|
|
|
+ const newJson: any = {}
|
|
|
|
+ const length = imgdata.length
|
|
|
|
+ for (let i = 0; i < length; i++) {
|
|
|
|
+ if (i % 4 === 0) {
|
|
|
|
+ const alpha = Math.round((imgdata[i + 3] / 255) * 100) / 100
|
|
|
|
+ const rgba = imgdata[i] + ',' + imgdata[i + 1] + ',' + imgdata[i + 2] + ',' + alpha
|
|
|
|
+ if (!newJson[rgba]) {
|
|
|
|
+ newJson[rgba] = 1
|
|
|
|
+ } else {
|
|
|
|
+ newJson[rgba]++
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ let maxNum = 0
|
|
|
|
+ let maxVal = ''
|
|
|
|
+ for (const key in newJson) {
|
|
|
|
+ if (newJson[key] > maxNum) {
|
|
|
|
+ maxNum = newJson[key]
|
|
|
|
+ maxVal = key
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ console.log('rgba:', maxVal + ';次数:' + maxNum)
|
|
|
|
+ return maxVal
|
|
|
|
+}
|
|
|
|
+function getHex(...value: any) {
|
|
|
|
+ const r = value[0].toString(16)
|
|
|
|
+ const g = value[1].toString(16)
|
|
|
|
+ const b = value[2].toString(16)
|
|
|
|
+ let hex = r + g + b
|
|
|
|
+ if (r.slice(0, 1) == r.slice(1, 1) && g.slice(0, 1) == g.slice(1, 1) && b.slice(0, 1) == b.slice(1, 1)) {
|
|
|
|
+ hex = r.slice(0, 1) + g.slice(0, 1) + b.slice(0, 1)
|
|
|
|
+ }
|
|
|
|
+ return hex
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export const initImage = async ({ image, frontColor, setFrontColor }: InitOption) => {
|
|
if (!image) {
|
|
if (!image) {
|
|
return console.warn(`return for img define ${image}`)
|
|
return console.warn(`return for img define ${image}`)
|
|
}
|
|
}
|
|
@@ -28,7 +77,16 @@ export const initImage = async ({ image }: InitOption) => {
|
|
image = await new Promise<HTMLImageElement>((resolve, reject) => {
|
|
image = await new Promise<HTMLImageElement>((resolve, reject) => {
|
|
const img = new Image()
|
|
const img = new Image()
|
|
img.src = image as string
|
|
img.src = image as string
|
|
- img.onload = () => {
|
|
|
|
|
|
+ img.onload = function () {
|
|
|
|
+ if (frontColor && setFrontColor && frontColor.value === 'transparent') {
|
|
|
|
+ const canvas = document.createElement('canvas')
|
|
|
|
+ const maxVal = getRgba(canvas, this)
|
|
|
|
+ console.log('maxVal:', maxVal)
|
|
|
|
+ const splitArr = maxVal.split(',')
|
|
|
|
+ const hex = getHex(Number(splitArr[0]), Number(splitArr[1]), Number(splitArr[2]))
|
|
|
|
+ setFrontColor('#' + hex)
|
|
|
|
+ }
|
|
|
|
+
|
|
resolve(img)
|
|
resolve(img)
|
|
}
|
|
}
|
|
img.onerror = reject
|
|
img.onerror = reject
|
|
@@ -42,11 +100,10 @@ interface MessageData {
|
|
blob?: Blob
|
|
blob?: Blob
|
|
}
|
|
}
|
|
|
|
|
|
-export const useSetImgBg = (option: Ref<SetImgBgOption>) => {
|
|
|
|
|
|
+export const useSetImgBg = (option: Ref<SetImgBgOption>, frontColor?: Ref<string>, setFrontColor?: any) => {
|
|
const imageWorker = new TintImageWorker()
|
|
const imageWorker = new TintImageWorker()
|
|
const drawing = ref<boolean>(false)
|
|
const drawing = ref<boolean>(false)
|
|
const dataUrl = ref('')
|
|
const dataUrl = ref('')
|
|
-
|
|
|
|
imageWorker.addEventListener('message', (e) => {
|
|
imageWorker.addEventListener('message', (e) => {
|
|
const data = e.data as MessageData
|
|
const data = e.data as MessageData
|
|
drawing.value = data.drawing
|
|
drawing.value = data.drawing
|
|
@@ -68,9 +125,22 @@ export const useSetImgBg = (option: Ref<SetImgBgOption>) => {
|
|
dataUrl.value = ''
|
|
dataUrl.value = ''
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- initImage({ image: opt.image }).then((image) => {
|
|
|
|
|
|
+ initImage({ image: opt.image, frontColor, setFrontColor }).then((image) => {
|
|
|
|
+ console.log('opt.image:', opt.image)
|
|
|
|
+
|
|
if (!image) return
|
|
if (!image) return
|
|
createImageBitmap(image).then((imageBitMap) => {
|
|
createImageBitmap(image).then((imageBitMap) => {
|
|
|
|
+ console.log('imageBitMap:', imageBitMap)
|
|
|
|
+ console.log('22222', {
|
|
|
|
+ type: 'init',
|
|
|
|
+ imageBitMap,
|
|
|
|
+ basePoint: opt.basePoint,
|
|
|
|
+ enableSharpen: opt.enableSharpen,
|
|
|
|
+ distance: opt.distance,
|
|
|
|
+ scale: opt.scale,
|
|
|
|
+ rotate: opt.rotate,
|
|
|
|
+ })
|
|
|
|
+
|
|
imageWorker.postMessage(
|
|
imageWorker.postMessage(
|
|
{
|
|
{
|
|
type: 'init',
|
|
type: 'init',
|