Created
March 12, 2018 22:42
-
-
Save TravisL12/ad50daaedfd0af18c502df9cc7ccbb3f to your computer and use it in GitHub Desktop.
Javascript Forrest!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
margin: 0; | |
} | |
#forrest { | |
position: relative; | |
width: 100vw; | |
height: 100vh; | |
background-color: lightblue; | |
} | |
#forrest .tree { | |
position: absolute; | |
height: 300px; | |
width: 150px; | |
} | |
#forrest .tree .leaves { | |
position: absolute; | |
width: 150px; | |
height: 150px; | |
border-radius: 50%; | |
background-color: lightgreen; | |
} | |
#forrest .tree .trunk { | |
position: absolute; | |
left: 52px; | |
bottom: 0; | |
width: 50px; | |
height: 90%; | |
background-color: brown; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function randomizer(max, min) { | |
min = min || 0; | |
max = max || 1; | |
return Math.round(Math.random() * (max - min) + min); | |
} | |
class Forrest { | |
constructor(forrestId) { | |
this.el = document.getElementById(forrestId); | |
this.trees = []; | |
document.addEventListener('click', () => { | |
this.addTree(); | |
}); | |
} | |
addTree() { | |
this.trees.push(new Tree(randomizer(this.el.offsetWidth), randomizer(this.el.offsetHeight))); | |
this.render(); | |
} | |
render() { | |
this.el.innerHTML = ''; // clear everything before drawing | |
const renderedTrees = []; | |
for (let i = 0; i < this.trees.length; i++) { | |
renderedTrees.push(this.trees[i].render()); | |
} | |
this.el.innerHTML = renderedTrees.join(''); | |
} | |
} | |
class Tree { | |
constructor(x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
render() { | |
return ` | |
<div class="tree" style="left: ${this.x}; top: ${this.y}"> | |
<div class="trunk"></div> | |
<div class="leaves"></div> | |
</div>`; | |
} | |
} | |
new Forrest('forrest'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
margin: 0; | |
} | |
#forrest { | |
position: relative; | |
width: 100vw; | |
height: 100vh; | |
background-color: lightblue; | |
} | |
#forrest .tree { | |
position: absolute; | |
height: 300px; | |
width: 150px; | |
} | |
#forrest .tree .leaves { | |
position: absolute; | |
width: 150px; | |
height: 150px; | |
border-radius: 50%; | |
background-color: lightgreen; | |
} | |
#forrest .tree .trunk { | |
position: absolute; | |
left: 52px; | |
bottom: 0; | |
width: 50px; | |
height: 90%; | |
background-color: brown; | |
} | |
function randomizer(max, min) { | |
min = min || 0; | |
max = max || 1; | |
return Math.round(Math.random() * (max - min) + min); | |
} | |
class Forrest { | |
constructor(forrestId) { | |
this.el = document.getElementById(forrestId); | |
this.trees = []; | |
document.addEventListener('click', () => { | |
this.addTree(); | |
}); | |
} | |
addTree() { | |
this.trees.push(new Tree(randomizer(this.el.offsetWidth), randomizer(this.el.offsetHeight))); | |
this.render(); | |
} | |
render() { | |
this.el.innerHTML = ''; // clear everything before drawing | |
const renderedTrees = []; | |
for (let i = 0; i < this.trees.length; i++) { | |
renderedTrees.push(this.trees[i].render()); | |
} | |
this.el.innerHTML = renderedTrees.join(''); | |
} | |
} | |
class Tree { | |
constructor(x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
render() { | |
return ` | |
<div class="tree" style="left: ${this.x}; top: ${this.y}"> | |
<div class="trunk"></div> | |
<div class="leaves"></div> | |
</div>`; | |
} | |
} | |
new Forrest('forrest'); | |
<!DOCTYPE> | |
<html> | |
<head> | |
<title>Forrest!</title> | |
<link rel="stylesheet" type="text/css" href="application.css"> | |
</head> | |
<body> | |
<div id='forrest'></div> | |
</body> | |
<script src="application.js"></script> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE> | |
<html> | |
<head> | |
<title>Forrest!</title> | |
<link rel="stylesheet" type="text/css" href="application.css"> | |
</head> | |
<body> | |
<div id='forrest'></div> | |
</body> | |
<script src="application.js"></script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment