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

add a graph with scores over time #31

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 26 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
height: 100%;
}

#pongCanvas {
canvas {
display: block;
border-radius: 4px;
overflow: hidden;
Expand Down Expand Up @@ -71,6 +71,7 @@
<div id="container">
<canvas id="pongCanvas" width="800" height="800"></canvas>
<div id="score"></div>
<canvas id="graph" width="1200" height="150"></canvas>
<p id="made">
made by <a href="https://koenvangilst.nl">Koen van Gilst</a> | source on
<a href="https://github.com/vnglst/pong-wars">github</a>
Expand All @@ -92,7 +93,9 @@
// Idea for Pong wars: https://twitter.com/nicolasdnl/status/1749715070928433161

const canvas = document.getElementById("pongCanvas");
const graph = document.getElementById("graph");
const ctx = canvas.getContext("2d");
const ctxGraph = graph.getContext("2d", { willReadFrequently: true });
const scoreElement = document.getElementById("score");

const DAY_COLOR = colorPalette.MysticMint;
Expand Down Expand Up @@ -192,6 +195,28 @@
}

scoreElement.textContent = `day ${dayScore} | night ${nightScore}`;
if (iteration % 10 !== 0) {
return;
}

const dayPart = graph.height * dayScore / (numSquaresX * numSquaresY);

const imageData = ctxGraph.getImageData(1, 0, graph.width, graph.height);
ctxGraph.putImageData(imageData, 0, 0);

ctxGraph.strokeStyle = DAY_COLOR;
ctxGraph.beginPath();
ctxGraph.moveTo(graph.width, 0);
ctxGraph.lineTo(graph.width, dayPart);
ctxGraph.lineWidth = 2;
ctxGraph.stroke();

ctxGraph.strokeStyle = NIGHT_COLOR;
ctxGraph.beginPath();
ctxGraph.moveTo(graph.width, dayPart);
ctxGraph.lineTo(graph.width, graph.height);
ctxGraph.lineWidth = 2;
ctxGraph.stroke();
}

function checkBoundaryCollision(x, y, dx, dy) {
Expand Down