App.vue 9.3 KB
Newer Older
L
LeoKu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<template>
  <main class="main">
    <Container>
      <div class="content-warpper">
        <div class="content-view">
          <Header />

          <div class="playground">
            <div class="avatar-wrapper">
              <VueColorAvatar
                ref="colorAvatarRef"
                :option="avatarOption"
                :size="280"
                :style="{
                  transform: `rotateY(${flipped ? -180 : 0}deg)`,
                }"
              />
            </div>

L
LeoKu 已提交
20
            <ActionBar @action="handleAction" />
L
LeoKu 已提交
21 22

            <div class="action-group">
L
LeoKu 已提交
23 24 25 26 27
              <button
                type="button"
                class="action-btn action-randomize"
                @click="handleGenerate"
              >
L
LeoKu 已提交
28 29
                {{ t('action.randomize') }}
              </button>
L
LeoKu 已提交
30

L
LeoKu 已提交
31
              <button
L
LeoKu 已提交
32 33
                type="button"
                class="action-btn action-download"
L
LeoKu 已提交
34 35 36 37 38 39 40 41 42
                :disabled="downloading"
                @click="handleDownload"
              >
                {{
                  downloading
                    ? `${t('action.downloading')}...`
                    : t('action.download')
                }}
              </button>
L
LeoKu 已提交
43 44 45 46 47 48 49 50

              <button
                type="button"
                class="action-btn action-multiple"
                @click="() => generateMultiple()"
              >
                {{ t('action.downloadMultiple') }}
              </button>
L
LeoKu 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64
            </div>
          </div>

          <Footer />

          <CodeModal :visible="codeVisible" @close="codeVisible = false" />

          <DownloadModal
            :visible="downloadModalVisible"
            :image-url="imageDataURL"
            @close=";(downloadModalVisible = false), (imageDataURL = '')"
          />
        </div>

L
LeoKu 已提交
65
        <ConfettiCanvas />
L
LeoKu 已提交
66 67 68 69 70 71 72 73

        <div class="gradient-bg">
          <div class="gradient-top"></div>
          <div class="gradient-bottom"></div>
        </div>
      </div>
    </Container>

L
LeoKu 已提交
74 75 76
    <BatchDownloadModal
      :visible="avatarListVisible"
      :avatar-list="avatarList"
L
LeoKu 已提交
77
      @regenerate="generateMultiple"
L
LeoKu 已提交
78 79 80
      @close=";(avatarListVisible = false), (avatarList = [])"
    />

L
LeoKu 已提交
81 82 83 84 85 86 87
    <Sider>
      <Configurator />
    </Sider>
  </main>
</template>

<script lang="ts" setup>
L
LeoKu 已提交
88
import { ref, watchEffect } from 'vue'
L
LeoKu 已提交
89 90 91 92
import { useI18n } from 'vue-i18n'

import ActionBar from '@/components/ActionBar.vue'
import Configurator from '@/components/Configurator.vue'
L
LeoKu 已提交
93 94 95
import BatchDownloadModal from '@/components/Modal/BatchDownloadModal.vue'
import CodeModal from '@/components/Modal/CodeModal.vue'
import DownloadModal from '@/components/Modal/DownloadModal.vue'
L
LeoKu 已提交
96 97 98
import VueColorAvatar, {
  type VueColorAvatarRef,
} from '@/components/VueColorAvatar.vue'
L
LeoKu 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
import { ActionType } from '@/enums'
import { useAvatarOption } from '@/hooks'
import Container from '@/layouts/Container.vue'
import Footer from '@/layouts/Footer.vue'
import Header from '@/layouts/Header.vue'
import Sider from '@/layouts/Sider.vue'
import { useStore } from '@/store'
import { REDO, UNDO } from '@/store/mutation-type'
import {
  getRandomAvatarOption,
  getSpecialAvatarOption,
  showConfetti,
} from '@/utils'
import {
  DOWNLOAD_DELAY,
  NOT_COMPATIBLE_AGENTS,
  TRIGGER_PROBABILITY,
} from '@/utils/constant'
L
LeoKu 已提交
117
import { recordEvent } from '@/utils/ga'
L
LeoKu 已提交
118

