-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsunset.js
76 lines (62 loc) · 1.84 KB
/
sunset.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
var svgN = "http://www.w3.org/2000/svg";
// sets svg object variables
var canvas = document.getElementById("canvas");
var sun = document.createElementNS(svgN, "circle");
// default canvas specifications and sun attribute
var canvasHeight = canvas.getBoundingClientRect().height;
var canvasWidth = canvas.getBoundingClientRect().width;
var canvasCenterY = canvasHeight / 2;
var canvasCenterX = canvasWidth / 2;
var sunCenterX = canvasCenterX;
var sunCenterY = canvasCenterY;
var sunRadius = canvasHeight / 10;
var sunColor = "#fcfa79";
// sets default position of the sun at the center of the canvas
sun.setAttribute("cx", sunCenterX);
sun.setAttribute("cy", sunCenterY);
sun.setAttribute("r", sunRadius);
sun.setAttribute("fill", sunColor);
function resetSun() {
sunCenterX = canvasCenterX;
sunCenterY = canvasCenterY;
sun.setAttribute("cx", sunCenterX);
sun.setAttribute("cy", sunCenterY);
sun.setAttribute("r", sunRadius);
}
function displaySun() {
canvas.appendChild(sun);
}
function removeSun() {
canvas.removeChild(sun);
}
function setSun() {
sunCenterY = sunCenterY + 2;
sun.setAttribute("cy", sunCenterY);
if (sunCenterY < (canvasHeight + sunRadius)) {
} else {
resetSun();
}
}
function createSunset(duration, from, to) {
sunset = sun.animate([
{ cy: from }, // Starting state
{ cy: to } // Ending state
], {
duration: duration, // Animation duration in milliseconds
easing: 'ease-out' // Easing function
});
sunWiden = sun.animate([
{ r: sunRadius },
{ r: sunRadius * 1.3 }
], {
duration: duration,
easing: 'ease-in-out'
});
sunset.play();
sunWiden.play();
resetSun()
}
function runSunSet() {
var duration = 5000; // ms
createSunset(duration, sunCenterY, sunCenterY + canvasHeight + sunRadius);
}