Files
gaoYuJournal/miniprogram/components/background/snowflake/index.ts
2025-12-05 12:40:19 +08:00

59 lines
1.1 KiB
TypeScript

// components/background/snow/index.ts
import Toolkit from "../../../utils/Toolkit";
interface Snowflake {
x: number;
s: number;
speed: number;
}
Component({
data: {
snowflakes: [] as Snowflake[],
timer: undefined as number | undefined,
docWidth: 375,
docHeight: 667,
},
methods: {
createSnowflake() {
const snowflake = {
x: Toolkit.random(0, this.data.docWidth),
s: Toolkit.random(6, 20),
speed: Toolkit.random(14, 26)
};
this.setData({
snowflakes: [...this.data.snowflakes, snowflake]
});
}
},
lifetimes: {
attached() {
const systemInfo = wx.getWindowInfo();
this.setData({
docWidth: systemInfo.windowWidth,
docHeight: systemInfo.windowHeight,
timer: setInterval(() => {
this.createSnowflake();
if (40 < this.data.snowflakes.length) {
if (this.data.timer) {
clearInterval(this.data.timer);
}
this.setData({
timer: undefined
})
}
}, 2000)
});
},
detached() {
if (this.data.timer) {
clearInterval(this.data.timer);
this.setData({
timer: undefined
})
}
}
}
})