1.1.0 • Published 1 year ago

vue-wheel-of-names-beta v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Vue-Spin-The-Wheel For Vue3

Alternative name: Wheel of Names, Wheel of Fortune, Spin the wheel

"Spin the Wheel" is a versatile digital tool designed to randomly select a name or item from a list. It is commonly used in various settings such as classrooms, meetings, raffles, and any situation requiring a random selection. This Vue component simplifies the creation and integration of a spin-the-wheel feature into your Vue3 applications, offering an engaging and interactive experience for users.

Install

yarn add vue-spin-the-wheel

or

npm install vue-spin-the-wheel

Demo

online

https://vue-spin-the-wheel.yaskur.com

Usage

<template>
  <div>
    <!-- type: image -->
    <SpinTheWheel
      style="width: 500px; max-width: 100%;"
      ref="wheelEl"
      type="image"
      :useWeight="true"
      :verify="canvasVerify"
      :prizeId="prizeId"
      :angleBase="-2"
      :prizes="prizesImage"
      @rotateStart="onImageRotateStart"
      @rotateEnd="onRotateEnd"
    >
      <template #wheel>
        <img src="./assets/wheel.png" style="width: 100%;transform: rotateZ(60deg)" />
      </template>
      <template #button>
        <img src="./assets/button.png" style="width: 180px"/>
      </template>
    </SpinTheWheel>


    <!-- type: canvas -->
    <SpinTheWheel
      style="width: 500px; max-width: 100%;"
      :verify="canvasVerify"
      :canvas="canvasOptions"
      :prizes="prizesCanvas"
      @rotateStart="onCanvasRotateStart"
      @rotateEnd="onRotateEnd"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import SpinTheWheel from 'vue-spin-the-wheel'
import 'vue-spin-the-wheel/style.css'

const prizeId = ref(0)

const wheelEl = ref()
const canvasVerify = ref(false) // Whether the turntable in canvas mode is enabled for verification
const verifyDuration = 2
const canvasOptions = {
  btnWidth: 140,
  borderColor: '#584b43',
  borderWidth: 6,
  lineHeight: 30
}

const prizesCanvas = [
  {
    id: 1, //* The unique id of each prize, an integer greater than 0
    name: 'Blue', // Prize name, display value when type is canvas (this parameter is not needed when type is image)
    value: 'Blue\'s value', //* Prize value, return value after spinning
    bgColor: '#45ace9', // Background color (no need for this parameter when type is image)
    color: '#ffffff', // Font color (this parameter is not required when type is image)
    probability: 30 //* Probability, up to 4 decimal places (the sum of the probabilities of all prizes
  },
  {
    id: 2,
    name: 'Red',
    value: 'Red\'s value',
    bgColor: '#dd3832',
    color: '#ffffff',
    probability: 40
  },
  {
    id: 3,
    name: 'Yellow',
    value: 'Yellow\'s value',
    bgColor: '#fef151',
    color: '#ffffff',
    probability: 30
  }
]

const prizesImage = [
  {
    id: 1, //* The unique id of each prize, an integer greater than 0
    value: 'Blue\'s value', //* Prize value, return value after spinning
    weight: 1 // Weight, if useWeight is true, the probability is calculated by weight (weight must be an integer), so probability is invalid
  },
  {
    id: 2,
    value: 'Red\'s value',
    weight: 0
  },
  {
    id: 3,
    value: 'Yellow\'s value',
    weight: 0
  }
]

const prizeRes = computed(() => {
  return prizesCanvas.find(item => item.id === prizeId.value) || prizesCanvas[0]
})


onMounted(() => {
  wheelEl.value.startRotate() // Can start rotation
})

// Simulate the request back-end interface
function testRequest (verified, duration) { // verified: whether to pass the verification, duration: delay time
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(verified)
    }, duration)
  })
}

function onCanvasRotateStart (rotate) {
  if (canvasVerify.value) {
    const verified = true // true: the test passed the verification, false: the test failed the verification
    testRequest(verified, verifyDuration * 1000).then((verifiedRes) => {
      if (verifiedRes) {
        console.log('Verification passed, start to rotate')
        rotate() // Call the callback, start spinning
        canvasVerify.value = false // Turn off verification mode
      } else {
        alert('Failed verification')
      }
    })
    return
  }
  console.log('onCanvasRotateStart')
}

function onImageRotateStart () {
  console.log('onImageRotateStart')
}

function onRotateEnd (prize) {
  alert(prize.value)
}

function onChangePrize (id) {
  prizeId.value = id
}
</script>

SpinTheWheel Events

Event nameDescriptionCallback parameters
rotateStartTriggered when the dial button is clickedWhen verify is true, there will be a callback, and the callback function will be called to start spinning
rotateEndTriggered at the end of the turntable animationThe entire prize Object

SpinTheWheel Methods

Event nameDescriptionCallback parameters
startRotateCan trigger rotationWhen verify is true, the callback function is triggered in the rotateStart event

SpinTheWheel Attributes

ParametersDescriptionTypeDefault Value
typeType of turntable (canvas, image)Stringcanvas
useWeightWhether to calculate probability by weightBooleanfalse
disabledWhether to disable (after disabled, click the button will not rotate)Booleanfalse
verifyWhether to enable verification modeBooleanfalse
canvas.radiusRadius of circle (type: canvas)Number250
canvas.textRadiusThe distance of the text from the center of the circle (type: canvas)Number190
canvas.textLengthA few characters in one line of the prize, beyond the line break (maximum two lines)Number6
canvas.textDirectionPrize text direction (horizontal, vertical)Stringhorizontal
canvas.lineHeightText line height (type: canvas)Number20
canvas.borderWidthRound outer border (type: canvas)Number0
canvas.borderColorColor value of the outer border (type: canvas)Stringtransparent
canvas.btnTextButton text (type: canvas)StringGO
canvas.btnWidthButton width (px)Number140
canvas.fontSizePrize size (px)Number34
durationTime to complete one rotation (unit: ms)Number6000
timingFunCss time function of rotation transitionStringcubic-bezier(0.36, 0.95, 0.64, 1)
angleBaseNumber of rotations (angleBase * 360 is the total angle of one rotation, it can be reversed when it is a negative number)Number10
prizeIdSpecify the id, it will spin to the prize of this id every time (when it is 0, the value can be changed during the rotation according to the probability of each prize itself to complete various show operations)Number0
prizesPrize list (structure reference Usage)Array/

📑 License

Vue Spin The Wheel is available under the MIT software license.


This Vue3 component is originally derived from vue-fortune-wheel. However, it lacked certain features that I needed. Therefore, I forked the project and added more features and capabilities to enhance its functionality and versatility.

1.1.0

1 year ago

1.0.0

1 year ago

2.4.0

1 year ago

2.3.14

1 year ago

2.3.12

1 year ago

2.3.11

1 year ago

2.3.9

1 year ago

2.3.8

1 year ago

2.3.7

1 year ago

2.3.6

1 year ago

2.3.5

1 year ago

2.3.4

1 year ago

2.3.2

1 year ago

2.3.1

1 year ago

2.2.1

1 year ago

0.1.2

1 year ago

2.1.2

1 year ago