-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient-line-liff.js
281 lines (244 loc) · 7.66 KB
/
client-line-liff.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
var socket = io();
var isLineUser = false;
// Configuration
var line_thickness = 7;
var line_colour = "blue";
// Variables
var canvas = $('#paper');
var ctx = canvas[0].getContext('2d');
var id = Math.round($.now() * Math.random()); // Generate a unique ID
var drawing = false; // A flag for drawing activity
var touchUsed = false; // A flag to figure out if touch was used
var deleting = false;
var clients = {};
var cursors = {};
var prev = {}; // Previous coordinates container
var lastEmit = $.now();
var replayData = [];
var userData = [];
window.onload = function (e) {
liff.init(function (data) {
initializeApp(data);
});
};
function initializeApp(data) {
let profile = liff.getProfile().then(function (profile) {
var lineUserData = data.context;
isLineUser = true;
userData = {
'isLineUser': isLineUser,
'userData': lineUserData,
'userProfile': profile
};
//socket.emit('debug', userData);
socket.emit('lineRegister', userData, function (msg){
alert(msg);
});
}).catch(function (error) {
window.alert("Error getting profile: " + error);
});
document.getElementById('submit-drawing-data').addEventListener('click', function () {
var drawKeyword = document.getElementById('drawKeyword').value;
socket.emit('submitData', userData, drawKeyword, replayData, function (msg){
replayData = [];
alert(msg);
});
});
document.getElementById('delete').addEventListener('click', function () {
line_thickness = 20;
line_colour = "white";
deleting = true;
});
document.getElementById('openwindowbutton').addEventListener('click', function () {
liff.openWindow({
url: 'https://line.me'
});
});
document.getElementById('getUserList').addEventListener('click', function () {
alert('getuserlist');
socket.emit('requestUserList', userData);
});
// closeWindow call
document.getElementById('closewindowbutton').addEventListener('click', function () {
liff.closeWindow();
});
// sendMessages call
document.getElementById('sendmessagebutton').addEventListener('click', function () {
liff.sendMessages([{
type: 'text',
text: "You've successfully sent a message! Hooray!"
}, {
type: 'sticker',
packageId: '2',
stickerId: '144'
}]).then(function () {
//window.alert("Message sent");
}).catch(function (error) {
window.alert("Error sending message: " + error);
});
});
}
$('#doReplay').click(function (e){
e.preventDefault;
socket.emit('replay', true);
});
$('#debug').click(function (e){
e.preventDefault;
socket.emit('debug', true);
});
// Drawing helper function
function drawLine(fromx, fromy, tox, toy)
{
ctx.lineWidth = line_thickness;
ctx.strokeStyle = line_colour;
ctx.lineCap = "round";
ctx.beginPath();
ctx.moveTo(fromx, fromy);
ctx.lineTo(tox, toy);
ctx.stroke();
}
// On mouse down
canvas.on('mousedown', function(e) {
replayData.push({
'x': e.pageX,
'y': e.pageY,
'touch': false,
'drawing': drawing,
'id': id
});
e.preventDefault();
drawing = true;
prev.x = e.pageX;
prev.y = e.pageY;
});
// On touch start
canvas.on('touchstart', function(e) {
var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
drawing = true;
prev.x = touch.pageX;
prev.y = touch.pageY;
});
// On mouse move
canvas.on('mousemove', function(e) {
// Emit the event to the server
if ($.now() - lastEmit > 30)
{
replayData.push({
'x': e.pageX,
'y': e.pageY,
'touch': false,
'drawing': drawing,
'id': id
});
lastEmit = $.now();
}
// Draw a line for the current user's movement
if (drawing)
{
drawLine(prev.x, prev.y, e.pageX, e.pageY);
prev.x = e.pageX;
prev.y = e.pageY;
}
});
// On touch move
canvas.on('touchmove', function(e) {
e.preventDefault();
var touch = e.originalEvent.touches[0] || e.originalEvent.changedTouches[0];
// Emit the event to the server
if ($.now() - lastEmit > 10)
{
/* socket.emit('mousemove', {
'x': touch.pageX,
'y': touch.pageY,
'startX': prev.x,
'startY': prev.y,
'touch': true,
'drawing': drawing,
'id': id,
'userData': userData,
'isLineUser': isLineUser
}); */
replayData.push({
'x': touch.pageX,
'y': touch.pageY,
'startX': prev.x,
'startY': prev.y,
'touch': true,
'drawing': drawing,
'id': id,
'userData': userData,
'isLineUser': isLineUser
});
lastEmit = $.now();
}
// Draw a line for the current user's movement
if (drawing)
{
drawLine(prev.x, prev.y, touch.pageX, touch.pageY);
prev.x = touch.pageX;
prev.y = touch.pageY;
}
});
// On mouse up
canvas.on('mouseup mouseleave', function(e) {
drawing = false;
if(deleting){
line_thickness = 7;
line_colour = "blue";
deleting = false;
}
});
// On touch end
canvas.on('touchend touchleave touchcancel', function(e) {
drawing = false;
if(deleting){
line_thickness = 7;
line_colour = "blue";
deleting = false;
}
});
// Keep users screen up to date with other users cursors & lines
socket.on('moving', function (data) {
console.log(data);
// Create cursor
if ( !(data.id in clients) )
{
cursors[data.id] = $('<div class="cursor">').appendTo('#cursors');
}
// Move cursor
cursors[data.id].css({
'left' : data.x,
'top' : data.y
});
// Set the starting point to where the user first touched
if (data.drawing && clients[data.id] && data.touch)
{
clients[data.id].x = data.startX;
clients[data.id].y = data.startY;
}
// Show drawing
if (data.drawing && clients[data.id])
{
// clients[data.id] holds the previous position of this user's mouse pointer
drawLine(clients[data.id].x, clients[data.id].y, data.x, data.y);
}
// Save state
clients[data.id] = data;
clients[data.id].updated = $.now();
});
socket.on('replay', function (data) {
console.log('Replay data received.');
console.log(data);
(function theLoop (data, i) {
setTimeout(function () {
rePlay(data[i]);
--i;
if (i >= 0) { // If i > 0, keep going
theLoop(data,i); // Call the loop again, and pass it the current value of i
}
}, 70);
})(data.reverse(), data.length-1);
});
socket.on('debug', function (data){
console.log(data);
});