识字小程序—hanzi-writer-miniprogram实现临摹笔画动画播放等

1.hanzi-writer-miniprogram的安装请看上一篇文章
小程序安装hanzi-writer-miniprogram:

2.效果图
在这里插入图片描述
可扫码体验

代码
pages/hanziWrite/hanziWrite.wxml

    <view class="title">汉字识字小程序</view>
	<view class="js-char-form char-form">
        <view class="char-form-item char-form-text">请输入汉字</view>
        <view class="char-form-item char-form-text">
            <input type="text" class="js-char char-input" size="1" maxlength="1" value="{{inputstr}}" bindinput="inputstrBtn" />
        </view>
        <view class="char-form-item">
            <button type="submit" class="update-btn" bindtap="updateCharacter">确定</button>
        </view>
    </view>
    
	<view class="actions clear_both">
		<button class="showCharacter" bindtap="showCharacter">显示笔画</button>
		<button class="hideCharacter" bindtap="hideCharacter">隐藏笔画</button>
		<button class="js-animate" bindtap="playAnimate">播放动画</button>
		<button class="js-quiz" bindtap="copybook">临摹</button>
    </view>
	<view id="target">
        <hanzi-writer-view id="hz-writer" width="300" height="300" />
    </view>
    <view class="actions clear_both">
        <button class="showCharacter read" bindtap="wordYun">跟我读</button>
        <button class="showCharacter read" bindtap="readStrokes">笔画</button>
	</view>

pages/hanziWrite/hanziWrite.wxss

/* pages/hanziWrite/hanziWrite.wxss */

 button {
  display: inline-block !important;
  outline: none;
  width: 80px !important;
  height: 35px !important;
  /* line-height: 35px !important; */
  padding:3px 5px !important;
  font-size: 16px !important;
  font-weight: 400 !important;
}

/* dd------------------------------------------------------- */
.clear_both:after{
	display: block;
	content: " ";
	clear: both;
	overflow: hidden;
	height: 0;
}
body, html {
	background: #ffffff;
	margin: 0;
	padding: 0;
	text-align: center;
	font-family: 'Raleway', sans-serif;
	font-size: 14px;
}
body {
	padding-bottom: 80px;
}
input, button {
	outline: none;
}
button {
  background: none;
  border: 1px solid #444;
  color: #444;
  cursor: pointer;
  transition: all 300ms;
}
 
button:hover {
  color: #1e6cd2;
  border: 1px solid #1e6cd2;
  box-shadow: none;
}
.myInput{
  margin: 20px 0;
}
button, #target, .title {
	box-shadow: 0 1px 5px rgba(0,0,0,0.15);
}

button, #target {
	text-shadow: 0 1px 1px white;
}

.title {
	color: white;
	background: #1abc9c;
	padding: 15px 0;
  margin: 0 0 30px;
  text-align: center;
}

#target {
  border-radius: 10px;
  border: 1px solid #ccc;
  box-shadow: 0 0 8rpx 0 #bfbfbf;
	clear: both;
	width: 300px;
	height: 300px;
	margin: 30px auto;
}

.char-form {
  margin: 0 0 20px;
  display: flex;
  
}
.char-form-text{
  height: 35px;
  line-height: 35px;
}
.char-form-item{
  flex-grow: 1;
  margin: 0 2vw;
}

.char-form button {
	padding: 8px 5px !important;
	line-height: 14px !important;
	display: inline-block !important;
	vertical-align: top !important;
}

.char-input {
  border: 1px solid #ccc;
	font-size: 16px;
	padding: 5px;
	width: 25px;
	text-align: center;
}

.actions {
	width: 90vw;
	margin: 0 auto;
}

.actions button {
	display: block;
	width: 80px;
	height: 3rem;
	border: 1px solid #1abc9c;
	color: #1abc9c;
	float: left;
	border-radius: 10px;
	margin-right: 4px !important;
	margin-bottom: 4px;
}
.actions button:last-child {
	margin-right: 0;
}

button {
	background: none;
	border: 1px solid #444;
	color: #444;
	cursor: pointer;
	transition: all 300ms;
}
.update-btn {
	width: 97px;
  border: 1px solid #1abc9c;
	color: #1abc9c;
}

button:hover {
	color: #1e6cd2;
	border: 1px solid #1e6cd2;
	box-shadow: none;
}
.char-input {
	width: 7rem;
}
#hz-writer{
  width: 300px !important;
  height: 300px !important;
}
.read{
  margin: 10px 0;
}

/* 拼音-----------------------------------------------start */
.pinyin{
  width: 90vw;
  height: 30px;
  border-top: 1px solid #bfbfbf;
  border-bottom: 1px solid #bfbfbf;
  margin: 15px auto;
}
.pinyin_top{
  height: 10px;
  border-bottom: 1px solid #bfbfbf;
  text-align: center;
}
.pinyin_text{
  line-height: 30px;
}
/* 拼音-------------------------------------------------end */

@media screen and (max-width:370px) {
  button {
    display: inline-block !important;
    outline: none;
    width: 20vw !important;
    height: 25px !important;
    padding:3px 5px !important;
    font-size: 12px !important;
  }
  .char-form-text {
      height: 25px;
      line-height: 25px;
  }
  .char-form button {
    padding: 5px 5px !important;
    line-height: 14px !important;
  }
  .char-input{
    padding: 1px;
  }
  /* .actions button {
    width: 65px;
  } */
}

