Skip to content

Commit adaa589

Browse files
committed
Grid working properly with static value and range
1 parent 7222a53 commit adaa589

1 file changed

Lines changed: 77 additions & 44 deletions

File tree

src/tweens/builders/StaggerBuilder.js

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,10 @@ var StaggerBuilder = function (value, options)
2626
var start = GetValue(options, 'start', 0);
2727
var ease = GetValue(options, 'ease', null);
2828
var grid = GetValue(options, 'grid', null);
29+
var axis = GetValue(options, 'axis', null);
2930

3031
var from = GetValue(options, 'from', 0);
3132

32-
// delay: this.tweens.stagger(100) // increase delay by 100ms for each elements.
33-
// delay: this.tweens.stagger(100, {start: 500}) // delay starts at 500ms then increase by 100ms for each elements.
34-
// delay: this.tweens.stagger(100, {from: 'center'}) (first, default, last, center, index)
35-
// delay: this.tweens.stagger([ 500, 1000 ]) // distributes evenly values between two numbers.
36-
3733
var fromFirst = (from === 'first');
3834
var fromCenter = (from === 'center');
3935
var fromLast = (from === 'last');
@@ -44,66 +40,103 @@ var StaggerBuilder = function (value, options)
4440
var value2 = (isRange) ? parseFloat(value[1]) : 0;
4541
var maxValue = Math.max(value1, value2);
4642

43+
if (isRange)
44+
{
45+
start += value1;
46+
}
47+
4748
var easeFunction = (ease) ? GetEaseFunction(ease) : null;
4849

49-
if (!grid)
50+
// target = The target object being tweened
51+
// key = The key of the property being tweened
52+
// value = The current value of that property
53+
// index = The index of the target within the Tween targets array
54+
// total = The total number of targets being tweened
55+
// tween = A reference to the Tween performing this update
56+
57+
result = function (target, key, value, index, total)
5058
{
51-
// target = The target object being tweened
52-
// key = The key of the property being tweened
53-
// value = The current value of that property
54-
// index = The index of the target within the Tween targets array
55-
// total = The total number of targets being tweened
56-
// tween = A reference to the Tween performing this update
57-
result = function (target, key, value, index, total, tween)
59+
// zero offset
60+
total--;
61+
62+
var fromIndex;
63+
64+
if (fromFirst)
5865
{
59-
// zero offset
60-
total--;
66+
fromIndex = (grid) ? 0 : index;
67+
}
68+
else if (fromCenter)
69+
{
70+
fromIndex = Math.abs((total / 2) - index);
71+
}
72+
else if (fromLast)
73+
{
74+
fromIndex = (grid) ? total : total - index;
75+
}
76+
else if (fromValue)
77+
{
78+
fromIndex = Math.abs(from - index);
79+
}
6180

62-
var fromIndex;
81+
var spacing = 0;
82+
var output = start;
83+
var max = total * maxValue;
6384

64-
if (fromFirst)
85+
if (grid)
86+
{
87+
var fromX = (!fromCenter) ? fromIndex % grid[0] : (grid[0] - 1) / 2;
88+
var fromY = (!fromCenter) ? Math.floor(fromIndex / grid[0]) : (grid[1] - 1) / 2;
89+
90+
var toX = index % grid[0];
91+
var toY = Math.floor(index / grid[0]);
92+
93+
var distanceX = fromX - toX;
94+
var distanceY = fromY - toY;
95+
96+
var gridSpace = Math.sqrt(distanceX * distanceX + distanceY * distanceY);
97+
98+
if (axis === 'x')
6599
{
66-
fromIndex = index;
100+
gridSpace = -distanceX;
67101
}
68-
else if (fromCenter)
102+
else if (axis === 'y')
69103
{
70-
fromIndex = Math.abs((total / 2) - index);
104+
gridSpace = -distanceY;
71105
}
72-
else if (fromLast)
106+
}
107+
108+
if (isRange)
109+
{
110+
if (fromCenter)
73111
{
74-
fromIndex = total - index;
112+
spacing += ((value2 - value1) / total) * (fromIndex * 2);
75113
}
76-
else if (fromValue)
114+
else
77115
{
78-
fromIndex = Math.abs(from - index);
116+
spacing += ((value2 - value1) / total) * fromIndex;
79117
}
80118

81-
var output = start;
82-
var max = total * maxValue;
83-
var spacing = value1;
119+
output += spacing;
84120

85-
if (isRange)
121+
if (grid)
86122
{
87-
spacing += ((value2 - value1) / total) * fromIndex;
123+
output += spacing + (gridSpace * value1);
88124
}
125+
}
126+
else
127+
{
128+
output += (grid) ? (gridSpace * value1) : fromIndex * value1;
129+
}
89130

90-
if (easeFunction)
91-
{
92-
output += (max * easeFunction(fromIndex / total));
93-
}
94-
else
95-
{
96-
output += (isRange) ? spacing : (fromIndex * value1);
97-
}
131+
// if (easeFunction)
132+
// {
133+
// output += (max * easeFunction(fromIndex / total));
134+
// }
98135

99-
console.log('i', index, 'of', total, 'fromIndex:', fromIndex, 'spacing:', spacing, 'result: ', output);
136+
console.log('>', index, '/', total, 'fromIndex:', fromIndex, 'spacing:', spacing, 'RESULT:', output, 'from', fromX, fromY, 'to', toX, toY, 'dist', distanceX, distanceY);
100137

101-
return output;
102-
};
103-
}
104-
else
105-
{
106-
}
138+
return output;
139+
};
107140

108141
return result;
109142
};

0 commit comments

Comments
 (0)