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

这篇文章介绍了如何在微信小程序中安装hanzi-writer-miniprogram,包括步骤和代码实现。用户可以输入汉字并查看笔画,还有跟读和动画功能,适合汉字学习者使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 = {})
  }
});

拼音

<think>好的,我现在需要处理用户关于Hanzi Writer的查询。首先,用户明确提到想寻找与汉字书写相关的工具或资源,并特别询问了Hanzi Writer。根据用户提供的引用内容,我需要整合这些信息,确保回答结构清晰且符合用户的要求。 首先,检查引用内容。引用[1]提到Hanzi Writer是汉字书写的工具,强调其作为桥梁的作用;引用[2]说明它是一个免费开源的JavaScript库,支持简繁体,有动画和练习功能;引用[3]指出其开源性质,提供笔画数据,适合开发和学习;引用[4]则涉及技术细节,如生成SVG和使用npm安装。需要将这些信息综合起来,分点介绍Hanzi Writer的功能、技术特点、安装方法等。 接下来,用户希望回答结构清晰,帮助解决问题。需要按照逻辑顺序组织内容,比如先介绍工具是什么,然后主要功能,技术分析,安装使用,应用场景,最后相关资源。同时,每个部分需要引用对应的来源,比如功能部分引用[2],技术分析引用[3],安装引用[4]等。 还要注意用户提到的系统级指令,比如数学公式的格式,但这里可能不涉及数学内容,所以重点放在中文回答、生成相关问题、正确引用上。确保在回答末尾添加相关问题,并正确标注引用标识,如[^1]、[^2]等。 可能需要注意用户提到的“引用站内引用”,但用户给出的引用内容似乎都是关于Hanzi Writer的不同方面,需要正确对应到各个引用编号。例如,介绍开源性质时引用[3],技术细节引用[2]和[4]等。 最后,生成的相关问题需要与Hanzi Writer相关,比如与其他工具的比较、教育应用、技术支持等,确保问题有针对性,帮助用户进一步探索。 现在需要将以上思考整理成自然的中文回答,分点明确,引用正确,并附上相关问题。同时,避免使用Markdown格式,按照用户的要求使用口语化中文,但结构清晰。</think>关于汉字书写工具Hanzi Writer的详细介绍如下: ### Hanzi Writer 汉字书写工具解析 1. **核心功能** Hanzi Writer提供动态笔顺演示与书写练习功能,支持简体/繁体汉字。用户可通过手绘模仿学习正确笔画顺序,适用于汉字初学者及书法爱好者。工具包含逐帧动画展示、实时笔画检测和错误提示,确保书写规范。 2. **技术实现** 基于JavaScript开发,采用SVG技术生成可交互的汉字笔顺动画。内置精确的笔画坐标数据,支持通过npm安装集成到Web项目中[^4]。开源特性允许开发者自定义动画速度、颜色等参数[^3]。 3. **安装与使用** ```bash npm install hanzi-writer ``` 代码调用示例: ```javascript const HanziWriter = require('hanzi-writer'); const writer = HanziWriter.create('target-div', '汉', { width: 100, strokeAnimationSpeed: 2 }); writer.animateCharacter(); ``` 4. **应用场景** - 语言学习平台集成汉字教学模块 - 开发汉字书法临摹APP - 教育机构制作汉字课件 5. **资源获取** 官方提供: - 实时演示平台(引用[2]) - GitHub开源库(引用[3]) - 详细API文档(引用[4])
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值