2017-12-18 22:04:17 +01:00
|
|
|
/// <reference path="./layoutView.ts" />
|
|
|
|
|
2017-07-11 10:15:17 +02:00
|
|
|
namespace pxsim.visuals {
|
2017-12-18 22:04:17 +01:00
|
|
|
|
|
|
|
const EV3_STYLE = `
|
2017-07-11 10:15:17 +02:00
|
|
|
svg.sim {
|
|
|
|
margin-bottom:1em;
|
|
|
|
}
|
|
|
|
svg.sim.grayscale {
|
|
|
|
-moz-filter: grayscale(1);
|
|
|
|
-webkit-filter: grayscale(1);
|
|
|
|
filter: grayscale(1);
|
|
|
|
}
|
|
|
|
.sim-button {
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
.sim-button:hover {
|
|
|
|
stroke-width: 2px !important;
|
|
|
|
stroke: white !important;
|
|
|
|
}
|
|
|
|
|
|
|
|
.sim-systemled {
|
|
|
|
fill:#333;
|
|
|
|
stroke:#555;
|
|
|
|
stroke-width: 1px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.sim-text {
|
|
|
|
font-family:"Lucida Console", Monaco, monospace;
|
|
|
|
font-size:8px;
|
|
|
|
fill:#fff;
|
|
|
|
pointer-events: none; user-select: none;
|
|
|
|
}
|
|
|
|
.sim-text.small {
|
|
|
|
font-size:6px;
|
|
|
|
}
|
|
|
|
.sim-text.inverted {
|
|
|
|
fill:#000;
|
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
/* Color Grid */
|
|
|
|
.sim-color-grid-circle:hover {
|
|
|
|
stroke-width: 0.4;
|
|
|
|
stroke: #000;
|
2017-07-11 10:15:17 +02:00
|
|
|
cursor: pointer;
|
|
|
|
}
|
2017-12-18 22:04:17 +01:00
|
|
|
.sim-color-wheel-half:hover {
|
|
|
|
stroke-width: 1;
|
|
|
|
stroke: #000;
|
|
|
|
fill: gray !important;
|
|
|
|
cursor: pointer;
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
const EV3_WIDTH = 99.984346;
|
|
|
|
const EV3_HEIGHT = 151.66585;
|
2017-07-13 22:01:39 +02:00
|
|
|
export const SCREEN_WIDTH = 178;
|
|
|
|
export const SCREEN_HEIGHT = 128;
|
2017-07-11 10:15:17 +02:00
|
|
|
export interface IBoardTheme {
|
|
|
|
accent?: string;
|
|
|
|
display?: string;
|
|
|
|
buttonOuter?: string;
|
|
|
|
buttonUps: string[];
|
|
|
|
buttonDown?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export var themes: IBoardTheme[] = ["#3ADCFE"].map(accent => {
|
|
|
|
return {
|
|
|
|
accent: accent,
|
|
|
|
buttonOuter: "#979797",
|
2017-12-18 22:04:17 +01:00
|
|
|
buttonUps: ["#a8aaa8", "#393939", "#a8aaa8", "#a8aaa8", "#a8aaa8", '#a8aaa8'],
|
|
|
|
buttonDown: "#000"
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export function randomTheme(): IBoardTheme {
|
|
|
|
return themes[Math.floor(Math.random() * themes.length)];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IBoardProps {
|
|
|
|
runtime?: pxsim.Runtime;
|
|
|
|
theme?: IBoardTheme;
|
|
|
|
disableTilt?: boolean;
|
|
|
|
wireframe?: boolean;
|
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
export class EV3View implements BoardView {
|
|
|
|
public static BOARD_WIDTH = 500;
|
|
|
|
public static BOARD_HEIGHT = 500;
|
|
|
|
|
|
|
|
public wrapper: HTMLDivElement;
|
2017-07-11 10:15:17 +02:00
|
|
|
public element: SVGSVGElement;
|
|
|
|
private style: SVGStyleElement;
|
|
|
|
private defs: SVGDefsElement;
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
private layoutView: LayoutView;
|
|
|
|
|
|
|
|
private controlGroup: ViewContainer;
|
|
|
|
private selectedNode: NodeType;
|
|
|
|
private selectedPort: number;
|
|
|
|
private controlView: View;
|
|
|
|
private cachedControlNodes: { [index: string]: View[] } = {};
|
|
|
|
private cachedDisplayViews: { [index: string]: LayoutElement[] } = {};
|
|
|
|
|
|
|
|
private closeGroup: ViewContainer;
|
|
|
|
private closeIconView: View;
|
|
|
|
|
2017-07-12 09:25:17 +02:00
|
|
|
private screenCanvas: HTMLCanvasElement;
|
|
|
|
private screenCanvasCtx: CanvasRenderingContext2D;
|
|
|
|
private screenCanvasData: ImageData;
|
2017-12-18 22:04:17 +01:00
|
|
|
|
|
|
|
private screenCanvasTemp: HTMLCanvasElement;
|
|
|
|
|
|
|
|
private screenScaledWidth: number;
|
|
|
|
private screenScaledHeight: number;
|
|
|
|
|
|
|
|
private width = 0;
|
|
|
|
private height = 0;
|
|
|
|
|
|
|
|
private g: SVGGElement;
|
|
|
|
|
|
|
|
public board: pxsim.EV3Board;
|
2017-07-11 10:15:17 +02:00
|
|
|
|
|
|
|
constructor(public props: IBoardProps) {
|
|
|
|
this.buildDom();
|
2017-12-18 22:04:17 +01:00
|
|
|
const dalBoard = board();
|
|
|
|
dalBoard.updateSubscribers.push(() => this.updateState());
|
2017-07-11 10:15:17 +02:00
|
|
|
if (props && props.wireframe)
|
|
|
|
svg.addClass(this.element, "sim-wireframe");
|
|
|
|
|
|
|
|
if (props && props.theme)
|
|
|
|
this.updateTheme();
|
2017-12-18 22:04:17 +01:00
|
|
|
|
2017-07-11 10:15:17 +02:00
|
|
|
if (props && props.runtime) {
|
2017-12-18 22:04:17 +01:00
|
|
|
this.board = this.props.runtime.board as pxsim.EV3Board;
|
2017-07-11 10:15:17 +02:00
|
|
|
this.board.updateSubscribers.push(() => this.updateState());
|
|
|
|
this.updateState();
|
|
|
|
}
|
2017-12-19 21:53:12 +01:00
|
|
|
let that = this;
|
|
|
|
window.setInterval(function(){
|
|
|
|
that.updateScreen();
|
|
|
|
}, 30);
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public getView(): SVGAndSize<SVGSVGElement> {
|
|
|
|
return {
|
2017-12-18 22:04:17 +01:00
|
|
|
el: this.wrapper as any,
|
2017-07-11 10:15:17 +02:00
|
|
|
y: 0,
|
|
|
|
x: 0,
|
2017-12-18 22:04:17 +01:00
|
|
|
w: EV3View.BOARD_WIDTH,
|
|
|
|
h: EV3View.BOARD_WIDTH
|
2017-07-11 10:15:17 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public getCoord(pinNm: string): Coord {
|
2017-12-18 22:04:17 +01:00
|
|
|
// Not needed
|
|
|
|
return undefined;
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public highlightPin(pinNm: string): void {
|
2017-12-18 22:04:17 +01:00
|
|
|
// Not needed
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public getPinDist(): number {
|
2017-12-18 22:04:17 +01:00
|
|
|
// Not needed
|
2017-07-11 10:15:17 +02:00
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
public updateTheme() {
|
2017-07-11 10:15:17 +02:00
|
|
|
let theme = this.props.theme;
|
2017-12-18 22:04:17 +01:00
|
|
|
this.layoutView.updateTheme(theme);
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public updateState() {
|
2017-12-18 22:04:17 +01:00
|
|
|
this.updateVisibleNodes();
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
private updateVisibleNodes() {
|
|
|
|
const inputNodes = ev3board().getInputNodes();
|
|
|
|
inputNodes.forEach((node, index) => {
|
|
|
|
const view = this.getDisplayViewForNode(node.id, index);
|
|
|
|
if (view) {
|
|
|
|
this.layoutView.setInput(index, view);
|
|
|
|
view.updateState();
|
|
|
|
}
|
|
|
|
});
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.getDisplayViewForNode(ev3board().getBrickNode().id, -1).updateState();
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
const outputNodes = ev3board().getMotors();
|
|
|
|
outputNodes.forEach((node, index) => {
|
|
|
|
const view = this.getDisplayViewForNode(node.id, index);
|
|
|
|
if (view) {
|
|
|
|
this.layoutView.setOutput(index, view);
|
|
|
|
view.updateState();
|
|
|
|
}
|
|
|
|
});
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
const selected = this.layoutView.getSelected();
|
|
|
|
if (selected && (selected.getId() !== this.selectedNode || selected.getPort() !== this.selectedPort)) {
|
|
|
|
this.selectedNode = selected.getId();
|
|
|
|
this.selectedPort = selected.getPort();
|
|
|
|
this.controlGroup.clear();
|
|
|
|
const control = this.getControlForNode(this.selectedNode, selected.getPort());
|
|
|
|
if (control) {
|
|
|
|
this.controlView = control;
|
|
|
|
this.controlGroup.addView(control);
|
|
|
|
}
|
|
|
|
this.closeIconView.setVisible(true);
|
|
|
|
} else if (!selected) {
|
|
|
|
this.controlGroup.clear();
|
|
|
|
this.controlView = undefined;
|
|
|
|
this.selectedNode = undefined;
|
|
|
|
this.selectedPort = undefined;
|
|
|
|
this.closeIconView.setVisible(false);
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.resize();
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
public resize() {
|
|
|
|
const bounds = this.element.getBoundingClientRect();
|
|
|
|
this.width = bounds.width;
|
|
|
|
this.height = bounds.height;
|
|
|
|
this.layoutView.layout(bounds.width, bounds.height);
|
2017-07-12 11:26:14 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
if (this.selectedNode) {
|
|
|
|
const scale = this.width / this.closeIconView.getInnerWidth() / 10;
|
|
|
|
// Translate close icon
|
|
|
|
this.closeIconView.scale(Math.max(0, Math.min(1, scale)));
|
|
|
|
const closeIconWidth = this.closeIconView.getWidth();
|
|
|
|
const closeIconHeight = this.closeIconView.getHeight();
|
|
|
|
const closeCoords = this.layoutView.getCloseIconCoords(closeIconWidth, closeIconHeight);
|
|
|
|
this.closeIconView.translate(closeCoords.x, closeCoords.y);
|
2017-07-13 22:01:39 +02:00
|
|
|
}
|
2017-07-12 09:25:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
if (this.controlView) {
|
|
|
|
const h = this.controlView.getInnerHeight();
|
|
|
|
const w = this.controlView.getInnerWidth();
|
|
|
|
const bh = this.layoutView.getModuleBounds().height - this.closeIconView.getHeight();
|
|
|
|
const bw = this.layoutView.getModuleBounds().width - (this.width * MODULE_INNER_PADDING_RATIO * 2);
|
|
|
|
this.controlView.scale(Math.min(bh / h, bw / w), false);
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
const controlCoords = this.layoutView.getSelectedCoords();
|
|
|
|
this.controlView.translate(controlCoords.x, controlCoords.y);
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.updateScreen();
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
private getControlForNode(id: NodeType, port: number) {
|
|
|
|
if (this.cachedControlNodes[id] && this.cachedControlNodes[id][port]) {
|
|
|
|
return this.cachedControlNodes[id][port];
|
|
|
|
}
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
let view: View;
|
|
|
|
switch (id) {
|
|
|
|
case NodeType.ColorSensor: {
|
|
|
|
const state = ev3board().getInputNodes()[port] as ColorSensorNode;
|
|
|
|
if (state.getMode() == ColorSensorMode.Colors) {
|
|
|
|
view = new ColorGridControl(this.element, this.defs, state, port);
|
|
|
|
} else if (state.getMode() == ColorSensorMode.Reflected) {
|
|
|
|
view = new ColorWheelControl(this.element, this.defs, state, port);
|
|
|
|
} else if (state.getMode() == ColorSensorMode.Ambient) {
|
|
|
|
view = new ColorWheelControl(this.element, this.defs, state, port);
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
2017-12-18 22:04:17 +01:00
|
|
|
break;
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
2017-12-18 22:04:17 +01:00
|
|
|
case NodeType.UltrasonicSensor: {
|
|
|
|
const state = ev3board().getInputNodes()[port] as UltrasonicSensorNode;
|
|
|
|
view = new DistanceSliderControl(this.element, this.defs, state, port);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NodeType.GyroSensor: {
|
|
|
|
const state = ev3board().getInputNodes()[port] as GyroSensorNode;
|
|
|
|
view = new RotationSliderControl(this.element, this.defs, state, port);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NodeType.MediumMotor:
|
|
|
|
case NodeType.LargeMotor: {
|
|
|
|
// const state = ev3board().getMotor(port)[0];
|
|
|
|
// view = new MotorInputControl(this.element, this.defs, state, port);
|
|
|
|
// break;
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
if (view) {
|
|
|
|
if (!this.cachedControlNodes[id]) this.cachedControlNodes[id] = [];
|
|
|
|
this.cachedControlNodes[id][port] = view;
|
|
|
|
return view;
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
return undefined;
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
private getDisplayViewForNode(id: NodeType, port: number): LayoutElement {
|
|
|
|
if (this.cachedDisplayViews[id] && this.cachedDisplayViews[id][port]) {
|
|
|
|
return this.cachedDisplayViews[id][port];
|
|
|
|
}
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
let view: LayoutElement;
|
|
|
|
switch (id) {
|
|
|
|
case NodeType.TouchSensor:
|
|
|
|
view = new TouchSensorView(port); break;
|
|
|
|
case NodeType.MediumMotor:
|
|
|
|
view = new MediumMotorView(port); break;
|
|
|
|
case NodeType.LargeMotor:
|
|
|
|
view = new LargeMotorView(port); break;
|
|
|
|
case NodeType.GyroSensor:
|
|
|
|
view = new GyroSensorView(port); break;
|
|
|
|
case NodeType.ColorSensor:
|
|
|
|
view = new ColorSensorView(port); break;
|
|
|
|
case NodeType.UltrasonicSensor:
|
|
|
|
view = new UltrasonicSensorView(port); break;
|
|
|
|
case NodeType.Brick:
|
|
|
|
//return new BrickView(0);
|
|
|
|
view = this.layoutView.getBrick(); break;
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
if (view) {
|
|
|
|
if (!this.cachedDisplayViews[id]) this.cachedDisplayViews[id] = [];
|
|
|
|
this.cachedDisplayViews[id][port] = view;
|
|
|
|
return view;
|
|
|
|
}
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
return undefined;
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
private buildDom() {
|
|
|
|
this.wrapper = document.createElement('div');
|
|
|
|
this.wrapper.style.display = 'inline';
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.element = svg.elt("svg", { height: "100%", width: "100%" }) as SVGSVGElement;
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.defs = svg.child(this.element, "defs") as SVGDefsElement;
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.style = svg.child(this.element, "style", {}) as SVGStyleElement;
|
|
|
|
this.style.textContent = EV3_STYLE;
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.layoutView = new LayoutView();
|
|
|
|
this.layoutView.inject(this.element);
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.controlGroup = new ViewContainer();
|
|
|
|
this.controlGroup.inject(this.element);
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.closeGroup = new ViewContainer();
|
|
|
|
this.closeGroup.inject(this.element);
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
// Add EV3 module element
|
|
|
|
this.layoutView.setBrick(new BrickView(-1));
|
2017-07-12 09:25:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.closeIconView = new CloseIconControl(this.element, this.defs, new PortNode(-1), -1);
|
|
|
|
this.closeIconView.registerClick(() => {
|
|
|
|
this.layoutView.clearSelected();
|
|
|
|
this.updateState();
|
|
|
|
})
|
|
|
|
this.closeGroup.addView(this.closeIconView);
|
|
|
|
this.closeIconView.setVisible(false);
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.resize();
|
|
|
|
this.updateState();
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
// Add Screen canvas to board
|
|
|
|
this.buildScreenCanvas();
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.wrapper.appendChild(this.element);
|
|
|
|
this.wrapper.appendChild(this.screenCanvas);
|
|
|
|
this.wrapper.appendChild(this.screenCanvasTemp);
|
2017-07-12 09:25:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
window.addEventListener("resize", e => {
|
|
|
|
this.resize();
|
2017-07-12 09:25:17 +02:00
|
|
|
});
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
2017-07-12 09:25:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
private buildScreenCanvas() {
|
2017-07-12 09:25:17 +02:00
|
|
|
this.screenCanvas = document.createElement("canvas");
|
2017-12-18 22:04:17 +01:00
|
|
|
this.screenCanvas.id = "board-screen-canvas";
|
|
|
|
this.screenCanvas.style.position = "absolute";
|
2017-07-12 09:25:17 +02:00
|
|
|
this.screenCanvas.style.cursor = "crosshair";
|
|
|
|
this.screenCanvas.onmousemove = (e: MouseEvent) => {
|
|
|
|
const x = e.clientX;
|
|
|
|
const y = e.clientY;
|
2017-12-18 22:04:17 +01:00
|
|
|
const bBox = this.screenCanvas.getBoundingClientRect();
|
|
|
|
this.updateXY(Math.floor((x - bBox.left) / this.screenScaledWidth * SCREEN_WIDTH),
|
|
|
|
Math.floor((y - bBox.top) / this.screenScaledHeight * SCREEN_HEIGHT));
|
2017-07-12 09:25:17 +02:00
|
|
|
}
|
|
|
|
this.screenCanvas.onmouseleave = () => {
|
2017-12-18 22:04:17 +01:00
|
|
|
this.updateXY(SCREEN_WIDTH, SCREEN_HEIGHT);
|
2017-07-12 09:25:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.screenCanvas.width = SCREEN_WIDTH;
|
|
|
|
this.screenCanvas.height = SCREEN_HEIGHT;
|
2017-12-18 22:04:17 +01:00
|
|
|
|
2017-07-12 09:25:17 +02:00
|
|
|
this.screenCanvasCtx = this.screenCanvas.getContext("2d");
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.screenCanvasTemp = document.createElement("canvas");
|
|
|
|
this.screenCanvasTemp.style.display = 'none';
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
private updateScreen() {
|
|
|
|
let state = ev3board().screenState;
|
2017-12-19 21:53:12 +01:00
|
|
|
if (!state.didChange()) return;
|
2017-12-18 22:04:17 +01:00
|
|
|
|
|
|
|
const bBox = this.layoutView.getBrick().getScreenBBox();
|
2017-12-18 22:19:49 +01:00
|
|
|
if (!bBox || bBox.width == 0) return;
|
2017-12-18 22:04:17 +01:00
|
|
|
|
2017-12-19 02:07:23 +01:00
|
|
|
const scale = (bBox.width - 4) / SCREEN_WIDTH;
|
|
|
|
this.screenScaledWidth = (bBox.width - 4);
|
2017-12-18 22:04:17 +01:00
|
|
|
this.screenScaledHeight = this.screenScaledWidth / SCREEN_WIDTH * SCREEN_HEIGHT;
|
|
|
|
|
2017-12-19 02:07:23 +01:00
|
|
|
this.screenCanvas.style.top = `${bBox.top + 2}px`;
|
|
|
|
this.screenCanvas.style.left = `${bBox.left + 2}px`;
|
2017-12-18 22:04:17 +01:00
|
|
|
this.screenCanvas.width = this.screenScaledWidth;
|
|
|
|
this.screenCanvas.height = this.screenScaledHeight;
|
|
|
|
|
|
|
|
this.screenCanvasData = this.screenCanvasCtx.getImageData(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
|
|
|
|
|
|
|
|
let sp = 3
|
|
|
|
const points = state.points
|
|
|
|
const data = this.screenCanvasData.data
|
|
|
|
for (let i = 0; i < points.length; ++i) {
|
|
|
|
data[sp] = points[i]
|
|
|
|
sp += 4;
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
// Move the image to another canvas element in order to scale it
|
|
|
|
this.screenCanvasTemp.style.width = `${SCREEN_WIDTH}`;
|
|
|
|
this.screenCanvasTemp.style.height = `${SCREEN_HEIGHT}`;
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.screenCanvasTemp.getContext("2d").putImageData(this.screenCanvasData, 0, 0);
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
this.screenCanvasCtx.scale(scale, scale);
|
|
|
|
this.screenCanvasCtx.drawImage(this.screenCanvasTemp, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private updateXY(width: number, height: number) {
|
|
|
|
const screenWidth = Math.max(0, Math.min(SCREEN_WIDTH, width));
|
|
|
|
const screenHeight = Math.max(0, Math.min(SCREEN_HEIGHT, height));
|
|
|
|
console.log(`width: ${screenWidth}, height: ${screenHeight}`);
|
|
|
|
|
|
|
|
// TODO: add a reporter for the hovered XY position
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|