init project
This commit is contained in:
61
miniprogram/components/background/snowflake/index.ts
Normal file
61
miniprogram/components/background/snowflake/index.ts
Normal file
@ -0,0 +1,61 @@
|
||||
// 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();
|
||||
console.log(this.data.snowflakes);
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user