feat:练字贴功能基本完成
This commit is contained in:
@@ -2,6 +2,7 @@ import WordDrawService from '../../service/wordDrawService';
|
||||
import { checkAndSaveImage } from '../../utils/saveImage';
|
||||
import { PAPER_SIZE } from '../../constants/colors';
|
||||
import { getWordsSvgData } from '../../utils/getWordsSvgJson';
|
||||
import { WORDS } from '../../constants/words';
|
||||
import { CharacterItem } from '../../types/characterType';
|
||||
|
||||
Page({
|
||||
@@ -9,6 +10,8 @@ Page({
|
||||
ctx: null as RenderingContext | null,
|
||||
wordDrawService: null as WordDrawService | null,
|
||||
svgWords: {} as Record<string, string[]>,
|
||||
maxRow: 0 as number,
|
||||
maxCol: 0 as number,
|
||||
|
||||
data: {
|
||||
// 选中的汉字列表
|
||||
@@ -21,18 +24,17 @@ Page({
|
||||
boxHeight: 0,
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.loadSvgWords();
|
||||
const hasShowIntroduction =
|
||||
wx.getStorageSync('hasShowIntroduction') || false;
|
||||
if (!hasShowIntroduction) {
|
||||
async onLoad() {
|
||||
await this.loadSvgWords();
|
||||
const hasShowPracticeDemo =
|
||||
wx.getStorageSync('hasShowPracticeDemo') || false;
|
||||
if (!hasShowPracticeDemo) {
|
||||
this.setData({
|
||||
words: ['好', '好', '学', '习', '天', '天', '向', '上']
|
||||
}, () => {
|
||||
wx.setStorageSync('hasShowPracticeDemo', true);
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
async onReady() {
|
||||
await this.setCanvasBoxSize();
|
||||
await this.initCanvas()
|
||||
},
|
||||
@@ -40,12 +42,9 @@ Page({
|
||||
async loadSvgWords() {
|
||||
try {
|
||||
wx.showToast({ title: '加载字体中...', icon: 'loading' });
|
||||
console.log("loadSvgWords");
|
||||
// 使用带缓存的SVG汉字数据获取函数
|
||||
const svgWordsData = await getWordsSvgData();
|
||||
|
||||
console.log("svgWordsData", svgWordsData);
|
||||
|
||||
this.svgWords = svgWordsData;
|
||||
wx.hideToast();
|
||||
|
||||
@@ -147,6 +146,30 @@ Page({
|
||||
this.setData({ words: next }, () => this.renderPracticeContent().catch(console.error));
|
||||
},
|
||||
|
||||
// 清空
|
||||
clearWords() {
|
||||
this.setData({ words: [] }, () => this.renderPracticeContent().catch(console.error));
|
||||
},
|
||||
|
||||
// 随机生成(取基础分类,过滤为可用SVG字,最多11个)
|
||||
refreshWords() {
|
||||
// 选取较简单的几个分类(如 0:基础独体字,1:生活高频字,2:自然与动作)
|
||||
const candidate = WORDS.slice(0, 3).reduce((acc: string[], c: any) => acc.concat(c.words), [] as string[]);
|
||||
// 去重
|
||||
const unique = Array.from(new Set(candidate));
|
||||
// 打乱
|
||||
const shuffled = unique.sort(() => Math.random() - 0.5);
|
||||
// 先取前 11 个
|
||||
const picked = shuffled.slice(0, 10);
|
||||
// 过滤为支持的SVG字
|
||||
const supported = this.getSupportedWords(picked);
|
||||
if (supported.length === 0) {
|
||||
wx.showToast({ title: '随机到的字暂不支持,重试一下', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
this.setData({ words: supported }, () => this.renderPracticeContent().catch(console.error));
|
||||
},
|
||||
|
||||
// 校验 & 分割
|
||||
isChineseText(text: string): boolean {
|
||||
const chineseRegex = /^[\u4e00-\u9fff]+$/;
|
||||
@@ -189,16 +212,16 @@ Page({
|
||||
if (ctx) {
|
||||
this.canvas = canvas;
|
||||
this.ctx = ctx;
|
||||
this.wordDrawService = new WordDrawService(canvas, ctx, {
|
||||
appName: '涂鸦丫小程序',
|
||||
appHint: '练字|识字|涂色|打印',
|
||||
title: '田字格 练 字 贴',
|
||||
subTitle: '按笔画临摹练习'
|
||||
});
|
||||
this.wordDrawService = new WordDrawService(canvas, ctx);
|
||||
|
||||
// 初始化时只绘制页眉和布局
|
||||
await this.wordDrawService.drawLayout();
|
||||
|
||||
// 绘制完成后计算并缓存最大行列数(与实际绘制一致)
|
||||
const { maxRow, maxCol } = this.wordDrawService.getMaxGridLayout();
|
||||
this.maxRow = maxRow;
|
||||
this.maxCol = maxCol;
|
||||
|
||||
// 如果有汉字数据,绘制练字内容
|
||||
const { words } = this.data as any;
|
||||
if (words && words.length > 0) {
|
||||
@@ -220,7 +243,6 @@ Page({
|
||||
}
|
||||
|
||||
const { words } = this.data as any;
|
||||
console.log("renderPracticeContent words", words);
|
||||
|
||||
if (!words || words.length === 0) {
|
||||
this.wordDrawService.drawContentEmpty();
|
||||
@@ -235,8 +257,19 @@ Page({
|
||||
return;
|
||||
}
|
||||
|
||||
// 处理布局和生成练字数据
|
||||
const characterData = this.processCharacterLayout(supportedWords);
|
||||
// 使用初始化阶段缓存的最大行列数;若未初始化则计算一次并缓存
|
||||
let maxRow = this.maxRow;
|
||||
let maxCol = this.maxCol;
|
||||
if (!maxRow || !maxCol) {
|
||||
const layout = this.wordDrawService!.getMaxGridLayout();
|
||||
this.maxRow = layout.maxRow;
|
||||
this.maxCol = layout.maxCol;
|
||||
maxRow = layout.maxRow;
|
||||
maxCol = layout.maxCol;
|
||||
}
|
||||
|
||||
// 处理布局和生成练字数据(基于实际最大行列数)
|
||||
const characterData = this.processCharacterLayout(supportedWords, maxRow, maxCol);
|
||||
this.wordDrawService.drawPracticeContent(characterData);
|
||||
},
|
||||
|
||||
@@ -247,8 +280,7 @@ Page({
|
||||
},
|
||||
|
||||
/** 处理汉字布局,生成练字数据 */
|
||||
processCharacterLayout(words: string[]): CharacterItem[] {
|
||||
const maxRow = 11, maxCol = 9;
|
||||
processCharacterLayout(words: string[], maxRow: number, maxCol: number): CharacterItem[] {
|
||||
let rowIndex = 0;
|
||||
const characterData: CharacterItem[] = [];
|
||||
const boundaryWords: string[] = [];
|
||||
@@ -257,10 +289,12 @@ Page({
|
||||
words.forEach((word: string) => {
|
||||
const strokes = this.svgWords[word];
|
||||
const strokeCount = strokes.length;
|
||||
const totalCells = 1 + strokeCount + 2; // 预览格 + 练习格 + 空白格
|
||||
const totalCells = 1 + strokeCount + 0; // 预览格 + 练习格 + 空白格
|
||||
const totalRows = Math.ceil(totalCells / maxCol);
|
||||
|
||||
rowIndex += totalRows;
|
||||
// 这里的意思是:把这一个汉字所占的行数(totalRows)累加到 rowIndex 上。
|
||||
// 这样 rowIndex 就记录了当前已经排布了多少行,用于判断是否超出了最大允许行数(maxRow)。
|
||||
rowIndex = rowIndex + totalRows;
|
||||
|
||||
if (rowIndex <= maxRow) {
|
||||
characterData.push({ character: word, strokes });
|
||||
@@ -268,22 +302,20 @@ Page({
|
||||
boundaryWords.push(word);
|
||||
}
|
||||
});
|
||||
|
||||
// 提示超出页面的汉字
|
||||
if (boundaryWords.length > 0) {
|
||||
wx.showToast({
|
||||
title: `练字贴已超过一页,部分汉字未包含在本次生成结果中`,
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
// // 提示超出页面的汉字
|
||||
// if (showToast && boundaryWords.length > 0) {
|
||||
// wx.showToast({
|
||||
// title: `练字贴已超过一页,部分汉字未包含在本次生成结果中`,
|
||||
// icon: 'none'
|
||||
// });
|
||||
// }
|
||||
|
||||
return characterData;
|
||||
},
|
||||
|
||||
// 下载打印练字贴
|
||||
exportToPrint() {
|
||||
const { words } = this.data as any;
|
||||
if (!this.canvas || words.length <= 0) {
|
||||
if (!this.canvas) {
|
||||
wx.showToast({ title: '请先生成练字贴', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user