Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save EncodeTheCode/a3da3210cc38216acc273524a0db0d52 to your computer and use it in GitHub Desktop.

Select an option

Save EncodeTheCode/a3da3210cc38216acc273524a0db0d52 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CS2 Tournament Bracket</title>
<style>
/* =========================================================
GLOBAL
========================================================= */
:root {
--bg: #080a0d;
--bg-2: #0d1014;
--panel: #11151a;
--panel-2: #171b21;
--panel-3: #1d2229;
--border: #2c333c;
--border-soft: #222830;
--text: #e8ebee;
--muted: #7c858f;
--gold: #d6a84f;
--gold-bright: #f0cf82;
--winner-bg: #344b38;
--winner-bg-2: #405d45;
--winner-border: #769b79;
--winner-text: #d9f0dc;
--dead-bg: #292d31;
--dead-border: #3a3e42;
--dead-text: #7e8388;
--connector: #414a55;
--connector-winner: #789a7b;
--connector-final: #b8954e;
--card-width: 260px;
--round-gap: 110px;
--match-gap: 32px;
--header-height: 74px;
}
/* =========================================================
RESET
========================================================= */
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
padding: 0;
min-width: 100%;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Arial,
Helvetica,
sans-serif;
}
body {
overflow-x: hidden;
}
/* =========================================================
PAGE
========================================================= */
.page {
min-height: 100vh;
background:
radial-gradient(
ellipse at top center,
rgba(214, 168, 79, 0.07),
transparent 48%
),
linear-gradient(
180deg,
#0b0e12,
#080a0d
);
}
/* =========================================================
HEADER
========================================================= */
.header {
position: sticky;
top: 0;
z-index: 100;
min-height: var(--header-height);
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 14px 24px;
background:
rgba(9, 11, 14, 0.96);
border-bottom:
1px solid var(--border);
backdrop-filter:
blur(12px);
}
.brand {
min-width: 0;
}
.title {
margin: 0;
font-size: 20px;
font-weight: 900;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.subtitle {
margin-top: 4px;
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
/* =========================================================
HEADER CONTROLS
========================================================= */
.controls {
display: flex;
align-items: center;
justify-content: flex-end;
flex-wrap: wrap;
gap: 7px;
}
.button {
appearance: none;
border: 1px solid var(--border);
background: #15191f;
color: #c8cdd3;
padding: 8px 12px;
border-radius: 2px;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.06em;
text-transform: uppercase;
cursor: pointer;
transition:
background 0.12s ease,
border-color 0.12s ease,
color 0.12s ease;
}
.button:hover {
background: #20252c;
border-color: #555e68;
color: white;
}
.button.active {
background:
rgba(214, 168, 79, 0.13);
border-color:
var(--gold);
color:
var(--gold-bright);
}
.button.reset {
border-color:
#754747;
color:
#d59696;
}
.button.reset:hover {
background:
rgba(120, 55, 55, 0.18);
border-color:
#a06161;
color:
#efb0b0;
}
/* =========================================================
MODE INDICATOR
========================================================= */
.mode {
display: flex;
align-items: center;
gap: 7px;
height: 32px;
padding: 0 10px;
border:
1px solid var(--border);
background:
#11151a;
color:
var(--muted);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.mode-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background:
#686e74;
}
.mode.interactive {
color:
#b9d8bc;
border-color:
rgba(118, 155, 121, 0.6);
}
.mode.interactive .mode-dot {
background:
#79b77e;
box-shadow:
0 0 9px rgba(121, 183, 126, 0.7);
}
/* =========================================================
LAYOUT NAV
========================================================= */
.layout-nav {
display: flex;
align-items: center;
justify-content: center;
gap: 6px;
padding: 12px 20px;
border-bottom:
1px solid rgba(255,255,255,0.04);
background:
rgba(10, 13, 17, 0.8);
}
.layout-label {
margin-right: 7px;
color:
#68717b;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
/* =========================================================
BRACKET VIEWPORT
========================================================= */
.bracket-viewport {
width: 100%;
overflow-x: auto;
overflow-y: hidden;
padding:
55px 45px 100px;
}
.bracket-viewport::-webkit-scrollbar {
height: 9px;
}
.bracket-viewport::-webkit-scrollbar-track {
background:
#0b0e12;
}
.bracket-viewport::-webkit-scrollbar-thumb {
background:
#353c45;
border-radius:
20px;
}
.bracket-viewport::-webkit-scrollbar-thumb:hover {
background:
#4b535e;
}
/* =========================================================
BRACKET ROOT
========================================================= */
.bracket {
position: relative;
width: max-content;
min-width: 100%;
min-height: 650px;
}
/* =========================================================
CONNECTOR SVG
========================================================= */
.connector-layer {
position: absolute;
top: 0;
left: 0;
z-index: 1;
width: 100%;
height: 100%;
pointer-events: none;
overflow: visible;
}
.connector {
fill: none;
stroke:
var(--connector);
stroke-width: 2;
vector-effect:
non-scaling-stroke;
stroke-linecap:
square;
stroke-linejoin:
miter;
}
.connector.winner {
stroke:
var(--connector-winner);
stroke-width:
2.5;
}
.connector.final {
stroke:
var(--connector-final);
stroke-width:
2.5;
}
/* =========================================================
ROUND
========================================================= */
.round {
position: relative;
z-index: 2;
flex: 0 0 var(--card-width);
width: var(--card-width);
}
.round-title {
height: 28px;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 14px;
color:
#777f89;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.round-matches {
display: flex;
flex-direction: column;
gap: var(--match-gap);
}
/* =========================================================
CLASSIC LAYOUT
========================================================= */
.bracket.classic {
display: flex;
align-items: flex-start;
gap: var(--round-gap);
padding:
0 20px;
}
.bracket.classic .round:nth-child(2) {
padding-top: 72px;
}
.bracket.classic .round:nth-child(3) {
padding-top: 216px;
}
/* =========================================================
MATCH
========================================================= */
.match {
position: relative;
width: var(--card-width);
overflow: hidden;
background:
linear-gradient(
180deg,
#191e24,
#12161b
);
border:
1px solid var(--border);
border-radius:
3px;
box-shadow:
0 12px 28px rgba(0,0,0,0.32);
}
.match-header {
height: 25px;
display: flex;
align-items: center;
justify-content: space-between;
padding:
0 9px;
background:
#101318;
border-bottom:
1px solid rgba(255,255,255,0.055);
color:
#747d87;
font-size:
9px;
font-weight:
900;
letter-spacing:
0.08em;
text-transform:
uppercase;
}
.match-id {
opacity: 0.65;
}
/* =========================================================
TEAM ROW
========================================================= */
.team {
position: relative;
min-height: 49px;
display: flex;
align-items: center;
padding:
0 10px;
background:
#181c22;
border-bottom:
1px solid rgba(255,255,255,0.055);
transition:
background 0.12s ease,
transform 0.12s ease;
}
.team:last-of-type {
border-bottom: 0;
}
.team.interactive {
cursor:
pointer;
}
.team.interactive:hover {
background:
#242a31;
transform:
translateX(2px);
}
.team-logo {
width: 28px;
height: 28px;
flex: 0 0 28px;
display: flex;
align-items: center;
justify-content: center;
margin-right: 9px;
background:
#0e1115;
border:
1px solid #343b44;
color:
#b7bec6;
font-size:
9px;
font-weight:
900;
overflow:
hidden;
}
.team-logo img {
width: 100%;
height: 100%;
display: block;
object-fit: contain;
}
.team-info {
min-width: 0;
flex: 1;
}
.team-name {
overflow:
hidden;
white-space:
nowrap;
text-overflow:
ellipsis;
color:
#e2e6ea;
font-size:
12px;
font-weight:
800;
}
.team-seed {
margin-right:
6px;
color:
#69727c;
font-size:
9px;
font-weight:
900;
}
.team-score {
min-width:
24px;
margin-left:
8px;
color:
#e2e6ea;
font-size:
14px;
font-weight:
900;
text-align:
right;
}
/* =========================================================
WINNER
========================================================= */
.team.winner {
background:
linear-gradient(
90deg,
var(--winner-bg-2),
var(--winner-bg)
);
border-color:
rgba(118,155,121,0.42);
}
.team.winner .team-name {
color:
var(--winner-text);
}
.team.winner .team-score {
color:
#e4f3e6;
}
.team.winner .team-logo {
border-color:
var(--winner-border);
}
/* =========================================================
ELIMINATED
========================================================= */
.team.eliminated {
background:
var(--dead-bg);
border-color:
var(--dead-border);
filter:
saturate(0.45);
}
.team.eliminated .team-name {
color:
var(--dead-text);
text-decoration:
line-through;
text-decoration-thickness:
1px;
text-decoration-color:
#656a6e;
/*
Embossed / debossed grey appearance.
*/
text-shadow:
1px 1px 0 #45494d,
-1px -1px 0 rgba(255,255,255,0.055);
}
.team.eliminated .team-score {
color:
#6d7277;
text-shadow:
1px 1px 0 #464a4e;
}
.team.eliminated .team-logo {
filter:
grayscale(1);
opacity:
0.48;
}
/* =========================================================
TBD
========================================================= */
.team.tbd {
color:
#666e77;
background:
#15191e;
}
.team.tbd .team-name {
color:
#646b73;
font-style:
italic;
}
/* =========================================================
CHAMPION
========================================================= */
.champion {
min-height:
53px;
display:
flex;
flex-direction:
column;
align-items:
center;
justify-content:
center;
gap:
3px;
background:
linear-gradient(
180deg,
rgba(214,168,79,0.15),
rgba(214,168,79,0.045)
);
border-top:
1px solid rgba(214,168,79,0.35);
color:
var(--gold);
font-size:
9px;
font-weight:
900;
letter-spacing:
0.13em;
text-transform:
uppercase;
}
.champion-name {
color:
var(--gold-bright);
font-size:
13px;
letter-spacing:
0;
text-transform:
none;
}
/* =========================================================
CENTERED LAYOUT
========================================================= */
.bracket.centered {
display:
grid;
grid-template-columns:
minmax(var(--card-width), 1fr)
var(--card-width)
minmax(var(--card-width), 1fr);
align-items:
center;
gap:
70px;
min-width:
1250px;
padding:
20px 50px;
}
.center-side {
display:
flex;
flex-direction:
column;
gap:
42px;
}
.center-side.left {
align-items:
flex-end;
}
.center-side.right {
align-items:
flex-start;
}
.center-group {
display:
flex;
flex-direction:
column;
gap:
18px;
}
.center-final {
display:
flex;
flex-direction:
column;
align-items:
center;
justify-content:
center;
}
/* =========================================================
COMPACT
========================================================= */
.bracket.compact {
display:
flex;
align-items:
center;
justify-content:
center;
gap:
55px;
min-width:
1050px;
padding:
25px 30px;
}
.bracket.compact .round-matches {
gap:
18px;
}
.bracket.compact .round:nth-child(2) {
padding-top:
50px;
}
.bracket.compact .round:nth-child(3) {
padding-top:
100px;
}
.bracket.compact .match {
transform:
scale(0.92);
transform-origin:
center;
}
/* =========================================================
SPECTATOR MODE
========================================================= */
body.spectator .team.interactive {
cursor:
default;
}
body.spectator .team.interactive:hover {
background:
inherit;
transform:
none;
}
/* =========================================================
EMPTY STATE
========================================================= */
.empty {
width:
100%;
min-height:
500px;
display:
flex;
align-items:
center;
justify-content:
center;
color:
#5e6670;
font-size:
12px;
}
/* =========================================================
RESPONSIVE
========================================================= */
@media (max-width: 900px) {
.header {
align-items:
flex-start;
flex-direction:
column;
}
.controls {
justify-content:
flex-start;
}
.layout-nav {
justify-content:
flex-start;
overflow-x:
auto;
}
.bracket-viewport {
padding:
35px 20px 80px;
}
}
</style>
</head>
<body>
<div class="page">
<!-- =====================================================
HEADER
====================================================== -->
<header class="header">
<div class="brand">
<h1
class="title"
id="tournamentTitle"
>
CS2 Championship
</h1>
<div
class="subtitle"
id="tournamentSubtitle"
>
Playoffs · Single Elimination
</div>
</div>
<div class="controls">
<div
class="mode"
id="modeIndicator"
>
<span class="mode-dot"></span>
<span id="modeText">
Spectator
</span>
</div>
<button
type="button"
class="button"
id="interactiveButton"
>
Interactive Mode
</button>
<button
type="button"
class="button reset"
id="resetButton"
>
Reset Tournament
</button>
</div>
</header>
<!-- =====================================================
LAYOUT SELECTOR
====================================================== -->
<nav class="layout-nav">
<span class="layout-label">
Layout
</span>
<button
type="button"
class="button active"
data-layout="classic"
>
Classic
</button>
<button
type="button"
class="button"
data-layout="centered"
>
Centered
</button>
<button
type="button"
class="button"
data-layout="compact"
>
Compact
</button>
</nav>
<!-- =====================================================
BRACKET
====================================================== -->
<main class="bracket-viewport">
<div
id="bracket"
class="bracket classic"
></div>
</main>
</div>
<script>
/* =========================================================
CONFIGURATION
=========================================================
CHANGE THESE VALUES.
========================================================= */
const CONFIG = {
/*
true = clicking teams is enabled
false = spectator mode
You can change this directly before loading
the page.
*/
interactiveMode: true,
tournamentTitle:
"IEM CS2 Championship",
tournamentSubtitle:
"Playoffs · Single Elimination",
bestOf:
"BO3",
finalBestOf:
"BO5",
cardWidth:
260,
roundGap:
110,
matchGap:
32,
showSeeds:
true,
showScores:
true
};
/* =========================================================
CURRENT LAYOUT
========================================================= */
let CURRENT_LAYOUT = "classic";
/* =========================================================
TEAM DATABASE
=========================================================
Each team exists ONCE.
Matches reference these IDs.
This is what keeps the amount of duplicated
data low.
========================================================= */
const TEAMS = {
navi: {
id: "navi",
name: "Natus Vincere",
seed: 1,
logo: "",
color: "#e4d449"
},
spirit: {
id: "spirit",
name: "Spirit",
seed: 2,
logo: "",
color: "#d83b3b"
},
vitality: {
id: "vitality",
name: "Vitality",
seed: 3,
logo: "",
color: "#e4bd42"
},
g2: {
id: "g2",
name: "G2 Esports",
seed: 4,
logo: "",
color: "#eeeeee"
},
faze: {
id: "faze",
name: "FaZe",
seed: 5,
logo: "",
color: "#d83232"
},
mouz: {
id: "mouz",
name: "MOUZ",
seed: 6,
logo: "",
color: "#d8a43b"
},
astralis: {
id: "astralis",
name: "Astralis",
seed: 7,
logo: "",
color: "#d71920"
},
liquid: {
id: "liquid",
name: "Liquid",
seed: 8,
logo: "",
color: "#5d8ed8"
}
};
/* =========================================================
INITIAL TOURNAMENT
=========================================================
IMPORTANT:
This object is NEVER modified.
Reset always clones this object.
========================================================= */
const INITIAL_TOURNAMENT = {
rounds: [
{
id: "quarterfinals",
name: "Quarterfinals",
matches: [
{
id: "qf1",
teams: ["navi", "faze"],
scores: [0, 0],
winner: null,
bestOf: "BO3"
},
{
id: "qf2",
teams: ["spirit", "g2"],
scores: [0, 0],
winner: null,
bestOf: "BO3"
},
{
id: "qf3",
teams: ["vitality", "astralis"],
scores: [0, 0],
winner: null,
bestOf: "BO3"
},
{
id: "qf4",
teams: ["mouz", "liquid"],
scores: [0, 0],
winner: null,
bestOf: "BO3"
}
]
},
{
id: "semifinals",
name: "Semifinals",
matches: [
{
id: "sf1",
teams: [null, null],
scores: [0, 0],
winner: null,
bestOf: "BO3"
},
{
id: "sf2",
teams: [null, null],
scores: [0, 0],
winner: null,
bestOf: "BO3"
}
]
},
{
id: "grand-final",
name: "Grand Final",
matches: [
{
id: "final",
teams: [null, null],
scores: [0, 0],
winner: null,
bestOf: "BO5"
}
]
}
]
};
/* =========================================================
LIVE TOURNAMENT STATE
========================================================= */
let tournament = null;
/* =========================================================
CLONE
========================================================= */
function clone(value) {
return JSON.parse(
JSON.stringify(value)
);
}
/* =========================================================
GET TEAM
========================================================= */
function getTeam(teamId) {
if (!teamId) {
return null;
}
return TEAMS[teamId] || null;
}
/* =========================================================
GET WINNER
========================================================= */
function winnerId(match) {
return match && match.winner
? match.winner
: null;
}
/* =========================================================
FIND MATCH
========================================================= */
function findMatch(matchId) {
for (
const round of tournament.rounds
) {
const match =
round.matches.find(
item =>
item.id === matchId
);
if (match) {
return match;
}
}
return null;
}
/* =========================================================
RESET
========================================================= */
function resetTournament() {
tournament =
clone(
INITIAL_TOURNAMENT
);
synchronizeBracket();
render();
}
/* =========================================================
SYNCHRONIZE BRACKET
=========================================================
This is the important part.
Winners are copied forward automatically.
========================================================= */
function synchronizeBracket() {
/*
Start with a clean copy of the
original team slots for rounds after
the quarterfinals.
This prevents stale teams from remaining
after a reset or after changing a winner.
*/
for (
let roundIndex = 1;
roundIndex < tournament.rounds.length;
roundIndex++
) {
for (
const match
of tournament.rounds[roundIndex].matches
) {
/*
Preserve the winner if the match
itself has already been decided,
but rebuild its input teams below.
*/
match.teams =
[null, null];
}
}
/*
Propagate every previous round.
*/
for (
let roundIndex = 0;
roundIndex < tournament.rounds.length - 1;
roundIndex++
) {
const currentRound =
tournament.rounds[
roundIndex
];
const nextRound =
tournament.rounds[
roundIndex + 1
];
currentRound.matches.forEach(
(match, matchIndex) => {
const winner =
winnerId(match);
if (!winner) {
return;
}
const nextMatchIndex =
Math.floor(
matchIndex / 2
);
const nextSlot =
matchIndex % 2;
const nextMatch =
nextRound.matches[
nextMatchIndex
];
if (!nextMatch) {
return;
}
nextMatch.teams[
nextSlot
] = winner;
}
);
}
/*
If a team disappears from a match because
an earlier result changed, clear the winner
of that match.
Example:
A beats B -> semifinal
Later you reset/change the earlier match.
The semifinal winner must no longer remain.
*/
for (
let roundIndex = 1;
roundIndex < tournament.rounds.length;
roundIndex++
) {
const round =
tournament.rounds[
roundIndex
];
round.matches.forEach(
match => {
if (
match.winner &&
!match.teams.includes(
match.winner
)
) {
match.winner =
null;
match.scores =
[0, 0];
}
}
);
}
/*
The previous pass may have cleared a winner,
so do one more propagation pass.
*/
for (
let roundIndex = 0;
roundIndex < tournament.rounds.length - 1;
roundIndex++
) {
const currentRound =
tournament.rounds[
roundIndex
];
const nextRound =
tournament.rounds[
roundIndex + 1
];
currentRound.matches.forEach(
(match, matchIndex) => {
const winner =
winnerId(match);
if (!winner) {
return;
}
const nextMatch =
nextRound.matches[
Math.floor(
matchIndex / 2
)
];
if (!nextMatch) {
return;
}
nextMatch.teams[
matchIndex % 2
] = winner;
}
);
}
}
/* =========================================================
GET TEAM STATUS
========================================================= */
function getTeamStatus(
match,
teamId
) {
if (!teamId) {
return "tbd";
}
if (!match.winner) {
return "active";
}
if (
match.winner === teamId
) {
return "winner";
}
return "eliminated";
}
/* =========================================================
TEAM HTML
========================================================= */
function createTeamHTML(
match,
teamId,
slot
) {
if (!teamId) {
return `
<div class="team tbd">
<div class="team-logo">—</div>
<div class="team-info">
<div class="team-name">
TBD
</div>
</div>
<div class="team-score"></div>
</div>
`;
}
const team =
getTeam(teamId);
if (!team) {
return `
<div class="team tbd">
<div class="team-logo">?</div>
<div class="team-info">
<div class="team-name">
Unknown Team
</div>
</div>
<div class="team-score"></div>
</div>
`;
}
const status =
getTeamStatus(
match,
teamId
);
const initials =
team.name
.replace(/[^a-zA-Z0-9]/g, "")
.substring(0, 2)
.toUpperCase();
const logo =
team.logo
?
`<img
src="${team.logo}"
alt="${team.name}"
>`
:
initials;
const seed =
CONFIG.showSeeds && team.seed
?
`<span class="team-seed">
#${team.seed}
</span>`
:
"";
const score =
CONFIG.showScores
?
`<div class="team-score">
${match.scores[slot] ?? 0}
</div>`
:
`<div class="team-score"></div>`;
const interactive =
CONFIG.interactiveMode
? "interactive"
: "";
return `
<div
class="team ${status} ${interactive}"
data-team-id="${team.id}"
data-match-id="${match.id}"
>
<div
class="team-logo"
style="
border-top: 2px solid ${team.color};
"
>
${logo}
</div>
<div class="team-info">
<div class="team-name">
${seed}
${team.name}
</div>
</div>
${score}
</div>
`;
}
/* =========================================================
MATCH HTML
========================================================= */
function createMatchHTML(
match,
isFinal = false
) {
const finalChampion =
isFinal &&
match.winner
? getTeam(match.winner)
: null;
return `
<article
class="match"
data-match-id="${match.id}"
>
<div class="match-header">
<span>
${match.bestOf}
</span>
<span class="match-id">
${match.id.toUpperCase()}
</span>
</div>
${createTeamHTML(
match,
match.teams[0],
0
)}
${createTeamHTML(
match,
match.teams[1],
1
)}
${
finalChampion
?
`
<div class="champion">
🏆 CHAMPION
<span class="champion-name">
${finalChampion.name}
</span>
</div>
`
:
""
}
</article>
`;
}
/* =========================================================
RENDER CLASSIC
========================================================= */
function renderClassic() {
const bracket =
document.getElementById(
"bracket"
);
bracket.className =
"bracket classic";
tournament.rounds.forEach(
(round, roundIndex) => {
const roundElement =
document.createElement(
"section"
);
roundElement.className =
"round";
roundElement.innerHTML = `
<div class="round-title">
${round.name}
</div>
<div class="round-matches">
</div>
`;
const matches =
roundElement.querySelector(
".round-matches"
);
round.matches.forEach(
match => {
matches.insertAdjacentHTML(
"beforeend",
createMatchHTML(
match,
roundIndex ===
tournament.rounds.length - 1
)
);
}
);
bracket.appendChild(
roundElement
);
}
);
}
/* =========================================================
RENDER CENTERED
========================================================= */
function renderCentered() {
const bracket =
document.getElementById(
"bracket"
);
bracket.className =
"bracket centered";
const left =
document.createElement(
"div"
);
left.className =
"center-side left";
const right =
document.createElement(
"div"
);
right.className =
"center-side right";
/*
Quarterfinals on left.
*/
const qf =
tournament.rounds[0];
const qfGroup =
document.createElement(
"div"
);
qfGroup.className =
"center-group";
qfGroup.innerHTML = `
<div class="round-title">
${qf.name}
</div>
`;
qf.matches.forEach(
match => {
qfGroup.insertAdjacentHTML(
"beforeend",
createMatchHTML(match)
);
}
);
left.appendChild(
qfGroup
);
/*
Semifinals on right.
*/
const sf =
tournament.rounds[1];
const sfGroup =
document.createElement(
"div"
);
sfGroup.className =
"center-group";
sfGroup.innerHTML = `
<div class="round-title">
${sf.name}
</div>
`;
sf.matches.forEach(
match => {
sfGroup.insertAdjacentHTML(
"beforeend",
createMatchHTML(match)
);
}
);
right.appendChild(
sfGroup
);
/*
Final in center.
*/
const finalRound =
tournament.rounds[2];
const finalMatch =
finalRound.matches[0];
const center =
document.createElement(
"div"
);
center.className =
"center-final";
center.innerHTML = `
<div class="round-title">
${finalRound.name}
</div>
${createMatchHTML(
finalMatch,
true
)}
`;
bracket.appendChild(left);
bracket.appendChild(center);
bracket.appendChild(right);
}
/* =========================================================
RENDER COMPACT
========================================================= */
function renderCompact() {
const bracket =
document.getElementById(
"bracket"
);
bracket.className =
"bracket compact";
tournament.rounds.forEach(
(round, roundIndex) => {
const roundElement =
document.createElement(
"section"
);
roundElement.className =
"round";
roundElement.innerHTML = `
<div class="round-title">
${round.name}
</div>
<div class="round-matches">
</div>
`;
const matches =
roundElement.querySelector(
".round-matches"
);
round.matches.forEach(
match => {
matches.insertAdjacentHTML(
"beforeend",
createMatchHTML(
match,
roundIndex ===
tournament.rounds.length - 1
)
);
}
);
bracket.appendChild(
roundElement
);
}
);
}
/* =========================================================
CREATE SVG CONNECTOR LAYER
========================================================= */
function createConnectorLayer() {
const bracket =
document.getElementById(
"bracket"
);
const old =
bracket.querySelector(
".connector-layer"
);
if (old) {
old.remove();
}
const svg =
document.createElementNS(
"http://www.w3.org/2000/svg",
"svg"
);
svg.classList.add(
"connector-layer"
);
bracket.prepend(
svg
);
return svg;
}
/* =========================================================
GET RECT
========================================================= */
function relativeRect(
element,
parent
) {
const rect =
element.getBoundingClientRect();
const parentRect =
parent.getBoundingClientRect();
return {
left:
rect.left -
parentRect.left,
right:
rect.right -
parentRect.left,
top:
rect.top -
parentRect.top,
bottom:
rect.bottom -
parentRect.top,
centerY:
rect.top -
parentRect.top +
rect.height / 2
};
}
/* =========================================================
DRAW CONNECTOR
========================================================= */
function drawConnector(
svg,
fromElement,
toElement,
bracket,
className
) {
if (
!fromElement ||
!toElement
) {
return;
}
const from =
relativeRect(
fromElement,
bracket
);
const to =
relativeRect(
toElement,
bracket
);
const startX =
from.right;
const endX =
to.left;
/*
The midpoint automatically adapts to
the current layout.
This is what prevents the lines from
breaking when card positions change.
*/
const middleX =
startX +
(endX - startX) / 2;
const path =
document.createElementNS(
"http://www.w3.org/2000/svg",
"path"
);
const d = `
M ${startX} ${from.centerY}
L ${middleX} ${from.centerY}
L ${middleX} ${to.centerY}
L ${endX} ${to.centerY}
`;
path.setAttribute(
"d",
d
);
path.setAttribute(
"class",
className || "connector"
);
svg.appendChild(
path
);
}
/* =========================================================
DRAW ALL CONNECTORS
========================================================= */
function drawConnectors() {
const bracket =
document.getElementById(
"bracket"
);
if (!bracket) {
return;
}
const svg =
createConnectorLayer();
const width =
Math.max(
bracket.scrollWidth,
bracket.offsetWidth
);
const height =
Math.max(
bracket.scrollHeight,
bracket.offsetHeight
);
svg.setAttribute(
"width",
width
);
svg.setAttribute(
"height",
height
);
svg.setAttribute(
"viewBox",
`0 0 ${width} ${height}`
);
/*
Connect each match to its next-round
match.
This uses actual DOM positions rather
than hardcoded coordinates.
*/
for (
let roundIndex = 0;
roundIndex < tournament.rounds.length - 1;
roundIndex++
) {
const currentRound =
tournament.rounds[
roundIndex
];
const nextRound =
tournament.rounds[
roundIndex + 1
];
currentRound.matches.forEach(
(match, matchIndex) => {
const nextMatchIndex =
Math.floor(
matchIndex / 2
);
const nextMatch =
nextRound.matches[
nextMatchIndex
];
if (!nextMatch) {
return;
}
const from =
bracket.querySelector(
`[data-match-id="${match.id}"]`
);
const to =
bracket.querySelector(
`[data-match-id="${nextMatch.id}"]`
);
if (!from || !to) {
return;
}
let connectorClass =
"connector";
if (
match.winner &&
nextMatch.teams.includes(
match.winner
)
) {
connectorClass +=
" winner";
}
if (
roundIndex ===
tournament.rounds.length - 2
) {
connectorClass +=
" final";
}
drawConnector(
svg,
from,
to,
bracket,
connectorClass
);
}
);
}
}
/* =========================================================
CLICK TEAM
========================================================= */
function handleTeamClick(
event
) {
if (!CONFIG.interactiveMode) {
return;
}
const team =
event.currentTarget;
const teamId =
team.dataset.teamId;
const matchId =
team.dataset.matchId;
if (!teamId || !matchId) {
return;
}
const match =
findMatch(
matchId
);
if (!match) {
return;
}
/*
A TBD slot cannot be selected.
*/
if (
!match.teams.includes(
teamId
)
) {
return;
}
/*
Clicking the current winner again
removes the result.
This is useful when correcting mistakes.
*/
if (
match.winner ===
teamId
) {
match.winner =
null;
match.scores =
[0, 0];
} else {
/*
Select the clicked team as winner.
*/
match.winner =
teamId;
/*
Simple placeholder score.
Winner gets 1.
You can replace this with real
match score entry later.
*/
const winnerSlot =
match.teams.findIndex(
id =>
id === teamId
);
const loserSlot =
winnerSlot === 0
? 1
: 0;
match.scores =
[0, 0];
match.scores[
winnerSlot
] = 1;
match.scores[
loserSlot
] = 0;
}
/*
Rebuild everything after every click.
*/
synchronizeBracket();
render();
}
/* =========================================================
ATTACH EVENTS
========================================================= */
function attachEvents() {
document
.querySelectorAll(
".team[data-team-id]"
)
.forEach(
team => {
if (
CONFIG.interactiveMode
) {
team.addEventListener(
"click",
handleTeamClick
);
}
}
);
}
/* =========================================================
UPDATE MODE
========================================================= */
function updateModeUI() {
const indicator =
document.getElementById(
"modeIndicator"
);
const modeText =
document.getElementById(
"modeText"
);
const button =
document.getElementById(
"interactiveButton"
);
if (
CONFIG.interactiveMode
) {
document.body.classList.remove(
"spectator"
);
indicator.classList.add(
"interactive"
);
modeText.textContent =
"Interactive";
button.classList.add(
"active"
);
button.textContent =
"Interactive: ON";
} else {
document.body.classList.add(
"spectator"
);
indicator.classList.remove(
"interactive"
);
modeText.textContent =
"Spectator";
button.classList.remove(
"active"
);
button.textContent =
"Interactive Mode";
}
}
/* =========================================================
RENDER
========================================================= */
function render() {
const bracket =
document.getElementById(
"bracket"
);
bracket.innerHTML =
"";
document.documentElement.style
.setProperty(
"--card-width",
`${CONFIG.cardWidth}px`
);
document.documentElement.style
.setProperty(
"--round-gap",
`${CONFIG.roundGap}px`
);
document.documentElement.style
.setProperty(
"--match-gap",
`${CONFIG.matchGap}px`
);
document.getElementById(
"tournamentTitle"
).textContent =
CONFIG.tournamentTitle;
document.getElementById(
"tournamentSubtitle"
).textContent =
CONFIG.tournamentSubtitle;
updateModeUI();
switch (
CURRENT_LAYOUT
) {
case "centered":
renderCentered();
break;
case "compact":
renderCompact();
break;
case "classic":
default:
renderClassic();
break;
}
attachEvents();
/*
Wait until the browser has finished
positioning the cards before measuring
them for connector lines.
*/
requestAnimationFrame(
() => {
requestAnimationFrame(
() => {
drawConnectors();
}
);
}
);
}
/* =========================================================
CHANGE LAYOUT
========================================================= */
function setLayout(
layout
) {
const validLayouts = [
"classic",
"centered",
"compact"
];
if (
!validLayouts.includes(
layout
)
) {
return;
}
CURRENT_LAYOUT =
layout;
document
.querySelectorAll(
"[data-layout]"
)
.forEach(
button => {
button.classList.toggle(
"active",
button.dataset.layout ===
layout
);
}
);
render();
}
/* =========================================================
CHANGE INTERACTIVE MODE
========================================================= */
function setInteractiveMode(
enabled
) {
CONFIG.interactiveMode =
Boolean(enabled);
render();
}
/* =========================================================
BUTTON EVENTS
========================================================= */
document
.getElementById(
"interactiveButton"
)
.addEventListener(
"click",
() => {
setInteractiveMode(
!CONFIG.interactiveMode
);
}
);
document
.getElementById(
"resetButton"
)
.addEventListener(
"click",
() => {
const confirmed =
window.confirm(
"Reset the tournament back to its original starting state?"
);
if (!confirmed) {
return;
}
resetTournament();
}
);
document
.querySelectorAll(
"[data-layout]"
)
.forEach(
button => {
button.addEventListener(
"click",
() => {
setLayout(
button.dataset.layout
);
}
);
}
);
/* =========================================================
RESIZE
========================================================= */
let resizeTimer = null;
window.addEventListener(
"resize",
() => {
clearTimeout(
resizeTimer
);
resizeTimer =
setTimeout(
() => {
drawConnectors();
},
50
);
}
);
/* =========================================================
PUBLIC API
========================================================= */
window.TournamentBracket = {
reset() {
resetTournament();
},
render() {
render();
},
setLayout(
layout
) {
setLayout(
layout
);
},
setInteractiveMode(
enabled
) {
setInteractiveMode(
enabled
);
},
getState() {
return clone(
tournament
);
}
};
/* =========================================================
INITIALIZE
========================================================= */
resetTournament();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment