-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawing-board.html
168 lines (153 loc) · 4.5 KB
/
Drawing-board.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html>
<head>
<title>h5 demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
.clear:after{
content: " ";
display: table;
clear: both;
}
.color-select{width: 100%;}
.color{width: 50px;height:50px;float: left;border: 2px solid #eee;margin-right: 20px;}
.color:first-child{margin-left: 20px;}
.color-in{border:2px solid #666;}
.color0{background-color: #FF0000}
.color1{background-color: #FF7F00}
.color2{background-color: #FFFF00}
.color3{background-color: #76EE00}
.color4{background-color: #458B00}
.color5{background-color: #00BFFF}
.color6{background-color: #000}
.color7{background-color: #FFF}
.line-select{width: 100%;}
.line{width:50px;height:50px;border-radius: 50%;margin: 20px;float: left;}
.line div{border-radius: 50%;background-color: #000;}
.line0{width:5px;height:5px;margin:22px;}
.line1{width:10px;height:10px;margin:20px;}
.line2{width:15px;height:15px;margin:17px;}
.line3{width:20px;height:20px;margin:15px;}
.line-in{border:2px solid #999;}
.canvas{border:2px solid #000;margin:20px;}
</style>
</head>
<body style="overflow: auto" oncontextmenu="return false;" onselectstart="return false;">
<div class="color-select clear">
<div class="color color0"></div>
<div class="color color1"></div>
<div class="color color2"></div>
<div class="color color3"></div>
<div class="color color4"></div>
<div class="color color5"></div>
<div class="color color6 color-in"></div>
<div class="color color7">橡皮擦</div>
</div>
<div class="line-select clear">
<div class="line line-in"><div class="line0"></div></div>
<div class="line"><div class="line1"></div></div>
<div class="line"><div class="line2"></div></div>
<div class="line"><div class="line3"></div></div>
</div>
<canvas id='canvas' class="canvas"></canvas>
<div>
<button id='c'>clear<tton>
<button id="rotate">rotate<tton>
</div>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
var canvas = document.getElementById('canvas');
canvas.addEventListener('mousemove', onMouseMove, false);
canvas.addEventListener('mousedown', onMouseDown, false);
canvas.addEventListener('mouseup', onMouseUp, false);
canvas.addEventListener('touchstart',onMouseDown,false);
canvas.addEventListener('touchmove',onMouseMove,false);
canvas.addEventListener('touchend',onMouseUp,false)
canvas.height = 400;
//canvas.width = getWidth() - 25;
canvas.width = 600;
var ctx = canvas.getContext('2d');
ctx.lineWidth = 5; // 设置线宽
ctx.strokeStyle = "#000"; // 设置线的颜色
ctx.shadowBlur = 1;
ctx.shadowOffsetX = 6;
$(".color").click(function() {
$(".color-in").removeClass("color-in");
$(this).addClass("color-in");
ctx.strokeStyle = $(this).css("background-color");
});
$(".line").click(function(){
$(".line-in").removeClass("line-in");
$(this).addClass("line-in");
ctx.lineWidth = $(this).find("div").width();
});
var flag = false;
function onMouseMove(evt){
evt.preventDefault();
if (flag){
var p = pos(evt);
ctx.lineTo(p.x, p.y);
ctx.stroke();
}
}
function onMouseDown(evt){
evt.preventDefault();
ctx.beginPath();
var p = pos(evt);
ctx.moveTo(p.x, p.y);
flag = true;
}
function onMouseUp(evt){
evt.preventDefault();
flag = false;
}
var clear = document.getElementById('c');
clear.addEventListener('click',function(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
},false);
var count_deg =1;
var rotate = document.getElementById('rotate');
rotate.addEventListener('click',function(){
var deg = 90;
canvas.style.transform = "rotate("+"-"+count_deg*deg+"deg)";
ctx.save();
ctx.translate(canvas.width/2,canvas.height/2);
ctx.rotate(90*Math.PI/180);
ctx.translate(-canvas.width/2,-canvas.height/2);
count_deg++;
if(deg*count_deg == 360){
count_deg =0;
}
},false);
function pos(event){
var x,y;
if(isTouch(event)){
x = event.touches[0].pageX;
y = event.touches[0].pageY;
}else{
x = event.layerX;
y = event.layerY;
}
return {x:x,y:y};
}
function isTouch(event){
var type = event.type;
if(type.indexOf('touch')>=0){
return true;
}else{
return false;
}
}
function getWidth(){
var xWidth = null;
if (window.innerWidth !== null) {
xWidth = window.innerWidth;
} else {
xWidth = document.body.clientWidth;
}
return xWidth;
}
</script>
</body>
</html>