All the shots and FT attempts in one animation made with NBA spatio-temporal data (maintained by neilmj) and paper.js.The data is from Golden State Warriors vs Denver Nuggets on January 13th 2016.
I took the following simple two steps.
-
Data cooking with Python
-
Animation with Paper.js
Data cooking with Python
The final goal of this step is to generate a JSON file for Paper.js.Concretely I like to have Let’s first get the data.
1 |
|
In the next snippet, we are going to call a play by play API from stats.nba.com/.
Play by play data contains how each event ends up like 2PT attemps, rebound, turnover or substitution.We are going to use this information to get only field goal and free throw attempts.
1 |
|
json_df has X, Y coordinates of all the players per observation.Each row is a time (recorded every 0.1 - 0.2 sec).Column zero is a X coordinate of player 1 (pids[0]), column one is a Y coordinate of the same player, column two is a X-coordinate of player 2 (pids [1]) and so on.
The last trick needed is to convert Python DataFrame to a right for Paper.js.
There are more than one way to do this but we are going to make a nba_data.js and define JavaScript variables in there.
1 |
|
Animation with Paper.js
Now we got the data and now we are going to make animation in JavaScript.The below is a simple html skelton.Put this in the project folder so this can find nba_data.js.I downloaded Paper.js by bower install paper
so the location might be
different from bower_components/paper/dist/paper-full.js depending on how
you download the library.
1 |
|
As you can see in the script tag above, Paper.js needs a special type=”text/paperscript” and canvas=ID.The content of a canvas is shown in html <canvas id = ID> part.
The last part is to put the following in the above WE ARE GOING TO FILL IN HERE.
1 |
|
The final output in html is a smooth one game shot highlight animatioin too big to be fit in this post. so here are some GIF extractions from the original JavaScript animation.
Though I prefer a black background with circle objects moving around, you can draw a basketball court or replace circiles with player photos (you know, there’s also API for photos).
The full code is available here