<template>
  <div id="app">
    <canvas id="world"></canvas>
    <div class="mask"></div>

    <div class="container">
      <div class="header">
        
        <p class="title">
          <img src="./logo.png" alt="logo">
          <span>WACACA</span>
        </p>
      </div>

      <div class="demo-block">
        <div class="demo-block__control">
          <label for="">缓动动画：</label>
          <select v-model="easing">
            <option v-for="(easing, i) in easings" :key="i" :value="easing.value">{{ easing.name }}</option>
          </select>
        </div>

        <div class="demo-block__main">
          <div class="demo" ref="demo"></div>
        </div>
      </div>

      <footer>
        <p>
          <span>JUSTEMIT</span>
          <span>&copy; {{ new Date().getFullYear() }}</span>
          <span>
            <a href="https://github.com/justemit/wacaca" target="_blank">GITHUB</a>
          </span>
        </p>
      </footer>
    </div>

    
  </div>
</template>

<script>
import initCanvas from './canvas'
import { Tween, Easing } from '../src'

const easings = Object.keys(Easing).reduce((accumulate, key) => {
  accumulate.push({ name: key, value: Easing[key] })
  return accumulate
}, [])

export default {
  name: 'App',

  data() {
    return {
      easings: easings,
      easing: Easing.bounceInOut,
      number: 0
    }
  },

  watch: {
    easing() {
      this.animate()
    }
  },

  mounted() {
    this.animate()
    initCanvas()
  },

  methods: {
    async animate() {
      let width = window.innerWidth * 0.96 - 30 - 50
      width > 500 && (width = 500 - 30 - 50)

      const oDemo = this.$refs['demo']

      const label = '结束'
      console.time(label)
      const tween = new Tween(0)
        .to(width)
        .duration(1500)
        .easing(this.easing)
        .onUpdate(val => {
          oDemo.style.marginLeft = val + 'px'
        })

      console.log(tween)

      setTimeout(() => {
        console.log('暂停: ', tween.pause())

        setTimeout(() => {
          console.log('恢复: ', tween.resume())
        }, 2000)
      }, 300)

      await tween.start()

      console.timeEnd(label)
    }
  }
}
</script>

<style>
*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

a {
  text-decoration: none;
}

#app {
  position: relative;
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  margin: 0 auto;

  width: 100vw;
  height: 100vh;
  background: #19171d;
}

canvas {
  position: absolute;
  left: 0;
  top: 0;
}

.mask {
  position: absolute;
  left: 0;
  top: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(25, 23, 29, 0.6);

  z-index: 1;
}

.container {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 10;

  width: 96%;
  max-width: 500px;
}

.header {
  position: relative;
  z-index: 10;
  text-align: center;
  margin-bottom: 30px;
}

.header img {
  width: 100px;
  height: 100px;
}

.header p {
  /* font-size: 1.2rem; */
  color: #fff;
}

.title {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 2rem;
}

.title span {
  margin-left: 10px;
}

.demo-block {
  border: 1px solid #eee;
  padding: 15px;
  background: #fff;
  border-radius: 4px;
  margin: 0 auto;
}

.demo-block__control {
  margin-bottom: 15px;
}

.demo-block__main {
  min-height: 1px;
  padding: 30px 0;
}

.demo {
  width: 50px;
  height: 50px;
  background: linear-gradient(to bottom right, #c2243c, #f37e50);

  color: #fff;
  line-height: 50px;
}

footer {
  padding: 15px;
  color: #e9e9e9;
  text-align: center;
}
</style>
