Skip to content

Commit e0d344f

Browse files
committedDec 22, 2023
fix: 点击色板颜色改变失效
1 parent 7791323 commit e0d344f

File tree

2 files changed

+80
-73
lines changed

2 files changed

+80
-73
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ node_modules
1111
dist
1212
dist-ssr
1313
*.local
14+
package-lock.json
1415

1516
# Editor directories and files
1617
.vscode/*
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,99 @@
11
<template>
22
<div ref="pickerAreaRef" class="spectrum-map" :style="pickerStyle">
3-
<div class="spectrum"></div>
3+
<div class="spectrum" @click="spectrumClick"></div>
44
<div class="picker-cursor" :style="pointerStyle"></div>
55
</div>
66
</template>
77

88
<script lang="ts" setup>
9-
import { ref, reactive, onMounted, computed } from 'vue'
10-
import { clamp, isDefined, usePointerSwipe } from '@vueuse/core'
11-
import {Color} from '@/utils/color/color'
9+
import { ref, reactive, onMounted, computed } from "vue"
10+
import { clamp, isDefined, usePointerSwipe } from "@vueuse/core"
11+
import { Color } from "@/utils/color/color"
1212
13-
const props = defineProps<{
14-
red: number
15-
green: number
16-
blue: number
17-
alpha: number
18-
hue: number
19-
saturation: number
20-
value: number
21-
updateColor: Function
22-
}>()
13+
const props = defineProps<{
14+
red: number;
15+
green: number;
16+
blue: number;
17+
alpha: number;
18+
hue: number;
19+
saturation: number;
20+
value: number;
21+
updateColor: Function;
22+
}>()
2323
24-
const pickerAreaRef = ref<HTMLDivElement>()
24+
const pickerAreaRef = ref<HTMLDivElement>()
2525
26-
const state = reactive({
27-
width: 0,
28-
height: 0,
29-
})
26+
const state = reactive({
27+
width: 0,
28+
height: 0
29+
})
3030
31-
const offsetLeft = computed(() => {
32-
return Math.round(props.saturation * state.width - 8)
33-
})
31+
const offsetLeft = computed(() => {
32+
return Math.round(props.saturation * state.width - 8)
33+
})
3434
35-
const offsetTop = computed(() =>
36-
Math.max(Math.round(state.height - props.value * state.height - 14), -6),
37-
)
35+
const offsetTop = computed(() =>
36+
Math.max(Math.round(state.height - props.value * state.height - 14), -6)
37+
)
3838
39-
const pointerStyle = computed(() => {
40-
return {
41-
backgroundColor: `rgb(${props.red}, ${props.green}, ${props.blue})`,
42-
left: `${offsetLeft.value}px`,
43-
top: `${offsetTop.value}px`,
44-
}
45-
})
39+
const pointerStyle = computed(() => {
40+
return {
41+
backgroundColor: `rgb(${props.red}, ${props.green}, ${props.blue})`,
42+
left: `${offsetLeft.value}px`,
43+
top: `${offsetTop.value}px`
44+
}
45+
})
4646
47-
const pickerStyle = computed(() => {
48-
const color = new Color(`hsva(${props.hue},1,1,1)`)
49-
// const color = new Color(new HSVA(props.hue, 1, 1, 1))
50-
// const { r, g, b } = color.rgba
51-
return {
52-
backgroundColor: color.rgb,
53-
}
54-
})
47+
const pickerStyle = computed(() => {
48+
const color = new Color(`hsva(${props.hue},1,1,1)`)
49+
// const color = new Color(new HSVA(props.hue, 1, 1, 1))
50+
// const { r, g, b } = color.rgba
51+
return {
52+
backgroundColor: color.rgb
53+
}
54+
})
5555
56-
onMounted(() => {
57-
if (isDefined(pickerAreaRef)) {
58-
state.width = pickerAreaRef.value.clientWidth
59-
state.height = pickerAreaRef.value.clientHeight
60-
}
61-
})
56+
onMounted(() => {
57+
if (isDefined(pickerAreaRef)) {
58+
state.width = pickerAreaRef.value.clientWidth
59+
state.height = pickerAreaRef.value.clientHeight
60+
}
61+
})
6262
63-
let rect: DOMRect | undefined
63+
let rect: DOMRect | undefined
6464
65-
const getColor = () => {
66-
if (!isDefined(rect)) return
67-
let x = clamp(posEnd.x - rect.x, 0, state.width)
68-
let y = clamp(posEnd.y - rect.y, 0, state.height)
69-
const saturation = x / state.width
70-
const value = 1 - y / state.height
71-
const color = new Color(`hsva(${props.hue},${saturation},${value},${props.alpha})`)
72-
// const color = new Color(new HSVA(props.hue, saturation, value, props.alpha))
73-
return {
74-
...color.getRgba(),
75-
saturation,
76-
value,
77-
}
65+
const getColor = () => {
66+
if (!isDefined(rect)) return
67+
let x = clamp(posEnd.x - rect.x, 0, state.width)
68+
let y = clamp(posEnd.y - rect.y, 0, state.height)
69+
const saturation = x / state.width
70+
const value = 1 - y / state.height
71+
const color = new Color(
72+
`hsva(${props.hue},${saturation},${value},${props.alpha})`
73+
)
74+
// const color = new Color(new HSVA(props.hue, saturation, value, props.alpha))
75+
return {
76+
...color.getRgba(),
77+
saturation,
78+
value
7879
}
80+
}
81+
82+
const spectrumClick = (obj: any) => {
83+
props.updateColor(getColor(), "onChange")
84+
}
7985
80-
const { posEnd } = usePointerSwipe(pickerAreaRef, {
81-
threshold: 0,
82-
onSwipeStart() {
83-
rect = pickerAreaRef.value?.getBoundingClientRect()
84-
props.updateColor(getColor(), 'onStartChange')
85-
},
86-
onSwipe() {
87-
props.updateColor(getColor(), 'onChange')
88-
},
89-
onSwipeEnd() {
90-
props.updateColor(getColor(), 'onEndChange')
91-
},
92-
})
86+
const { posEnd } = usePointerSwipe(pickerAreaRef, {
87+
threshold: 0,
88+
onSwipeStart() {
89+
rect = pickerAreaRef.value?.getBoundingClientRect()
90+
props.updateColor(getColor(), "onStartChange")
91+
},
92+
onSwipe() {
93+
props.updateColor(getColor(), "onChange")
94+
},
95+
onSwipeEnd() {
96+
props.updateColor(getColor(), "onEndChange")
97+
}
98+
})
9399
</script>

0 commit comments

Comments
 (0)
Please sign in to comment.