feat:绘制图形

This commit is contained in:
2025-09-12 16:56:00 +08:00
parent 3b4b2dcd64
commit 6acc5d676c
27 changed files with 76464 additions and 112 deletions
-7
View File
@@ -1,7 +0,0 @@
{
"navigationBarTitleText": "首页",
"navigationBarBackgroundColor": "#d2e7d8",
"homeButton": true,
"backgroundColor": "#e8eddb",
"enablePullDownRefresh": false
}
+183 -17
View File
@@ -3,31 +3,197 @@ page {
height: 100vh;
display: flex;
flex-direction: column;
background-color: #f5f5f5;
}
.container {
padding: 20rpx;
height: 100vh;
overflow-y: auto;
}
/* 输入区域样式 */
.input-section {
background-color: #fff;
border-radius: 20rpx;
padding: 30rpx;
margin-bottom: 20rpx;
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
}
.input-row {
display: flex;
align-items: center;
gap: 20rpx;
}
.word-input {
flex: 1;
height: 80rpx;
border: 2rpx solid #e0e0e0;
border-radius: 40rpx;
padding: 0 30rpx;
font-size: 32rpx;
background-color: #fafafa;
}
.generate-btn {
width: 160rpx;
height: 80rpx;
background-color: #07c160;
color: #fff;
border-radius: 40rpx;
font-size: 28rpx;
border: none;
display: flex;
align-items: center;
justify-content: center;
}
/* 通用区域样式 */
.section-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20rpx;
}
.section-title {
font-size: 32rpx;
font-weight: bold;
color: #333;
}
.clear-btn {
background-color: #ff4757;
color: #fff;
border-radius: 20rpx;
font-size: 24rpx;
padding: 10rpx 20rpx;
border: none;
}
/* 已选择的汉字区域 */
.selected-section {
background-color: #fff;
border-radius: 20rpx;
padding: 30rpx;
margin-bottom: 20rpx;
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
}
.word-list {
display: flex;
flex-wrap: wrap;
gap: 20rpx;
}
.word-item.selected {
background-color: #07c160;
color: #fff;
border-radius: 50rpx;
padding: 20rpx 30rpx;
position: relative;
min-width: 80rpx;
text-align: center;
}
.word-text {
font-size: 32rpx;
font-weight: bold;
}
.remove-btn {
position: absolute;
top: -10rpx;
right: -10rpx;
width: 40rpx;
height: 40rpx;
background-color: #ff4757;
color: #fff;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 24rpx;
font-weight: bold;
cursor: pointer;
}
/* 可选汉字区域 */
.available-section {
background-color: #fff;
border-radius: 20rpx;
padding: 30rpx;
margin-bottom: 20rpx;
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
}
.word-grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 20rpx;
}
.word-item.available {
background-color: #f0f0f0;
color: #333;
border-radius: 50rpx;
padding: 20rpx;
text-align: center;
border: 2rpx solid #e0e0e0;
transition: all 0.3s ease;
}
.word-item.available:active {
background-color: #e0e0e0;
transform: scale(0.95);
}
.word-item.available.disabled {
background-color: #ccc;
color: #999;
cursor: not-allowed;
}
.word-item.available.disabled:active {
background-color: #ccc;
transform: none;
}
/* 功能模块区域 */
.cards-section {
background-color: #fff;
border-radius: 20rpx;
padding: 30rpx;
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
}
.scroll-view {
flex: 1;
overflow-y: hidden;
height: 400rpx;
}
.card {
margin: 10px;
padding: 10px;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
width: calc(33.33% - 20px);
/* Adjust width for three cards per row */
margin: 10rpx;
padding: 20rpx;
background-color: #f8f9fa;
border-radius: 20rpx;
box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
width: calc(33.33% - 20rpx);
box-sizing: border-box;
/* Ensure padding and margin are included in the width */
display: inline-block;
/* Allow cards to sit next to each other */
vertical-align: top;
/* Align cards to the top */
transition: all 0.3s ease;
}
.card-title {
font-size: 18px;
font-weight: bold;
color: #333;
}
.card:active {
transform: scale(0.95);
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.2);
}
.card-title {
font-size: 28rpx;
font-weight: bold;
color: #333;
text-align: center;
}
+110
View File
@@ -1,5 +1,8 @@
Page({
data: {
inputWord: '', // 输入框中的汉字
wordList: [] as string[], // 选中的汉字列表
availableWords: [] as string[], // 从grade3.json中提取的前30个汉字
cards: [
{
key: 0,
@@ -24,6 +27,113 @@ Page({
],
},
onLoad() {
this.loadGrade3Words();
},
// 加载grade3.json中的前30个汉字
loadGrade3Words() {
const fs = wx.getFileSystemManager();
try {
const fileContent = fs.readFileSync('grade3.json', 'utf8') as string;
const grade3Data = JSON.parse(fileContent);
const words = Object.keys(grade3Data).slice(0, 30);
this.setData({
availableWords: words
});
} catch (error) {
console.error('加载grade3.json失败:', error);
// 如果文件读取失败,使用一些示例汉字
const fallbackWords = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '人', '口', '手', '足', '目', '耳', '鼻', '舌', '心', '肝', '脾', '肺', '肾', '胃', '肠', '胆', '膀', '胱', '皮', '毛'];
this.setData({
availableWords: fallbackWords
});
}
},
// 输入框输入事件
onInputChange(e: WechatMiniprogram.Input) {
this.setData({
inputWord: e.detail.value
});
},
// 生成按钮点击事件
onGenerateClick() {
const { inputWord, wordList } = this.data;
if (inputWord && inputWord.trim()) {
// 检查是否已经存在
if (!wordList.includes(inputWord.trim())) {
const newWordList = [...wordList, inputWord.trim()];
this.setData({
wordList: newWordList,
inputWord: '' // 清空输入框
});
wx.showToast({
title: '已添加到列表',
icon: 'success'
});
} else {
wx.showToast({
title: '该汉字已存在',
icon: 'none'
});
}
} else {
wx.showToast({
title: '请输入汉字',
icon: 'none'
});
}
},
// 选择汉字点击事件
onWordSelect(e: WechatMiniprogram.TouchEvent) {
const { word } = e.currentTarget.dataset;
const { wordList } = this.data;
if (!wordList.includes(word)) {
const newWordList = [...wordList, word];
this.setData({
wordList: newWordList
});
wx.showToast({
title: '已选择',
icon: 'success'
});
} else {
wx.showToast({
title: '已选择过',
icon: 'none'
});
}
},
// 从列表中移除汉字
onRemoveWord(e: WechatMiniprogram.TouchEvent) {
const { index } = e.currentTarget.dataset;
const { wordList } = this.data;
const newWordList = wordList.filter((_, i) => i !== index);
this.setData({
wordList: newWordList
});
wx.showToast({
title: '已移除',
icon: 'success'
});
},
// 清空所有选中的汉字
onClearAll() {
this.setData({
wordList: []
});
wx.showToast({
title: '已清空',
icon: 'success'
});
},
tapCard(e: WechatMiniprogram.TouchEvent) {
const { url } = e.currentTarget.dataset;
wx.navigateTo({ url });
+79 -10
View File
@@ -1,12 +1,81 @@
<!--index.wxml-->
<scroll-view class="scroll-view" scroll-y type="list">
<view
class="card"
wx:for="{{cards}}"
wx:for-item="card"
wx:key="*this"
bindtap="tapCard"
data-url="{{card.url}}">
<view class="card-title">{{card.title}}</view>
<view class="container">
<!-- 输入区域 -->
<view class="input-section">
<view class="input-row">
<input
class="word-input"
placeholder="请输入汉字"
value="{{inputWord}}"
bindinput="onInputChange"
maxlength="10" />
<button class="generate-btn" bindtap="onGenerateClick">生成</button>
</view>
</view>
</scroll-view>
<!-- 已选择的汉字列表 -->
<view class="selected-section" wx:if="{{wordList.length > 0}}">
<view class="section-header">
<text class="section-title"
>已选择的汉字 ({{wordList.length}})</text
>
<button class="clear-btn" bindtap="onClearAll" size="mini">
清空
</button>
</view>
<view class="word-list">
<view
class="word-item selected"
wx:for="{{wordList}}"
wx:key="*this"
wx:for-item="word"
wx:for-index="index">
<text class="word-text">{{word}}</text>
<text
class="remove-btn"
bindtap="onRemoveWord"
data-index="{{index}}"
>×</text
>
</view>
</view>
</view>
<!-- 可选汉字区域 -->
<view class="available-section">
<view class="section-header">
<text class="section-title"
>可选汉字 ({{availableWords.length}})</text
>
</view>
<view class="word-grid">
<view
class="word-item available {{wordList.includes(word) ? 'disabled' : ''}}"
wx:for="{{availableWords}}"
wx:key="*this"
wx:for-item="word"
bindtap="onWordSelect"
data-word="{{word}}">
<text class="word-text">{{word}}</text>
</view>
</view>
</view>
<!-- 原有的卡片区域 -->
<view class="cards-section">
<view class="section-header">
<text class="section-title">功能模块</text>
</view>
<scroll-view class="scroll-view" scroll-y type="list">
<view
class="card"
wx:for="{{cards}}"
wx:for-item="card"
wx:key="*this"
bindtap="tapCard"
data-url="{{card.url}}">
<view class="card-title">{{card.title}}</view>
</view>
</scroll-view>
</view>
</view>
+6 -6
View File
@@ -26,12 +26,12 @@ Page({
wx.getStorageSync('hasShowIntroduction') || false;
// 判断是否在PC端开发工具上运行
// const systemInfo = wx.getDeviceInfo();
// if (systemInfo.platform === 'devtools') {
// this.setData({
// isPCDevtool: true,
// });
// }
const systemInfo = wx.getDeviceInfo();
if (systemInfo.platform === 'devtools') {
this.setData({
isPCDevtool: true,
});
}
if (!hasShowIntroduction) {
this.setData({
+20 -35
View File
@@ -10,6 +10,7 @@ Page({
boxHeight: 0,
boxWidth: 0,
shapeDrawService: null as ShapeDrawService | null,
timer: null as any,
data: {
shapeList: [] as ShapeCard[],
@@ -93,6 +94,13 @@ Page({
}
},
handleSelectShape() {
this.setData({
showSelectShapePopup: true,
selectedShapeIndex: -1
});
},
openSelectShapePopup(e: any) {
const { index } = e.detail;
this.setData({
@@ -104,7 +112,6 @@ Page({
closeSelectShapePopup() {
this.setData({
showSelectShapePopup: false,
selectedShapeIndex: -1
});
},
@@ -174,12 +181,11 @@ Page({
});
}
// 先关闭弹窗
this.setData({
showSelectShapePopup: false,
selectedShapeIndex: -1,
shapeList: newShapeList
shapeList: newShapeList,
}, () => {
// 绘制canvas
this.drawCanvas();
});
},
@@ -233,56 +239,29 @@ Page({
this.setData({
showColorPopup: true,
currentColor: fillColor,
currentIndex: index
selectedShapeIndex: index
});
},
onCloseColorPopup() {
this.setData({
showColorPopup: false,
currentColor: '',
currentIndex: 0
selectedShapeIndex: 0
});
},
onChangeColor(e: any) {
const { color } = e.detail;
const { currentIndex } = this.data;
const { selectedShapeIndex } = this.data;
this.setData({
showColorPopup: false,
[`shapeList[${currentIndex}].fillColor`]: color
[`shapeList[${selectedShapeIndex}].fillColor`]: color
}, () => {
// 绘制canvas
this.drawCanvas();
})
},
// 测试ShapeDrawService
// testShapeDrawService() {
// if (!this.shapeDrawService) {
// wx.showToast({
// title: 'Canvas未初始化',
// icon: 'none'
// });
// return;
// }
// // 使用当前选中的图形进行测试
// const testShapes = this.data.shapeList.slice(0, 3); // 只测试前3个图形
// if (testShapes.length === 0) {
// wx.showToast({
// title: '请先选择图形',
// icon: 'none'
// });
// return;
// }
// this.shapeDrawService.draw(testShapes);
// wx.showToast({
// title: '绘制完成',
// icon: 'success'
// });
// },
onShareAppMessage() {
return {
title: '涂鸦丫-涂色|识字|画画|打印',
@@ -303,4 +282,10 @@ Page({
return;
}
},
onUnload() {
if (this.timer) {
clearTimeout(this.timer);
}
},
});
+2 -1
View File
@@ -4,7 +4,7 @@
<view class="btn-area">
<toy-button
type="green"
bind:click="openSelectShapePopup"
bind:click="handleSelectShape"
width="290rpx">
选择图形
</toy-button>
@@ -33,6 +33,7 @@
</view>
</view>
<shape-picker
selectedShapeList="{{shapeList}}"
show="{{showSelectShapePopup}}"
selectedShapeIndex="{{selectedShapeIndex}}"
bind:onClose="closeSelectShapePopup"
+89
View File
@@ -0,0 +1,89 @@
# 汉字选择页面 (wordDemo)
## 功能描述
这是一个汉字输入和选择的页面,主要包含以下功能:
### 上部区域 - 汉字输入
- 输入框:可以输入任意汉字(最多10个字符)
- 生成按钮:点击后将输入的汉字添加到已选择列表中
- **汉字校验**:自动校验输入内容是否为汉字,非汉字会提示错误
- **智能分割**:支持输入多个汉字,自动分割为单个汉字存储
### 中部区域 - 已选择的汉字
- 显示所有已选择的汉字
- 每个汉字都有删除按钮(×)
- 清空按钮:一键清空所有已选择的汉字
- 显示已选择汉字的数量
### 下部区域 - 可选汉字
-`demo/grade3.json` 文件中提取前30个汉字
- 以6列网格形式展示
- 已选择的汉字会显示为禁用状态
- 点击未选择的汉字可以添加到列表中
## 核心特性
### 🔍 **汉字校验**
- 使用Unicode范围 `\u4e00-\u9fff` 校验汉字
- 非汉字输入会显示Toast提示"请输入汉字"
- 确保 `wordList` 中只存储纯汉字
### ✂️ **智能分割**
- 支持输入多个汉字(如:"你好世界")
- 自动分割为单个汉字:["你", "好", "世", "界"]
- 过滤重复汉字,避免重复添加
- 显示实际添加的汉字数量
### 💾 **数据管理**
- `wordList` 中每个元素都是单个汉字
- 自动去重,避免重复存储
- 支持批量添加和单个删除
## 数据结构
- `inputWord`: 输入框中的汉字
- `wordList`: 已选择的汉字列表(string[]类型,每个元素为单个汉字)
- `availableWords`: 从grade3.json中提取的前30个汉字
## 使用方法
1. 在输入框中输入汉字(支持多个汉字)
2. 点击"生成"按钮,系统自动校验和分割
3. 点击下方网格中的汉字进行选择
4. 已选择的汉字会显示在上方,可以单独删除或一键清空
5. 所有选择的汉字都存储在 `this.data.wordList`
## 技术实现
### 汉字校验
```typescript
isChineseText(text: string): boolean {
const chineseRegex = /^[\u4e00-\u9fff]+$/;
return chineseRegex.test(text);
}
```
### 文本分割
```typescript
splitToSingleChars(text: string): string[] {
const chineseRegex = /[\u4e00-\u9fff]/g;
const matches = text.match(chineseRegex);
return matches || [];
}
```
## 文件结构
- `index.ts`: 页面逻辑文件
- `index.wxml`: 页面模板文件
- `index.less`: 页面样式文件
- `index.json`: 页面配置文件
+6
View File
@@ -0,0 +1,6 @@
{
"navigationBarTitleText": "汉字选择",
"navigationBarBackgroundColor": "#d2e7d8",
"backgroundColor": "#f5f5f5",
"enablePullDownRefresh": false
}
+159
View File
@@ -0,0 +1,159 @@
/**index.less**/
page {
height: 100vh;
background-color: #f5f5f5;
}
.container {
padding: 20rpx;
height: 100vh;
overflow-y: auto;
}
/* 输入区域样式 */
.input-section {
background-color: #fff;
border-radius: 20rpx;
padding: 30rpx;
margin-bottom: 20rpx;
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
}
.input-row {
display: flex;
align-items: center;
gap: 20rpx;
}
.word-input {
flex: 1;
height: 80rpx;
border: 2rpx solid #e0e0e0;
border-radius: 40rpx;
padding: 0 30rpx;
font-size: 32rpx;
background-color: #fafafa;
}
.generate-btn {
width: 160rpx;
height: 80rpx;
background-color: #07c160;
color: #fff;
border-radius: 40rpx;
font-size: 28rpx;
border: none;
display: flex;
align-items: center;
justify-content: center;
}
/* 通用区域样式 */
.section-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20rpx;
}
.section-title {
font-size: 32rpx;
font-weight: bold;
color: #333;
}
.clear-btn {
background-color: #ff4757;
color: #fff;
border-radius: 20rpx;
font-size: 24rpx;
padding: 10rpx 20rpx;
border: none;
}
/* 已选择的汉字区域 */
.selected-section {
background-color: #fff;
border-radius: 20rpx;
padding: 30rpx;
margin-bottom: 20rpx;
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
}
.word-list {
display: flex;
flex-wrap: wrap;
gap: 20rpx;
}
.word-item.selected {
background-color: #07c160;
color: #fff;
border-radius: 50rpx;
padding: 20rpx 30rpx;
position: relative;
min-width: 80rpx;
text-align: center;
}
.word-text {
font-size: 32rpx;
font-weight: bold;
}
.remove-btn {
position: absolute;
top: -10rpx;
right: -10rpx;
width: 40rpx;
height: 40rpx;
background-color: #ff4757;
color: #fff;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 24rpx;
font-weight: bold;
cursor: pointer;
}
/* 可选汉字区域 */
.available-section {
background-color: #fff;
border-radius: 20rpx;
padding: 30rpx;
box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
}
.word-grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 20rpx;
}
.word-item.available {
background-color: #f0f0f0;
color: #333;
border-radius: 50rpx;
padding: 20rpx;
text-align: center;
border: 2rpx solid #e0e0e0;
transition: all 0.3s ease;
}
.word-item.available:active {
background-color: #e0e0e0;
transform: scale(0.95);
}
.word-item.available.disabled {
background-color: #ccc;
color: #999;
cursor: not-allowed;
}
.word-item.available.disabled:active {
background-color: #ccc;
transform: none;
}
+148
View File
@@ -0,0 +1,148 @@
Page({
data: {
inputWord: '', // 输入框中的汉字
wordList: [] as string[], // 选中的汉字列表
availableWords: [] as string[], // 从grade3.json中提取的前30个汉字
},
onLoad() {
this.loadGrade3Words();
},
// 加载grade3.json中的前30个汉字
loadGrade3Words() {
const fs = wx.getFileSystemManager();
try {
const fileContent = fs.readFileSync('demo/grade3.json', 'utf8') as string;
const grade3Data = JSON.parse(fileContent);
const words = Object.keys(grade3Data).slice(0, 30);
this.setData({
availableWords: words
});
} catch (error) {
console.error('加载grade3.json失败:', error);
// 如果文件读取失败,使用一些示例汉字
const fallbackWords = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '人', '口', '手', '足', '目', '耳', '鼻', '舌', '心', '肝', '脾', '肺', '肾', '胃', '肠', '胆', '膀', '胱', '皮', '毛'];
this.setData({
availableWords: fallbackWords
});
}
},
// 输入框输入事件
onInputChange(e: WechatMiniprogram.Input) {
this.setData({
inputWord: e.detail.value
});
},
// 生成按钮点击事件
onGenerateClick() {
const { inputWord, wordList } = this.data;
if (inputWord && inputWord.trim()) {
const inputText = inputWord.trim();
// 校验输入是否为汉字
if (!this.isChineseText(inputText)) {
wx.showToast({
title: '请输入汉字',
icon: 'none'
});
return;
}
// 将输入文本分割为单个汉字
const singleChars = this.splitToSingleChars(inputText);
// 过滤掉已存在的汉字
const newChars = singleChars.filter(char => !wordList.includes(char));
if (newChars.length === 0) {
wx.showToast({
title: '所有汉字都已存在',
icon: 'none'
});
return;
}
// 添加到列表中
const newWordList = [...wordList, ...newChars];
this.setData({
wordList: newWordList,
inputWord: '' // 清空输入框
});
wx.showToast({
title: `已添加${newChars.length}个汉字`,
icon: 'success'
});
} else {
wx.showToast({
title: '请输入汉字',
icon: 'none'
});
}
},
// 校验文本是否为汉字
isChineseText(text: string): boolean {
// 汉字Unicode范围:\u4e00-\u9fff
const chineseRegex = /^[\u4e00-\u9fff]+$/;
return chineseRegex.test(text);
},
// 将文本分割为单个汉字
splitToSingleChars(text: string): string[] {
// 使用正则表达式匹配每个汉字
const chineseRegex = /[\u4e00-\u9fff]/g;
const matches = text.match(chineseRegex);
return matches || [];
},
// 选择汉字点击事件
onWordSelect(e: WechatMiniprogram.TouchEvent) {
const { word } = e.currentTarget.dataset;
const { wordList } = this.data;
if (!wordList.includes(word)) {
const newWordList = [...wordList, word];
this.setData({
wordList: newWordList
});
wx.showToast({
title: '已选择',
icon: 'success'
});
} else {
wx.showToast({
title: '已选择过',
icon: 'none'
});
}
},
// 从列表中移除汉字
onRemoveWord(e: WechatMiniprogram.TouchEvent) {
const { index } = e.currentTarget.dataset;
const { wordList } = this.data;
const newWordList = wordList.filter((_, i) => i !== index);
this.setData({
wordList: newWordList
});
wx.showToast({
title: '已移除',
icon: 'success'
});
},
// 清空所有选中的汉字
onClearAll() {
this.setData({
wordList: []
});
wx.showToast({
title: '已清空',
icon: 'success'
});
},
});
+63
View File
@@ -0,0 +1,63 @@
<!--index.wxml-->
<view class="container">
<!-- 上部:输入区域 -->
<view class="input-section">
<view class="input-row">
<input
class="word-input"
placeholder="请输入汉字(支持多个汉字)"
value="{{inputWord}}"
bindinput="onInputChange"
maxlength="20" />
<button class="generate-btn" bindtap="onGenerateClick">生成</button>
</view>
</view>
<!-- 已选择的汉字列表 -->
<view class="selected-section" wx:if="{{wordList.length > 0}}">
<view class="section-header">
<text class="section-title"
>已选择的汉字 ({{wordList.length}})</text
>
<button class="clear-btn" bindtap="onClearAll" size="mini">
清空
</button>
</view>
<view class="word-list">
<view
class="word-item selected"
wx:for="{{wordList}}"
wx:key="*this"
wx:for-item="word"
wx:for-index="index">
<text class="word-text">{{word}}</text>
<text
class="remove-btn"
bindtap="onRemoveWord"
data-index="{{index}}"
>×</text
>
</view>
</view>
</view>
<!-- 下部:可选汉字区域 -->
<view class="available-section">
<view class="section-header">
<text class="section-title"
>可选汉字 ({{availableWords.length}})</text
>
</view>
<view class="word-grid">
<view
class="word-item available {{wordList.includes(word) ? 'disabled' : ''}}"
wx:for="{{availableWords}}"
wx:key="*this"
wx:for-item="word"
bindtap="onWordSelect"
data-word="{{word}}">
<text class="word-text">{{word}}</text>
</view>
</view>
</view>
</view>