-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.html
369 lines (246 loc) · 8.63 KB
/
canvas.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<!DOCTYPE html>
<html>
<head>
<title>Remarkable Canvas</title>
</head>
<style>
canvas {
position: absolute;
top: 8px;
left: 8px;
}
</style>
<body>
<canvas width="1404" height="1053" id="main"></canvas>
<script>
var csMain = document.getElementById("main");
var csDraw = document.createElement("canvas");
[csDraw.width, csDraw.height] = [csMain.width, csMain.height];
document.body.appendChild(csDraw);
var overlays = [];
var index = -1;
var page = -1;
var landscape = {landscape};
var dark = false;
var active = false;
var color = null;
var COL_BG = ["rgb(235, 237, 239)", "rgb(35, 37, 39)"];
var COL_PEN = ["rgb(200, 210, 220)", "rgb(80, 90, 100)"];
var COL_DRAW = ["rgb(255, 255, 255)", "rgb(0, 0, 0)"];
var COL_STROKE = ["black", "white"];
var COL_BORDER = ["rgb(200, 210, 220)", "rgb(80, 90, 100)"];
function paint(pen) {
csOver = overlays[page].at(index);
cxOver = csOver.getContext("2d");
cxOver.lineWidth = Math.exp(pen.z) - 1;
cxOver.strokeStyle = color;
cxOver.lineTo(pen.x, pen.y);
cxOver.closePath();
cxOver.stroke();
cxOver.beginPath();
cxOver.moveTo(pen.x, pen.y);
}
function addOverlay() {
var i = overlays[page].length;
while (i--) {
if (overlays[page][i].hidden) {
document.body.removeChild(overlays[page][i]);
overlays[page].splice(i, 1);
}
}
var csOver = document.createElement("canvas");
[csOver.width, csOver.height] = [csDraw.width, csDraw.height];
[csOver.style.backgroundColor, csOver.style.opacity] = ["transparent", 1];
document.body.appendChild(csOver);
overlays[page].push(csOver);
index = -1;
}
function onPenMove(pen, hovering) {
cxDraw = csDraw.getContext("2d");
if (hovering) {
active = false;
cxDraw.closePath();
cxDraw.clearRect(0, 0, csDraw.width, csDraw.height);
csOver = overlays[page].at(index);
cxOver = csOver.getContext("2d");
cxOver.moveTo(pen.x, pen.y);
} else {
if (!active) {
active = true;
addOverlay();
csOver = overlays[page].at(index);
cxOver = csOver.getContext("2d");
cxOver.moveTo(pen.x, pen.y);
}
}
cxDraw.fillStyle = COL_PEN[Number(dark)];
cxDraw.beginPath();
cxDraw.arc(pen.x, pen.y, 6, 0, 2*Math.PI);
cxDraw.fill();
}
function addPage() {
overlays.push([]);
page = overlays.length - 1;
addOverlay();
}
function resetPage() {
for (let i = 0; i < overlays[page].length; i++) {
document.body.removeChild(overlays[page][i]);
}
overlays[page].length = 0;
addOverlay();
}
function applyTheme() {
document.body.style.backgroundColor = COL_BG[Number(dark)];
cxDraw = csDraw.getContext("2d");
csDraw.style.border = COL_BORDER[Number(dark)];
csDraw.style.backgroundColor = COL_DRAW[Number(dark)];
if (COL_STROKE.includes(color) || color == null) {
color = COL_STROKE[Number(dark)];
}
}
function toggleView() {
landscape = !landscape;
[csDraw.width, csDraw.height] = [csMain.height, csMain.width];
[csMain.width, csMain.height] = [csDraw.width, csDraw.height];
resetPage();
}
function setOverlay(visible) {
csOver = overlays[page].at(index);
csOver.hidden = !visible;
}
function setPage(visible) {
for (let i = 0; i < overlays[page].length; i++) {
csOver = overlays[page].at(i);
csOver.hidden = !visible;
}
}
function keyDown(e) {
if (e.keyCode == 13) { // enter: toggle landscape/portrait
// toggleView();
} else if (e.keyCode == 9) { // tab: toggle dark/bright
dark = !dark;
applyTheme();
} else if (e.keyCode == 32) { // space: clear canvas
resetPage();
} else if (e.keyCode == 75) { // blac*k*
color = "black";
} else if (e.keyCode == 87) { // *w*hite
color = "white";
} else if (e.keyCode == 82) { // *r*ed
color = "red";
} else if (e.keyCode == 71) { // *g*reen
color = "green";
} else if (e.keyCode == 66) { // *b*lue
color = "blue";
} else if (e.keyCode == 89) { // *y*ellow
color = "yellow";
} else if (e.keyCode == 67) { // *c*yan
color = "cyan";
} else if (e.keyCode == 77) { // *m*agenta
color = "magenta";
} else if (e.keyCode == 37) { // left
setOverlay(false);
index = index - 1;
} else if (e.keyCode == 39) { // right
index = Math.min(index + 1, -1);
setOverlay(true);
} else if (e.keyCode == 33) { // page-up
setPage(false);
page = Math.max(page - 1, 0);
setPage(true);
} else if (e.keyCode == 34) { // page-down
setPage(false);
if (page + 1 == overlays.length) {
addPage();
} else {
page = page + 1;
setPage(true);
}
}
e.preventDefault();
}
function setup() {
var SCREEN = Object.freeze({width: 1872*11.2, height: 1404*11.2, pressure: 4095});
let scaleX = csDraw.width / SCREEN.width;
let scaleY = csDraw.height / SCREEN.height;
let scaleZ = 1 / SCREEN.pressure;
let websocket = new WebSocket("ws://" + location.host + "/pen");
websocket.onmessage = function(event) {
// let data = JSON.parse(event.data);
let data = event.data.split(" ");
// if ("pen" in data) {
if (data[0] == "pen") {
// data = data["pen"]; // x, y, pressure
data = [parseInt(data[1]), parseInt(data[2]), parseInt(data[3])];
if (landscape) {
pen = Object.freeze({x: data[0]*scaleX, y: data[1]*scaleY, z: data[2]*scaleZ});
} else {
pen = Object.freeze({x: (data[1])*scaleY, y: (SCREEN.width-data[0])*scaleX, z: data[2]*scaleZ});
}
onPenMove(pen, false);
paint(pen);
console.log(data[0], data[1], data[2]);
} else
// if ("cur" in data) {
if (data[0] == "cur") {
// data = data["cur"]; // x, y
data = [parseInt(data[1]), parseInt(data[2])];
if (landscape) {
pen = Object.freeze({x: data[0]*scaleX, y: data[1]*scaleY, z: 0});
} else {
pen = Object.freeze({x: (data[1])*scaleY, y: (SCREEN.width-data[0])*scaleX, z: 0});
}
onPenMove(pen, true);
console.log(data[0], data[1]);
} else
// if ("act" in data) {
if (data[0] == "act") {
// data = data['act'];
data = data[1];
if (data == "undo") {
setOverlay(false);
index = index - 1;
} else
if (data == "redo") {
index = Math.min(index + 1, -1);
setOverlay(true);
} else
if (data == "new") {
setPage(false);
addPage();
}
}
}
addPage();
applyTheme();
if (!landscape) {
[csDraw.width, csDraw.height] = [csMain.height, csMain.width];
[csMain.width, csMain.height] = [csDraw.width, csDraw.height];
}
document.addEventListener('keydown', keyDown);
}
setup();
function download() {
var disp = document.createElement('canvas');
[disp.width, disp.height] = [csDraw.width, csDraw.height];
var ctx = disp.getContext('2d');
var i = overlays[page].length;
while (i--) {
if (!overlays[page][i].hidden) {
ctx.drawImage(overlays[page][i], 0, 0);
}
}
var img = disp.toDataURL();
var link = document.createElement('a');
link.download = 'remarkable.png';
link.href = img;
link.click();
}
function onclick(e) {
download();
}
window.addEventListener('click', onclick);
</script>
</body>
</html>