feat: 增加找字模板
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import { WATER_COLORS, PAPER_SIZE } from '../../constants/colors';
|
||||
import { WORDS } from '../../constants/words';
|
||||
import TextDrawService from '../../service/textDrawService';
|
||||
import {
|
||||
DrawServiceFactory,
|
||||
IDrawService,
|
||||
TemplateType,
|
||||
} from '../../service/drawServiceFactory';
|
||||
import { checkAndSaveImage } from '../../utils/saveImage';
|
||||
|
||||
Page({
|
||||
@@ -8,7 +12,7 @@ Page({
|
||||
ctx: null as RenderingContext | null,
|
||||
boxHeight: 0,
|
||||
boxWidth: 0,
|
||||
textDrawService: null as TextDrawService | null,
|
||||
drawService: null as IDrawService | null,
|
||||
|
||||
data: {
|
||||
cardList: [] as CardList,
|
||||
@@ -18,7 +22,7 @@ Page({
|
||||
currentColor: '',
|
||||
showIntroductionPopup: false,
|
||||
showSelectWordPopup: false, // 选择文字弹窗
|
||||
selectedTemplate: 'grid', // 选中的模板类型
|
||||
selectedTemplate: 'find', // 选中的模板类型
|
||||
env: 'release',
|
||||
isPCDevtool: false,
|
||||
},
|
||||
@@ -36,21 +40,24 @@ Page({
|
||||
}
|
||||
|
||||
if (!hasShowIntroduction) {
|
||||
this.setData({
|
||||
cardList: [
|
||||
{ color: '#FF0000', word: '涂' },
|
||||
{ color: '#00FF00', word: '鸦' },
|
||||
{ color: '#FF7F00', word: '丫' },
|
||||
{ color: '#00FFFF', word: '欢' },
|
||||
{ color: '#FFFF00', word: '迎' },
|
||||
{ color: '#8A2BE2', word: '您' },
|
||||
],
|
||||
selectedWords: ['涂', '鸦', '丫', '欢', '迎', '您'],
|
||||
showIntroductionPopup: true,
|
||||
env: getApp().globalData.env || 'release',
|
||||
}, () => {
|
||||
this.drawCanvas();
|
||||
});
|
||||
this.setData(
|
||||
{
|
||||
cardList: [
|
||||
{ color: '#FF0000', word: '涂' },
|
||||
{ color: '#00FF00', word: '鸦' },
|
||||
{ color: '#FF7F00', word: '丫' },
|
||||
{ color: '#00FFFF', word: '欢' },
|
||||
{ color: '#FFFF00', word: '迎' },
|
||||
{ color: '#8A2BE2', word: '您' },
|
||||
],
|
||||
selectedWords: ['涂', '鸦', '丫', '欢', '迎', '您'],
|
||||
showIntroductionPopup: true,
|
||||
env: getApp().globalData.env || 'release',
|
||||
},
|
||||
() => {
|
||||
this.drawCanvas();
|
||||
},
|
||||
);
|
||||
} else {
|
||||
this.setData({
|
||||
env: getApp().globalData.env || 'release',
|
||||
@@ -88,13 +95,25 @@ Page({
|
||||
if (ctx) {
|
||||
this.canvas = canvas;
|
||||
this.ctx = ctx;
|
||||
this.textDrawService = new TextDrawService(canvas, ctx);
|
||||
// 根据当前选中的模板类型创建对应的绘制服务
|
||||
const templateType = this.data
|
||||
.selectedTemplate as TemplateType;
|
||||
this.drawService =
|
||||
DrawServiceFactory.create(
|
||||
templateType,
|
||||
canvas,
|
||||
ctx,
|
||||
);
|
||||
this.setData({ boxWidth, boxHeight });
|
||||
|
||||
// 初始化完成后,如果有cardList内容则立即绘制
|
||||
const currentCardList = this.data.cardList;
|
||||
if (currentCardList.length > 0) {
|
||||
this.textDrawService.draw(currentCardList);
|
||||
this.drawService
|
||||
.draw(currentCardList)
|
||||
.catch((err) => {
|
||||
console.error('绘制失败:', err);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,35 +123,63 @@ Page({
|
||||
.exec();
|
||||
},
|
||||
|
||||
/**
|
||||
* 创建或更新绘制服务
|
||||
* 根据当前选中的模板类型创建对应的服务实例
|
||||
*/
|
||||
createDrawService() {
|
||||
if (this.canvas && this.ctx) {
|
||||
const templateType = this.data.selectedTemplate as TemplateType;
|
||||
this.drawService = DrawServiceFactory.create(
|
||||
templateType,
|
||||
this.canvas,
|
||||
this.ctx,
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
/** 变更cardList 并更新canvas */
|
||||
drawCanvas(
|
||||
updatedCardList?: Array<{ color: string; word: string }>,
|
||||
callback: () => void = () => { },
|
||||
callback: () => void = () => {},
|
||||
) {
|
||||
if (updatedCardList) {
|
||||
this.setData({
|
||||
cardList: updatedCardList,
|
||||
selectedWords: updatedCardList.map((item) => item.word)
|
||||
}, () => {
|
||||
// 如果cardList有内容且canvas未初始化,先初始化canvas
|
||||
if (!this.canvas) {
|
||||
this.initCanvas();
|
||||
}
|
||||
// 如果canvas已初始化且有内容,则绘制
|
||||
if (this.textDrawService && updatedCardList!.length > 0) {
|
||||
this.textDrawService.draw(updatedCardList!);
|
||||
}
|
||||
callback();
|
||||
});
|
||||
this.setData(
|
||||
{
|
||||
cardList: updatedCardList,
|
||||
selectedWords: updatedCardList.map((item) => item.word),
|
||||
},
|
||||
() => {
|
||||
// 如果cardList有内容且canvas未初始化,先初始化canvas
|
||||
if (!this.canvas) {
|
||||
this.initCanvas();
|
||||
} else {
|
||||
// 如果canvas已初始化,确保绘制服务是最新的
|
||||
this.createDrawService();
|
||||
}
|
||||
// 如果canvas已初始化且有内容,则绘制
|
||||
if (this.drawService && updatedCardList!.length > 0) {
|
||||
this.drawService.draw(updatedCardList!).catch((err) => {
|
||||
console.error('绘制失败:', err);
|
||||
});
|
||||
}
|
||||
callback();
|
||||
},
|
||||
);
|
||||
} else {
|
||||
const currentCardList = this.data.cardList;
|
||||
// 如果cardList有内容且canvas未初始化,先初始化canvas
|
||||
if (!this.canvas) {
|
||||
this.initCanvas();
|
||||
} else {
|
||||
// 如果canvas已初始化,确保绘制服务是最新的
|
||||
this.createDrawService();
|
||||
}
|
||||
// 如果canvas已初始化且有内容,则绘制
|
||||
if (this.textDrawService && currentCardList.length > 0) {
|
||||
this.textDrawService.draw(currentCardList);
|
||||
if (this.drawService && currentCardList.length > 0) {
|
||||
this.drawService.draw(currentCardList).catch((err) => {
|
||||
console.error('绘制失败:', err);
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -199,7 +246,9 @@ Page({
|
||||
color = colorHex;
|
||||
} else {
|
||||
// 否则随机选择一个可用颜色
|
||||
const randomIndex = Math.floor(Math.random() * shuffledColors.length);
|
||||
const randomIndex = Math.floor(
|
||||
Math.random() * shuffledColors.length,
|
||||
);
|
||||
color = shuffledColors[randomIndex];
|
||||
// 从可用颜色中移除已使用的颜色
|
||||
shuffledColors.splice(randomIndex, 1);
|
||||
@@ -207,7 +256,7 @@ Page({
|
||||
|
||||
return {
|
||||
word: char,
|
||||
color: color
|
||||
color: color,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -291,7 +340,7 @@ Page({
|
||||
// 清空cardList时,重置canvas相关引用,因为DOM元素会被移除
|
||||
this.canvas = null;
|
||||
this.ctx = null;
|
||||
this.textDrawService = null;
|
||||
this.drawService = null;
|
||||
this.drawCanvas([]);
|
||||
},
|
||||
|
||||
@@ -305,7 +354,8 @@ Page({
|
||||
// 随机获取6个不重复的文字
|
||||
const selectedWords: string[] = [];
|
||||
while (selectedWords.length < 6) {
|
||||
const randomWord = allWords[Math.floor(Math.random() * allWords.length)];
|
||||
const randomWord =
|
||||
allWords[Math.floor(Math.random() * allWords.length)];
|
||||
if (!selectedWords.includes(randomWord)) {
|
||||
selectedWords.push(randomWord);
|
||||
}
|
||||
@@ -323,7 +373,7 @@ Page({
|
||||
// 组合文字和颜色
|
||||
const cardList = selectedWords.map((word, index) => ({
|
||||
color: selectedColors[index],
|
||||
word
|
||||
word,
|
||||
}));
|
||||
this.drawCanvas(cardList);
|
||||
},
|
||||
@@ -342,17 +392,17 @@ Page({
|
||||
// 获取颜色字对应的16进制颜色值
|
||||
getColorByWord(word: string): string | null {
|
||||
const colorMap: Record<string, string> = {
|
||||
'红': '#FF0000',
|
||||
'蓝': '#0000FF',
|
||||
'绿': '#00FF00',
|
||||
'黄': '#FFFF00',
|
||||
'黑': '#000000',
|
||||
'白': '#FFFFFF',
|
||||
'紫': '#800080',
|
||||
'橙': '#FF7F00',
|
||||
'粉': '#FF69B4',
|
||||
'棕': '#A52A2A',
|
||||
'灰': '#808080'
|
||||
红: '#FF0000',
|
||||
蓝: '#0000FF',
|
||||
绿: '#00FF00',
|
||||
黄: '#FFFF00',
|
||||
黑: '#000000',
|
||||
白: '#FFFFFF',
|
||||
紫: '#800080',
|
||||
橙: '#FF7F00',
|
||||
粉: '#FF69B4',
|
||||
棕: '#A52A2A',
|
||||
灰: '#808080',
|
||||
};
|
||||
return colorMap[word] || null;
|
||||
},
|
||||
@@ -365,7 +415,7 @@ Page({
|
||||
const colorHex = this.getColorByWord(word);
|
||||
return {
|
||||
color: colorHex || colors[index].hex,
|
||||
word
|
||||
word,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -382,16 +432,20 @@ Page({
|
||||
// 模板选择事件
|
||||
onSelectTemplate(e: WechatMiniprogram.TouchEvent) {
|
||||
const { template } = e.currentTarget.dataset;
|
||||
this.setData({
|
||||
selectedTemplate: template
|
||||
});
|
||||
|
||||
// 这里后续可以添加根据模板类型更新canvas绘制效果的逻辑
|
||||
console.log('选择的模板类型:', template);
|
||||
|
||||
// 如果有文字卡片,可以重新绘制canvas
|
||||
if (this.data.cardList.length > 0) {
|
||||
this.drawCanvas();
|
||||
}
|
||||
this.setData(
|
||||
{
|
||||
selectedTemplate: template,
|
||||
},
|
||||
() => {
|
||||
// 模板切换后,重新创建绘制服务并重新绘制
|
||||
if (this.canvas && this.ctx) {
|
||||
this.createDrawService();
|
||||
// 如果有文字卡片,重新绘制canvas
|
||||
if (this.data.cardList.length > 0) {
|
||||
this.drawCanvas();
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user