-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
123 lines (109 loc) · 2.81 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>汉字动画</title>
<style>
body {
font-family: 'Microsoft YaHei', sans-serif;
background-color: #f2f2f2;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
text-align: center;
}
input[type='text'] {
font-size: 24px;
padding: 10px;
border: none;
border-radius: 5px;
width: 200px;
margin-right: 10px;
}
button {
font-size: 24px;
padding: 10px 20px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: #fff;
cursor: pointer;
}
#box {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
margin-top: 40px;
}
.hanzi-writer {
width: 120px;
height: 120px;
margin: 10px;
border: 1px solid #ccc;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
border-radius: 5px;
background-color: #fff;
}
</style>
<script src="./js/hanzi-writer@3.5.js"></script>
</head>
<body>
<div class="container">
<h1>汉字动画</h1>
<div>
<input type="text" placeholder="请输入汉字" id="ipt" />
<button id="btn">确定</button>
</div>
<div id="box"></div>
</div>
<script>
const $ = (id) => document.querySelector(id);
const ipt = $('#ipt');
const btn = $('#btn');
const box = $('#box');
let writers = []; // 存放当前所有的writer
// 监听btn点击 获取ipt内容
btn.addEventListener('click', () => {
let text = ipt.value || '';
text = text.trim();
if (!text) {
alert('请输入汉字');
return;
}
// 判断text 只能是中文
if (!/^[\u4e00-\u9fa5]+$/.test(text)) {
alert('请输入汉字,不能出现空格和其他');
return;
}
// 清空box内容
box.innerHTML = '';
// 清空上一次的动画
writers = [];
text.split('').forEach((item) => {
const writer = HanziWriter.create(box, item, {
width: 100,
height: 100,
padding: 5,
charDataLoader: async function (char, onComplete) {
const charData = await getJSON(char);
onComplete(charData);
}
});
writer.animateCharacter();
writer.loopCharacterAnimation();
writers.push(writer); // 将当前的writer保存起来
});
});
function getJSON(char) {
return fetch('./data/hanzi-writer-data/' + char + '.json').then((res) => res.json()).catch(() => {
alert(`没有匹配到${char}`)
});
}
</script>
</body>
</html>