forked from Moddable-OpenSource/moddable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.js
60 lines (58 loc) · 1.66 KB
/
mod.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
import {Outline} from "commodetto/outline";
export default class extends Behavior {
onCreate(shape) {
shape.duration = 4000;
}
onFinished(shape) {
shape.time = 0;
shape.start();
}
onTimeChanged(shape) {
let f = shape.fraction * 4;
let which = Math.floor(f);
if (which == 4) which = 3;
f -= which;
const fillPath = new Outline.CanvasPath;
const strokePath = new Outline.CanvasPath;
switch (which) {
case 0:
f = f * 2 * Math.PI;
fillPath.moveTo(240, 120);
fillPath.lineTo(160, 120);
fillPath.lineTo(160 + (80 * Math.cos(f)), 120 + (80 * Math.sin(f)));
strokePath.arc(160, 120, 80, 0, f);
shape.bubble("onLabel", `CanvasPath arc`);
break;
case 1:
f = f * 2 * Math.PI;
fillPath.moveTo(240, 120);
fillPath.lineTo(160, 120);
fillPath.lineTo(160 + (80 * Math.cos(f)), 120 + (60 * Math.sin(f)));
strokePath.ellipse(160, 120, 80, 60, 0, 0, f);
shape.bubble("onLabel", `CanvasPath ellipse`);
break;
case 2:
const y1 = 120 + 100 * f;
const y2 = 120 - 100 * f;
fillPath.moveTo(40, 120);
fillPath.lineTo(120, y1);
fillPath.lineTo(200, y2);
fillPath.lineTo(280, 120);
strokePath.moveTo(40, 120)
strokePath.bezierCurveTo(120, y1, 200, y2, 280, 120);
shape.bubble("onLabel", `CanvasPath bezierCurveTo`);
break;
case 3:
const y = 120 + 100 * ((2 * f) - 1);
fillPath.moveTo(40, 120);
fillPath.lineTo(160, y);
fillPath.lineTo(280, 120);
strokePath.moveTo(40, 120);
strokePath.quadraticCurveTo(160, y, 280, 120);
shape.bubble("onLabel", `CanvasPath quadraticCurveTo`);
break;
}
shape.fillOutline = Outline.stroke(fillPath, 2);
shape.strokeOutline = Outline.stroke(strokePath, 4);
}
}