feat:添加随机生成和清空功能
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { WATER_COLORS, PAPER_SIZE } from '../../constants/colors';
|
||||
import { WORDS } from '../../constants/words';
|
||||
import TextDrawService from '../../service/textDrawService';
|
||||
|
||||
Page({
|
||||
@@ -82,13 +83,15 @@ Page({
|
||||
updatedCardList?: Array<{ color: string; word: string }>,
|
||||
callback: () => void = () => { },
|
||||
) {
|
||||
if (updatedCardList && updatedCardList.length >= 0) {
|
||||
this.setData({ cardList: updatedCardList }, callback);
|
||||
if (updatedCardList) {
|
||||
this.setData({ cardList: updatedCardList }, () => {
|
||||
this.textDrawService?.draw(updatedCardList!);
|
||||
callback();
|
||||
});
|
||||
} else {
|
||||
updatedCardList = this.data.cardList;
|
||||
this.textDrawService?.draw(updatedCardList);
|
||||
}
|
||||
|
||||
this.textDrawService?.draw(updatedCardList);
|
||||
},
|
||||
|
||||
tapCard(e: WechatMiniprogram.TouchEvent) {
|
||||
@@ -247,4 +250,42 @@ Page({
|
||||
query: '/pages/index/index',
|
||||
};
|
||||
},
|
||||
|
||||
clearCardList() {
|
||||
this.drawCanvas([]);
|
||||
},
|
||||
|
||||
// 随机生成
|
||||
refreshCardList() {
|
||||
// 获取前4个分类的所有文字
|
||||
const allWords = WORDS.slice(0, 4).reduce((acc, category) => {
|
||||
return acc.concat(category.words);
|
||||
}, [] as string[]);
|
||||
|
||||
// 随机获取6个不重复的文字
|
||||
const selectedWords: string[] = [];
|
||||
while (selectedWords.length < 6) {
|
||||
const randomWord = allWords[Math.floor(Math.random() * allWords.length)];
|
||||
if (!selectedWords.includes(randomWord)) {
|
||||
selectedWords.push(randomWord);
|
||||
}
|
||||
}
|
||||
|
||||
// 随机获取6个不重复的颜色
|
||||
const colors = [...WATER_COLORS.basic12];
|
||||
const selectedColors: string[] = [];
|
||||
while (selectedColors.length < 6) {
|
||||
const randomIndex = Math.floor(Math.random() * colors.length);
|
||||
const color = colors.splice(randomIndex, 1)[0];
|
||||
selectedColors.push(color.hex);
|
||||
}
|
||||
|
||||
// 组合文字和颜色
|
||||
const cardList = selectedWords.map((word, index) => ({
|
||||
color: selectedColors[index],
|
||||
word
|
||||
}));
|
||||
console.log('cardList:', cardList);
|
||||
this.drawCanvas(cardList);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user