-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.html
508 lines (407 loc) · 17 KB
/
index.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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<html>
<head>
<style>
canvas {
border: 1px solid black;
}
/*button {*/
/*padding: 4px;*/
/*}*/
</style>
</head>
<body>
<h2> Bezier Curve </h2>
<canvas id="Editor" width="600" height="400"></canvas>
<p>
Shift + Click: Add Point
</p>
<p>
<input type="checkbox" id="smooth" checked />
<label for="smooth"> Keep Smooth </label>
</p>
<a href="https://github.com/jareguo/curve-editor"> https://github.com/jareguo/curve-editor </a>
<!--TYPES-->
<script>
function v2 (x, y) {
return {
x: x || 0,
y: y || 0
};
}
v2.add = function (lhs, rhs) {
return v2(lhs.x + rhs.x, lhs.y + rhs.y);
};
v2.sub = function (lhs, rhs) {
return v2(lhs.x - rhs.x, lhs.y - rhs.y);
};
v2.mul = function (v, scaling) {
return v2(v.x * scaling, v.y * scaling);
};
v2.div = function (v, scaling) {
return v2(v.x / scaling, v.y / scaling);
};
v2.lerp = function (from, to, ratio) {
return v2(from.x + (to.x - from.x) * ratio, from.y + (to.y - from.y) * ratio);
};
v2.sqrDistance = function (lhs, rhs) {
var dx = lhs.x - rhs.x;
var dy = lhs.y - rhs.y;
return dx * dx + dy * dy;
};
v2.distance = function (lhs, rhs) {
return Math.sqrt(v2.sqrDistance(lhs, rhs));
};
v2.dir = function (lhs, rhs) {
var len = v2.distance(v2, {x: 0, y: 0});
return v2(v2.x / len, v2.y / len);
};
function Curve (points) {
this.points = points || [];
this.beziers = [];
this.ratios = [];
this.progresses = [];
this.length = 0;
this.computeBeziers();
}
Curve.prototype.computeBeziers = function () {
this.beziers.length = 0;
this.ratios.length = 0;
this.progresses.length = 0;
this.length = 0;
for (var i = 1; i < this.points.length; i++) {
var startPoint = this.points[i - 1];
var endPoint = this.points[i];
var bezier = new Bezier();
bezier.start = startPoint.pos;
bezier.startCtrlPoint = startPoint.out;
bezier.end = endPoint.pos;
bezier.endCtrlPoint = endPoint.in;
this.beziers.push(bezier);
this.length += bezier.getLength();
}
var current = 0;
for (var i = 0; i < this.beziers.length; i++) {
var bezier = this.beziers[i];
this.ratios[i] = bezier.getLength() / this.length;
this.progresses[i] = current = current + this.ratios[i];
}
return this.beziers;
};
function Bezier () {
this.start = v2();
this.end = v2();
this.startCtrlPoint = v2(); // cp0, cp1
this.endCtrlPoint = v2(); // cp2, cp3
}
// Get point at relative position in curve according to arc length
// - u [0 .. 1]
Bezier.prototype.getPointAt = function ( u ) {
var t = this.getUtoTmapping( u );
return this.getPoint( t );
};
function bezierAt (C1, C2, C3, C4, t) {
var t1 = 1 - t;
return C1 * t1 * t1 * t1 +
C2 * 3 * t1 * t1 * t +
C3 * 3 * t1 * t * t +
C4 * t * t * t;
}
// Get point at time t
// - t [0 .. 1]
Bezier.prototype.getPoint = function ( t ) {
var x = bezierAt(this.start.x, this.startCtrlPoint.x, this.endCtrlPoint.x, this.end.x, t);
var y = bezierAt(this.start.y, this.startCtrlPoint.y, this.endCtrlPoint.y, this.end.y, t);
return new v2(x, y);
};
// Get total curve arc length
Bezier.prototype.getLength = function () {
var lengths = this.getLengths();
return lengths[ lengths.length - 1 ];
};
// Get list of cumulative segment lengths
Bezier.prototype.getLengths = function ( divisions ) {
if ( ! divisions ) divisions = (this.__arcLengthDivisions) ? (this.__arcLengthDivisions): 200;
if ( this.cacheArcLengths
&& ( this.cacheArcLengths.length === divisions + 1 )) {
//console.log( "cached", this.cacheArcLengths );
return this.cacheArcLengths;
}
var cache = [];
var current, last = this.getPoint( 0 );
var p, sum = 0;
cache.push( 0 );
for ( p = 1; p <= divisions; p ++ ) {
current = this.getPoint ( p / divisions );
sum += v2.distance(current, last);
cache.push( sum );
last = current;
}
this.cacheArcLengths = cache;
return cache; // { sums: cache, sum:sum }; Sum is in the last element.
};
Bezier.prototype.getUtoTmapping = function ( u, distance ) {
var arcLengths = this.getLengths();
var i = 0, il = arcLengths.length;
var targetArcLength; // The targeted u distance value to get
if ( distance ) {
targetArcLength = distance;
} else {
targetArcLength = u * arcLengths[ il - 1 ];
}
//var time = Date.now();
// binary search for the index with largest value smaller than target u distance
var low = 0, high = il - 1, comparison;
while ( low <= high ) {
i = Math.floor( low + ( high - low ) / 2 ); // less likely to overflow, though probably not issue here, JS doesn't really have integers, all numbers are floats
comparison = arcLengths[ i ] - targetArcLength;
if ( comparison < 0 ) {
low = i + 1;
continue;
} else if ( comparison > 0 ) {
high = i - 1;
continue;
} else {
high = i;
break;
// DONE
}
}
i = high;
//console.log('b' , i, low, high, Date.now()- time);
if ( arcLengths[ i ] == targetArcLength ) {
var t = i / ( il - 1 );
return t;
}
// we could get finer grain at lengths, or use simple interpolatation between two points
var lengthBefore = arcLengths[ i ];
var lengthAfter = arcLengths[ i + 1 ];
var segmentLength = lengthAfter - lengthBefore;
// determine where we are between the 'before' and 'after' points
var segmentFraction = ( targetArcLength - lengthBefore ) / segmentLength;
// add that fractional amount to t
var t = ( i + segmentFraction ) / ( il -1 );
return t;
};
</script>
<!--RENDERING-->
<script>
function drawPoint (pos, strokeColor, fillColor, radius) {
var radius = radius || 5;
ctx.lineWidth = 1;
ctx.strokeStyle = strokeColor;
ctx.fillStyle = fillColor;
ctx.beginPath();
ctx.arc(pos.x, pos.y, radius, 0, Math.PI * 2, true);
ctx.fill();
ctx.stroke();
}
function drawLine (from, to) {
ctx.lineWidth = 1;
ctx.strokeStyle = "#000";
ctx.beginPath();
ctx.moveTo(from.x, from.y);
ctx.lineTo(to.x, to.y);
ctx.stroke();
}
function getBezierPos (bezier, progress) {
var p01 = v2.lerp(bezier.start, bezier.startCtrlPoint, progress);
var p12 = v2.lerp(bezier.startCtrlPoint, bezier.endCtrlPoint, progress);
var p23 = v2.lerp(bezier.endCtrlPoint, bezier.end, progress);
var p012 = v2.lerp(p01, p12, progress);
var p123 = v2.lerp(p12, p23, progress);
var p0123 = v2.lerp(p012, p123, progress);
return p0123;
}
function drawCanvas (Speed) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
var CurvePointStrokeColor = "#000";
var ControlPointStrokeColor = "#090";
var PointFillColor = "rgba(200,200,200,0.5)";
var CurveColor = "#44F";
// curve points
var i;
for (i = 0; i < curve.points.length; i++) {
var point = curve.points[i];
drawPoint(point.pos, CurvePointStrokeColor, PointFillColor);
}
for (i = 0; i < curve.beziers.length; i++) {
var bezier = curve.beziers[i];
// control points
drawPoint(bezier.startCtrlPoint, ControlPointStrokeColor, PointFillColor);
drawPoint(bezier.endCtrlPoint, ControlPointStrokeColor, PointFillColor);
drawLine(bezier.start, bezier.startCtrlPoint);
drawLine(bezier.end, bezier.endCtrlPoint);
}
var bezierProgress = 0;
var bezierIndex = 0;
while (bezierProgress < 1) {
bezierProgress += Speed;
if (bezierProgress > curve.progresses[bezierIndex]) {
bezierIndex++;
bezierIndex %= curve.beziers.length;
}
var realProgress = (bezierProgress - (bezierIndex > 0 ? curve.progresses[bezierIndex - 1] : 0) ) / curve.ratios[bezierIndex];
var pos = curve.beziers[bezierIndex].getPointAt(realProgress);
drawPoint(pos, CurveColor, CurveColor, 1);
}
}
</script>
<!--INIT-->
<script>
var InitPoints = [
{
pos: v2(50, 50),
out: v2(200, 150)
},
{
in: v2(170, 260),
pos: v2(250, 330),
out: v2(300, 220)
},
{
in: v2(450, 300),
pos: v2(500, 100)
}
];
var canvas = document.getElementById("Editor");
var ctx = canvas.getContext("2d");
var curve = new Curve(InitPoints);
var smooth = document.getElementById("smooth");
drawCanvas();
</script>
<!--CONTROL POSITION-->
<script>
var SqrHitRadius = Math.pow(9, 2);
var draggingPoint = -1;
var draggingType = '';
canvas.onmousedown = dragStart;
canvas.onmousemove = dragging;
canvas.addEventListener('mouseup', dragEnd);
function getMousePos (event) {
return v2(event.pageX - canvas.offsetLeft, event.pageY - canvas.offsetTop);
}
function dragStart (event) {
var mousePos = getMousePos(event);
for (var i = 0; i < curve.points.length; i++) {
var point = curve.points[i];
//points.push(bezier.start, bezier.startCtrlPoint, bezier.endCtrlPoint, bezier.end);
var dis = v2.sqrDistance(point.pos, mousePos);
if (dis < SqrHitRadius) {
draggingPoint = i;
draggingType = 'pos';
return;
}
if (point.in) {
dis = v2.sqrDistance(point.in, mousePos);
if (dis < SqrHitRadius) {
draggingPoint = i;
draggingType = 'in';
return;
}
}
if (point.out) {
dis = v2.sqrDistance(point.out, mousePos);
if (dis < SqrHitRadius) {
draggingPoint = i;
draggingType = 'out';
return;
}
}
}
}
function keepSmooth (point, mousePos, oldPos) {
if (draggingType === 'pos') {
var delta = v2.sub(mousePos, oldPos);
if (point.in) {
point.in = v2.add(point.in, delta);
}
if (point.out) {
point.out = v2.add(point.out, delta);
}
}
else {
var another;
var anotherType;
if (draggingType === 'in') {
anotherType = 'out';
}
else if (draggingType === 'out') {
anotherType = 'in';
}
another = point[anotherType];
if (another) {
var dir = v2.sub(point.pos, mousePos);
var len = v2.distance(mousePos, point.pos);
if (len > 0.01) {
dir = v2.div(dir, len);
var anotherLen = v2.distance(another, point.pos);
dir = v2.mul(dir, anotherLen);
point[anotherType] = v2.add(dir, point.pos);
}
}
}
}
function dragging (event) {
if (draggingType) {
var mousePos = getMousePos(event);
var point = curve.points[draggingPoint];
var oldPos = point[draggingType];
point[draggingType] = mousePos;
if (smooth.checked) {
keepSmooth(point, mousePos, oldPos);
}
curve.computeBeziers();
drawCanvas();
}
}
function dragEnd (event) {
if (draggingType) {
draggingType = '';
draggingPoint = -1;
event.stopImmediatePropagation();
}
}
</script>
<!-- ADD POINT -->
<script>
canvas.addEventListener('mouseup', function (event) {
if (event.shiftKey) {
var points = curve.points;
var prev = points[points.length - 1];
prev.out = v2.add(prev.pos, v2.sub(prev.pos, prev.in));
var newPos = getMousePos(event);
curve.points.push({
pos: newPos,
in: v2.lerp(newPos, prev.out, 0.3)
});
curve.computeBeziers();
drawCanvas();
event.stopImmediatePropagation();
}
});
</script>
<!-- ANIMATION -->
<script>
var Speed = 0.01;
var PointColor = "#F22";
var bezierIndex = 0;
var bezierProgress = 0;
function animate () {
window.requestAnimationFrame(animate);
bezierProgress += Speed;
if (bezierProgress > curve.progresses[bezierIndex]) {
if (bezierProgress > 1) {
bezierProgress -= 1;
}
bezierIndex++;
bezierIndex %= curve.beziers.length;
}
var realProgress = (bezierProgress - (bezierIndex > 0 ? curve.progresses[bezierIndex - 1] : 0) ) / curve.ratios[bezierIndex];
var pos = curve.beziers[bezierIndex].getPointAt(realProgress);
drawCanvas(Speed);
drawPoint(pos, PointColor, PointColor, 6);
}
window.requestAnimationFrame(animate);
</script>
</body>
</html>