|
| 1 | +<script setup lang="ts"> |
| 2 | +import { FortuneWheel } from 'vue3-fortune-wheel'; |
| 3 | +import type { Data, ImgParams } from 'vue3-fortune-wheel'; |
| 4 | +
|
| 5 | +const base = import.meta.env.BASE_URL; |
| 6 | +
|
| 7 | +const choices = useQueryParamOrStorage({ name: 'choices', storageName: 'fortune-wheel:chs', defaultValue: '' }); |
| 8 | +const spinDuration = useQueryParamOrStorage({ name: 'duration', storageName: 'fortune-wheel:dur', defaultValue: 5 }); |
| 9 | +
|
| 10 | +const colors = [ |
| 11 | + { bgColor: '#FF5733', color: '#FFFFFF' }, |
| 12 | + { bgColor: '#2E86C1', color: '#FFFFFF' }, |
| 13 | + { bgColor: '#28B463', color: '#FFFFFF' }, |
| 14 | + { bgColor: '#F1C40F', color: '#000000' }, |
| 15 | + { bgColor: '#8E44AD', color: '#FFFFFF' }, |
| 16 | + { bgColor: '#E74C3C', color: '#FFFFFF' }, |
| 17 | + { bgColor: '#1ABC9C', color: '#000000' }, |
| 18 | + { bgColor: '#34495E', color: '#FFFFFF' }, |
| 19 | + { bgColor: '#D35400', color: '#FFFFFF' }, |
| 20 | + { bgColor: '#2C3E50', color: '#FFFFFF' }, |
| 21 | +]; |
| 22 | +
|
| 23 | +const wheel = ref<InstanceType<typeof FortuneWheel> | null>(null); |
| 24 | +const data = computed(() => { |
| 25 | + return (choices.value || '').split('\n') |
| 26 | + .filter(s => s && s !== '') |
| 27 | + .map((choice, index) => ({ id: index + 1, value: choice, ...colors[index % colors.length] })); |
| 28 | +}); |
| 29 | +
|
| 30 | +const logo: ImgParams = { |
| 31 | + src: `${base}logo.png`, |
| 32 | + width: 100, |
| 33 | + height: 100, |
| 34 | +}; |
| 35 | +
|
| 36 | +const randomGift = computed(() => { |
| 37 | + return Math.floor(Math.random() * data.value.length) + 1; |
| 38 | +}); |
| 39 | +
|
| 40 | +const result = ref<Data>(null); |
| 41 | +function done(result: Data) { |
| 42 | + result.value = result; |
| 43 | +} |
| 44 | +
|
| 45 | +function restart() { |
| 46 | + wheel.value?.spin(); |
| 47 | +} |
| 48 | +</script> |
| 49 | + |
| 50 | +<template> |
| 51 | + <div> |
| 52 | + <n-form-item label="Spin Duration (seconds)"> |
| 53 | + <n-input-number v-model:value="spinDuration" :min="1" /> |
| 54 | + </n-form-item> |
| 55 | + |
| 56 | + <c-input-text |
| 57 | + v-model:value="choices" |
| 58 | + label="Wheel choices (one per line)" |
| 59 | + placeholder="Wheel choices (one per line)" |
| 60 | + /> |
| 61 | + |
| 62 | + <n-divider /> |
| 63 | + |
| 64 | + <FortuneWheel |
| 65 | + ref="wheel" |
| 66 | + v-model="randomGift" |
| 67 | + middle-circle |
| 68 | + :img-params="logo" |
| 69 | + :anim-duration="spinDuration * 1000" |
| 70 | + :data="data" |
| 71 | + @done="done" |
| 72 | + /> |
| 73 | + |
| 74 | + <c-button @click="restart()"> |
| 75 | + Restart |
| 76 | + </c-button> |
| 77 | + |
| 78 | + <n-divider /> |
| 79 | + |
| 80 | + <c-card v-if="result" title="Result"> |
| 81 | + <input-copyable :value="result.value" readonly /> |
| 82 | + </c-card> |
| 83 | + </div> |
| 84 | +</template> |
0 commit comments