-
Notifications
You must be signed in to change notification settings - Fork 1
/
trianglify.animate.js
222 lines (193 loc) · 10.4 KB
/
trianglify.animate.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Angular Trianglify Animate
* github: https://github.com/machei/angular-trianglify-animate
* @author: machei
* @thanks: grohlf & empathetic-alligator
*/
function scale(valueIn, baseMin, baseMax, limitMin, limitMax) {
return ((limitMax - limitMin) * (valueIn - baseMin) / (baseMax - baseMin)) + limitMin;
}
angular.module('moduleTrianglifyAnimate', [])
.constant('configTrianglifyAnimate', {
speed:1,
vectorX: 0,
vectorY: 1,
baseMax: 0,
baseMin: 500,
theta: 0.4,
pathPassSymbols : 'MZLHmzlh' //MhvHvhvH1vhvHVLz
})
.directive("ngTrianglifyAnimate", ["$compile", function($compile) {
return {
restrict:'EA',
scope:{
interval: '='
},
controller: ['$scope', '$element', '$attrs', '$interval', 'configTrianglifyAnimate', function ($scope, $element, $attrs, $interval, configTrianglifyAnimate) {
// Replace img with svg
if($element[0].tagName === "IMG" && window.File && window.FileList && window.FileReader){
var $e = $element[0] , $p = $element[0].parentNode;
var xhr = new XMLHttpRequest();
xhr.open("GET", $e.src, false);
xhr.responseType = "xml";
function response(e){
var parser, xmlDoc;
if (window.XMLHttpRequest){ // Chorme, Firefox, Opera
parser=new DOMParser();
xmlDoc = parser.parseFromString(e.target.response,"text/xml");
}else{ // IE
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(e.target.response);
}
svg = xmlDoc.getElementsByTagName("svg")[0];
svg.setAttribute('ng-trianglify-animate','');
for(var i=0; i<$e.attributes.length; i++){
svg.setAttribute($e.attributes[i].nodeName,$e.attributes[i].value);
}
svg = angular.element(svg);
$element.replaceWith(svg);
$compile(svg)($scope);
$element = svg;
}
xhr.onload = response; // error on firefox
xhr.send();
return true;
}
// Attrs
angular.forEach(['speed', 'vectorX', 'vectorY', 'baseMax', 'baseMin', 'theta'], function( key, index ) {
$scope[key] = angular.isDefined($attrs[key]) ? (index < 6 ? $attrs[key] : $scope.$parent.$eval($attrs[key])) : configTrianglifyAnimate[key];
});
//** Init Shapes **//
// Chrome&Firefox&Opera works except IE 11:
// For IE:
//var d = polygons[i].getAttribute('points').split(' ');
//for(var q=0; q<d.length; q++){
// var point = d[q].split(',')
// polys[i] = {x:point[0], y:point[1]};
//}
var elements = {
polys: { points:[], cached: [] },
paths: { points:[], cached: [] },
circles: { points:[], cached: [] },
rects: { points:[], cached: [] },
ellipses: { points:[], cached: [] },
lines: { points:[], cached: [] },
polylines: { points:[], cached: [] }
};
// Shape <polygon>
var polygons = $element[0].getElementsByTagName('polygon');
for(var i=0; i<polygons.length; i++) elements.polys.points.push(polygons[i].points);
// Shape <path>
var el_paths = $element[0].getElementsByTagName('path');
for(var i=0; i<el_paths.length; i++) elements.paths.points.push(el_paths[i].pathSegList);
// Shape <circle>
var el_circle = $element[0].getElementsByTagName('circle');
for(var i=0; i<el_circle.length; i++) elements.circles.points.push({'x':el_circle[i].cx,'y':el_circle[i].cy,'r':el_circle[i].r});
// Shape <rect>
var el_rect = $element[0].getElementsByTagName('rect');
for(var i=0; i<el_rect.length; i++) elements.rects.points.push({x:el_rect[i].x, y:el_rect[i].y, width:el_rect[i].width, height:el_rect[i].height});
// Shape <ellipse>
var el_ellipse = $element[0].getElementsByTagName('ellipse');
for(var i=0; i<el_ellipse.length; i++) elements.ellipses.points.push({'x':el_ellipse[i].rx, 'y':el_ellipse[i].ry});
// Shape <line>
var el_line = $element[0].getElementsByTagName('line');
for(var i=0; i<el_line.length; i++) elements.lines.points.push({'x1':el_line[i].x1, 'y1':el_line[i].y1, 'x2':el_line[i].x2, 'y2':el_line[i].y2});
// Shape <polyline>
var el_polyline = $element[0].getElementsByTagName('polyline');
for(var i=0; i<el_polyline.length; i++) elements.polylines.points.push(el_polyline[i].points);
// Copy values
for(var element in elements){
var p = elements[element].points;
for(var i=0; i<p.length; i++){
switch(element){
case 'line': //x1,x2,y1,y2
elements[element].cached.push({x1: p[i].x1, x2: p[i].x2, y1: p[i].y1, y2:p[i].y2 });
break;
case 'polys':
elements[element].cached[i] = [];
var $path;
for(var q=0, $path = p[i], $item; q < $path.numberOfItems; q++){
$item= $path.getItem(q);
var w = {};
w['x'] = $item.x;
w['y'] = $item.y;
elements[element].cached[i].push(w);
}
break;
case 'paths':
elements[element].cached[i] = [];
for(var q=0, $path = p[i], $item; q < $path.numberOfItems; q++)
if(configTrianglifyAnimate.pathPassSymbols.indexOf(($item = $path.getItem(q)).pathSegTypeAsLetter) > -1)
elements[element].cached[i].push({x: $item.x, y: $item.y});
else elements[element].cached[i].push(null);
break;
default:
elements[element].cached.push({x: p[i].x ,y: p[i].y});
}
}
}
$scope.elements = elements;
if(!window.hasOwnProperty('f')) window.f = [];
f.push(elements);
//angular.extend($scope, {'polys':polys, 'paths':paths, 'circles':circles, 'rects':rects, 'ellipses':ellipses, 'lines':lines, 'polylines':polylines});
$scope.theta = 0;
var calculate = function(l1, l2, theta, points){
// polygons
// Problem IE11 : F12 tools do not currently support extensive Scalable Vector Graphics (SVG) debugging, but several console messages are displayed to help debug SVG code.
// Shape <polygon>
for(var i = 0, temp, $poly; i < $scope.elements.polys.points.length; i++) {
for(var q=0, $poly = $scope.elements.polys.points[i], $item; q < $poly.numberOfItems; q++){
temp = $poly.getItem(q);
temp[l1] += Math.sin($scope.theta + scale($scope.elements.polys.cached[i][q][l2], 0, 500, 0, 2 * Math.PI)) * 0.5;
}
}
// Shape <path>
for(var i = 0, $path; i < $scope.elements.paths.points.length; i++) {
for(var q = 0, $path = $scope.elements.paths.points[i], $item; q < $path.numberOfItems; q++)
if(configTrianglifyAnimate.pathPassSymbols.indexOf(($item = $path.getItem(q)).pathSegTypeAsLetter) > -1){
if($scope.elements.paths.cached[i][q]){
if($scope.elements.paths.cached[i][q].hasOwnProperty(l2) && $item[l1] && !isNaN($item[l1]) && !isNaN($scope.elements.paths.cached[i][q][l2]))
$item[l1] += Math.sin($scope.theta + scale($scope.elements.paths.cached[i][q][l2], 0, 500, 0, 2 * Math.PI)) * 0.5;
else if($item[l1] && !isNaN($item[l1]))
$item[l1] += Math.sin($scope.theta + scale($scope.elements.paths.cached[i][q][l1], 0, 500, 0, 2 * Math.PI)) * 0.5;
}
}
//$item[l1] += Math.sin($scope.theta + scale($item[l2], 0, 500, 0, 2 * Math.PI)) * 0.5;
}
// Shape <circle>
for(var i = 0, $circle; i < $scope.elements.circles.points.length; i++) {
($circle = $scope.elements.circles.points[i])[l1] += Math.sin($scope.theta + scale($scope.elements.circles.cached[i][l2], 0, 500, 0, 2 * Math.PI)) * 0.5;
}
// Shape <rect>
for(var i = 0, $rect; i < $scope.elements.rects.points.length; i++) {
($rect = $scope.elements.rects.points[i])[l1] += Math.sin($scope.theta + scale($scope.elements.rects.cached[i][l2], 0, 500, 0, 2 * Math.PI)) * 0.5;
}
// Shape <ellipse>
for(var i = 0, $ellipse; i < $scope.elements.ellipses.points.length; i++) {
($ellipse = $scope.elements.ellipses.points[i])[l1] += Math.sin($scope.theta + scale($scope.elements.ellipses.cached[i][l2], 0, 500, 0, 2 * Math.PI)) * 0.5;
}
// Shape <line>
for(var i = 0, $line; i < $scope.elements.lines.points.length; i++) {
($line = $scope.elements.lines.points[i])[points[0]] += + Math.sin($scope.theta + scale($scope.elements.lines.cached[i][points[2]], 0, 500, 0, 2 * Math.PI)) * 0.5;
$line[points[1]] += Math.sin($scope.theta + scale($scope.elements.lines.cached[i][points[3]], 0, 500, 0, 2 * Math.PI)) * 0.5;
}
// Shape <polyline>
for(var i = 0, $polyline; i < $scope.elements.polylines.points.length; i++) {
for(var q=0, $polyline = $scope.elements.polylines.points[i], $item; q < $polyline.numberOfItems; q++)
($item=$polyline.getItem(q))[l1] += Math.sin($scope.theta + scale($item[l2], 0, 500, 0, 2 * Math.PI)) * 0.5;
}
};
var animate = function(){
if($scope.vectorY > 0 && $scope.vectorX == 0) calculate('y', 'x', $scope.theta, ['y1','y2','x1','x2']);
else if($scope.vectorX > 0 && $scope.vectorY == 0) calculate('x', 'y', $scope.theta, ['x1','x2','y1','y2']);
else{
calculate('y', 'y', $scope.theta, ['y1','y2','y1','y2']);
calculate('x', 'x', $scope.theta, ['x1','x2','x1','x2']);
}
$scope.theta += 0.04;
};
$scope.animate = $interval(function(){animate()}, 10/$scope.speed, false);
}]
};
}]);