Skip to content

Commit 93782a8

Browse files
author
Lysandre Costes
committed
Consolidate shared formatters and court constants in court.js
1 parent 60f58a1 commit 93782a8

6 files changed

Lines changed: 38 additions & 49 deletions

File tree

website/js/clutch.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
const playerSel = document.getElementById('clutch-player');
1111
const tipEl = document.getElementById('clutch-tooltip');
1212

13-
const { W, H, toX, toY, drawCourt } = Court;
13+
const { W, H, toX, toY, drawCourt, pct } = Court;
1414
const GRID = 25;
1515
const HEX_W = 50 / GRID;
1616
const HEX_H = HEX_W * Math.sqrt(3) / 2;
@@ -51,8 +51,6 @@
5151
.interpolator(t => d3.interpolateRdBu(1 - t))
5252
.clamp(true);
5353

54-
function pct(v) { return `${(v * 100).toFixed(1)}%`; }
55-
5654
function showTip(event, html, containerSvg = svg.node()) {
5755
const rect = containerSvg.closest('.section-viz').getBoundingClientRect();
5856
tipEl.style.left = `${event.clientX - rect.left + 12}px`;

website/js/court.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,32 @@ const Court = (() => {
8585
return g;
8686
}
8787

88-
return { SCALE, W, H, BX, BY, toX, toY, drawCourt };
88+
const pct = v => `${(v * 100).toFixed(1)}%`;
89+
90+
const TEAM_SHORT_NAMES = {
91+
'Golden State Warriors': 'Warriors',
92+
'Los Angeles Lakers': 'Lakers',
93+
'Los Angeles Clippers': 'Clippers',
94+
'Oklahoma City Thunder': 'Thunder',
95+
'Cleveland Cavaliers': 'Cavaliers',
96+
'Houston Rockets': 'Rockets',
97+
'Brooklyn Nets': 'Nets',
98+
'Philadelphia 76ers': '76ers',
99+
'Washington Wizards': 'Wizards',
100+
'San Antonio Spurs': 'Spurs',
101+
'Boston Celtics': 'Celtics',
102+
'Denver Nuggets': 'Nuggets',
103+
'Miami Heat': 'Heat',
104+
'Milwaukee Bucks': 'Bucks',
105+
'Phoenix Suns': 'Suns',
106+
'Dallas Mavericks': 'Mavs',
107+
'New York Knicks': 'Knicks',
108+
};
109+
const shortTeam = name => TEAM_SHORT_NAMES[name] || name;
110+
111+
return {
112+
SCALE, W, H, BX, BY,
113+
CORNER_Y_DATA, CORNER_SVG_Y, CORNER_SVG_XL, CORNER_SVG_XR, R3, FT_SVG_Y,
114+
toX, toY, drawCourt, pct, shortTeam,
115+
};
89116
})();

website/js/fingerprint.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
const wrapEl = document.querySelector('.fingerprint-wrap');
88

99
const C = Court;
10-
const CORNER_Y_DATA = 5.25 + Math.sqrt(23.75 ** 2 - 22 ** 2);
11-
const CORNER_SVG_Y = C.toY(CORNER_Y_DATA);
12-
const CORNER_XL = C.toX(-22);
13-
const CORNER_XR = C.toX(22);
14-
const FT_Y = C.toY(19);
15-
const R3_PX = 23.75 * C.SCALE;
10+
const CORNER_SVG_Y = C.CORNER_SVG_Y;
11+
const CORNER_XL = C.CORNER_SVG_XL;
12+
const CORNER_XR = C.CORNER_SVG_XR;
13+
const FT_Y = C.FT_SVG_Y;
14+
const R3_PX = C.R3;
1615
const RESTRICTED_R = 40;
1716
const restrictedCircle = `M ${C.BX},${C.BY} m -${RESTRICTED_R},0 a ${RESTRICTED_R},${RESTRICTED_R} 0 1,0 ${RESTRICTED_R * 2},0 a ${RESTRICTED_R},${RESTRICTED_R} 0 1,0 -${RESTRICTED_R * 2},0`;
1817
const paintRect = `M ${C.toX(-8)},${C.H} L ${C.toX(8)},${C.H} L ${C.toX(8)},${FT_Y} L ${C.toX(-8)},${FT_Y} Z`;
@@ -50,7 +49,7 @@
5049
Court.drawCourt(miniCourtSvg, { color: '#505070', opacity: 0.7, lw: 1 });
5150
const MINI_COURT_LINES = miniCourtSvg.select('.court-lines').node().outerHTML;
5251

