forked from IjzerenHein/famous-animatedIcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimatedIcon.js
132 lines (117 loc) · 3.88 KB
/
AnimatedIcon.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* This Source Code is licensed under the MIT license. If a copy of the
* MIT-license was not distributed with this file, You can obtain one at:
* http://opensource.org/licenses/mit-license.html.
*
* @author: Hein Rutjes (IjzerenHein)
* @license MIT
* @copyright Gloey Apps, 2014
*/
/*jslint browser:true, nomen:true, vars:true, plusplus:true*/
/*global define*/
define(function(require, exports, module) {
// import dependencies
var Surface = require('famous/core/Surface');
var StateModifier = require('famous/modifiers/StateModifier');
var View = require('famous/core/View');
var Transform = require('famous/core/Transform');
var Easing = require('famous/transitions/Easing');
/**
* @enum
*/
var Shape = {
HAMBURGER: 0,
LEFT_ARROW: 1
};
/**
* @class
* @extends View
* @param {Object} [options] Configuration options
*/
function AnimatedIcon(options) {
View.apply(this, arguments);
this._createLines();
this._createShapes();
this.setShape(Shape.HAMBURGER, {duration: 0});
}
AnimatedIcon.prototype = Object.create(View.prototype);
AnimatedIcon.prototype.constructor = AnimatedIcon;
AnimatedIcon.Shape = Shape;
AnimatedIcon.DEFAULT_OPTIONS = {
defaultTransition: {duration: 300, curve: Easing.inOutQuart}
};
/**
* Create line modifiers & surfaces
*/
AnimatedIcon.prototype._createLines = function() {
var i;
this.lines = [];
for (i = 0; i < 3; i++) {
var modifier = new StateModifier({
align: [0.5, 0.0],
origin: [0.0, 0.5]
});
var surface = new Surface({
classes: ['line']
});
this.add(modifier).add(surface);
this.lines.push(modifier);
}
};
/**
* Create shapes
*/
AnimatedIcon.prototype._createShapes = function() {
this.shapes = [];
// Hamburger
this.shapes.push({
name: 'hamburger',
lines: [
{ size: [24, 2], transform: Transform.translate(0, 8, 0) },
{ size: [24, 2], transform: Transform.translate(0, 15, 0) },
{ size: [24, 2], transform: Transform.translate(0, 22, 0) }
]
});
// Left-arrow
this.shapes.push({
name: 'left-arrow',
lines: [
{ size: [10, 2], transform: Transform.multiply(Transform.translate(10, 21.5, 0), Transform.rotateZ((Math.PI / 180) * 225)) },
{ size: [16, 2], transform: Transform.multiply(Transform.translate(20, 15, 0), Transform.rotateZ((Math.PI / 180) * 180)) },
{ size: [10, 2], transform: Transform.multiply(Transform.translate(3, 15.5, 0), Transform.rotateZ((Math.PI / 180) * 315)) }
]
});
};
/**
* Sets the shape
*/
AnimatedIcon.prototype.setShape = function(shapeIndex, transition, callback) {
if (this._shapeIndex === shapeIndex) {
if (callback) {
callback();
}
return;
}
this._shapeIndex = shapeIndex;
transition = transition || this.options.defaultTransition;
var i;
var shape = this.shapes[shapeIndex];
for (i = 0; i < shape.lines.length; i++) {
this.lines[i].halt();
this.lines[i].setSize(shape.lines[i].size, transition);
if (i === 0) {
this.lines[i].setTransform(shape.lines[i].transform, transition, callback);
}
else {
this.lines[i].setTransform(shape.lines[i].transform, transition);
}
}
};
/**
* Get the shape
*/
AnimatedIcon.prototype.getShape = function(shapeIndex, transition) {
return this._shapeIndex;
};
module.exports = AnimatedIcon;
});