Initial project
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
<template>
|
||||
<div class="tui-background tui-be-flower-fall">
|
||||
<div :ref="el => fillNodes(el as HTMLElement, i - 1)" class="leaf" v-for="i in leafSize" :key="i"></div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import Toolkit from "~/utils/Toolkit";
|
||||
import Resizer from "~/utils/Resizer";
|
||||
|
||||
defineOptions({
|
||||
name: "BEFlowerFall"
|
||||
});
|
||||
|
||||
/** 叶子对象 */
|
||||
class Leaf {
|
||||
|
||||
/** 横轴 */
|
||||
x = 0;
|
||||
|
||||
/** 纵轴 */
|
||||
y = 0;
|
||||
|
||||
/** 背景偏移 */
|
||||
bgI = 0;
|
||||
|
||||
/** 跳跃状态 */
|
||||
jump = false;
|
||||
|
||||
/** 透明度 */
|
||||
opacity = 1;
|
||||
}
|
||||
|
||||
const docEl = document.documentElement;
|
||||
const leafF = ref<number>(0);
|
||||
const leafs: Leaf[] = [];
|
||||
const leafEls: HTMLElement[] = [];
|
||||
const leafSize = 16;
|
||||
const leafTimer = ref();
|
||||
const leafPause = ref<boolean>(false);
|
||||
const leafVector = ref<number>(0);
|
||||
|
||||
const fillNodes = (el: HTMLElement, index: number) => {
|
||||
if (el) {
|
||||
leafEls[index] = el;
|
||||
}
|
||||
};
|
||||
|
||||
const onResize = (width: number) => leafPause.value = width < 1200;
|
||||
|
||||
onMounted(() => {
|
||||
|
||||
// 生成叶子
|
||||
for (let i = 0; i < leafSize; i++) {
|
||||
leafs[i] = new Leaf();
|
||||
leafs[i].x = Toolkit.random(0, docEl.clientWidth);
|
||||
leafs[i].y = Toolkit.random(-docEl.clientHeight, -32);
|
||||
}
|
||||
// 掉落叶子
|
||||
clearInterval(leafTimer.value);
|
||||
leafTimer.value = setInterval(() => {
|
||||
// 随机上方位置向下飘落,维护一个向量值作为风向,可以向左向右
|
||||
//
|
||||
// 暂定 2 秒刷新风力向量,向量偏转范围 -10 - +10,分别表示左右向
|
||||
// -5 - +5 有固定 10% 几率增加或减小,同时增减或同时无增减都不改变向量
|
||||
// -10 - -5 有 0%, 2%, 4%... 几率再次减小
|
||||
// +5 - +10 有 8%, 6%, 4%... 几率再次增加
|
||||
// 降低垂直飘落的几率,扩大向量影响范围
|
||||
|
||||
if (!leafPause.value) {
|
||||
if (leafF.value % 10 === 0) {
|
||||
// 刷新风力向量
|
||||
let add = false,
|
||||
sub = false;
|
||||
if (-5 <= leafVector.value) {
|
||||
sub = Math.random() < 0.1;
|
||||
if (5 < leafVector.value) {
|
||||
add = Math.random() < (10 - leafVector.value) / 50;
|
||||
}
|
||||
}
|
||||
if (leafVector.value <= 5) {
|
||||
add = Math.random() < 0.1;
|
||||
if (leafVector.value < -5) {
|
||||
sub = Math.random() < (leafVector.value + 10) / 50;
|
||||
}
|
||||
}
|
||||
if (add !== sub) {
|
||||
leafVector.value = leafVector.value + (add ? 1 : -1);
|
||||
}
|
||||
}
|
||||
// 叶子飘落
|
||||
for (let i = 0; i < leafSize; i++) {
|
||||
const leaf = leafs[i];
|
||||
|
||||
if (leafVector.value < 0) {
|
||||
leaf.x += Toolkit.random(leafVector.value * 4 - 10, -10);
|
||||
} else {
|
||||
if (leafVector.value === 0) {
|
||||
leaf.x += Toolkit.random(-8, 8);
|
||||
} else {
|
||||
leaf.x += Toolkit.random(10, 10 + leafVector.value * 4);
|
||||
}
|
||||
}
|
||||
// 跳跃机制
|
||||
if (leafVector.value !== 0 && leaf.jump) {
|
||||
leaf.y += Toolkit.random(-12, Math.abs(leafVector.value));
|
||||
leaf.jump = Math.random() < 0.4; // 二次跳跃几率
|
||||
} else {
|
||||
leaf.y += Toolkit.random(10 + Math.abs(leafVector.value) * 5, 10);
|
||||
leaf.jump = Math.random() < 0.03; // 跳跃几率
|
||||
}
|
||||
// 操作 DOM
|
||||
const el = leafEls[i];
|
||||
el.style.top = leaf.y + "px";
|
||||
el.style.left = leaf.x + "px";
|
||||
el.style.backgroundPosition = "0 -" + Toolkit.random(0, 3) * 32 + "px";
|
||||
el.style.opacity = leaf.opacity.toString();
|
||||
|
||||
// y 掉出最大值或没有掉出最大值 X 却不在宽度内的,视为掉出屏幕,重新飘落
|
||||
if ((0 < leaf.y && (leaf.x < 0 || docEl.clientWidth < leaf.x)) || docEl.clientHeight < leaf.y) {
|
||||
// 根据风向反向偏移起始 X 轴
|
||||
const offset = -leafVector.value * 100;
|
||||
// 飘落越出屏幕,重新计算位置
|
||||
leafs[i].x = Toolkit.random(offset, docEl.clientWidth + offset);
|
||||
leafs[i].y = Toolkit.random(-docEl.clientHeight * 0.5, -32);
|
||||
// 随机透明度
|
||||
leafs[i].opacity = Toolkit.random(40, 100) * 0.01;
|
||||
}
|
||||
}
|
||||
// 帧循环
|
||||
leafF.value++;
|
||||
}
|
||||
}, 100);
|
||||
|
||||
onResize(Resizer.getWidth());
|
||||
Resizer.addListener("TUI_BE_FLOWER_FALL", onResize);
|
||||
});
|
||||
onUnmounted(() => Resizer.removeListener("TUI_BE_FLOWER_FALL"));
|
||||
</script>
|
||||
|
||||
<style lang="less" scoped>
|
||||
@import "../style";
|
||||
|
||||
.tui-be-flower-fall {
|
||||
image-rendering: pixelated;
|
||||
|
||||
.leaf {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
position: absolute;
|
||||
background: url("assets/petal.png") no-repeat;
|
||||
image-rendering: pixelated;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user