-
Notifications
You must be signed in to change notification settings - Fork 0
/
animate.js
228 lines (199 loc) · 9.24 KB
/
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
223
224
225
226
227
228
var GLOBAL_SETTINGS = {
"queryParameters": (function (a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i) {
var p = a[i].split('=', 2);
if (p.length == 1)
b[p[0]] = "";
else
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&')),
"canvasId": "canvas"
};
var getCanvas = function () {
return $("#" + GLOBAL_SETTINGS.canvasId);
};
var getCanvasNatively = function(){
return document.getElementById("drawingCanvas");
};
var currentScene = 0;
var scenes = [];
var loadModel = function(){
var model = GLOBAL_SETTINGS.queryParameters["model"];
model = JSON.parse(decodeURIComponent(model));
renderModel(model);
// $.get("smallModel.json", function (modelData) {
// var renderedData = typeof modelData === 'object' ? modelData : JSON.parse(modelData);
// renderModel(renderedData);
// });
};
var renderModel = function (modelData) {
$.each(modelData.scenes, function (index, value) {
scenes.push(value);
});
playScene();
};
var playScene = function () {
getCanvas().addClass("fadeOut");
getCanvas().removeClass("fadeIn");
setTimeout(function () {
getCanvas().removeClass("fadeOut");
getCanvas().addClass("fadeIn");
getCanvas().empty();
if (currentScene < scenes.length) {
var scene = scenes[currentScene];
if(scene.animation!==undefined && scene.animation!==null){
$.get("animations/"+scene.animation+"/"+scene.animation+".json", function(data){
var animation = typeof data === 'object' ? data : JSON.parse(data);
if(animation!==undefined && animation!==null) {
$.each(animation.components, function (index, component) {
renderElementWithAnimation(component, getCanvas(), scene);
});
currentScene++;
setTimeout(playScene, animation.duration);
}
});
}
}
else {
getCanvas().html("<h1>Fin</h1>");
}
}, 2000);
};
var record = function(){
var canvas = getCanvasNatively();
if(canvas!==null) {
console.log(getCanvasNatively().toDataURL());
}
window.setTimeout(record, 2000);
};
record();
var elementId = 0;
var transformMotionPath = function (originalPath) {
//TODO transform path accoring to scaling values;
return originalPath;
};
var renderElementWithAnimation = function (element, parentElement, scene) {
var id = elementId++;
var svgContainerIdPrefix = "svgContainer";
var svgContainerWrapperIdPrefix = "svgContainerWrapper";
if(element.svg!==undefined && element.svg!==null) {
$.get("animations/" + scene.animation + "/" + element.svg + "?" + id, function (svgData) {
var index = parseInt(this.url.split("?")[1]);
var svgContainerWrapper = $("<div class=\"svgContainerWrapper\" id=\"" + svgContainerWrapperIdPrefix + index + "\"><div id=\"" + svgContainerIdPrefix + index + "\" class=\"svgContainer\"></div></div>");
var svgContainer = $(svgContainerWrapper.children()[0]);
if (element.style !== undefined) {
//Add defined css styles to wrapper element
$.each(element.style, function (key, styleValue) {
svgContainerWrapper.css(key, styleValue);
});
}
svgContainer.append(svgData.rootElement);
var added = false;
$.each(parentElement.children(), function (i, child) {
child = $(child);
if (child.attr("id").indexOf(svgContainerWrapperIdPrefix > -1)) {
var indexOfCurrentItem = parseInt(child.attr("id").substring(svgContainerWrapperIdPrefix.length));
if (index < indexOfCurrentItem) {
svgContainerWrapper.insertBefore(child);
added = true;
}
}
}).promise().done(function () {
if (!added) {
parentElement.append(svgContainerWrapper);
}
});
if (element.viewbox !== undefined) {
svgContainer.children()[0].setAttribute("viewBox", element.viewbox);
}
else {
var predefinedWidth = svgContainer.children()[0].getAttribute("width");
var predefinedHeight = svgContainer.children()[0].getAttribute("height");
if (predefinedHeight !== undefined && predefinedHeight !== null && predefinedWidth !== undefined && predefinedWidth !== null) {
svgContainer.children()[0].setAttribute("viewBox", "0 0 " + predefinedWidth + " " + predefinedHeight);
}
}
if (element.width !== undefined) {
svgContainerWrapper.css("width", element.width + "px");
svgContainer.children()[0].setAttribute("width", element.width + "px");
}
if (element.height !== undefined) {
svgContainerWrapper.css("height", element.height + "px");
svgContainer.children()[0].setAttribute("height", element.height + "px");
}
//Parametrize object
if (scene.parameters !== undefined && scene !== null) {
$.each(scene.parameters, function (key, value) {
var element = svgContainer.find(".parametrizable." + key);
if (element !== undefined && element.length > 0) {
$.each(element, function (i, el) {
el.textContent = value;
});
}
});
}
//Manage defined animations
if (element.animations !== undefined) {
$.each(element.animations, function (key, animation) {
if (animation.enabled === undefined || animation.enabled) {
if (animation.css !== undefined) {
$.each(animation.css, function (key, css) {
if (key === "motionPath") {
if (css.toLowerCase().indexOf("path") !== 0) {
var path = svgContainer.find("." + css);
if (path !== undefined && path !== null) {
svgContainerWrapper.css(key, transformMotionPath("path('" + path.attr("d") + "')"));
handled = true;
}
}
else {
svgContainerWrapper.css(key, transformMotionPath(css));
}
document.getElementById(svgContainerWrapperIdPrefix + id).animate(
animation.animation, animation.timing
);
}
else {
svgContainerWrapper.css(key, css);
}
});
}
document.getElementById(svgContainerIdPrefix + id).animate(
animation.animation, animation.timing
);
//Override by animation specific parameters
$.each(animation.parameters, function (key, value) {
var element = svgContainer.find(".parametrizable." + key);
if (element !== undefined) {
element[0].textContent = value;
}
});
}
});
}
if (element.subElements !== undefined) {
$.each(element.subElements, function (key, subElement) {
var handled = false;
if (subElement.hook !== undefined) {
var hooks = svgContainer.find("." + subElement.hook);
$.each(hooks, function (idx, hookElement) {
renderElementWithAnimation(subElement, $(hookElement), scene);
handled = true;
})
}
if (!handled) {
renderElementWithAnimation(subElement, svgContainer, scene);
}
})
}
});
}
else if(element.image!==undefined && element.image!==null){
var svgContainerWrapper = $("<div class=\"svgContainerWrapper\" id=\"" + svgContainerWrapperIdPrefix + id + "\"><div id=\"" + svgContainerIdPrefix + id + "\" class=\"svgContainer\"><img src=\"animations/" + scene.animation + "/"+element.image+"\" style=\"width: "+element.width+"px; height: "+element.height+"px;\"/></div></div>");
parentElement.append(svgContainerWrapper);
}
};