前端应用 - 汉字笔顺书写演示带拼音及发音进阶版

本版本更加适合小学一年级学生学习汉字笔顺,优化后说明:

1.支持多文字演示,可以上下页切换

2.支持连续书写演示和连续书写练习

html:

<!DOCTYPE html>
<html lang="en-us">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="description" content="">
        <meta name="keywords" content="">
        <meta name="viewport" content="width=device-width,user-scalable=no, initial-scale=1">
        <title>汉字笔顺书写演示</title>
        <link rel="stylesheet" href="static/css/styles.css" />
    </head>
    <body>
        <h1 class="title">汉字笔顺书写演示</h1>
        <div class="actions">
            <button class="js-animate">书写演示</button>
            <button class="js-quiz">书写练习</button>
        </div>
        <div id="pinyin"></div>
        <!--汉字所在的田字格背景图-->
        <svg xmlns="http://www.w3.org/2000/svg" width="360" height="360" id="target">
            <line x1="0" y1="0" x2="360" y2="0" stroke="#DDD"/>
            <line x1="0" y1="0" x2="0" y2="360" stroke="#DDD"/>
            <line x1="0" y1="360" x2="360" y2="360" stroke="#DDD"/>
            <line x1="360" y1="0" x2="360" y2="360" stroke="#DDD"/>
        
            <line x1="0" y1="180" x2="360" y2="180" stroke="#DDD"/>
            <line x1="180" y1="0" x2="180" y2="360" stroke="#DDD"/>
            <line x1="0" y1="0" x2="360" y2="360" stroke="#DDD"/>
            <line x1="0" y1="360" x2="360" y2="0" stroke="#DDD"/>
        </svg>
        <div style="clear: both;"></div>
        <div class="toolsImageBtn">
            <input type="button" title="上一个字"  value="上一个" class="btn goPrev">
            <input type="button" title="下一个字"  value="下一个" class="btn goNext">
            <input type="button" title="开始"     value="开始"   class="btn goOk">
        </div>
        <textarea class="form-control" rows="3" name="demo" id="demo" name-id="demo" placeholder="请在这里输入需要练习的生字"></textarea>
        <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/hanzi-writer@2.2/dist/hanzi-writer.min.js"></script>
        <script type="text/javascript" src="static/js/app.js?v=2021.9.30 17:55"></script>
        <script type="text/javascript" src="static/js/pinyin_dict_withtone.js"></script>
        <script type="text/javascript" src="static/js/pinyinUtil.js"></script>
    </body>
</html>

app.js:

$(function() {
    var writer;
    var isCharVisible;
    var isOutlineVisible;
    var hzArr = [];

    function printStrokePoints(data) {
        var pointStrs = data.drawnPath.points.map(point => `{x: ${point.x}, y: ${point.y}}`);
    }

    function updateCharacter(character) {
        var targetBgHtml = '<line x1="0" y1="0" x2="360" y2="0" stroke="#DDD"/>'+
                '<line x1="0" y1="0" x2="0" y2="360" stroke="#DDD"/>'+
                '<line x1="0" y1="360" x2="360" y2="360" stroke="#DDD"/>'+
                '<line x1="360" y1="0" x2="360" y2="360" stroke="#DDD"/>'+
                '<line x1="0" y1="180" x2="360" y2="180" stroke="#DDD"/>'+
                '<line x1="180" y1="0" x2="180" y2="360" stroke="#DDD"/>'+
                '<line x1="0" y1="0" x2="360" y2="360" stroke="#DDD"/>'+
                '<line x1="0" y1="360" x2="360" y2="0" stroke="#DDD"/>';
        $('#target').html(targetBgHtml);
        var fayin  = 'https://tts.baidu.com/text2audio.mp3?tex='+character+'&cuid=baike&lan=ZH&ctp=1&pdt=301&vol=100&rate=32&per=2&spd=3&pit=3'
        var pinyin = pinyinUtil.getPinyin(character, ' ', true, 0);
        var laba   = '<span class="boyin"><img src="./static/images/laba.png" style="width: 32px;"></a>';
        var boyin  = '<audio src="'+fayin+'" id="boyin"></audio>';
        $('#pinyin').html(pinyin + ' '+laba + boyin);
        window.location.hash = character;
        writer = HanziWriter.create('target', character, {
            width: 360,
            height: 360,
            radicalColor: '#166E16',
            onCorrectStroke: printStrokePoints,
            onMistake: printStrokePoints,
            showOutline: true
        });
        isCharVisible = true;
        isOutlineVisible = true;
        window.writer = writer;
    }
    updateCharacter('永');
    $(document).on('click','.boyin',function(){
        document.getElementById("boyin").play();
    });
    $(document).on('click','.js-animate',function(){
        $(this).addClass("check").siblings().removeClass("check");
        writer.animateCharacter();
    });
    $(document).on('click','.js-quiz',function(){
        $(this).addClass("check").siblings().removeClass("check");
        writer.quiz({
            showOutline: true
        });
    });
    $(document).on('click','.goOk',function(){
        var _val = $('textarea[name="demo"]').val().trim();
            _val = _val.replace(/[^\u4e00-\u9fa5]/gi,"");
        $('textarea[name="demo"]').val(_val);
        var _arr = _val.split("");
        hzArr    = _arr.filter(s => $.trim(s).length > 0);
        if(hzArr.length > 0){
            $('.goPrev').attr('val',hzArr.length);
            $('.goNext').attr('val',0);            
            updateCharacter(hzArr[0]);
        }
    });
    $(document).on('click','.goPrev',function(){
        var _val = $(this).attr('val');
        if(hzArr.length > 0){
            var thisIndex = Number(_val)-1;
            thisIndex = thisIndex < 0?0:thisIndex;
            updateCharacter(hzArr[thisIndex]);
            $('.goPrev').attr('val',thisIndex);
            $('.goNext').attr('val',thisIndex+1);
            if($('.js-animate').hasClass("check")){
                writer.animateCharacter();
            }
            if($('.js-quiz').hasClass("check")){
                writer.quiz({showOutline: true});
            }
        }
    });
    $(document).on('click','.goNext',function(){
        var _val = $(this).attr('val');
        if(hzArr.length > 0){
            var thisIndex = Number(_val)+1;
            thisIndex = thisIndex >= hzArr.length?0:thisIndex;
            updateCharacter(hzArr[thisIndex]);
            $('.goPrev').attr('val',thisIndex-1);
            $('.goNext').attr('val',thisIndex);
            if($('.js-animate').hasClass("check")){
                writer.animateCharacter();
            }
            if($('.js-quiz').hasClass("check")){
                writer.quiz({showOutline: true});
            }
        }
    });
});

演示:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值