Skip to content

Commit 051e7da

Browse files
committed
Added “bezier” transition to Fibonacci
1 parent 996f63d commit 051e7da

File tree

1 file changed

+63
-24
lines changed

1 file changed

+63
-24
lines changed

src/js/patterns/Fibonacci.js

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import PathHelper from '@markroland/path-helper'
2+
13
/**
24
* Fibonacci
35
*/
@@ -27,17 +29,16 @@ class Fibonacci {
2729
"displayValue": true
2830
}
2931
},
30-
"rtc": {
31-
"name": "Return to Center",
32+
"transition": {
33+
"name": "Transition",
3234
"value": null,
3335
"input": {
34-
"type": "createCheckbox",
35-
"attributes" : [{
36-
"type" : "checkbox",
37-
"checked" : null
38-
}],
39-
"params": [0, 1, 0],
40-
"displayValue": false
36+
"type": "createSelect",
37+
"options": {
38+
"direct": "Direct",
39+
"center": "Center",
40+
"bezier": "Bezier Curve"
41+
}
4142
}
4243
},
4344
"reverse": {
@@ -62,18 +63,15 @@ class Fibonacci {
6263

6364
// Update object
6465
this.config.decay.value = parseFloat(document.querySelector('#pattern-controls > div:nth-child(1) > input').value);
65-
this.config.rtc.value = false;
66-
if (document.querySelector('#pattern-controls > div:nth-child(2) > input[type=checkbox]').checked) {
67-
this.config.rtc.value = true;
68-
}
66+
this.config.transition.value = document.querySelector('#pattern-controls > div:nth-child(2) > select').value;
6967

7068
// Display selected values
7169
document.querySelector('#pattern-controls > div.pattern-control:nth-child(1) > span').innerHTML = this.config.decay.value.toFixed(4);
7270

7371
// Calculate the path
7472
let path = this.calc(
7573
this.config.decay.value,
76-
this.config.rtc.value
74+
this.config.transition.value
7775
);
7876

7977
// Update object
@@ -87,7 +85,7 @@ class Fibonacci {
8785
*
8886
* Type: Radial
8987
**/
90-
calc(radius_shrink_factor, return_to_center)
88+
calc(radius_shrink_factor, transition)
9189
{
9290

9391
const min_x = this.env.table.x.min;
@@ -121,22 +119,63 @@ class Fibonacci {
121119
x = r * Math.cos(theta);
122120
y = r * Math.sin(theta);
123121

124-
// Add point to path
125-
path.push([x,y]);
126-
127-
// Go back to center
128-
if (return_to_center) {
122+
if (transition == "bezier") {
123+
if (path.length) {
124+
path = path.concat(
125+
this.transition(
126+
path[path.length - 1],
127+
[x,y],
128+
)
129+
)
130+
}
131+
} else if (transition == "center") {
129132
path.push([0,0]);
130133
}
131-
}
132134

133-
// End in center
134-
if (!return_to_center) {
135-
path.push([0,0]);
135+
// Add new point to path
136+
path.push([x,y]);
136137
}
137138

139+
// Always end in center
140+
path.push([0,0]);
141+
138142
return path;
139143
}
144+
145+
transition(p1, p2) {
146+
147+
const option = 2;
148+
149+
let transitionPath = [];
150+
151+
const PathHelp = new PathHelper();
152+
153+
if (option == 1) {
154+
155+
const midpoint = PathHelp.midpoint(p1, p2);
156+
157+
const theta = Math.atan2(midpoint[1], midpoint[0]);
158+
const r = Math.sqrt(midpoint[0] ** 2 + midpoint[1] ** 2);
159+
const r_factor = 0.5;
160+
161+
const point = [
162+
r_factor * r * Math.cos(theta),
163+
r_factor * r * Math.sin(theta)
164+
];
165+
166+
transitionPath.push(point);
167+
168+
} else if (option == 2) {
169+
170+
let bezier = PathHelp.quadraticBezierPath(p1, [0, 0], p2, 10);
171+
172+
bezier = bezier.slice(1, -1);
173+
transitionPath = bezier;
174+
175+
}
176+
177+
return transitionPath;
178+
}
140179
}
141180

142181
export default Fibonacci;

0 commit comments

Comments
 (0)