-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.html
More file actions
209 lines (203 loc) · 5.62 KB
/
Copy pathdraw.html
File metadata and controls
209 lines (203 loc) · 5.62 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="/lib/css/normalize.min.css" rel="stylesheet">
<link href="/lib/css/common.css" rel="stylesheet">
<style for="canvas">
.canvas-div{
height: 4111px;
width: 1024px;
margin: 0 auto;
}
#canvas{
height: 4111px;
width: 1024px;
margin: 0 auto;
}
/*.paint{*/
/*background-color: #dddddd;*/
/*position: fixed;*/
/*width: 4rem;*/
/*height: 12rem;*/
/*right: 1rem;*/
/*top: 10rem;*/
/*}*/
</style>
</head>
<body>
<div class="container">
<header role="banner">
<div class="back-top">
<i class="material-icons">keyboard_arrow_up</i>
</div>
</header>
<nav role="menu">
<ul>
<li>
<a href="/">IO</a>
</li>
<li>
<a href="/im.html">IM</a>
</li>
<li>
<a href="/draw.html">Draw</a>
</li>
</ul>
</nav>
<div class="blank"></div>
<div class="canvas-div">
<canvas id="canvas"></canvas>
</div>
<!--<div class="paint">-->
<!--<input>-->
<!--</div>-->
<footer>
<div class="copy left"><a href="http://guoshikai.com">guoshikai.com</a> © 2016</div>
<div class="right">
<a href="https://github.com/shikaiguo">Github</a>
</div>
</footer>
</div>
<script src="/lib/js/jquery-3.1.1.min.js"></script>
<script src="/lib/layer/layer.js"></script>
<script>
$(function(){
$(document).scroll(scrollListener);
scrollListener();
webSocketInit();
$(".back-top").click(function(){
$("body").animate({ scrollTop: 0 }, 200);
});
});
</script>
<script>
var navHeight = false;
var navStatus = false;
function scrollListener(){
var documentScrollTop = $(document).scrollTop();
/* nav */
navHeight = navHeight || $("nav").offset().top + 1;
var f = documentScrollTop > navHeight;
if(f !== navStatus){
navStatus = f;
$("nav,.blank").toggleClass("nav-pin");
$(".back-top").fadeToggle(200);
}
}
var canvas;
var context;
function canvasInit(){
canvas = $("#canvas")[0];
canvas.setAttribute('width', $("#canvas").width());
canvas.setAttribute('height', $("#canvas").height());
if (!canvas.getContext) {
return;
}
context = canvas.getContext("2d");
context.lineJoin = "round";
context.lineWidth = 2;
$("#canvas").mousedown(function(e){
pointPush(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
down = true;
});
$("#canvas").mousemove(function(e){
if(true === down){
pointPush(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
}
});
$("#canvas").mouseup(function(e){
down = false;
});
// $("#canvas").mouseleave(function(e){
// down = false;
// });
}
</script>
<script>
var ws;
var url = "ws://api.guoshikai.com:64378";
var fd;
var color;
function webSocketInit(){
try {
ws = new WebSocket(url);
ws.onopen = function (f){
canvasInit();
}
ws.onmessage = function (f) {
var data = $.parseJSON(f.data);
if("undefined" !== typeof data.user){
fd = data.user;
color = data.color||"#000";
return false;
}
redraw(data.fd, data);
}
ws.onclose = function (msg) {}
// 超时 5s内没连接成功就断开
setTimeout(function(){
if(1 !== ws.readyState){
ws.close();
var confirm = layer.confirm('WebSocket连接超时,是否重连?', {
btn: ['是','否']
}, function(){
layer.close(confirm);
webSocketInit();
});
}
}, 5000);
}
catch (ex) {
console.log(ex);
}
}
</script>
<script>
var down = false;
var lastPointArr = [];
function pointPush(x, y, drag){
if("undefined" === typeof drag){
drag = false;
}
if("undefined" !== typeof lastPointArr[fd] && (Math.abs(lastPointArr[fd].x-x)+Math.abs(lastPointArr[fd].y-y) < 3)){
return false;
}
sendFrame({
x: x,
y: y,
d: drag,
c: color
})
redraw(fd, {
x: x,
y: y,
d: drag,
c: color
});
}
function redraw(fd, data){
context.strokeStyle = data.c;
context.beginPath();
if(data.d && "undefined" !== typeof lastPointArr[fd]){
context.moveTo(lastPointArr[fd].x, lastPointArr[fd].y);
}else{
context.moveTo(data.x-1, data.y);
}
context.lineTo(data.x, data.y);
context.closePath();
context.stroke();
lastPointArr[fd]=data;
}
// 统一发送方法
function sendFrame(f){
try {
ws.send(JSON.stringify(f));
}catch (ex) {
console.log(ex);
}
}
</script>
</body>
</html>