/
sketch.js
435 lines (338 loc) 路 10.5 KB
/
sketch.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// prevent touches on mobile to scroll window
var firstMove;
window.addEventListener('touchstart', function(e) {
firstMove = true;
});
window.addEventListener('touchmove', function(e) {
if (firstMove) {
e.preventDefault();
firstMove = false;
}
});
// ensure full screen on resizing
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
// p5 sketch from Folio Item
/**
* p5.js collaborative drawing client.
*
* Requirements - this script requires the following libraries:
* - Mousetrap
* - FileSaver.js
* - ReconnectingWebsocket
* - p5.js
* - github.com/kelektiv/node-uuid
*/
/**
* Websocket server configuration.
*/
const local = true;
let host = 'smartgeometry.herokuapp.com';
let port = '80';
if (local) {
host = '127.0.0.1';
port = '8080';
}
/**
* Current (cumulative) drawing strokes from both the user
* and other clients connected to the drawing application.
*/
let strokes = [];
/**
* Current model being used by SketchRNN.
*/
let model_name = 'bird';
/**
* An stream of incoming strokes to draw, sketched from
* other clients.
*/
let incomingStrokes = [];
/**
* The stroke that is currently being drawn by the user.
*/
let currentStroke = [];
//let drawingMode = 'POLYLINE';
let drawingMode = 'SKETCH';
/**
* Minimum distance (in pixels) between points in a sketched polyline.
*/
let tolerance = 15;
/**
* Sketch-rnn drawing variables.
*/
var model_x, model_y;
var model_prev_pen = [1, 0, 0];
var last_x, last_y;
/**
* The setup() function serves to initialize your p5.js sketch.
* Things like setting the canvas size, frame rate, or other
* global settings.
*/
function setup() {
if (location.href.split('#s=').length > 1) {
strokes = JSON.parse(unescape(location.href.split('#s=')[1]));
}
// make sure we enforce some minimum size of our demo
screen_width = Math.max(window.innerWidth, 480);
screen_height = Math.max(window.innerHeight, 320);
// start drawing from somewhere in middle of the canvas
start_x = screen_width / 2.0;
start_y = screen_height / 3.0;
// make the canvas and clear the screens
createCanvas(screen_width, screen_height);
frameRate(60);
strokeWeight(3);
stroke(0);
noFill()
}
let baseR = Math.random();
let baseG = Math.random();
let baseB = Math.random();
function draw() {
background(255);
var origin_x = 0; //windowWidth * 0.5;
var origin_y = 0; //windowHeight * 0.5;
model_x = origin_x;
model_y = origin_y;
drawAbsoluteStrokes(strokes);
}
function drawAbsoluteStrokes() {
alpha = 0;
var prev_location;
var model_prev_pen = [1, 0, 0];
for (var i = 0; i < strokes.length; i++) {
var location = strokes[i];
//alpha += 2.0 / (strokes.size() / 5);
//color c = color(0, 255 * alpha / 2, 255 - 0.5 * alpha * 255, 255);
//stroke(c);
//fill(c);
let x = location[0];
let y = location[1];
let model_pen_down = location[2];
let model_pen_up = location[3];
let model_pen_end = location[4];
// Not needed since we are iteration on points (not on the model)
// if (model_prev_pen[2] == 1) {
// break;
// }
if (prev_location != undefined && model_prev_pen[0] == 1) {
strokeCap(ROUND);
line(prev_location[0], prev_location[1], x, y);
}
model_prev_pen[0] = model_pen_down;
model_prev_pen[1] = model_pen_up;
model_prev_pen[2] = model_pen_end;
prev_location = location;
}
}
// Draw relative strokes (sketch-rnn like)
function drawRelativeStrokes() {
alpha = 0;
for (var i = 0; i < strokes.length; i++) {
var location = strokes[i];
//alpha += 2.0 / (strokes.size() / 5);
//color c = color(0, 255 * alpha / 2, 255 - 0.5 * alpha * 255, 255);
//stroke(c);
//fill(c);
let model_dx = location[0];
let model_dy = location[1];
let model_pen_down = location[2];
let model_pen_up = location[3];
let model_pen_end = location[4];
// Not needed since we are iteration on points (not on the model)
//if (model_prev_pen[2] == 1) {
// break;
//}
if (model_prev_pen[0] == 1) {
line(model_x, model_y, model_x + model_dx, model_y + model_dy);
}
model_x += model_dx;
model_y += model_dy;
model_prev_pen[0] = model_pen_down;
model_prev_pen[1] = model_pen_up;
model_prev_pen[2] = model_pen_end;
}
}
function mouseDragged() {
if (drawingMode == 'SKETCH') {
tryAddPoint(mouseX, mouseY, 1, 0, 0)
}
}
function mousePressed() {
if (mouseButton == RIGHT) {
strokes = []
} else {
if (drawingMode == 'POLYLINE') {
tryAddPoint(mouseX, mouseY, 1, 0, 0)
} else if (drawingMode == 'SKETCH') {
addPoint(mouseX, mouseY, 1, 0, 0)
}
}
}
let instantPrediction = true;
let predictionInputStrokes = [];
function mouseReleased() {
addPoint(mouseX, mouseY, 0, 1, 0);
currentStroke = [];
if (instantPrediction) {
requestPrediction(predictionInputStrokes);
predictionInputStrokes = [];
}
}
// Math and graphics helpers
function tryAddPoint(x, y, p1, p2, p3) {
let lastPoint = strokes[strokes.length - 1]
const d = distance(lastPoint[0], lastPoint[1], x, y)
if (d > tolerance) {
addPoint(x, y, p1, p2, p3);
}
}
function addPoint(x, y, p1, p2, p3) {
if (y < windowHeight * 0.88) {
var location = [x, y, p1, p2, p3];
strokes.push(location);
currentStroke.push(location);
predictionInputStrokes.push(location);
}
}
function distance(x0, y0, x1, y1) {
const dx = x1 - x0;
const dy = y1 - y0;
return Math.sqrt(dx * dx + dy * dy);
}
function absolute2relative(strokes) {
var rStrokes = [];
let prev_x, prev_y;
for (var i in strokes) {
let x = strokes[i][0];
let y = strokes[i][1];
let p1 = strokes[i][2];
let p2 = strokes[i][3];
let p3 = strokes[i][4];
if (i > 0) {
rStrokes.push([x - prev_x, y - prev_y, p1, p2, p3]);
}
prev_x = x;
prev_y = y;
}
return rStrokes;
}
// Draw SVG graphic of "absolute" strokes
function getSVG(strokes) {
var s = '<?xml version="1.0" encoding="utf-8"?>';
s += '<!-- Generator: Nono.ma p5.js drawing client -->';
s += '<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" ' +
'xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" ' +
'viewBox="0 0 ' + windowWidth + ' ' + windowHeight + '" ' +
'style="enable-background:new 0 0 ' + windowWidth + ' ' + windowHeight + ';" ' +
'xml:space="preserve">';
s += '<style type="text/css">.o-line-0{fill:none;stroke:#000000;stroke-width:3;}</style>';
var prev_location;
var model_prev_pen = [1, 0, 0];
for (var i = 0; i < strokes.length; i++) {
if (i == 0 || model_prev_pen[1] == 1) {
s += '<polyline class="o-line-0" points="';
}
var location = strokes[i];
let x = location[0];
let y = location[1];
let model_pen_down = location[2];
let model_pen_up = location[3];
let model_pen_end = location[4];
// Not needed since we are iteration on points (not on the model)
// predictions would kill the drawing loop
//if (model_prev_pen[2] == 1) {
// break;
//}
if (i > 0) {
s += ' ';
}
s += x + ',' + y;
if (model_pen_up == 1 || i == strokes.length - 1) {
// TODO: set this globally
s += '" style="fill:none;stroke:black;stroke-width:3"/>';
}
model_prev_pen[0] = model_pen_down;
model_prev_pen[1] = model_pen_up;
model_prev_pen[2] = model_pen_end;
prev_location = location;
}
s += '</svg>';
return s;
}
/**
* Keyboard shortcuts with Mousetrap
* https://github.com/ccampbell/mousetrap
*/
Mousetrap.bind(['command+x', 'command+o', 'ctrl+x'], function(e) {
console.log('clear');
strokes = [];
return false;
});
Mousetrap.bind(['command+s', 'ctrl+s'], function(e) {
console.log('save');
saveSVG(strokes);
return false;
});
Mousetrap.bind('command+i', function(e) {
console.log('predict');
requestPrediction(strokes);
return false;
});
var requestPrediction = function(strokes) {
let url = 'http://' + host + ':' + port + '/simple_predict_absolute';
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Parse strokes from string to JSON
let newStrokes = JSON.parse(this.responseText);
// Fix first stroke to not draw connecting line
newStrokes[0] = [newStrokes[0][1], newStrokes[0][1], 0, 1, 0];
// Queue all new strokes for animation
for (var i in newStrokes) {
var location = newStrokes[i];
incomingStrokes.push(location);
}
// console.log(this);
}
};
xhttp.open("POST", url, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("model=" + model_name + "&strokes=" + JSON.stringify(strokes));
}
/**
* FileSaver helpers
*/
function saveSVG(strokes) {
var svg = getSVG(strokes);
var blob = new Blob([svg], {
type: "image/svg+xml"
});
saveAs(blob, "drawing.svg");
}
function saveText(s) {
var blob = new Blob([s], {
type: "text/plain;charset=utf-8"
});
saveAs(blob, "draw.txt");
}
// var handleDistributeStroke = function(m) {
// var newStrokes = m.params.strokes;
// console.log(newStrokes);
// for (var i in newStrokes) {
// var location = newStrokes[i];
// incomingStrokes.push(location);
// }
// };
let incomingStrokesProcessor;
let processIncomingStrokes = function() {
incomingStrokesProcessor = setInterval(function() {
var i = 0;
while (incomingStrokes.length && i < 2) {
strokes.push(incomingStrokes.shift());
i++;
}
}, 25);
};
processIncomingStrokes();