Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // typeof but better
- function getType(obj) {
- if (obj === null) return 'null';
- if (obj === undefined) return 'undefined';
- return obj.__proto__.constructor.name;
- }
- class Vector2 {
- constructor(x=0, y=0) {
- this.x = x;
- this.y = y;
- }
- lerp(t, v1, v2) {
- return new Vector2(lerp(t, a.x, b.x), lerp(t, a.y, b.y));
- }
- }
- // lerp functions
- const lerp = (t, a, b) => t*(b-a)+a;
- const slurp = (t, a, b) => new Vector2(lerp(t, a.x, b.x), lerp(t, a.y, b.y));
- function bezier(t, ...pts) {
- let lerpFunc = lerp;
- if (getType(pts[0]) == 'Vector2') {
- lerpFunc = slurp;
- }
- if (pts.length == 2) {
- return lerpFunc(t, pts[0], pts[1]);
- }
- let bez1 = [...pts];
- let bez2 = [...pts];
- bez1.pop();
- bez2.shift();
- return lerpFunc(t, bezier(t, ...bez1), bezier(t, ...bez2));
- }
- console.log(bezier(0.5, 1, 0, 1, 0));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement