Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 1 | import {aos} from 'aos/configuration_generated'; |
| 2 | import {aos} from 'aos/network/connect_generated'; |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 3 | import {Connection} from 'aos/network/www/proxy'; |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 4 | import {frc971} from 'frc971/control_loops/drivetrain/drivetrain_status_generated'; |
| 5 | import {frc971} from 'y2020/vision/sift/sift_generated'; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 6 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 7 | import {FIELD_LENGTH, FIELD_WIDTH, FT_TO_M, IN_TO_M} from './constants'; |
| 8 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 9 | // (0,0) is field center, +X is toward red DS |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 10 | const FIELD_SIDE_Y = FIELD_WIDTH / 2; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 11 | const FIELD_EDGE_X = FIELD_LENGTH / 2; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 12 | |
| 13 | const DS_WIDTH = 69 * IN_TO_M; |
| 14 | const DS_ANGLE = 20 * Math.PI / 180; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 15 | const DS_END_X = FIELD_EDGE_X - DS_WIDTH * Math.sin(DS_ANGLE); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 16 | const DS_INSIDE_Y = FIELD_SIDE_Y - DS_WIDTH * Math.cos(DS_ANGLE); |
| 17 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 18 | const TRENCH_X = 108 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 19 | const TRENCH_WIDTH = 55.5 * IN_TO_M; |
| 20 | const TRENCH_INSIDE = FIELD_SIDE_Y - TRENCH_WIDTH; |
| 21 | |
| 22 | const SPINNER_LENGTH = 30 * IN_TO_M; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 23 | const SPINNER_TOP_X = 374.59 * IN_TO_M - FIELD_EDGE_X; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 24 | const SPINNER_BOTTOM_X = SPINNER_TOP_X - SPINNER_LENGTH; |
| 25 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 26 | const SHIELD_BOTTOM_X = -116 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 27 | const SHIELD_BOTTOM_Y = 43.75 * IN_TO_M; |
| 28 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 29 | const SHIELD_TOP_X = 116 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 30 | const SHIELD_TOP_Y = -43.75 * IN_TO_M; |
| 31 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 32 | const SHIELD_RIGHT_X = -51.06 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 33 | const SHIELD_RIGHT_Y = -112.88 * IN_TO_M; |
| 34 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 35 | const SHIELD_LEFT_X = 51.06 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 36 | const SHIELD_LEFT_Y = 112.88 * IN_TO_M; |
| 37 | |
| 38 | const SHIELD_CENTER_TOP_X = (SHIELD_TOP_X + SHIELD_LEFT_X) / 2 |
| 39 | const SHIELD_CENTER_TOP_Y = (SHIELD_TOP_Y + SHIELD_LEFT_Y) / 2 |
| 40 | |
| 41 | const SHIELD_CENTER_BOTTOM_X = (SHIELD_BOTTOM_X + SHIELD_RIGHT_X) / 2 |
| 42 | const SHIELD_CENTER_BOTTOM_Y = (SHIELD_BOTTOM_Y + SHIELD_RIGHT_Y) / 2 |
| 43 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 44 | const INITIATION_X = FIELD_EDGE_X - 120 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 45 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 46 | const TARGET_ZONE_TIP_X = FIELD_EDGE_X - 30 * IN_TO_M; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 47 | const TARGET_ZONE_WIDTH = 48 * IN_TO_M; |
| 48 | const LOADING_ZONE_WIDTH = 60 * IN_TO_M; |
| 49 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 50 | const ROBOT_WIDTH = 28 * IN_TO_M; |
| 51 | const ROBOT_LENGTH = 30 * IN_TO_M; |
| 52 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 53 | /** |
| 54 | * All the messages that are required to display camera information on the field. |
| 55 | * Messages not readable on the server node are ignored. |
| 56 | */ |
| 57 | const REQUIRED_CHANNELS = [ |
| 58 | { |
| 59 | name: '/pi1/camera', |
| 60 | type: 'frc971.vision.sift.ImageMatchResult', |
| 61 | }, |
| 62 | { |
| 63 | name: '/pi2/camera', |
| 64 | type: 'frc971.vision.sift.ImageMatchResult', |
| 65 | }, |
| 66 | { |
| 67 | name: '/pi3/camera', |
| 68 | type: 'frc971.vision.sift.ImageMatchResult', |
| 69 | }, |
| 70 | { |
| 71 | name: '/pi4/camera', |
| 72 | type: 'frc971.vision.sift.ImageMatchResult', |
| 73 | }, |
| 74 | { |
| 75 | name: '/pi5/camera', |
| 76 | type: 'frc971.vision.sift.ImageMatchResult', |
| 77 | }, |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 78 | { |
| 79 | name: '/drivetrain', |
| 80 | type: 'frc971.control_loops.drivetrain.Status', |
| 81 | }, |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 82 | ]; |
| 83 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 84 | export class FieldHandler { |
| 85 | private canvas = document.createElement('canvas'); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 86 | private imageMatchResult: frc971.vision.sift.ImageMatchResult|null = null; |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame^] | 87 | private drivetrainStatus: DrivetrainStatus|null = null; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 88 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 89 | constructor(private readonly connection: Connection) { |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 90 | document.body.appendChild(this.canvas); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 91 | |
| 92 | this.connection.addConfigHandler(() => { |
| 93 | this.sendConnect(); |
| 94 | }); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 95 | this.connection.addHandler( |
| 96 | frc971.vision.sift.ImageMatchResult.getFullyQualifiedName(), (res) => { |
| 97 | this.handleImageMatchResult(res); |
| 98 | }); |
| 99 | this.connection.addHandler(frc971.control_loops.drivetrain.Status.getFullyQualifiedName(), (data) => { |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 100 | this.handleDrivetrainStatus(data); |
| 101 | }); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | private handleImageMatchResult(data: Uint8Array): void { |
| 105 | const fbBuffer = new flatbuffers.ByteBuffer(data); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 106 | this.imageMatchResult = |
| 107 | frc971.vision.sift.ImageMatchResult.getRootAsImageMatchResult(fbBuffer); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 110 | private handleDrivetrainStatus(data: Uint8Array): void { |
| 111 | const fbBuffer = new flatbuffers.ByteBuffer(data); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 112 | this.drivetrainStatus = frc971.control_loops.drivetrain.Status.getRootAsStatus(fbBuffer); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 115 | private sendConnect(): void { |
| 116 | const builder = new flatbuffers.Builder(512); |
| 117 | const channels: flatbuffers.Offset[] = []; |
| 118 | for (const channel of REQUIRED_CHANNELS) { |
| 119 | const nameFb = builder.createString(channel.name); |
| 120 | const typeFb = builder.createString(channel.type); |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 121 | aos.Channel.startChannel(builder); |
| 122 | aos.Channel.addName(builder, nameFb); |
| 123 | aos.Channel.addType(builder, typeFb); |
| 124 | const channelFb = aos.Channel.endChannel(builder); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 125 | channels.push(channelFb); |
| 126 | } |
| 127 | |
Austin Schuh | 7c75e58 | 2020-11-14 16:41:18 -0800 | [diff] [blame] | 128 | const channelsFb = aos.message_bridge.Connect.createChannelsToTransferVector(builder, channels); |
| 129 | aos.message_bridge.Connect.startConnect(builder); |
| 130 | aos.message_bridge.Connect.addChannelsToTransfer(builder, channelsFb); |
| 131 | const connect = aos.message_bridge.Connect.endConnect(builder); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 132 | builder.finish(connect); |
| 133 | this.connection.sendConnectMessage(builder); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | drawField(): void { |
| 137 | const MY_COLOR = 'red'; |
| 138 | const OTHER_COLOR = 'blue'; |
| 139 | const ctx = this.canvas.getContext('2d'); |
| 140 | // draw perimiter |
| 141 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 142 | ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 143 | ctx.lineTo(DS_END_X, FIELD_SIDE_Y); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 144 | ctx.lineTo(-DS_END_X, FIELD_SIDE_Y); |
| 145 | ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y); |
| 146 | ctx.lineTo(-FIELD_EDGE_X, -DS_INSIDE_Y); |
| 147 | ctx.lineTo(-DS_END_X, -FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 148 | ctx.lineTo(DS_END_X, -FIELD_SIDE_Y); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 149 | ctx.lineTo(FIELD_EDGE_X, -DS_INSIDE_Y); |
| 150 | ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 151 | ctx.stroke(); |
| 152 | |
| 153 | // draw shield generator |
| 154 | ctx.beginPath(); |
| 155 | ctx.moveTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y); |
| 156 | ctx.lineTo(SHIELD_RIGHT_X, SHIELD_RIGHT_Y); |
| 157 | ctx.lineTo(SHIELD_TOP_X, SHIELD_TOP_Y); |
| 158 | ctx.lineTo(SHIELD_LEFT_X, SHIELD_LEFT_Y); |
| 159 | ctx.lineTo(SHIELD_BOTTOM_X, SHIELD_BOTTOM_Y); |
| 160 | ctx.moveTo(SHIELD_CENTER_TOP_X, SHIELD_CENTER_TOP_Y); |
| 161 | ctx.lineTo(SHIELD_CENTER_BOTTOM_X, SHIELD_CENTER_BOTTOM_Y); |
| 162 | ctx.stroke(); |
| 163 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 164 | this.drawHalfField(ctx, 'red'); |
| 165 | ctx.rotate(Math.PI); |
| 166 | this.drawHalfField(ctx, 'blue'); |
| 167 | ctx.rotate(Math.PI); |
| 168 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 169 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 170 | drawHalfField(ctx, color: string): void { |
| 171 | // trenches |
| 172 | ctx.strokeStyle = color; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 173 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 174 | ctx.moveTo(TRENCH_X, FIELD_SIDE_Y); |
| 175 | ctx.lineTo(TRENCH_X, TRENCH_INSIDE); |
| 176 | ctx.lineTo(-TRENCH_X, TRENCH_INSIDE); |
| 177 | ctx.lineTo(-TRENCH_X, FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 178 | ctx.stroke(); |
| 179 | |
| 180 | ctx.strokeStyle = 'black'; |
| 181 | ctx.beginPath(); |
| 182 | ctx.moveTo(SPINNER_TOP_X, FIELD_SIDE_Y); |
| 183 | ctx.lineTo(SPINNER_TOP_X, TRENCH_INSIDE); |
| 184 | ctx.lineTo(SPINNER_BOTTOM_X, TRENCH_INSIDE); |
| 185 | ctx.lineTo(SPINNER_BOTTOM_X, FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 186 | ctx.stroke(); |
| 187 | |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 188 | ctx.beginPath(); |
| 189 | ctx.moveTo(INITIATION_X, FIELD_SIDE_Y); |
| 190 | ctx.lineTo(INITIATION_X, -FIELD_SIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 191 | ctx.stroke(); |
| 192 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 193 | // target/loading |
| 194 | ctx.strokeStyle = color; |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 195 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 196 | ctx.moveTo(FIELD_EDGE_X, DS_INSIDE_Y); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 197 | ctx.lineTo(TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * TARGET_ZONE_WIDTH); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 198 | ctx.lineTo(FIELD_EDGE_X, DS_INSIDE_Y - TARGET_ZONE_WIDTH); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 199 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 200 | ctx.moveTo(-FIELD_EDGE_X, DS_INSIDE_Y); |
| 201 | ctx.lineTo(-TARGET_ZONE_TIP_X, DS_INSIDE_Y - 0.5 * LOADING_ZONE_WIDTH); |
| 202 | ctx.lineTo(-FIELD_EDGE_X, DS_INSIDE_Y - LOADING_ZONE_WIDTH); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 203 | ctx.stroke(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 204 | } |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 205 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 206 | drawCamera(x: number, y: number, theta: number): void { |
| 207 | const ctx = this.canvas.getContext('2d'); |
| 208 | ctx.save(); |
| 209 | ctx.translate(x, y); |
| 210 | ctx.rotate(theta); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 211 | ctx.beginPath(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 212 | ctx.moveTo(0.5, 0.5); |
| 213 | ctx.lineTo(0, 0); |
| 214 | ctx.lineTo(0.5, -0.5); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 215 | ctx.stroke(); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 216 | ctx.beginPath(); |
| 217 | ctx.arc(0, 0, 0.25, -Math.PI/4, Math.PI/4); |
| 218 | ctx.stroke(); |
| 219 | ctx.restore(); |
| 220 | } |
| 221 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 222 | drawRobot(x: number, y: number, theta: number): void { |
| 223 | const ctx = this.canvas.getContext('2d'); |
| 224 | ctx.save(); |
| 225 | ctx.translate(x, y); |
| 226 | ctx.rotate(theta); |
| 227 | ctx.rect(-ROBOT_LENGTH / 2, -ROBOT_WIDTH / 2, ROBOT_LENGTH, ROBOT_WIDTH); |
| 228 | ctx.stroke(); |
| 229 | ctx.beginPath(); |
| 230 | ctx.moveTo(0, 0); |
| 231 | ctx.lineTo(ROBOT_LENGTH / 2, 0); |
| 232 | ctx.stroke(); |
| 233 | ctx.restore(); |
| 234 | } |
| 235 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 236 | draw(): void { |
| 237 | this.reset(); |
| 238 | this.drawField(); |
| 239 | //draw cameras |
| 240 | if (this.imageMatchResult) { |
Philipp Schrader | a227d04 | 2020-11-14 17:33:52 -0800 | [diff] [blame^] | 241 | for (let i = 0; i < this.imageMatchResult.cameraPosesLength(); i++) { |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 242 | const pose = this.imageMatchResult.cameraPoses(i); |
| 243 | const mat = pose.fieldToCamera(); |
| 244 | const x = mat.data(3); |
| 245 | const y = mat.data(7); |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 246 | const theta = Math.atan2( |
| 247 | -mat.data(8), |
| 248 | Math.sqrt(Math.pow(mat.data(9), 2) + Math.pow(mat.data(10), 2))); |
| 249 | this.drawCamera(x, y, theta); |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Alex Perry | 2124ae8 | 2020-03-07 14:19:06 -0800 | [diff] [blame] | 253 | if (this.drivetrainStatus) { |
| 254 | this.drawRobot( |
| 255 | this.drivetrainStatus.x(), this.drivetrainStatus.y(), |
| 256 | this.drivetrainStatus.theta()); |
| 257 | } |
| 258 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 259 | window.requestAnimationFrame(() => this.draw()); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | reset(): void { |
| 263 | const ctx = this.canvas.getContext('2d'); |
| 264 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
| 265 | const size = window.innerHeight * 0.9; |
| 266 | ctx.canvas.height = size; |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 267 | const width = size / 2 + 20; |
| 268 | ctx.canvas.width = width; |
| 269 | ctx.clearRect(0, 0, size, width); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 270 | |
Alex Perry | b49a3fb | 2020-02-29 15:26:54 -0800 | [diff] [blame] | 271 | // Translate to center of display. |
| 272 | ctx.translate(width / 2, size / 2); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 273 | // Coordinate system is: |
| 274 | // x -> forward. |
| 275 | // y -> to the left. |
| 276 | ctx.rotate(-Math.PI / 2); |
| 277 | ctx.scale(1, -1); |
Alex Perry | 5427c9a | 2020-02-15 17:43:45 -0800 | [diff] [blame] | 278 | |
| 279 | const M_TO_PX = (size - 10) / FIELD_LENGTH; |
| 280 | ctx.scale(M_TO_PX, M_TO_PX); |
| 281 | ctx.lineWidth = 1 / M_TO_PX; |
| 282 | } |
| 283 | } |