Line data Source code
1 16 : <template>
2 330 : <div
3 : id="maskEditor_brush"
4 : data-testid="brush-cursor"
5 : :style="{
6 : position: 'absolute',
7 : opacity: brushOpacity,
8 : width: `${brushSize}px`,
9 : height: `${brushSize}px`,
10 : left: `${brushLeft}px`,
11 : top: `${brushTop}px`,
12 : borderRadius: borderRadius,
13 : pointerEvents: 'none'
14 : }"
15 : >
16 : <div
17 : id="maskEditor_brushPreviewGradient"
18 : data-testid="brush-cursor-gradient"
19 : :style="{
20 : display: gradientVisible ? 'block' : 'none',
21 : background: gradientBackground
22 : }"
23 : ></div>
24 : </div>
25 : </template>
26 :
27 : <script setup lang="ts">
28 : import { useElementBounding } from '@vueuse/core'
29 : import { computed, watch } from 'vue'
30 :
31 : import { getEffectiveBrushSize, getEffectiveHardness } from '#/brushUtils'
32 : import { BrushShape } from '#/types'
33 : import { useMaskEditorStore } from '#/maskEditorStore'
34 :
35 : const { containerRef } = defineProps<{
36 : containerRef?: HTMLElement
37 : }>()
38 :
39 29 : const store = useMaskEditorStore()
40 29 : const {
41 : left: containerOffsetLeft,
42 : top: containerOffsetTop,
43 : update: updateContainerOffset
44 : } = useElementBounding(() => containerRef)
45 :
46 29 : watch(() => store.cursorPoint, updateContainerOffset, { flush: 'sync' })
47 :
48 29 : const brushOpacity = computed(() => {
49 57 : return store.brushVisible ? 1 : 0
50 : })
51 :
52 29 : const brushRadius = computed(() => {
53 32 : const size = store.brushSettings.size
54 32 : const hardness = store.brushSettings.hardness
55 : const effectiveSize = getEffectiveBrushSize(size, hardness)
56 32 : return effectiveSize * store.zoomRatio
57 : })
58 :
59 29 : const brushSize = computed(() => {
60 32 : return brushRadius.value * 2
61 : })
62 :
63 29 : const brushLeft = computed(() => {
64 313 : return (
65 : store.cursorPoint.x +
66 : store.panOffset.x -
67 : brushRadius.value -
68 : containerOffsetLeft.value
69 : )
70 : })
71 :
72 29 : const brushTop = computed(() => {
73 313 : return (
74 : store.cursorPoint.y +
75 : store.panOffset.y -
76 : brushRadius.value -
77 : containerOffsetTop.value
78 : )
79 : })
80 :
81 29 : const borderRadius = computed(() => {
82 29 : return store.brushSettings.type === BrushShape.Rect ? '0%' : '50%'
83 : })
84 :
85 29 : const gradientVisible = computed(() => {
86 29 : return store.brushPreviewGradientVisible
87 : })
88 :
89 29 : const gradientBackground = computed(() => {
90 32 : const size = store.brushSettings.size
91 32 : const hardness = store.brushSettings.hardness
92 32 : const effectiveSize = getEffectiveBrushSize(size, hardness)
93 32 : const effectiveHardness = getEffectiveHardness(size, hardness, effectiveSize)
94 :
95 32 : if (effectiveHardness === 1) {
96 32 : return 'rgba(255, 0, 0, 0.5)'
97 : }
98 :
99 0 : const midStop = effectiveHardness * 100
100 0 : const outerStop = 100
101 : // Add an intermediate stop to approximate the squared falloff
102 : // At 50% of the fade region, squared falloff is 0.25 (relative to max)
103 : const fadeMidStop = midStop + (outerStop - midStop) * 0.5
104 :
105 0 : return `radial-gradient(
106 : circle,
107 : rgba(255, 0, 0, 0.5) 0%,
108 : rgba(255, 0, 0, 0.5) ${midStop}%,
109 : rgba(255, 0, 0, 0.125) ${fadeMidStop}%,
110 : rgba(255, 0, 0, 0) ${outerStop}%
111 : )`
112 : })
113 : </script>
|