|
@@ -1,7 +1,9 @@
|
|
|
-import { ref, onScopeDispose, watch, nextTick, unref } from 'vue'
|
|
|
+import { ref, onScopeDispose, watch, nextTick, unref, computed, watchEffect } from 'vue'
|
|
|
import type { Ref } from 'vue'
|
|
|
import { isDom } from '@/utils/common'
|
|
|
import TintImageWorker from '@/utils/image.worker?worker'
|
|
|
+import useMainStore from '@/store/main'
|
|
|
+import { useRoute } from 'vue-router'
|
|
|
|
|
|
export type RGBA = number[]
|
|
|
|
|
@@ -68,39 +70,51 @@ function getHex(...value: any) {
|
|
|
return hex
|
|
|
}
|
|
|
|
|
|
-export const initImage = async ({ image, frontColor, setFrontColor }: InitOption) => {
|
|
|
- if (!image) {
|
|
|
- return console.warn(`return for img define ${image}`)
|
|
|
- }
|
|
|
-
|
|
|
- if (!isDom<HTMLImageElement>(image)) {
|
|
|
- image = await new Promise<HTMLImageElement>((resolve, reject) => {
|
|
|
- const img = new Image()
|
|
|
- img.src = image as string
|
|
|
- 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)
|
|
|
- }
|
|
|
- img.onerror = reject
|
|
|
- })
|
|
|
- }
|
|
|
- return image
|
|
|
-}
|
|
|
-
|
|
|
interface MessageData {
|
|
|
drawing: boolean
|
|
|
blob?: Blob
|
|
|
}
|
|
|
|
|
|
export const useSetImgBg = (option: Ref<SetImgBgOption>, frontColor?: Ref<string>, setFrontColor?: any) => {
|
|
|
+ const { fullPath } = useRoute()
|
|
|
+ const mainStore = useMainStore()
|
|
|
+ const userSetColor = computed(() => {
|
|
|
+ return mainStore.userMarkConfig?.[fullPath]?.frontColor
|
|
|
+ })
|
|
|
+ watchEffect(() => {
|
|
|
+ if (!!userSetColor.value && setFrontColor) {
|
|
|
+ setFrontColor(userSetColor.value)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ const initImage = async ({ image, frontColor, setFrontColor }: InitOption) => {
|
|
|
+ if (!image) {
|
|
|
+ return console.warn(`return for img define ${image}`)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!isDom<HTMLImageElement>(image)) {
|
|
|
+ image = await new Promise<HTMLImageElement>((resolve, reject) => {
|
|
|
+ const img = new Image()
|
|
|
+ img.src = image as string
|
|
|
+ img.onload = function () {
|
|
|
+ if (frontColor && setFrontColor) {
|
|
|
+ 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]))
|
|
|
+ if (!userSetColor?.value) {
|
|
|
+ setFrontColor('#' + hex)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ resolve(img)
|
|
|
+ }
|
|
|
+ img.onerror = reject
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return image
|
|
|
+ }
|
|
|
+
|
|
|
const imageWorker = new TintImageWorker()
|
|
|
const drawing = ref<boolean>(false)
|
|
|
const dataUrl = ref('')
|
|
@@ -126,8 +140,6 @@ export const useSetImgBg = (option: Ref<SetImgBgOption>, frontColor?: Ref<string
|
|
|
return
|
|
|
}
|
|
|
initImage({ image: opt.image, frontColor, setFrontColor }).then((image) => {
|
|
|
- console.log('opt.image:', opt.image)
|
|
|
-
|
|
|
if (!image) return
|
|
|
createImageBitmap(image).then((imageBitMap) => {
|
|
|
imageWorker.postMessage(
|