-
Notifications
You must be signed in to change notification settings - Fork 18
/
display.ts
69 lines (59 loc) · 1.76 KB
/
display.ts
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
/* MoMath Math Square Display interface
* Provides a description of the display, defined by an HTMLDivElement of the
* given dimentions, and common utilities for rendering interfaces.
*/
import * as Sensor from 'sensors';
export const width = 576
export const height = 576
export type Vector = {
x: number
y: number
}
export const sensorWidth = width/Sensor.width
export const sensorHeight = height/Sensor.height
/* convert pixel coordinates to sensor Index */
export function toSensor(v: Vector): Sensor.Index;
export function toSensor(x: number, y: number): Sensor.Index;
export function toSensor(x: number|Vector, y?: number): Sensor.Index {
if (typeof x === 'object') {
y = x.y;
x = x.x;
}
return new Sensor.Index(
(x+0.5)/sensorWidth,
(<number>y+0.5)/sensorHeight);
};
/* convert sensor Index coordinates to pixel coordinates */
export function fromSensor(v: Sensor.Vector): Vector {
return {
x:(v.x+0.5)*sensorWidth-0.5,
y:(v.y+0.5)*sensorHeight-0.5
};
}
/* same as THREECtx.teamColors but as strings */
export const teamColors = [
'#393739', '#838385',
'#8e1e1f', '#895d1f',
'#9c9e5b', '#1f6f25',
'#1f579f', '#6d1e9f',
'#882558', '#9f8353',
'#704850', '#7c809f'];
abstract class CanvasContext {
canvas: HTMLCanvasElement
constructor (container: HTMLDivElement) {
this.canvas = document.createElement('canvas');
this.canvas.width = width;
this.canvas.height = height;
container.appendChild(this.canvas);
}
}
export class Canvas2DContext extends CanvasContext {
context: CanvasRenderingContext2D
constructor (container: HTMLDivElement) {
super(container)
const ctx = this.canvas.getContext('2d');
if (!ctx)
throw "Could not get 2D rendering context for canvas";
this.context = ctx;
}
}