iT邦幫忙

2023 iThome 鐵人賽

DAY 26
0
SideProject30

想要學 50 音!系列 第 26

[DAY-26] 文字轉盤~~

  • 分享至 

  • xImage
  •  

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={{ mobile: 1, tablet: 2, laptop: 3 }}
      style={{ textAlign: "center" }}
    >
      <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": idx + 1,
                "--clr":
                  "#" +
                  (((1 << 24) * Math.random()) | 0)
                    .toString(16)
                    .padStart(6, "0"),
              }}
            >
              <Tooltip style={{ zIndex: 999 }} 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


上一篇
[DAY-25] 今天想要耍廢
下一篇
[DAY - 27] 轉盤出什麼字 我就寫什麼字~
系列文
想要學 50 音!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言