|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <div>{{ pattern }}</div> |
| 4 | + <svg :class="`patternlock ${success === true ? 'success' : (success === false ? 'error' : '')}`" ref="svg" id="lock" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> |
| 5 | + <g ref="lockActives" class="lock-actives"></g> |
| 6 | + <g ref="lockLines" class="lock-lines"></g> |
| 7 | + <g class="lock-dots"> |
| 8 | + <circle cx="20" cy="20" r="2"/> |
| 9 | + <circle cx="50" cy="20" r="2"/> |
| 10 | + <circle cx="80" cy="20" r="2"/> |
| 11 | + |
| 12 | + <circle cx="20" cy="50" r="2"/> |
| 13 | + <circle cx="50" cy="50" r="2"/> |
| 14 | + <circle cx="80" cy="50" r="2"/> |
| 15 | + |
| 16 | + <circle cx="20" cy="80" r="2"/> |
| 17 | + <circle cx="50" cy="80" r="2"/> |
| 18 | + <circle cx="80" cy="80" r="2"/> |
| 19 | + </g> |
| 20 | + </svg> |
| 21 | + </div> |
| 22 | +</template> |
| 23 | + |
| 24 | +<script lang="ts" setup> |
| 25 | + // Props |
| 26 | + const props = defineProps<{ |
| 27 | + correctPattern: number[] |
| 28 | + }>() |
| 29 | +
|
| 30 | + const svgns = 'http://www.w3.org/2000/svg' |
| 31 | + const moveEvent = ['touchmove', 'mousemove'] |
| 32 | + const start = ['touchstart', 'mousedown'] |
| 33 | +
|
| 34 | + const success = ref<boolean | undefined>() |
| 35 | + const code = ref<SVGCircleElement[]>([]) |
| 36 | + const svg = useTemplateRef('svg') |
| 37 | + const lines = useTemplateRef('lockLines') |
| 38 | + const actives = useTemplateRef('lockActives') |
| 39 | +
|
| 40 | + let dots: SVGCircleElement[] = [] |
| 41 | + let pt: DOMPoint |
| 42 | + let currentLine: SVGLineElement | undefined |
| 43 | + let currentHandler: EventListener | undefined |
| 44 | +
|
| 45 | + const pattern: ComputedRef<number[]> = computed(() => { |
| 46 | + if (code.value) { |
| 47 | + return code.value.map(i => dots.indexOf(i) + 1) |
| 48 | + } else { |
| 49 | + return [] |
| 50 | + } |
| 51 | + }) |
| 52 | +
|
| 53 | + function vibrate() { |
| 54 | + // @ts-ignore |
| 55 | + if (navigator.vibrate) { |
| 56 | + window.navigator.vibrate(25) |
| 57 | + } |
| 58 | + } |
| 59 | +
|
| 60 | + function clear() { |
| 61 | + code.value = [] |
| 62 | + currentLine = undefined |
| 63 | + currentHandler = undefined |
| 64 | + success.value = undefined |
| 65 | + if (lines.value) { |
| 66 | + lines.value.innerHTML = '' |
| 67 | + } |
| 68 | + if (actives.value) { |
| 69 | + actives.value.innerHTML = '' |
| 70 | + } |
| 71 | + } |
| 72 | +
|
| 73 | + function end() { |
| 74 | + stopTrack(currentLine) |
| 75 | + if (currentLine) { |
| 76 | + currentLine.remove() |
| 77 | + } |
| 78 | + moveEvent.forEach(ev => svg.value?.removeEventListener(ev, discoverDot)) |
| 79 | + |
| 80 | + if (props.correctPattern && props.correctPattern.length > 0) { |
| 81 | + success.value = props.correctPattern.join(',') === pattern.value.join(',') |
| 82 | + } |
| 83 | + } |
| 84 | +
|
| 85 | + function stopTrack(line: SVGLineElement | undefined, target?: SVGCircleElement) { |
| 86 | + if (line === undefined) { |
| 87 | + return |
| 88 | + } |
| 89 | + moveEvent.forEach(ev => { |
| 90 | + if (currentHandler) { |
| 91 | + svg.value?.removeEventListener(ev, currentHandler) |
| 92 | + } |
| 93 | + }) |
| 94 | + if (target === undefined) { |
| 95 | + return |
| 96 | + } |
| 97 | + const x = target.getAttribute('cx') |
| 98 | + const y = target.getAttribute('cy') |
| 99 | + line.setAttribute('x2', `${x}`) |
| 100 | + line.setAttribute('y2', `${y}`) |
| 101 | + } |
| 102 | +
|
| 103 | + function beginTrack(target: SVGCircleElement) { |
| 104 | + code.value.push(target) |
| 105 | + const x = target.getAttribute('cx') |
| 106 | + const y = target.getAttribute('cy') |
| 107 | + if (x && y) { |
| 108 | + const line = createNewLine(x, y) |
| 109 | + const marker = createNewMarker(x, y) |
| 110 | + actives.value?.appendChild(marker) |
| 111 | + currentHandler = updateLine(line) |
| 112 | + moveEvent.forEach(ev => { |
| 113 | + if (currentHandler) { |
| 114 | + svg.value?.addEventListener(ev, currentHandler) |
| 115 | + } |
| 116 | + }) |
| 117 | + lines.value?.appendChild(line) |
| 118 | + vibrate() |
| 119 | + return line |
| 120 | + } else { |
| 121 | + return undefined |
| 122 | + } |
| 123 | + } |
| 124 | +
|
| 125 | + function createNewMarker(x: string, y: string) { |
| 126 | + const marker = document.createElementNS(svgns, 'circle') |
| 127 | + marker.setAttribute('cx', x) |
| 128 | + marker.setAttribute('cy', y) |
| 129 | + marker.setAttribute('r', '6') |
| 130 | + return marker |
| 131 | + } |
| 132 | +
|
| 133 | + function createNewLine(x1: string, y1: string, x2?: string, y2?: string) { |
| 134 | + const line = document.createElementNS(svgns, 'line') |
| 135 | + line.setAttribute('x1', x1) |
| 136 | + line.setAttribute('y1', y1) |
| 137 | + if (x2 === undefined || y2 == undefined) { |
| 138 | + line.setAttribute('x2', x1) |
| 139 | + line.setAttribute('y2', y1) |
| 140 | + } else { |
| 141 | + line.setAttribute('x2', x2) |
| 142 | + line.setAttribute('y2', y2) |
| 143 | + } |
| 144 | + return line |
| 145 | + } |
| 146 | +
|
| 147 | + function isUsed(target: SVGCircleElement) { |
| 148 | + for (let i = 0; i < code.value.length; i++) { |
| 149 | + if (code.value[i] === target) { |
| 150 | + return true |
| 151 | + } |
| 152 | + } |
| 153 | + return false |
| 154 | + } |
| 155 | +
|
| 156 | + function isAvailable(target: SVGCircleElement) { |
| 157 | + for (let i = 0; i < dots.length; i++) { |
| 158 | + if (dots[i] === target) { |
| 159 | + return true |
| 160 | + } |
| 161 | + } |
| 162 | + return false |
| 163 | + } |
| 164 | +
|
| 165 | + function updateLine(line: SVGLineElement) { |
| 166 | + return (e: Event) => { |
| 167 | + e.preventDefault() |
| 168 | + if (currentLine !== line) { |
| 169 | + return |
| 170 | + } |
| 171 | + let pos = svgPosition(e.target as SVGGraphicsElement, e) |
| 172 | + line.setAttribute('x2', `${pos.x}`) |
| 173 | + line.setAttribute('y2', `${pos.y}`) |
| 174 | + return false |
| 175 | + } |
| 176 | + } |
| 177 | +
|
| 178 | + function getMousePos(e: any) { |
| 179 | + return { |
| 180 | + x: e.clientX || e.originalEvent.touches[0].clientX, |
| 181 | + y :e.clientY || e.originalEvent.touches[0].clientY |
| 182 | + } |
| 183 | + } |
| 184 | +
|
| 185 | + function discoverDot(e: Event, target?: SVGCircleElement) { |
| 186 | + if (!target) { |
| 187 | + const { x, y } = getMousePos(e) |
| 188 | + target = document.elementFromPoint(x, y) as SVGCircleElement |
| 189 | + } |
| 190 | + if (target) { |
| 191 | + if (isAvailable(target) && !isUsed(target)) { |
| 192 | + stopTrack(currentLine, target) |
| 193 | + currentLine = beginTrack(target) |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | +
|
| 198 | + function svgPosition(element: SVGGraphicsElement | null, e: Event) { |
| 199 | + let { x, y } = getMousePos(e) |
| 200 | + pt.x = x |
| 201 | + pt.y = y |
| 202 | + return pt.matrixTransform(element?.getScreenCTM()?.inverse()) |
| 203 | + } |
| 204 | +
|
| 205 | + onMounted(() => { |
| 206 | + if (svg.value) { |
| 207 | + dots = Array.from(svg.value.querySelectorAll('circle')) |
| 208 | + pt = svg.value.createSVGPoint() |
| 209 | +
|
| 210 | + start.forEach(ev => svg.value?.addEventListener(ev, e => { |
| 211 | + clear() |
| 212 | + e.preventDefault() |
| 213 | +
|
| 214 | + moveEvent.forEach(eev => svg.value?.addEventListener(eev, discoverDot)) |
| 215 | + const endEvent = e.type == 'touchstart' ? 'touchend' : 'mouseup' |
| 216 | + document.addEventListener(endEvent, () => end(), { once: true }) |
| 217 | + })) |
| 218 | + } |
| 219 | + }) |
| 220 | +</script> |
| 221 | + |
| 222 | +<style> |
| 223 | +#lock { |
| 224 | + width: 100%; |
| 225 | + height: calc(100% - 15vh); |
| 226 | + padding-bottom: 12vh; |
| 227 | + min-height: 120px; |
| 228 | +} |
| 229 | +
|
| 230 | +svg.patternlock g.lock-lines line { |
| 231 | + stroke-width: 1.5; |
| 232 | + stroke: black; |
| 233 | + opacity: 0.5; |
| 234 | +} |
| 235 | +
|
| 236 | +svg.patternlock g.lock-dots circle { |
| 237 | + stroke: transparent; |
| 238 | + fill: black; |
| 239 | + stroke-width: 13.5; |
| 240 | +} |
| 241 | +
|
| 242 | +svg.patternlock g.lock-actives circle { |
| 243 | + fill: black; |
| 244 | + opacity: .4; |
| 245 | + animation: lock-activate-dot .15s 0s ease 1; |
| 246 | + transform-origin: center; |
| 247 | +} |
| 248 | +
|
| 249 | +svg.patternlock g.lock-lines line { |
| 250 | + stroke-width: 1.5; |
| 251 | + stroke-linecap: round; |
| 252 | +} |
| 253 | +
|
| 254 | +svg.patternlock.success g.lock-actives circle { |
| 255 | + fill: var(--bs-success); |
| 256 | +} |
| 257 | +
|
| 258 | +svg.patternlock.error g.lock-actives circle { |
| 259 | + fill: var(--bs-danger); |
| 260 | +} |
| 261 | +
|
| 262 | +@keyframes lock-activate-dot { |
| 263 | + 0% { |
| 264 | + transform: scale(0); |
| 265 | + } |
| 266 | + 75% { |
| 267 | + transform: scale(1.1); |
| 268 | + } |
| 269 | + 100% { |
| 270 | + transform: scale(1.0); |
| 271 | + } |
| 272 | +} |
| 273 | +</style> |
0 commit comments