-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
142 lines (111 loc) · 3.76 KB
/
app.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
// Modules
import utils from "./modules/utils"
// App statics
require("./app.css")
// Dependencies
const io = require("./dependencies/socket.io")
const SimpleSignalClient = require("./dependencies/simple-signal-client.min")
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
screenWidth = w.innerWidth || e.clientWidth || g.clientWidth,
screenHeight = w.innerHeight|| e.clientHeight|| g.clientHeight;
var PI = 3.14159265;
var canvas = document.getElementById("main");
canvas.setAttribute('width', screenWidth);
canvas.setAttribute('height', screenHeight);
var ctx = canvas.getContext("2d");
ctx.strokeStyle = "#FF0000";
var room = utils.getParameterByName('room') || '1'
var socket = io('https://kaleidraw-signal-server-bglcynwuea.now.sh');
var signal = new SimpleSignalClient(socket, {room: room});
var peers = [];
signal.on('ready', function (ids) {
ids.forEach((id) => signal.connect(id));
});
signal.on('request', (request) => request.accept());
signal.on('peer', (peer) => {
peer.on('data', (data) => {
var oldColor = ctx.strokeStyle;
data = JSON.parse(data);
ctx.strokeStyle = data.color;
drawRadialPointsOnScreen(peer.id, data.radius, data.theta)
ctx.strokeStyle = oldColor; // reset color
})
peers.push(peer);
var index = peers.length - 1
peer.on('close', () => {
peers.splice(index, 1)
})
peer.on('error', (err) => {
console.error(err)
peers.splice(index, 1)
})
});
alert("Hello 2");
function sendToPeers(data) {
peers.forEach((peer) => peer.write(JSON.stringify(data)));
}
function drawLineOnScreen(x1, y1, x2, y2) {
// var rect = canvas.getBoundingClientRect();
// var x = e.clientX - rect.left;
// var y = e.clientY - rect.top;
// ctx.fillRect(x, y, 3, 3);
drawPool.push([x1, y1, x2, y2])
}
var drawPool = []
function draw () {
drawPool.forEach((line) => {
ctx.moveTo(line[0], line[1]);
ctx.lineTo(line[2], line[3]);
ctx.stroke();
});
drawPool = []; //empty the pool
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
function drawNewPoints (e) {
var rect = canvas.getBoundingClientRect();
var x = e.clientX - rect.left;
var y = e.clientY - rect.top;
var centerX = (screenWidth / 2);
var centerY = (screenHeight / 2);
var deltaX = x - centerX;
var deltaY = y - centerY;
var radius = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2));
var theta = Math.atan((deltaY / deltaX)) * 180 / 3.14159265;
drawRadialPointsOnScreen(null, radius, theta)
sendToPeers({
color: ctx.strokeStyle,
radius: radius,
theta: theta
});
}
var lastPoints = {};
function drawRadialPointsOnScreen (setID, radius, theta) {
var dozent = theta / ((12.0 / 36.0) * 30);
var centerX = (screenWidth / 2);
var centerY = (screenHeight / 2);
lastPoints[setID] = lastPoints[setID] || [];
for (var i = 0; i < 36; i++) {
if (i != dozent) {
var newTheta = (i * ((12.0 / 36) * 30)) + theta;
var x = centerX + radius * (Math.cos(newTheta * PI / 180));
var y = centerY + radius * (Math.sin(newTheta * PI / 180));
lastPoints[setID][i] = lastPoints[setID][i] || [x,y];
drawLineOnScreen(lastPoints[setID][i][0], lastPoints[setID][i][1], x, y);
lastPoints[setID][i] = [x, y];
}
}
}
canvas.addEventListener("click", function(e) {
drawNewPoints(e);
});
canvas.addEventListener("mousedown", function(e) {
lastPoints[null] = []; //clear line connections
canvas.addEventListener("mousemove", drawNewPoints);
});
canvas.addEventListener("mouseup", function(e) {
canvas.removeEventListener("mousemove", drawNewPoints);
});