1 minute read

Tags: ,

GitHub

我要學50音

  • 實作後的成果
  • 隨著每天的更新都會變化唷唷~~

Day 26

import { Grid } from "@mui/material";
import React from "react";
import WordsSpinningWheel from "./WordsSpinningWheel";

//
// IMPORT ZONE
//

function Day25() {
  return (
    <Grid
      container
      spacing=
      style=
    >
      <Grid item xs={12}>
        <WordsSpinningWheel />
      </Grid>
    </Grid>
  );
}

export default Day25;
import React, { useEffect, useState } from "react";
import { hiraganaList } from "../../shared/model/aiueoObj";
import "./wordsSpinningWheelStyle.css";
import { Tooltip } from "@mui/material";
//
// IMPORT ZONE
//

const WordsSpinningWheel = () => {
  const [wordsList, setWordsList] = useState([]);
  useEffect(() => {
    const tmpWordsList = hiraganaList.reduce((state, current) => {
      const words = current.words.filter((d) => {
        if (d) {
          return d;
        }
        return null;
      });
      state = state.concat(words);
      return state;
    }, []);
    console.log(tmpWordsList.length);
    setWordsList(tmpWordsList);
  }, []);

  const handleSpinOnClick = () => {
    let wheel = document.querySelector(".wheel");
    let value = Math.ceil(Math.random() * 3600);

    wheel.style.transform = `rotate(${value}deg)`;
  };

  return (
    <div class="main">
      <div class="container">
        <div class="spinBtn" onClick={() => handleSpinOnClick()}>
          SPIN
        </div>
        <div class="wheel">
          {wordsList.map((d, idx) => (
            <div
              key={idx}
              class="number"
              style=--i
            >
              <Tooltip style= title={d}>
                <span>{d}</span>
              </Tooltip>
            </div>
          ))}
        </div>
      </div>
    </div>
  );
};

export default WordsSpinningWheel;
.main{
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 80vh;
    background: #333;
}

.container {
position: relative;
width: 450px;
height: 450px;
display: flex;
justify-content: center;
align-items: center;
}

.container .spinBtn {
    position: absolute;
    width: 60px;
    height: 60px;
    background: #fff;
    border-radius: 50%;
    z-index: 10;
    display: flex;
    justify-content: center;
    align-items: center;
    text-transform: uppercase;
    font-weight: 600;
    color:#333;
    letter-spacing: 0.1em;
    border: 4px solid rgba(0,0,0,0.75);
    cursor: pointer;
    user-select: none;
}

.container .spinBtn::before{
    content: "";
    position: absolute;
    top: -28px;
    width: 20px;
    height: 30px;
    background: #fff;
    clip-path: polygon(50% 0%, 15% 100%, 85% 100%);
}

.container .wheel {
    position: absolute;
    top:0;
    left:0;
    width: 100%;
    height: 100%;
    background: #333;
    border-radius: 50%;
    overflow: hidden;
    box-shadow: 0 0 0 5px #333, 0 0 0 15px #fff, 0 0 0 18px #111;
    transition: transform 5s ease-in-out ;
}

.container .wheel .number{
    position: absolute;
    width: 50%;
    height: 50%;
    background: var(--clr);
    transform-origin: bottom right;
    transform: rotate(calc(7.88deg * var(--i)));
    clip-path: polygon(0 0, 18% 0, 100% 100%, 0 5%);
    display: flex;
    justify-content: center;
    align-items:center;
    user-select: none;
    cursor: pointer;
}


.container .wheel .number span { 
    position: relative;
    transform: rotate(45deg);
    font-size: 1em;
    margin-left: 15px;
    font-weight: 700;
    color:#fff;
    text-shadow: 3px 5px 2px rgba(0,0,0,0,0.15);
}

Reference

  • Material UI
  • [Spin Wheel using CSS & Javascript Lucky Spinning Wheel Game](https://www.youtube.com/watch?v=F3-lK_-PQr0)