Lomiri
cubic-bezier.js
1 /*
2  * Copyright (C) 2016 Canonical Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 //====================================\\
18 // 13thParallel.org BeziĆ©r Curve Code \\
19 // by Dan Pupius (www.pupius.net) \\
20 //====================================\\
21 
22 
23 var coord = function (x,y) {
24  if(!x) var x=0;
25  if(!y) var y=0;
26  return {x: x, y: y};
27 }
28 
29 function B4(t) { return t*t*t }
30 function B3(t) { return 3*t*t*(1-t) }
31 function B2(t) { return 3*t*(1-t)*(1-t) }
32 function B1(t) { return (1-t)*(1-t)*(1-t) }
33 
34 var getBezier = function(percent,C1,C2,C3,C4) {
35  var pos = new coord();
36  pos.x = C1.x*B1(percent) + C2.x*B2(percent) + C3.x*B3(percent) + C4.x*B4(percent);
37  pos.y = C1.y*B1(percent) + C2.y*B2(percent) + C3.y*B3(percent) + C4.y*B4(percent);
38  return pos;
39 }