Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Buttons #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions game.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ var G_MATRIX = {
// relative to the beginning of the canvas.

refresh_rate: 1000 / 60, // speed of evolution of the game (in ms)

isRunning : false,

initialState: true
},

get_cell_value: function (i, j) {
Expand Down Expand Up @@ -147,9 +151,13 @@ window.requestAnimFrame = (function(callback) {

function animate (canvas) {

var canvas = document.getElementById("myCanvas");

var context = canvas.getContext('2d');

context.clearRect(0, 0, canvas.width, canvas.height);
if (G_MATRIX.config.isRunning){
context.clearRect(0, 0, canvas.width, canvas.height);


G_MATRIX.draw(context);

Expand All @@ -159,16 +167,36 @@ function animate (canvas) {
requestAnimFrame(function() {
animate(canvas);
});

} else {
context.clearRect(0, 0, canvas.width, canvas.height);
G_MATRIX.draw(context);
G_MATRIX.next_iteration();
}
}


window.onload = function () {
var canvas = document.getElementById('myCanvas');

// random initial state
//G_MATRIX.blink();
G_MATRIX.init();

// launch animation
function play() {
if (G_MATRIX.config.initialState) {
var canvas = document.getElementById("myCanvas");
G_MATRIX.init();
G_MATRIX.config.isRunning = true;
animate(canvas);
G_MATRIX.config.initialState = false;
} else {
var canvas = document.getElementById("myCanvas");
G_MATRIX.next_iteration();
G_MATRIX.config.isRunning = true;
animate(canvas);
}
}

function pause() {
var canvas = document.getElementById("myCanvas");
G_MATRIX.config.isRunning = false;
animate(canvas);
};
}

document.getElementById("playButton").addEventListener("click", play);
document.getElementById("pauseButton").addEventListener("click", pause);
document.getElementById("resetButton").addEventListener("click", function () {location.reload();
});
29 changes: 27 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,28 @@
<meta charset="utf-8" />
<title>Conway's Game of Life</title>
<style type="text/css" media="screen">
#myCanvas{
border: 1px solid #9c9898;

#myCanvas {
border: 1px solid #9c9898;
}

body {
display: flex;
flex-direction: row;
justify-content: center;
height: 100vh;
}

main {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 400px;
}

canvas {
margin-bottom: 2rem;
}
</style>
<script type="text/javascript" src="game.js"></script>
Expand All @@ -14,5 +34,10 @@
<body>
<h1>Conway's Game of Life</h1>
<canvas id="myCanvas" width="1000" height="750"></canvas>
<div>
<button id="playButton">Play</button>
<button id="pauseButton">Pause</button>
<button id="resetButton">Reset</button>
</div>
</body>
</html>