Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix eslint setting #26

Merged
merged 1 commit into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/type-annotation-spacing": ["warn", {
"before": false,
"after": true
}],
"no-redeclare":"off",
"prefer-destructuring":"off",
"no-underscore-dangle":"off",
Expand Down
184 changes: 92 additions & 92 deletions lib/crosshair-manager.ts
Original file line number Diff line number Diff line change
@@ -1,120 +1,120 @@
import { Map as MaplibreMap } from 'maplibre-gl';

export default class CrosshairManager {
private map: MaplibreMap | undefined;
private map: MaplibreMap | undefined;

private width: number | undefined;
private width: number | undefined;

private height: number | undefined;
private height: number | undefined;

private svgCanvas: SVGElement | undefined;
private svgCanvas: SVGElement | undefined;

private xLine: SVGElement | undefined;
private xLine: SVGElement | undefined;

private yLine: SVGElement | undefined;
private yLine: SVGElement | undefined;

private color = '#535353';
private color = '#535353';

constructor(
map: MaplibreMap | undefined,
) {
this.map = map;
this.mapResize = this.mapResize.bind(this);
}
constructor(
map: MaplibreMap | undefined,
) {
this.map = map;
this.mapResize = this.mapResize.bind(this);
}

public create() {
this.updateValues();
if (this.map !== undefined) {
this.map.on('resize', this.mapResize);
this.createCanvas(this.map.getCanvasContainer());
} else {
console.error('map object is null');
}
public create() {
this.updateValues();
if (this.map !== undefined) {
this.map.on('resize', this.mapResize);
this.createCanvas(this.map.getCanvasContainer());
} else {
console.error('map object is null');
}
}

private updateValues() {
this.width = this.map?.getCanvas().clientWidth;
this.height = this.map?.getCanvas().clientHeight;
}
private updateValues() {
this.width = this.map?.getCanvas().clientWidth;
this.height = this.map?.getCanvas().clientHeight;
}

private mapResize() {
this.updateValues();
this.updateCanvas();
}
private mapResize() {
this.updateValues();
this.updateCanvas();
}

private updateCanvas() {
if (
this.svgCanvas !== undefined
private updateCanvas() {
if (
this.svgCanvas !== undefined
&& this.yLine !== undefined
&& this.xLine !== undefined
&& this.width !== undefined
&& this.height !== undefined) {
this.svgCanvas.setAttribute('width', `${this.width}px`);
this.svgCanvas.setAttribute('height', `${this.height}px`);
const halfWidth = this.width / 2;
const halfHeight = this.height / 2;
this.yLine.setAttribute('x1', `${halfWidth}px`);
this.yLine.setAttribute('y1', '0px');
this.yLine.setAttribute('x2', `${halfWidth}px`);
this.yLine.setAttribute('y2', `${this.height}px`);

this.xLine.setAttribute('x1', `${0}px`);
this.xLine.setAttribute('y1', `${halfHeight}px`);
this.xLine.setAttribute('x2', `${this.width}px`);
this.xLine.setAttribute('y2', `${halfHeight}px`);
} else {
console.error('element value is null');
}
this.svgCanvas.setAttribute('width', `${this.width}px`);
this.svgCanvas.setAttribute('height', `${this.height}px`);
const halfWidth = this.width / 2;
const halfHeight = this.height / 2;
this.yLine.setAttribute('x1', `${halfWidth}px`);
this.yLine.setAttribute('y1', '0px');
this.yLine.setAttribute('x2', `${halfWidth}px`);
this.yLine.setAttribute('y2', `${this.height}px`);

this.xLine.setAttribute('x1', `${0}px`);
this.xLine.setAttribute('y1', `${halfHeight}px`);
this.xLine.setAttribute('x2', `${this.width}px`);
this.xLine.setAttribute('y2', `${halfHeight}px`);
} else {
console.error('element value is null');
}
}

private createCanvas(container) {
if (
this.width !== undefined
private createCanvas(container) {
if (
this.width !== undefined
&& this.height !== undefined) {
const canvas = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
canvas.style.position = 'relative';
canvas.setAttribute('width', `${this.width}px`);
canvas.setAttribute('height', `${this.height}px`);
const halfWidth = this.width / 2;
const halfHeight = this.height / 2;
this.yLine = canvas.appendChild(this.createLine(halfWidth, 0, halfWidth, this.height, this.color, '2px'));
this.xLine = canvas.appendChild(this.createLine(0, halfHeight, this.width, halfHeight, this.color, '2px'));
container?.appendChild(canvas);
this.svgCanvas = canvas;
}
const canvas = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
canvas.style.position = 'relative';
canvas.setAttribute('width', `${this.width}px`);
canvas.setAttribute('height', `${this.height}px`);
const halfWidth = this.width / 2;
const halfHeight = this.height / 2;
this.yLine = canvas.appendChild(this.createLine(halfWidth, 0, halfWidth, this.height, this.color, '2px'));
this.xLine = canvas.appendChild(this.createLine(0, halfHeight, this.width, halfHeight, this.color, '2px'));
container?.appendChild(canvas);
this.svgCanvas = canvas;
}
}

private createLine(x1, y1, x2, y2, color, w) {
const aLine = document.createElementNS('http://www.w3.org/2000/svg', 'line');
aLine.setAttribute('x1', x1);
aLine.setAttribute('y1', y1);
aLine.setAttribute('x2', x2);
aLine.setAttribute('y2', y2);
aLine.setAttribute('stroke-dasharray', '5,5');
aLine.setAttribute('stroke', color);
aLine.setAttribute('stroke-width', w);
return aLine;
}

public destroy() {
if (this.xLine !== undefined) {
this.xLine.remove();
this.xLine = undefined;
}

if (this.yLine !== undefined) {
this.yLine.remove();
this.yLine = undefined;
}

private createLine(x1, y1, x2, y2, color, w) {
const aLine = document.createElementNS('http://www.w3.org/2000/svg', 'line');
aLine.setAttribute('x1', x1);
aLine.setAttribute('y1', y1);
aLine.setAttribute('x2', x2);
aLine.setAttribute('y2', y2);
aLine.setAttribute('stroke-dasharray', '5,5');
aLine.setAttribute('stroke', color);
aLine.setAttribute('stroke-width', w);
return aLine;
if (this.svgCanvas !== undefined) {
this.svgCanvas.remove();
this.svgCanvas = undefined;
}

public destroy() {
if (this.xLine !== undefined) {
this.xLine.remove();
this.xLine = undefined;
}

if (this.yLine !== undefined) {
this.yLine.remove();
this.yLine = undefined;
}

if (this.svgCanvas !== undefined) {
this.svgCanvas.remove();
this.svgCanvas = undefined;
}

if (this.map !== undefined) {
this.map.off('resize', this.mapResize);
this.map = undefined;
}
if (this.map !== undefined) {
this.map.off('resize', this.mapResize);
this.map = undefined;
}
}
}
Loading