-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsvg-animations.js
executable file
·142 lines (119 loc) · 4.71 KB
/
jsvg-animations.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
// ----------------------------- //
// JSVG-ANIMATIONS 1.0 //
// DEVELOPPED BY ANTOINE NEDELEC //
// ----------------------------- //
// DEPENDENCY: none //
// ----------------------------- //
// Plugin
(function() {
// Define our constructor
this.jsvganimation = function() {}
// Public Methods
jsvganimation.initSVG = function(rootElement) {
let svgElement = null;
if(rootElement.tagName.toLowerCase() === "svg") {
svgElement = rootElement;
} else if(rootElement.tagName.toLowerCase() !== "svg") {
svgElement = rootElement.querySelector("svg");
if(svgElement.tagName.toLowerCase() !== "svg") {
console.log("rootElement and its chidlren have no SVG to be found !");
}
}
// INIT LINES
let svgLines = svgElement.querySelectorAll('path[data-type="line"]');
for (let i = 0, len = svgLines.length; i < len; i++) {
let path = svgLines[i];
let length = path.getTotalLength();
let timing = path.getAttribute('data-timing') || 1;
path.style.fill = "none";
// Clear any previous transition
path.style.transition = path.style.WebkitTransition = 'none';
// Set up the starting positions (0)
path.style.strokeDasharray = length + ' ' + length;
path.style.strokeDashoffset = length;
path.style.opacity = 1;
// Setting transition NOW would make the transition happen
setTimeout(function() {
path.style.transition = path.style.WebkitTransition = 'stroke-dashoffset '+timing+'s ease-in-out';
}, 1);
}
// INIT FIGURES
let svgFigures = svgElement.querySelectorAll('path[data-type="figure"]');
for (let i = 0, len = svgFigures.length; i < len; i++) {
let path = svgFigures[i];
let timing = path.getAttribute('data-timing') || 1;
let fill = path.getAttribute('data-fill') || null;
path.style.fillOpacity = 0;
// Setting transition NOW would make the transition happen
setTimeout(function() {
path.style.transition = path.style.WebkitTransition = 'fill-opacity '+timing+'s ease-in-out';
if(fill != null) { path.style.fill = fill; }
}, 1);
}
// LAUNCH LINES with data-show=start
svgLines = svgElement.querySelectorAll('path[data-type="line"][data-show="start"]');
for (let i = 0, len = svgLines.length; i < len; i++) {
this.showSvgLine(svgLines[i]);
}
// LAUNCH FIGURES with data-show=start
svgFigures = svgElement.querySelectorAll('path[data-type="figure"][data-show="start"]');
for (let i = 0, len = svgFigures.length; i < len; i++) {
showSvgFigure(svgFigures[i]);
}
svgElement.addEventListener('mouseenter', function(e) {
// EVENT FOR LINES with data-show=hover
svgLines = svgElement.querySelectorAll('path[data-type="line"][data-show="hover"]');
for (let i = 0, len = svgLines.length; i < len; i++) {
showSvgLine(svgLines[i]);
}
// EVENT FOR FIGURES with data-show=hover
svgFigures = svgElement.querySelectorAll('path[data-type="figure"][data-show="hover"]');
for (let i = 0, len = svgFigures.length; i < len; i++) {
showSvgFigure(svgFigures[i]);
}
});
svgElement.addEventListener('mouseleave', function(e) {
// EVENT FOR LINES with data-show=hover
svgLines = svgElement.querySelectorAll('path[data-type="line"][data-show="hover"]');
for (let i = 0, len = svgLines.length; i < len; i++) {
hideSvgLine(svgLines[i]);
}
// EVENT FOR FIGURES with data-show=hover
svgFigures = svgElement.querySelectorAll('path[data-type="figure"][data-show="hover"]');
for (let i = 0, len = svgFigures.length; i < len; i++) {
hideSvgFigure(svgFigures[i]);
}
});
// End if init, show the SVG.
svgElement.style.opacity = 1;
}
// Private Methods
function showSvgLine (path) {
let delay = path.getAttribute('data-delay') || 0;
let timing = path.getAttribute('data-timing') || 1;
setTimeout(function() {
// Clear any previous transition
let length = path.getTotalLength();
// Define our transition
path.style.strokeDashoffset = 0;
}, delay * 100);
}
function hideSvgLine (path) {
let timing = path.getAttribute('data-timing') || 1;
// Clear any previous transition
var length = path.getTotalLength();
// Define our transition
path.style.strokeDashoffset = length;
}
function showSvgFigure (path) {
let delay = path.getAttribute('data-delay') || 0;
setTimeout(function(){
// Define our transition
path.style.fillOpacity = 1;
}, delay * 100);
}
function hideSvgFigure (path) {
// Define our transition
path.style.fillOpacity = 0;
}
}());