Skip to content

Commit 1cada81

Browse files
committed
Implemented eclipse and sun
1 parent bbe66c6 commit 1cada81

File tree

7 files changed

+130
-17
lines changed

7 files changed

+130
-17
lines changed

assets/solar-eclipse.png

394 KB
Loading

node_modules/.bin/eslint

-15
This file was deleted.

node_modules/.bin/eslint

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/App.jsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react"
22
import Moon from "./Moon.jsx"
33
import Wave from "./Wave.jsx"
4+
import Sun from "./Sun.jsx"
45

56
class App extends React.Component {
67
constructor() {
@@ -12,6 +13,7 @@ class App extends React.Component {
1213
return (
1314
<div>
1415
<Wave />
16+
<Sun />
1517
<Moon />
1618
</div>
1719
)

src/components/Sun.jsx

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import React from "react";
2+
import sunSketch from "../sketches/sunSketch.js";
3+
import P5Wrapper from "./P5Wrapper.jsx";
4+
import eclipseSketch from "../sketches/eclipseSketch.js";
5+
6+
class Sun extends React.Component {
7+
constructor(props) {
8+
super(props);
9+
10+
this.getData = this.getData.bind(this);
11+
this.getCurrentYear = this.getCurrentYear.bind(this);
12+
this.getNextEclipse = this.getNextEclipse.bind(this);
13+
14+
this.state = {
15+
error: null,
16+
daysUntilEclipse: null,
17+
isLoading: false,
18+
debug: false
19+
};
20+
}
21+
22+
componentDidMount() {
23+
this.setState({ isLoading: true });
24+
this.getData();
25+
setInterval(this.getData, 600 * 1000);
26+
}
27+
28+
componentWillMount() {
29+
clearInterval(this.getData);
30+
}
31+
32+
getData() {
33+
const currentYear = this.getCurrentYear();
34+
fetch(`http://api.usno.navy.mil/eclipses/solar?year=${currentYear}`)
35+
.then(res => res.json())
36+
.then(
37+
result => {
38+
this.getNextEclipse(result.eclipses_in_year);
39+
this.setState({ isLoading: false, daysUntilEclipse: Math.random(0,10)*10 });
40+
},
41+
error => this.setState({ error, isloading: false })
42+
);
43+
}
44+
45+
getCurrentYear() {
46+
return new Date().getFullYear();
47+
}
48+
49+
getNextEclipse(eclipses) {
50+
51+
}
52+
53+
render() {
54+
if (this.state.error) {
55+
return <div>ERROR: {this.state.error}</div>;
56+
} else if (this.state.isLoading) {
57+
return <div>Loading...</div>;
58+
} else if (this.state.debug) {
59+
return (
60+
<div>
61+
<P5Wrapper sketch={sunSketch} debug={this.state.debug} />;
62+
<P5Wrapper sketch={eclipseSketch} debug={this.state.debug} />;
63+
</div>
64+
);
65+
} else {
66+
return (
67+
<div>
68+
<P5Wrapper sketch={sunSketch} daysUntilEclipse={this.state.daysUntilEclipse} />
69+
<P5Wrapper sketch={eclipseSketch} daysUntilEclipse={this.state.daysUntilEclipse} />;
70+
</div>
71+
);
72+
}
73+
}
74+
}
75+
76+
export default Sun;

src/sketches/eclipseSketch.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default function sketch(p) {
2+
const solarEclipseImg = p.loadImage("assets/solar-eclipse.png");
3+
let daysUntilEclipse;
4+
p.draw = function () {
5+
if (daysUntilEclipse && daysUntilEclipse < 10) {
6+
p.clear();
7+
p.tint(255, 255-(Math.abs(daysUntilEclipse)*17));
8+
p.imageMode(p.CENTER);
9+
p.image(solarEclipseImg, (p.windowWidth/2), (p.windowHeight/2)+5, 0);
10+
}
11+
}
12+
13+
p.myCustomRedrawAccordingToNewPropsHandler = function (props) {
14+
if (props.daysUntilEclipse) {
15+
daysUntilEclipse = props.daysUntilEclipse;
16+
}
17+
}
18+
19+
p.setup = function () {
20+
p.createCanvas(p.windowWidth, p.windowHeight);
21+
}
22+
23+
}

src/sketches/moonSketch.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var moment = require('moment');
22

33
export default function sketch(p) {
44
let x = 0,
5-
y = 0,
5+
y = -500,
66
phaseMod = 0;
77

88
var ratio = 0;
@@ -33,6 +33,7 @@ export default function sketch(p) {
3333

3434
p.draw = function () {
3535
if (data) {
36+
p.clear();
3637
const moonPhase = data[0].phase;
3738
const progress = getTimeProgress();
3839
p.noStroke();
@@ -58,7 +59,7 @@ export default function sketch(p) {
5859
p.ellipse(p.windowWidth / 2, p.windowHeight / 2, 500);
5960
p.fill(255, 245, 200);
6061
ratio = calcRatio(data[0].date, data[1].date, ratio);
61-
p.arc(p.windowWidth / 2, p.windowHeight / 2, 500, 500, p.HALF_PI, p.HALF_PI + p.PI);
62+
p.arc(p.windowWidth / 2, (p.windowHeight / 2), 500, 500, p.HALF_PI, p.HALF_PI + p.PI);
6263
p.arc(p.windowWidth / 2, p.windowHeight / 2, (500 - (ratio / 500)), 500, p.HALF_PI + p.PI, p.HALF_PI);
6364
if ((500 - (ratio / 500)) == 0) {
6465
data[0].phase = "Last Quarter";

src/sketches/sunSketch.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export default function sketch(p) {
2+
let x = 0;
3+
let y = 0;
4+
5+
let daysUntilEclipse;
6+
7+
p.draw = function() {
8+
if (daysUntilEclipse) {
9+
p.clear();
10+
p.fill('yellow');
11+
p.ellipse((p.windowWidth/2 + daysUntilEclipse*-20), p.windowHeight/2, 500, 500);
12+
}
13+
}
14+
15+
p.myCustomRedrawAccordingToNewPropsHandler = function (props) {
16+
if (props.daysUntilEclipse) {
17+
daysUntilEclipse = props.daysUntilEclipse;
18+
}
19+
}
20+
21+
p.setup = function() {
22+
p.createCanvas(p.windowWidth, p.windowHeight);
23+
}
24+
25+
}

0 commit comments

Comments
 (0)