-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathbump.js
75 lines (70 loc) · 1.71 KB
/
bump.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
import pointRadial from "../pointRadial.js";
class Bump {
constructor(context, x) {
this._context = context;
this._x = x;
}
areaStart() {
this._line = 0;
}
areaEnd() {
this._line = NaN;
}
lineStart() {
this._point = 0;
}
lineEnd() {
if (this._line || (this._line !== 0 && this._point === 1)) this._context.closePath();
this._line = 1 - this._line;
}
point(x, y) {
x = +x, y = +y;
switch (this._point) {
case 0: {
this._point = 1;
if (this._line) this._context.lineTo(x, y);
else this._context.moveTo(x, y);
break;
}
case 1: this._point = 2; // falls through
default: {
if (this._x) this._context.bezierCurveTo(this._x0 = (this._x0 + x) / 2, this._y0, this._x0, y, x, y);
else this._context.bezierCurveTo(this._x0, this._y0 = (this._y0 + y) / 2, x, this._y0, x, y);
break;
}
}
this._x0 = x, this._y0 = y;
}
}
class BumpRadial {
constructor(context) {
this._context = context;
}
lineStart() {
this._point = 0;
}
lineEnd() {}
point(x, y) {
x = +x, y = +y;
if (this._point === 0) {
this._point = 1;
} else {
const p0 = pointRadial(this._x0, this._y0);
const p1 = pointRadial(this._x0, this._y0 = (this._y0 + y) / 2);
const p2 = pointRadial(x, this._y0);
const p3 = pointRadial(x, y);
this._context.moveTo(...p0);
this._context.bezierCurveTo(...p1, ...p2, ...p3);
}
this._x0 = x, this._y0 = y;
}
}
export function bumpX(context) {
return new Bump(context, true);
}
export function bumpY(context) {
return new Bump(context, false);
}
export function bumpRadial(context) {
return new BumpRadial(context);
}