Spaces:
No application file

File size: 12,924 Bytes
c4c8ae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import random
import string


def render_pitch_html(coordinates):
    id = ''.join(random.choices(string.ascii_letters + string.digits, k=10))

    return f"""
    <!doctype html>
    <html lang="en">
      <head>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
        <script type="text/javascript">
          function buildFootballRenderer(anchor) {{
            var X_FIELD_SCALE = 54.4;
            var Y_FIELD_SCALE = 83.6;
            var Z_FIELD_SCALE = 1;

            var FOOTBALL_BALL_RADIUS = 0.11 / X_FIELD_SCALE;
            var FOOTBALL_CENTER_CIRCLE_RADIUS = 9.15 / X_FIELD_SCALE;
            var FOOTBALL_CORNER_ARC_RADIUS = 1 / X_FIELD_SCALE;
            var FOOTBALL_PITCH_HALF_WIDTH = 55 / X_FIELD_SCALE;  // ~1
            var FOOTBALL_PITCH_HALF_HEIGHT = 36 / Y_FIELD_SCALE; // ~0.42
            var FOOTBALL_PITCH_RIM_X = 5 / X_FIELD_SCALE;
            var FOOTBALL_PITCH_RIM_Y = 4 / Y_FIELD_SCALE;
            var FOOTBALL_GOAL_AREA_WIDTH = FOOTBALL_PITCH_HALF_WIDTH * (1 - 0.92); // https://github.com/ephere-football/football/blob/939cad3f3b037502a91a383b132b98a6d017299b/third_party/gfootball_engine/src/onthepitch/referee.cpp#L183
            var FOOTBALL_GOAL_HEIGHT = 3.7 * 2 / Y_FIELD_SCALE;
            var FOOTBALL_GOAL_WIDTH = 2.55 / X_FIELD_SCALE;
            var FOOTBALL_GOAL_Z_LENGTH = 2.5; // Height in 3D of the Goal
            var FOOTBALL_PENALTY_X = 11 / X_FIELD_SCALE; // https://github.com/ephere-football/football/blob/939cad3f3b037502a91a383b132b98a6d017299b/third_party/gfootball_engine/src/onthepitch/referee.cpp#L593-L595
            var FOOTBALL_GOAL_AREA_ARC_RADIUS = 9.15 / X_FIELD_SCALE;
            var FOOTBALL_PENALTY_AREA_HEIGHT = 40.3 / Y_FIELD_SCALE;
            var FOOTBALL_PENALTY_AREA_WIDTH = 16.5 / X_FIELD_SCALE;

            // Renderer constants
            var BALL_ALTITUDE_EXPANSION_FACTOR = 1 / FOOTBALL_GOAL_Z_LENGTH;
            var ZOOM = 2;

            function adjustContext(context) {{
              var {{ width, height }} = context.canvas;
              var paddingXRatio = FOOTBALL_PITCH_RIM_X / FOOTBALL_PITCH_HALF_WIDTH;
              var paddingYRatio = FOOTBALL_PITCH_RIM_Y / FOOTBALL_PITCH_HALF_HEIGHT;
              context.maxWidth = width * (1 - paddingXRatio);
              context.maxHeight = height * (1 - paddingYRatio);
            }}

            function adjustPosition(context, x, y) {{
              return [
                x * (context.maxWidth / 2) / FOOTBALL_PITCH_HALF_WIDTH,
                y * (context.maxHeight / 2) / FOOTBALL_PITCH_HALF_HEIGHT
              ];
            }}

            function adjustSizeH(context, size) {{
              return size * (context.maxHeight / 2) / FOOTBALL_PITCH_HALF_HEIGHT;
            }}

            function adjustSizeW(context, size, zoom) {{
              return size * (context.maxWidth / 2) / FOOTBALL_PITCH_HALF_WIDTH * (zoom ?? 1);
            }}

            function createContext(canvas, devicePixelRatio) {{
              // Get the device pixel ratio, falling back to 1.
              var dpr = devicePixelRatio || 1;
              // Get the size of the canvas in CSS pixels.
              var rect = canvas.getBoundingClientRect();
              // Give the canvas pixel dimensions of their CSS
              // size * the device pixel ratio.
              canvas.width = rect.width * dpr;
              canvas.height = rect.height * dpr;
              var ctx = canvas.getContext('2d');
              ctx.imageSmoothingEnabled = true;
              // Scale all drawing operations by the dpr, so you
              // don't have to worry about the difference.
              ctx.scale(dpr, dpr);
              // 0,0 will be the point of reference
              ctx.translate(canvas.width / 2, canvas.height / 2);
              return ctx;
            }}

            function drawBall(context, state) {{
              var [x, y, z] = state;
              var [centerX, centerY] = adjustPosition(context, x, y);
              var radius = adjustSizeW(context, FOOTBALL_BALL_RADIUS, ZOOM) * (1 + z * BALL_ALTITUDE_EXPANSION_FACTOR);
              context.beginPath();
              context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
              context.fillStyle = 'white';
              context.fill();
              context.lineWidth = 1;
              context.strokeStyle = 'black';
              context.stroke();
            }}

            function drawMatchInfo(context, state) {{
              var padding = 12;
              context.font = "12px serif";
              context.textBaseline = "top";
              context.fillText("STEPS: " + state.steps + "; SCORE: " + state.score.join(" - "), -context.maxWidth / 2 + padding, -context.maxHeight / 2 + padding);
            }}

            // README: https://en.wikipedia.org/wiki/Football_pitch
            function drawPitch(context) {{
              // Clear
              context.clearRect(-context.canvas.width / 2, -context.canvas.height / 2, context.canvas.width, context.canvas.height);
              // Options
              context.fillStyle = "white";
              context.lineWidth = 1;
              context.strokeStyle = 'white';
              // Touch line
              context.beginPath();
              context.rect(-context.maxWidth / 2, -context.maxHeight / 2, context.maxWidth, context.maxHeight);
              context.stroke();
              // Half-way line
              context.moveTo(0, -context.maxHeight / 2);
              context.lineTo(0, context.maxHeight / 2);
              context.stroke();
              // Centre circle
              context.beginPath();
              context.arc(0, 0, adjustSizeW(context, FOOTBALL_CENTER_CIRCLE_RADIUS), 0, 2 * Math.PI);
              context.stroke();
              context.beginPath();
              context.arc(0, 0, adjustSizeW(context, FOOTBALL_BALL_RADIUS), 0, 2 * Math.PI, true);
              context.fill();
              context.stroke();
              // Corner arc
              context.moveTo(-context.maxWidth / 2, -context.maxHeight / 2);
              context.arc(-context.maxWidth / 2, -context.maxHeight / 2, adjustSizeW(context, FOOTBALL_CORNER_ARC_RADIUS), 0, Math.PI / 2);
              context.moveTo(-context.maxWidth / 2, context.maxHeight / 2);
              context.arc(-context.maxWidth / 2, context.maxHeight / 2, adjustSizeW(context, FOOTBALL_CORNER_ARC_RADIUS), -Math.PI / 2, 0);
              context.moveTo(context.maxWidth / 2, -context.maxHeight / 2);
              context.arc(context.maxWidth / 2, -context.maxHeight / 2, adjustSizeW(context, FOOTBALL_CORNER_ARC_RADIUS), Math.PI / 2, Math.PI);
              context.moveTo(context.maxWidth / 2, context.maxHeight / 2);
              context.arc(context.maxWidth / 2, context.maxHeight / 2, adjustSizeW(context, FOOTBALL_CORNER_ARC_RADIUS), -Math.PI, -Math.PI / 2);
              context.stroke();
              // Goal area
              var goalAreaWidth = adjustSizeW(context, FOOTBALL_GOAL_AREA_WIDTH);
              var goalAreaHeight = goalAreaWidth / (FOOTBALL_PENALTY_AREA_WIDTH / FOOTBALL_PENALTY_AREA_HEIGHT) * 1.3;
              context.beginPath();
              context.rect(
                -(context.maxWidth / 2),
                -goalAreaHeight / 2,
                goalAreaWidth,
                goalAreaHeight
              );
              context.rect(
                (context.maxWidth / 2) - goalAreaWidth,
                -goalAreaHeight / 2,
                goalAreaWidth,
                goalAreaHeight
              );
              context.stroke();
              // Penalty area
              var penaltyAreaHeight = adjustSizeH(context, FOOTBALL_PENALTY_AREA_HEIGHT);
              var penaltyAreaWidth = adjustSizeW(context, FOOTBALL_PENALTY_AREA_WIDTH);
              context.beginPath();
              context.rect(
                -(context.maxWidth / 2),
                -penaltyAreaHeight / 2,
                penaltyAreaWidth,
                penaltyAreaHeight
              );
              context.rect(
                (context.maxWidth / 2) - penaltyAreaWidth,
                -penaltyAreaHeight / 2,
                penaltyAreaWidth,
                penaltyAreaHeight
              );
              context.stroke();
              // Penalty mark
              var [penaltyX] = adjustPosition(context, FOOTBALL_PENALTY_X, 0);
              context.beginPath();
              context.arc(-(context.maxWidth / 2) + penaltyX, 0, adjustSizeW(context, FOOTBALL_BALL_RADIUS), 0, 2 * Math.PI, true);
              context.fill();
              context.stroke();
              context.beginPath();
              context.arc((context.maxWidth / 2) - penaltyX, 0, adjustSizeW(context, FOOTBALL_BALL_RADIUS), 0, 2 * Math.PI, true);
              context.fill();
              context.stroke();
              // Penalty arc
              context.save();
              context.rect(-(context.maxWidth / 2) + penaltyAreaWidth, -penaltyAreaHeight / 2, context.maxWidth - penaltyAreaWidth * 2, penaltyAreaHeight);
              context.clip();
              context.beginPath();
              context.arc(-(context.maxWidth / 2) + penaltyX, 0, adjustSizeW(context, FOOTBALL_GOAL_AREA_ARC_RADIUS), 0, 2 * Math.PI);
              context.stroke();
              context.beginPath();
              context.arc((context.maxWidth / 2) - penaltyX, 0, adjustSizeW(context, FOOTBALL_GOAL_AREA_ARC_RADIUS), 0, 2 * Math.PI);
              context.stroke();
              context.restore();
              // Goal
              var goalHeight = adjustSizeH(context, FOOTBALL_GOAL_HEIGHT);
              var goalWidth = adjustSizeW(context, FOOTBALL_GOAL_WIDTH) / 2;
              context.beginPath();
              context.rect(
                -(context.maxWidth / 2) - goalWidth,
                -goalHeight / 2,
                goalWidth,
                goalHeight
              );
              context.rect(
                (context.maxWidth / 2),
                -goalHeight / 2,
                goalWidth,
                goalHeight
              );
              context.closePath();
              context.strokeStyle = 'magenta';
              context.stroke();
            }}

            function drawPlayer(context, state, color) {{
              var [x, y] = state;
              var [centerX, centerY] = adjustPosition(context, x, y);
              var radius = adjustSizeW(context, FOOTBALL_BALL_RADIUS * 3, ZOOM);
              context.beginPath();
              context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
              context.fillStyle = color;
              context.fill();
              context.lineWidth = 1;
              context.strokeStyle = 'black';
              context.stroke();

              var caption = [x.toFixed(2), y.toFixed(2)].join(",");
              context.textBaseline = "top";
              context.fillText(caption, centerX - (context.measureText(caption).width / 2), centerY + 12);
            }}

            function drawPoint(context, state, color, notes) {{
              var [x, y] = state;
              var [centerX, centerY] = adjustPosition(context, x, y);
              var radius = adjustSizeW(context, FOOTBALL_BALL_RADIUS, ZOOM);
              context.beginPath();
              context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
              context.fillStyle = color;
              context.fill();
              context.lineWidth = 1;
              context.strokeStyle = 'black';
              context.stroke();

              var caption = [x.toFixed(2), y.toFixed(2)].join(",") + " (" + notes + ")";
              context.textBaseline = "top";
              context.fillText(caption, centerX - (context.measureText(caption).width / 2), centerY + 12);
            }}

            function drawPreMatch(context) {{
              adjustContext(context);
              drawPitch(context);
            }}

            var canvasCtx = createContext(anchor);

            return {{
              drawPoint: (position, color, notes) => drawPoint(canvasCtx, position, color, notes),
              drawPreMatch: () => drawPreMatch(canvasCtx)
            }};
          }}
        </script>
      </head>
      <body>
        <div class="ratio ratio-21x9">
          <div class="w-100 h-100">
            <canvas id="{id}" class="w-100 h-100 rounded" style="background: black;">
            </canvas>
          </div>
        </div>
        <script>
          (() => {{
            const r = buildFootballRenderer(document.querySelector("#{id}"));
            r.drawPreMatch();
            {' '.join([f'r.drawPoint([{x}, {y}], "{color}", "{notes}");' for [x, y, color, notes] in coordinates])}
          }})();
        </script>
      </body>
    </html>
    """