L
LeoKu 已提交
119
import { name as appName } from '../package.json'
L
LeoKu 已提交
120
import ConfettiCanvas from './components/ConfettiCanvas.vue'
L
LeoKu 已提交
121
import type { AvatarOption } from './types'
L
LeoKu 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

const store = useStore()

const [avatarOption, setAvatarOption] = useAvatarOption()

const { t } = useI18n()

const colorAvatarRef = ref<VueColorAvatarRef>()

function handleGenerate() {
  if (Math.random() <= TRIGGER_PROBABILITY) {
    let colorfulOption = getSpecialAvatarOption()
    while (
      JSON.stringify(colorfulOption) === JSON.stringify(avatarOption.value)
    ) {
      colorfulOption = getSpecialAvatarOption()
    }
L
LeoKu 已提交
139
    colorfulOption.wrapperShape = avatarOption.value.wrapperShape
L
LeoKu 已提交
140 141 142 143 144 145
    setAvatarOption(colorfulOption)
    showConfetti()
  } else {
    const randomOption = getRandomAvatarOption(avatarOption.value)
    setAvatarOption(randomOption)
  }
L
LeoKu 已提交
146 147 148 149

  recordEvent('click_randomize', {
    event_category: 'click',
  })
L
LeoKu 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
}

const downloadModalVisible = ref(false)
const downloading = ref(false)
const imageDataURL = ref('')

async function handleDownload() {
  try {
    downloading.value = true
    const avatarEle = colorAvatarRef.value?.avatarRef

    const userAgent = window.navigator.userAgent.toLowerCase()
    const notCompatible = NOT_COMPATIBLE_AGENTS.some(
      (agent) => userAgent.indexOf(agent) !== -1
    )

    if (avatarEle) {
      const html2canvas = (await import('html2canvas')).default
      const canvas = await html2canvas(avatarEle, {
        backgroundColor: null,
      })
      const dataURL = canvas.toDataURL()

      if (notCompatible) {
        imageDataURL.value = dataURL
        downloadModalVisible.value = true
      } else {
        const trigger = document.createElement('a')
        trigger.href = dataURL
L
LeoKu 已提交
179
        trigger.download = `${appName}.png`
L
LeoKu 已提交
180 181 182
        trigger.click()
      }
    }
L
LeoKu 已提交
183 184 185 186

    recordEvent('click_download', {
      event_category: 'click',
    })
L
LeoKu 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199
  } finally {
    setTimeout(() => {
      downloading.value = false
    }, DOWNLOAD_DELAY)
  }
}

const flipped = ref(false)
const codeVisible = ref(false)

function handleAction(actionType: ActionType) {
  switch (actionType) {
    case ActionType.Undo:
200
      store[UNDO]()
L
LeoKu 已提交
201 202 203 204
      recordEvent('action_undo', {
        event_category: 'action',
        event_label: 'Undo',
      })
L
LeoKu 已提交
205 206 207
      break

    case ActionType.Redo:
208
      store[REDO]()
L
LeoKu 已提交
209 210 211 212
      recordEvent('action_redo', {
        event_category: 'action',
        event_label: 'Redo',
      })
L
LeoKu 已提交
213 214 215 216
      break

    case ActionType.Flip:
      flipped.value = !flipped.value
L
LeoKu 已提交
217 218 219 220
      recordEvent('action_flip_avatar', {
        event_category: 'action',
        event_label: 'Flip Avatar',
      })
L
LeoKu 已提交
221 222 223 224
      break

    case ActionType.Code:
      codeVisible.value = !codeVisible.value
L
LeoKu 已提交
225 226 227
      recordEvent('action_view_code', {
        event_category: 'action',
        event_label: 'View Avatar Option Code',
L
LeoKu 已提交
228
      })
L
LeoKu 已提交
229 230 231
      break
  }
}
L
LeoKu 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

const avatarListVisible = ref(false)
const avatarList = ref<AvatarOption[]>([])

watchEffect(() => {
  avatarListVisible.value =
    Array.isArray(avatarList.value) && avatarList.value.length > 0
})

