-
Notifications
You must be signed in to change notification settings - Fork 0
/
scamper.js
391 lines (331 loc) · 9.57 KB
/
scamper.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
/*
Scamper - Even points along a cubic Bezier curve
Dan Amelang
Evelyn Eastmond
Viewpoints Research Institute
(c) 2014-2015
*/
"use strict"; // for strict mode
// ------------------------------------------
// Scamper
//
// Constructor for Scamper instances.
//
var Scamper = function(opts) {
//////////////////////////////////////////////
// PUBLIC
//////////////////////////////////////////////
this.beginStroke = function(x, y, p) {
curRawStroke = [];
curRawSampledStroke = [];
curFilteredStroke = [];
stepOffset = stepInterval;
lastControlPoint = null;
this.extendStroke(x, y, p);
}
this.extendStroke = function(x, y, p) {
var stepPoints = [];
var point = new Point(x,y,p);
pointCounter++;
//
// Raw
//
/*if(curRawStroke.last().equals(point)) {
return; // ignore dupes TODO: ??
}*/
curRawStroke.push(point);
//
// Sampled and filtered
//
if (pointCounter % sample === 0) {
// Push sampled point
/*if(curRawSampledStroke.last().equals(point)) {
return; // ignore dupes TODO: ??
}*/
curRawSampledStroke.push(point);
// Filter next-to-last input point
var len = curRawSampledStroke.length;
if(len >= 3) {
var fpoint = calculateFilteredPoint(
curRawSampledStroke[len - 3],
curRawSampledStroke[len - 2],
curRawSampledStroke[len - 1]
);
//if(fpoint) {
// Push sampled, filtered, point
curFilteredStroke.push(fpoint);
//}
}
// TODO:
// - Handle single point and double point strokes
// 3 points needed for a look-ahead bezier
var len = curFilteredStroke.length;
if(len >= 3) {
stepPoints = createBezier(
curFilteredStroke[len - 3],
curFilteredStroke[len - 2],
curFilteredStroke[len - 1]
);
}
}
return stepPoints;
}
this.endStroke = function(x, y, p) {
// make sure we create step points until the end
var stepPoints = [];
var point = new Point(x,y,p);
//
// Raw
//
curRawStroke.push(point);
//
// Sampled and filtered
//
// Push sampled point
curRawSampledStroke.push(point);
// Filter next-to-last input point
var len = curRawSampledStroke.length;
if(len >= 3) {
console.log('curRawSampleStroke.length: ' + curRawSampledStroke.length);
var fpoint = calculateFilteredPoint(
curRawSampledStroke[len - 3],
curRawSampledStroke[len - 2],
curRawSampledStroke[len - 1]
);
curFilteredStroke.push(fpoint);
} else {
console.log(curRawSampledStroke.length);
}
// TODO:
// - Handle single point and double point strokes
// 3 points needed for a look-ahead bezier
var len = curFilteredStroke.length;
if(len >= 3) {
console.log('curFilteredStroke.length: ' + curFilteredStroke.length);
stepPoints = createBezier(
curFilteredStroke[len - 3],
curFilteredStroke[len - 2],
curFilteredStroke[len - 1]
);
} else {
console.log(curFilteredStroke.length);
}
//
// Raw
//
curRawStroke.push(point);
//
// Sampled and filtered
//
// Push sampled point
curRawSampledStroke.push(point);
// Filter next-to-last input point
var len = curRawSampledStroke.length;
if(len >= 3) {
console.log('curRawSampleStroke.length: ' + curRawSampledStroke.length);
var fpoint = calculateFilteredPoint(
curRawSampledStroke[len - 3],
curRawSampledStroke[len - 2],
curRawSampledStroke[len - 1]
);
curFilteredStroke.push(fpoint);
} else {
console.log(curRawSampledStroke.length);
}
// TODO:
// - Handle single point and double point strokes
// 3 points needed for a look-ahead bezier
var len = curFilteredStroke.length;
if(len >= 3) {
console.log('curFilteredStroke.length: ' + curFilteredStroke.length);
var pts = createBezier(
curFilteredStroke[len - 3],
curFilteredStroke[len - 2],
curFilteredStroke[len - 1]
);
for(var i = 0; i < pts.length; i++) {
stepPoints.push(pts[i]);
}
} else {
console.log(curFilteredStroke.length);
}
return stepPoints;
}
//////////////////////////////////////////////
// PRIVATE
//////////////////////////////////////////////
// Args
if(!opts) {
opts = {
step: 5,
sample: 1
};
}
// State
var curRawStroke = [];
var curRawSampledStroke = [];
var curFilteredStroke = [];
var lastControlPoint = null;
var filterWeight = 0.5;
var filterWeightInverse = 1 - filterWeight;
var stepOffset = 0.0;
var stepInterval = opts.step ? opts.step : 5;
var pointCounter = 0;
var sample = opts.sample ? opts.sample : 1;
// ------------------------------------------
// createBezier
//
// Create a look-ahead cubic bezier based on
// 3 input points and call point ha ndler
// function on evenly spaced step points
// along that curve.
//
function createBezier(pt0, pt1, pt2) {
// Endpoints and control points
var p0 = pt0;
var p1 = 0.0;
var p2 = 0.0;
var p3 = pt1;
// Value access
var p0_x = p0.x;
var p0_y = p0.y;
var p0_p = p0.p;
var p3_x = p3.x;
var p3_y = p3.y;
var p3_p = p3.p;
// Calculate p1
if(!lastControlPoint) {
p1 = new Point(
p0_x + (p3_x - p0_x) * 0.33,
p0_y + (p3_y - p0_y) * 0.33,
p0_p + (p3_p - p0_p) * 0.33
);
} else {
p1 = lastControlPoint.getMirroredPt(p0);
}
// Calculate p2
if (pt2) {
p2 = new Point(
//p3_x - (((p3_x - p0_x) + (pt2.x - p3_x)) / 6),
//p3_y - (((p3_y - p0_y) + (pt2.y - p3_y)) / 6),
//p3_p - (((p3_p - p0_p) + (pt2.p - p3_p)) / 6)
p3_x - (((p3_x - p0_x) + (pt2.x - p3_x)) * 0.1666),
p3_y - (((p3_y - p0_y) + (pt2.y - p3_y)) * 0.1666),
p3_p - (((p3_p - p0_p) + (pt2.p - p3_p)) * 0.1666)
);
} else {
p2 = new Point(
p0_x + (p3_x - p0_x) * 0.66,
p0_y + (p3_y - p0_y) * 0.66,
p0_p + (p3_p - p0_p) * 0.66
);
}
// Set last control point
lastControlPoint = p2;
// Calculate even steps along curve
var stepPoints = calculateStepPoints(p0, p1, p2, p3);
// Return points
return stepPoints;
}
// ------------------------------------------
// calculateStepPoints
//
// Calculates even steps along a bezier with
// control points (p0, p1, p2, p3).
//
function calculateStepPoints(p0, p1, p2, p3) {
var stepPoints = [];
var i = stepInterval;
// Value access
var p0_x = p0.x;
var p0_y = p0.y;
var p0_p = p0.p;
// Algebraic conveniences, not geometric
var A_x = p3.x - 3 * p2.x + 3 * p1.x - p0_x;
var A_y = p3.y - 3 * p2.y + 3 * p1.y - p0_y;
var A_p = p3.p - 3 * p2.p + 3 * p1.p - p0_p;
var B_x = 3 * p2.x - 6 * p1.x + 3 * p0_x;
var B_y = 3 * p2.y - 6 * p1.y + 3 * p0_y;
var B_p = 3 * p2.p - 6 * p1.p + 3 * p0_p;
var C_x = 3 * p1.x - 3 * p0_x;
var C_y = 3 * p1.y - 3 * p0_y;
var C_p = 3 * p1.p - 3 * p0_p;
var t = (i - stepOffset) / Math.sqrt(C_x * C_x + C_y * C_y);
while (t <= 1.0) {
// Point
var step_x = t * (t * (t * A_x + B_x) + C_x) + p0_x;
var step_y = t * (t * (t * A_y + B_y) + C_y) + p0_y;
var step_p = t * (t * (t * A_p + B_p) + C_p) + p0_p;
stepPoints.push(new Point(
step_x,
step_y,
step_p
));
// Step distance until next one
var s_x = t * (t * 3 * A_x + 2 * B_x) + C_x; // dx/dt
var s_y = t * (t * 3 * A_y + 2 * B_y) + C_y; // dy/dt
var s = Math.sqrt(s_x * s_x + s_y * s_y); // s = derivative in 2D space
var dt = i / s; // i = interval / derivative in 2D
t = t + dt;
}
// TODO: Maybe use a better approximation for distance along the bezier?
if (stepPoints.length == 0) // We didn't step at all along this Bezier
stepOffset = stepOffset + p0.getDistance(p3);
else
stepOffset = stepPoints.last().getDistance(p3);
return stepPoints;
}
// ------------------------------------------
// calculateFilteredPoint
//
// Returns a filtered, sanitized version of
// point p2 between points p1 and p3.
//
function calculateFilteredPoint(p1, p2, p3) {
//if (p1 == null || p2 == null || p3 == null)
// return null; // Not enough points yet to filter
var m = p1.getMidPt(p3);
return new Point(
filterWeight * p2.x + filterWeightInverse * m.x,
filterWeight * p2.y + filterWeightInverse * m.y,
filterWeight * p2.p + filterWeightInverse * m.p
);
}
// ------------------------------------------
// POINT
//
function Point(x, y, p) {
this.x = x; // x-coordinate
this.y = y; // y-coordinate
this.p = p; // pressure
}
Point.prototype.equals = function(pt) {
return pt && this.x === pt.x && this.y === pt.y && this.p === pt.p;
}
Point.prototype.getMidPt = function(pt) {
return new Point(
(this.x + pt.x) / 2,
(this.y + pt.y) / 2,
(this.p + pt.p) / 2
);
}
Point.prototype.getMirroredPt = function(pt) {
return new Point(
this.x + 2 * (pt.x - this.x),
this.y + 2 * (pt.y - this.y),
this.p + 2 * (pt.p - this.p)
);
}
Point.prototype.getDistance = function(pt) {
// TODO: use Manhattan distance?
var dx = this.x - pt.x;
var dy = this.y - pt.y;
return Math.sqrt(dx * dx + dy * dy);
}
// ------------------------------------------
// UTILS
//
Array.prototype.last = function(){
return this[this.length - 1];
}
} // Scamper