2019-05-09 12:32:01 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { set_hidpi_canvas, pixel_ratio } from './Helpers/Canvas.mjs';
|
|
|
|
import { RenderGradient } from './Helpers/GradientHelpers.mjs';
|
|
|
|
|
2021-01-30 21:27:39 +00:00
|
|
|
import gradients from './Gradients.mjs';
|
2019-05-09 12:32:01 +00:00
|
|
|
|
2021-01-30 21:27:39 +00:00
|
|
|
class Gauge {
|
2019-05-09 12:32:01 +00:00
|
|
|
constructor(in_canvas) {
|
|
|
|
this.canvas = in_canvas;
|
|
|
|
|
|
|
|
set_hidpi_canvas(this.canvas);
|
|
|
|
|
|
|
|
this.context = this.canvas.getContext("2d");
|
|
|
|
}
|
|
|
|
|
2021-01-30 21:27:39 +00:00
|
|
|
/**
|
|
|
|
* Sets the reading type to display on this gauge.
|
|
|
|
* Pulls gradient definitions from Gradients.mjs.
|
|
|
|
* @param {string} new_reading_type The reading type code to display.
|
|
|
|
*/
|
|
|
|
set_reading_type(new_reading_type) {
|
|
|
|
if(typeof gradients[new_reading_type] == "undefined") {
|
|
|
|
console.warn(`[Gauge] Warning: Unknown reading type ${new_reading_type} (defaulting to "unknown")`);
|
|
|
|
new_reading_type = "unknown";
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set_spec(gradients[new_reading_type]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the gradient spec to display.
|
|
|
|
* Automatically re-renders the gauge for convenience.
|
|
|
|
* @param {Object} arg The gradient spec to set display.
|
|
|
|
*/
|
2019-06-11 11:42:59 +00:00
|
|
|
set_spec({ gradient: spec, max }) {
|
2019-05-09 12:32:01 +00:00
|
|
|
this.spec = spec;
|
|
|
|
this.max = max;
|
2021-01-30 21:27:39 +00:00
|
|
|
|
|
|
|
this.render();
|
2019-05-09 12:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let guage_size = {
|
2019-05-09 20:38:42 +00:00
|
|
|
x: this.canvas.width * 0.6,
|
2019-05-09 12:32:01 +00:00
|
|
|
y: this.canvas.height * 0.05,
|
|
|
|
width: this.canvas.width * 0.3,
|
|
|
|
height: this.canvas.height * 0.9
|
|
|
|
};
|
|
|
|
|
|
|
|
this.clear_canvas();
|
|
|
|
this.render_gauge(guage_size);
|
|
|
|
this.render_labels(guage_size);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
clear_canvas() {
|
|
|
|
this.context.clearRect(
|
|
|
|
0, 0,
|
|
|
|
this.canvas.width, this.canvas.height
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render_gauge(guage_size) {
|
|
|
|
this.context.save();
|
|
|
|
|
|
|
|
let gradient_spec = RenderGradient(this.spec, this.max);
|
|
|
|
// console.log(gradient_spec);
|
|
|
|
|
|
|
|
let gradient = this.context.createLinearGradient(
|
2019-05-09 20:34:01 +00:00
|
|
|
0, guage_size.y + guage_size.height,
|
|
|
|
0, guage_size.y
|
2019-05-09 12:32:01 +00:00
|
|
|
);
|
|
|
|
for (let point in gradient_spec)
|
|
|
|
gradient.addColorStop(parseFloat(point), gradient_spec[point]);
|
|
|
|
|
|
|
|
this.context.fillStyle = gradient;
|
2019-05-09 20:38:42 +00:00
|
|
|
this.context.fillRect(
|
|
|
|
guage_size.x, guage_size.y,
|
|
|
|
guage_size.width, guage_size.height
|
|
|
|
);
|
2019-05-09 12:32:01 +00:00
|
|
|
|
|
|
|
this.context.restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
render_labels(guage_size) {
|
|
|
|
this.context.save();
|
|
|
|
|
|
|
|
this.context.font = "12px Ubuntu, sans-serif";
|
|
|
|
this.context.textBaseline = "middle";
|
|
|
|
this.context.strokeStyle = "rgba(0, 0, 0, 0.5)";
|
|
|
|
this.context.lineWidth = 1.5 * pixel_ratio;
|
|
|
|
|
|
|
|
for (let point in this.spec) {
|
2019-05-09 20:34:01 +00:00
|
|
|
let value = 1 - (parseFloat(point) / this.max);
|
2019-05-09 12:32:01 +00:00
|
|
|
|
2019-05-09 20:38:42 +00:00
|
|
|
let text_width = this.context.measureText(point).width;
|
|
|
|
let draw_x = guage_size.x - 5 - text_width;
|
2019-05-09 12:32:01 +00:00
|
|
|
let draw_y = guage_size.y + (value * guage_size.height);
|
|
|
|
|
2019-05-09 12:38:51 +00:00
|
|
|
// console.log(`Writing '${point}' to (${draw_x}, ${draw_y})`);
|
2019-05-09 12:32:01 +00:00
|
|
|
this.context.fillText(point, draw_x, draw_y);
|
|
|
|
|
|
|
|
this.context.beginPath();
|
2019-05-09 20:38:42 +00:00
|
|
|
this.context.moveTo(guage_size.x + guage_size.width, draw_y);
|
|
|
|
this.context.lineTo(draw_x + text_width, draw_y);
|
2019-05-09 12:32:01 +00:00
|
|
|
this.context.stroke();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.context.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 21:27:39 +00:00
|
|
|
export default Gauge;
|