async function generateMultiple(count = 5 * 6) {
  const { default: hash } = await import('object-hash')

  const avatarMap = [...Array(count)].reduce<Map<string, AvatarOption>>(
    (res) => {
      let randomAvatarOption: AvatarOption
      let hashKey: string

      do {
        randomAvatarOption = getRandomAvatarOption(avatarOption.value)
        hashKey = hash.sha1(randomAvatarOption)
      } while (
        randomAvatarOption.background.color === 'transparent' ||
        res.has(hashKey)
      )

      res.set(hashKey, randomAvatarOption)

      return res
    },
    new Map()
  )

  avatarList.value = Array.from(avatarMap.values())

  recordEvent('click_generate_multiple', {
    event_category: 'click',
  })
}
L
LeoKu 已提交
270 271 272
</script>

<style lang="scss" scoped>
273 274
@use 'src/styles/var';

L
LeoKu 已提交
275 276 277 278
.main {
  width: 100%;
  height: 100%;
  overflow: hidden;
279 280
  color: var.$color-text;
  background-color: var.$color-page-bg;
L
LeoKu 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

  .content-warpper {
    height: 100%;
    transform: scale(1);

    .content-view {
      position: relative;
      z-index: 110;
      display: flex;
      flex-direction: column;
      height: 100%;
      overflow-y: auto;
    }
  }
}

.playground {
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem 0;

  .avatar-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;

310
    @media screen and (max-width: var.$screen-sm) {
L
LeoKu 已提交
311 312 313 314 315 316 317 318 319
      transform: scale(0.85);
    }
  }

  .action-group {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 4rem;
L
LeoKu 已提交
320
    column-gap: 1rem;
L
LeoKu 已提交
321

L
LeoKu 已提交
322 323 324 325 326 327 328
    @supports not (column-gap: 1rem) {
      .action-btn {
        margin: 0 0.5rem;
      }
    }

    .action-btn {
L
LeoKu 已提交
329 330 331
      min-width: 6rem;
      height: 2.5rem;
      padding: 0 1rem;
332
      color: var.$color-text;
L
LeoKu 已提交
333
      font-weight: bold;
334
      background: var.$color-gray;
L
LeoKu 已提交
335 336 337 338 339 340
      border-radius: 0.6rem;
      cursor: pointer;
      transition: color 0.2s;
      user-select: none;

      &:hover {
341
        color: lighten(var.$color-text, 10);
L
LeoKu 已提交
342 343
      }

L
LeoKu 已提交
344 345
      &:disabled,
      &[disabled] {
346
        color: rgba(var.$color-text, 0.5);
L
LeoKu 已提交
347 348 349
        cursor: default;
      }
    }
L
LeoKu 已提交
350 351 352 353 354 355

    @media screen and (max-width: var.$screen-sm) {
      .action-multiple {
        display: none;
      }
    }
L
LeoKu 已提交
356 357 358
  }
}

L
LeoKu 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
@supports (filter: blur(4rem)) or (-webkit-filter: blur(4rem)) or
  (-moz-filter: blur(4rem)) {
  .gradient-bg {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;

    @mixin gradient-style($color) {
      position: absolute;
      width: 100vh;
      height: 100vh;
      background-image: radial-gradient(
        rgba($color, 0.8) 20%,
        rgba($color, 0.6) 40%,
        rgba($color, 0.4) 60%,
        rgba($color, 0.2) 80%,
        transparent 100%
      );
      border-radius: 50%;
      opacity: 0.2;
      filter: blur(4rem);
    }
L
LeoKu 已提交
383

L
LeoKu 已提交
384
    .gradient-top {
385
      @include gradient-style(var.$color-secondary);
L
LeoKu 已提交
386

L
LeoKu 已提交
387 388 389
      top: -50%;
      right: -20%;
    }
L
LeoKu 已提交
390

L
LeoKu 已提交
391
    .gradient-bottom {
392
      @include gradient-style(var.$color-accent);
L
LeoKu 已提交
393

L
LeoKu 已提交
394 395 396
      bottom: -50%;
      left: -20%;
    }
L
LeoKu 已提交
397 398 399
  }
}
</style>