53-
function pct(v) { return `${(v * 100).toFixed(1)}%`; }
52+
const pct = Court.pct;
5453

5554
function zoneInsight(d, overallFg) {
5655
if (!d.n) return 'No shots recorded in this zone.';

website/js/player-evolution.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,7 @@
3636
const innerW = W - margin.left - margin.right;
3737
const innerH = H - margin.top - margin.bottom;
3838

39-
function pct(v) { return `${(v * 100).toFixed(1)}%`; }
40-
function shortTeam(team) {
41-
return team
42-
.replace('Golden State Warriors', 'Warriors')
43-
.replace('Los Angeles Lakers', 'Lakers')
44-
.replace('Los Angeles Clippers', 'Clippers')
45-
.replace('Oklahoma City Thunder', 'Thunder')
46-
.replace('Cleveland Cavaliers', 'Cavaliers')
47-
.replace('Houston Rockets', 'Rockets')
48-
.replace('Brooklyn Nets', 'Nets')
49-
.replace('Philadelphia 76ers', '76ers')
50-
.replace('Washington Wizards', 'Wizards');
51-
}
39+
const { pct, shortTeam } = Court;
5240

5341
function zoneShare(stint, zone) {
5442
return stint.zones.find(z => z.zone === zone)?.share || 0;

website/js/revolution.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
const tipEl = document.getElementById('rev-tooltip');
1111
const mapWrap = svg.node().closest('.map-wrap');
1212

13-
const { W, H, toX, toY, drawCourt } = Court;
13+
const { W, H, toX, toY, drawCourt, pct } = Court;
1414
const GRID = 25;
1515
const HEX_W = 50 / GRID;
1616
const HEX_H = HEX_W * Math.sqrt(3) / 2;
@@ -28,8 +28,6 @@
2828
.interpolator(t => d3.interpolateRdBu(1 - t))
2929
.clamp(true);
3030

31-
function pct(v) { return `${(v * 100).toFixed(1)}%`; }
32-
3331
function showTip(event, html) {
3432
if (!tipEl || !mapWrap) return;
3533
const rect = mapWrap.getBoundingClientRect();

website/js/team.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,13 @@
4747
});
4848
seasonSel.value = teamsRaw.seasons[teamsRaw.seasons.length - 1];
4949

50-
function pct(v) { return `${(v * 100).toFixed(1)}%`; }
50+
const { pct, shortTeam } = Court;
5151
function ppDiff(teamShare, leagueShare) {
5252
const d = (teamShare - leagueShare) * 100;
5353
const sign = d > 0 ? '+' : '';
5454
return `${sign}${d.toFixed(1)} pp`;
5555
}
5656

57-
function shortTeam(name) {
58-
return name
59-
.replace('Golden State Warriors', 'Warriors')
60-
.replace('Los Angeles Lakers', 'Lakers')
61-
.replace('Los Angeles Clippers', 'Clippers')
62-
.replace('Oklahoma City Thunder', 'Thunder')
63-
.replace('Cleveland Cavaliers', 'Cavaliers')
64-
.replace('Houston Rockets', 'Rockets')
65-
.replace('Brooklyn Nets', 'Nets')
66-
.replace('Philadelphia 76ers', '76ers')
67-
.replace('Washington Wizards', 'Wizards')
68-
.replace('San Antonio Spurs', 'Spurs')
69-
.replace('Boston Celtics', 'Celtics')
70-
.replace('Denver Nuggets', 'Nuggets')
71-
.replace('Miami Heat', 'Heat')
72-
.replace('Milwaukee Bucks', 'Bucks')
73-
.replace('Phoenix Suns', 'Suns')
74-
.replace('Dallas Mavericks', 'Mavs')
75-
.replace('New York Knicks', 'Knicks');
76-
}
77-
7857
function populateTeams() {
7958
const season = seasonSel.value;
8059
const teams = teamsRaw.teams_by_season[season] || [];

0 commit comments

Comments
 (0)