-
Notifications
You must be signed in to change notification settings - Fork 15
/
ModuleGrid.ts
182 lines (144 loc) · 5.45 KB
/
ModuleGrid.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
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
import { debounce } from '@/helpers/debounce';
import { Value } from '@/models/Value';
import { Module } from '@/modules/Module';
import { assert, isFunction, isNumber, isString } from '@/validation/assert';
export interface ModuleGridGetLinesCountParams {
/** Width of the container */
containerWidth: number;
}
export interface ModuleGridShouldShowLabelParams {
/** Line number currently displayed */
i: number;
/** Number of all lines */
linesCount: number;
}
export interface ModuleGridSettings {
/** Color of grid text and lines. Can be any CSS valid color string */
color: string;
/** Height of grid lines (in pixels, no unit) */
height: number;
/** Font size of grid text (in pixels, no unit) */
fontSize: number;
/** Font family of grid text (should be supported by the browser) */
fontFamily: string;
/** Vertical distance between text and lines (in pixels, no unit) */
textPadding: number;
/** Formatter used to format values before rendering them as text */
formatter: (realValue: number) => string;
/** Returns count of lines, that should be rendered for grid */
getLinesCount: (opt: ModuleGridGetLinesCountParams) => number;
/** Function that determines whether line label should be drawn or not */
shouldShowLabel: (params: ModuleGridShouldShowLabelParams) => boolean;
}
/**
* Module showing grid (by default beneath rail) giving overview
* what values are where.
* - clickable
* - uses canvas for rendering, to not pollute DOM
*
* Uses `.jsr_grid` as grid parent CSS class.
*/
export class ModuleGrid extends Module {
private grid!: HTMLCanvasElement;
private context!: CanvasRenderingContext2D;
private settings: ModuleGridSettings;
constructor (settings: Partial<ModuleGridSettings> = {}) {
super();
this.assertSettings(settings);
this.settings = Object.assign({
color: 'rgba(0, 0, 0, 0.3)',
height: 10,
fontSize: 10,
fontFamily: 'sans-serif',
textPadding: 5,
formatter: String,
getLinesCount: ({ containerWidth }) => Math.min(
100,
Math.floor(containerWidth / 10),
),
shouldShowLabel: ({ i, linesCount }) => {
return i === 0 || i === linesCount || i % 10 === 0;
},
} as ModuleGridSettings, settings);
}
public destroy () {
this.grid.remove();
window.removeEventListener('resize', this.handleWindowResize);
}
public initView () {
this.grid = document.createElement('canvas');
this.grid.classList.add('jsr_grid');
this.context = this.grid.getContext('2d')!;
this.renderer.addChild(this.grid);
this.drawGrid();
window.addEventListener('resize', this.handleWindowResize);
this.grid.addEventListener('click', this.handleClick);
}
private drawGrid () {
const width = this.renderer.getContainer().offsetWidth;
const height = this.settings.height + this.settings.fontSize + this.settings.textPadding;
const pixelRatio = window.devicePixelRatio || 1;
const context = this.context;
const linesCount = this.settings.getLinesCount({
containerWidth: width,
});
const ratio = 1 / linesCount;
this.grid.style.width = `${width}px`;
this.grid.width = width * pixelRatio;
this.grid.style.height = `${height}px`;
this.grid.height = height * pixelRatio;
context.scale(pixelRatio, pixelRatio);
context.clearRect(0, 0, width, this.settings.height);
context.beginPath();
context.lineWidth = 1;
context.fillStyle = context.strokeStyle = this.settings.color;
context.font = `${this.settings.fontSize}px ${this.settings.fontFamily}`;
context.textBaseline = 'top';
for (let i = 0; i <= linesCount; i += 1) {
// Draw line
let left = i * ratio * width;
left = Math.round(left * 100.0) / 100.0;
context.moveTo(left, 0);
context.lineTo(left, this.settings.height);
const shouldShowLabel = this.settings.shouldShowLabel({
i,
linesCount,
});
// Draw text
if (shouldShowLabel) {
// Determine the position of text
if (i === 0) {
context.textAlign = 'left';
} else if (i === linesCount) {
context.textAlign = 'right';
} else {
context.textAlign = 'center';
}
const value = Value.fromRatio({
ratio: i / linesCount,
max: this.config.max,
min: this.config.min,
});
const roundedValue = value.changeReal(Math.round(value.asReal() / this.config.step) * this.config.step);
const text = this.settings.formatter(roundedValue.asReal());
context.fillText(text.toString(), i * ratio * width, this.settings.height + this.settings.textPadding);
}
}
context.closePath();
context.stroke();
}
private handleWindowResize = debounce(() => {
this.drawGrid();
}, 50);
private handleClick = (e: MouseEvent) => {
this.input.setClosestRatioValue(this.renderer.positionToRelative(e.clientX));
};
private assertSettings (settings: Partial<ModuleGridSettings>) {
settings.formatter && assert('Grid.formatter', settings.formatter, isFunction);
settings.color && assert('Grid.color', settings.color, isString);
settings.height && assert('Grid.height', settings.height, isNumber);
settings.fontSize && assert('Grid.fontSize', settings.fontSize, isNumber);
settings.fontFamily && assert('Grid.fontFamily', settings.fontFamily, isString);
settings.textPadding && assert('Grid.textPadding', settings.textPadding, isNumber);
}
}