-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas-handle.html
127 lines (114 loc) · 3.45 KB
/
canvas-handle.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
124
125
126
127
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<meta name="renderer" content="webkit">
<title>Canvas 绘图</title>
<meta name="description" content="首页描述"/>
<meta name="keywords" content="首页关键词"/>
<meta name="author" content="Web Layout:amu"/>
<meta name="format-detection" content="telephone=no,date=no,address=no,email=no,url=no"/>
<link rel="stylesheet" href="./src/style/base.css">
<style>
#block{
width: 100%;
height: 100%;
}
canvas{border: 1px seagreen dashed;}
#canvas2{
transform-origin: 0 0;
width: 600px;height: 300px;max-width: 100%;
box-sizing: border-box;
margin-top: 10px;
}
#canvas2:hover{cursor :url(assets/images/draw.ico) 0 28,auto;}
#save_href{display: none;}
</style>
</head>
<body>
<div class="paper">
<canvas id="canvasId" width="700" height="500"></canvas><br />
<input type="button" value="clear" onclick="hw.clear();" />
</div>
</body>
<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)";
var _this = this;
this.canvas.onmousedown = function (e) { _this.downEvent(e)};
this.canvas.onmousemove = function (e) { _this.moveEvent(e)};
this.canvas.onmouseup = function (e) { _this.upEvent(e)};
this.canvas.onmouseout = function (e) { _this.upEvent(e)};
this.moveFlag = false;
this.upof = {};
this.radius = 0;
this.has = [];
this.lineMax = 30;
this.lineMin = 3;
this.linePressure = 1;
this.smoothness = 80;
}
Handwriting.prototype.clear = function () {
this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
}
Handwriting.prototype.downEvent = function (e) {
this.moveFlag = true;
this.has = [];
this.upof = this.getXY(e);
}
Handwriting.prototype.moveEvent = function (e) {
if (!this.moveFlag)
return;
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 (this.has.length<=4)
return;
var len = Math.round(this.has[0].dis/2)+1;
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();
}
}
Handwriting.prototype.upEvent = function (e) {
this.moveFlag = false;
}
Handwriting.prototype.getXY = function (e)
{
return {
x : e.clientX - this.canvas.offsetLeft + (document.body.scrollLeft || document.documentElement.scrollLeft),
y : e.clientY - 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 = 2.5;
hw.smoothness = 100;
</script>
</html>