我是靠谱客的博主 深情羊,这篇文章主要介绍手写毛笔字效果-手机版,现在分享给大家,希望可以做个参考。

复制代码
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> <meta name="viewport" content="width=400"> <title> canvas手写毛笔字效果 </title> <style type="text/css"> body { margin: 0; padding: 0; background-color: #999999; } h1 { text-align: center; } #canvasId { background-color: #FFFFcc; } .button { width: 100px; height: 40px; } </style> </head> <body> <h1>手写毛笔字效果-手机版</h1> <canvas id="canvasId" width="400" height="400"></canvas><br /> <input type="button" value="全部清除" class="button" οnclick="hw.clear();" /> <input type="button" value="清除最后一笔" class="button" οnclick="hw.historyBack();" /> <script type="text/javascript"> function Handwriting(id) { this.canvas = document.getElementById(id); this.ctx = this.canvas.getContext("2d"); this.ctx.fillStyle = "rgba(0,0,0,0.25)"; this.canvas.addEventListener("touchstart", this.downEvent.bind(this), false); this.canvas.addEventListener("touchmove", this.moveEvent.bind(this), false); this.canvas.addEventListener("touchend", this.upEvent.bind(this), false); this.canvas.addEventListener("contextmenu", function(e){ e.preventDefault() }, false); //this.canvas.onmousedown = this.downEvent.bind(this); //this.canvas.onmousemove = this.moveEvent.bind(this); //this.canvas.onmouseup = this.upEvent.bind(this); //this.canvas.onmouseout = this.upEvent.bind(this); this.moveFlag = false; this.upof = {}; this.radius = 0; this.has = []; this.startOf = null; this.lineMax = 30; this.lineMin = 3; this.linePressure = 1; this.smoothness = 80; this.history = []; } Handwriting.prototype.clear = function () { this.history = []; this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height); } Handwriting.prototype.historyBack = function () { this.history.pop(); this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height); for (var i = 0; i < this.history.length; i++) { var h = this.history[i]; for (var j = 0; j < h.length; j+=3) { this.ctx.beginPath(); this.ctx.arc(h[j],h[j+1],h[j+2],0,2*Math.PI,true); this.ctx.fill(); } } } Handwriting.prototype.downEvent = function (e) { this.moveFlag = true; this.has = []; this.history.push([]); this.upof = this.getXY(e); this.startOf = this.upof; } Handwriting.prototype.moveEvent = function (e) { if (!this.moveFlag) return; e.preventDefault(); var of = this.getXY(e); var up = this.upof; var ur = this.radius; this.has.unshift({time:new Date().getTime() ,dis:this.distance(up,of)}); var dis = 0; var time = 0; for (var n = 0; n < this.has.length-1; n++) { dis += this.has[n].dis; time += this.has[n].time-this.has[n+1].time; if (dis>this.smoothness) break; } var or = Math.min(time/dis*this.linePressure+this.lineMin , this.lineMax) / 2; this.radius = or; this.upof = of; if (dis<7) return; if (this.startOf) { up = this.startOf; ur = or; this.startOf = null; } var len = Math.ceil(this.distance(up,of)/2); for (var i = 0; i < len; i++) { var x = up.x + (of.x-up.x)/len*i; var y = up.y + (of.y-up.y)/len*i; var r = ur + (or-ur)/len*i; this.ctx.beginPath(); this.ctx.arc(x,y,r,0,2*Math.PI,true); this.ctx.fill(); this.history[this.history.length-1].push(x,y,r); } } Handwriting.prototype.upEvent = function (e) { this.moveFlag = false; } Handwriting.prototype.getXY = function (e) { var x = e.touches[0].clientX; var y = e.touches[0].clientY; //var x = e.clientX; //var y = e.clientY; return { x : x - this.canvas.offsetLeft + (document.body.scrollLeft || document.documentElement.scrollLeft), y : y - this.canvas.offsetTop + (document.body.scrollTop || document.documentElement.scrollTop) } } Handwriting.prototype.distance = function (a,b) { var x = b.x-a.x , y = b.y-a.y; return Math.sqrt(x*x+y*y); } var hw = new Handwriting("canvasId"); hw.lineMax = 40; hw.lineMin = 5; hw.linePressure = 3.1; hw.smoothness = 100; </script> </body> </html>


在线演示:https://js-game.github.io/Handwriting/handwriting.html

最后

以上就是深情羊最近收集整理的关于手写毛笔字效果-手机版的全部内容,更多相关手写毛笔字效果-手机版内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(85)

评论列表共有 0 条评论

立即
投稿
返回
顶部