-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
100 lines (94 loc) · 3.03 KB
/
main.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
var mainContainer = document.getElementById("mainContainer");
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
function getWidth() {
return Math.max(
document.body.scrollWidth,
document.documentElement.scrollWidth,
document.body.offsetWidth,
document.documentElement.offsetWidth,
document.documentElement.clientWidth
);
}
function getHeight() {
return Math.max(
document.body.scrollHeight,
document.documentElement.scrollHeight,
document.body.offsetHeight,
document.documentElement.offsetHeight,
document.documentElement.clientHeight
);
}
function createTriangles(type) {
var centerX = getWidth() / 2;
var centerY = getHeight() / 2;
console.log(centerX, centerY);
var triangleRatio = 10;
var black = true;
for (var i = 0; i < 10; i++) {
// var svgID = "svg" + (i + 1);
// var svg = document.getElementById(svgID);
var svg = document.getElementById("svgID");
var topPointX = centerX;
var topPointY = centerY - 30 * triangleRatio;
var leftPointX = centerX - 26 * triangleRatio;
var leftPointY = centerY + 15 * triangleRatio;
var rightPointX = centerX + 26 * triangleRatio;
var rightPointY = centerY + 15 * triangleRatio;
var points =
topPointX +
", " +
topPointY +
" " +
leftPointX +
", " +
leftPointY +
" " +
rightPointX +
", " +
rightPointY;
var centerX = (topPointX + leftPointX + rightPointX) / 3;
var centerY = (topPointY + leftPointY + rightPointY) / 3;
var newTriangle = document.createElementNS(
"http://www.w3.org/2000/svg",
"polygon"
);
newTriangle.setAttributeNS(null, "points", points);
newTriangle.setAttributeNS(null, "id", "triangle" + i);
if (black == true) {
newTriangle.setAttributeNS(
null,
"style",
"fill:black; stroke:white; stroke-width:5"
);
black = false;
} else {
newTriangle.setAttributeNS(
null,
"style",
"fill:white; stroke:black; stroke-width:5"
);
black = true;
}
// var rotateInput = "rotate(180, " + centerX + ", " + centerY + ")";
//newTriangle.setAttribute("transform", rotateInput);
var animateTriangle = document.createElementNS(
"http://www.w3.org/2000/svg",
"animateTransform"
);
var animateFrom = "" + 0 + " " + centerX + " " + centerY;
var animateTo = "" + 360 + " " + centerX + " " + centerY;
var speedCounter = 10 - i;
animateTriangle.setAttributeNS(null, "attributeName", "transform");
animateTriangle.setAttributeNS(null, "attributeType", "XML");
animateTriangle.setAttributeNS(null, "type", "rotate");
animateTriangle.setAttributeNS(null, "from", animateFrom);
animateTriangle.setAttributeNS(null, "to", animateTo);
animateTriangle.setAttributeNS(null, "dur", speedCounter);
animateTriangle.setAttributeNS(null, "repeatCount", "indefinite");
newTriangle.appendChild(animateTriangle);
svg.appendChild(newTriangle);
triangleRatio -= 1;
}
}