pages/hanziWrite/hanziWrite.js

// pages/hanziWrite/hanziWrite.js
//index.js
//获取应用实例
const app = getApp()
import createHanziWriterContext from 'hanzi-writer-miniprogram';
//引入插件:微信同声传译
const plugin = requirePlugin('WechatSI');
// import createHanziWriterContext from '../../miniprogram_npm/hanzi-writer-miniprogram/hanzi-writer-view';                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                -view.js");
Page({
  properties: {},
  data: {
    inputstr: '天',
    inputCharacter: '天',
    content: '天',//内容
    src:'', //
    pinyinval:'',
  },
  onLoad: function(options) {
    console.log("Do something initial when launch")
    this.drawCharacter();
    // 跟我读
    this.readCharacter();
    //拼音
    this.pinyin();
  },
  // 跟读
  readCharacter:function(){
    //创建内部 audio 上下文 InnerAudioContext 对象。
    this.innerAudioContext = wx.createInnerAudioContext();
    this.innerAudioContext.onError(function (res) {
      console.log(res);
      wx.showToast({
        title: '语音播放失败',
        icon: 'none',
      })
    }) 
  },
  // 画出文字
  drawCharacter:function(){
    this.writerCtx = createHanziWriterContext({
      id: 'hz-writer',
      character:this.data.inputCharacter,
      page: this,
      radicalColor: '#1abc9c',//偏旁部首颜色
    });
  },
  // 输入框
  inputstrBtn:function(e){
    console.log(e.detail.value)
    var val = e.detail.value;
    this.setData({
      inputstr: val,
      content: val,//跟我读内容
    });
  },

  //更新
  updateCharacter:function(e){
    // this.writerCtx.showCharacter();
    this.setData({
      inputCharacter: this.data.inputstr
    });
    this.pinyin();
    this.drawCharacter();
  },
  
  //显示笔画
  showCharacter:function(e){
    this.writerCtx.showCharacter();
  },

  //隐藏笔画
  hideCharacter:function(e){
    this.writerCtx.hideCharacter();
  },

  //播放动画
  playAnimate:function(e){    
    this.writerCtx.loopCharacterAnimation();
  },

  //临摹
  copybook:function(e){
    this.writerCtx.quiz();
    // var colorName ='#ff0'
    // this.writerCtx.updateColor(colorName, colorVal, options);
  },
  // 跟我读-----------------------------------------------------start
  // 文字转语音
  wordYun:function (e) {
    var that = this;
    var content = this.data.content;
    plugin.textToSpeech({
      lang: "zh_CN",
      tts: true,
      content: content,
      success: function (res) {
        console.log(res);
        console.log("succ tts", res.filename);
        that.setData({
          src: res.filename
        })
        that.yuyinPlay();
 
      },
      fail: function (res) {
        console.log("fail tts", res)
      }
    })
  },
  
  //播放语音
  yuyinPlay: function (e) {
    if (this.data.src == '') {
      console.log(暂无语音);
      return;
    }
    this.innerAudioContext.src = this.data.src //设置音频地址
    this.innerAudioContext.play(); //播放音频
  },
  // 跟我读----------------------------------------------------end
  // 笔画
  readStrokes:function(){
    var character = "你"
    // this.writerCtx.loadCharacterData(character, options = {})
  }
});

拼音

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 28
    评论
好的,以下是一个简单的拆分书法字笔画的 Python 程序,它使用了一个常见的汉字笔画数据集: ```python import json # 加载汉字笔画数据集 with open('hanzi_strokes.json', 'r', encoding='utf-8') as f: hanzi_strokes = json.load(f) def split_strokes(word): """ 拆分汉字笔画 """ if word in hanzi_strokes: return hanzi_strokes[word] else: return None # 测试 word = input("请输入一个汉字:") strokes = split_strokes(word) if strokes: print("{} 的笔画为:{}".format(word, strokes)) else: print("未找到该汉字的笔画数据!") ``` 要使用此程序,您需要首先将包含汉字笔画数据的 JSON 文件下载到您的计算机上。这里提供了一个简单的数据集,包含了常见汉字的笔画数据,您可以将其保存为 `hanzi_strokes.json` 文件。 JSON 文件的格式如下: ```json { "一": ["h", "s"], "乙": ["p", "w"], "二": ["h", "h"], "十": ["h", "s"], "丁": ["p", "w", "w"], "厂": ["h", "p", "s"], "七": ["h", "p", "s"], "卜": ["s"], "人": ["p", "h"], "入": ["h", "p"], "八": ["h", "p", "s"], ... } ``` 其中,每个汉字对应一个笔画列表,列表中的每个元素表示一个笔画,每个笔画用一个字母表示。 程序首先加载汉字笔画数据集,然后定义一个函数 `split_strokes`,用于拆分汉字笔画。该函数接受一个汉字作为参数,如果找到该汉字的笔画数据,就返回一个笔画列表,否则返回 `None`。 最后,程序通过输入一个汉字,调用 `split_strokes` 函数来拆分该汉字的笔画,并输出结果。 注意,这个程序只是一个简单的示例,它并不能处理所有汉字,并且可能存在一些错误和缺陷。如果您需要更准确和全面的汉字笔画数据,可以考虑使用更专业的数据集或工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值