More types and small refactorings (runs with Bun and Deno 2.0)

This commit is contained in:
Christian Paul 2024-11-03 10:42:19 +01:00
parent 98051b62ca
commit e18d1214d8
9 changed files with 64 additions and 43 deletions

View File

@ -7,8 +7,8 @@
TODO: params parsing and so on
#*/
import config from './src/config';
import Mapscii from './src/Mapscii';
import config from './src/config.ts';
import Mapscii from './src/Mapscii.ts';
import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers';

View File

@ -11,9 +11,10 @@
Will either be merged into node-drawille or become an own module at some point
*/
import { Buffer } from 'node:buffer';
import stringWidth from 'string-width';
import config from './config';
import utils from './utils';
import config from './config.ts';
import utils from './utils.ts';
const asciiMap = {
// '▬': [2+32, 4+64],

View File

@ -12,7 +12,7 @@
*/
import bresenham from 'bresenham';
import earcut from 'earcut';
import BrailleBuffer from './BrailleBuffer';
import BrailleBuffer from './BrailleBuffer.ts';
class Canvas {
private buffer: BrailleBuffer;

View File

@ -7,6 +7,7 @@
*/
import RBush from 'rbush';
import stringWidth from 'string-width';
import { Feature } from './Renderer.ts';
export default class LabelBuffer {
private tree: RBush;
@ -17,15 +18,15 @@ export default class LabelBuffer {
this.margin = 5;
}
clear() {
clear(): void {
this.tree.clear();
}
project(x: number, y: number) {
project(x: number, y: number): [number, number] {
return [Math.floor(x/2), Math.floor(y/4)];
}
writeIfPossible(text: string, x: number, y: number, feature, margin: number) {
writeIfPossible(text: string, x: number, y: number, feature, margin: number): boolean {
margin = margin || this.margin;
const point = this.project(x, y);
@ -39,20 +40,20 @@ export default class LabelBuffer {
}
}
featuresAt(x: number, y: number) {
featuresAt(x: number, y: number): Feature[] {
this.tree.search({minX: x, maxX: x, minY: y, maxY: y});
}
_hasSpace(text: string, x: number, y: number) {
_hasSpace(text: string, x: number, y: number): boolean {
return !this.tree.collides(this._calculateArea(text, x, y));
}
_calculateArea(text: string, x: number, y: number, margin = 0) {
return {
minX: x-margin,
minY: y-margin/2,
maxX: x+margin+stringWidth(text),
maxY: y+margin/2,
minY: y-margin / 2,
maxX: x+margin + stringWidth(text),
maxY: y+margin / 2,
};
}
};

View File

@ -4,15 +4,15 @@
UI and central command center
*/
import fs from 'fs';
import fs from 'node:fs';
import keypress from 'keypress';
import TermMouse from 'term-mouse';
import type Canvas from './Canvas';
import Renderer from './Renderer';
import TileSource from './TileSource';
import utils from './utils';
import globalConfig from './config';
import type Canvas from './Canvas.ts';
import Renderer from './Renderer.ts';
import TileSource from './TileSource.ts';
import utils from './utils.ts';
import globalConfig from './config.ts';
let config = globalConfig;

View File

@ -7,12 +7,15 @@
import x256 from 'x256';
import simplify from 'simplify-js';
import Canvas from './Canvas';
import LabelBuffer from './LabelBuffer';
import Styler from './Styler';
import utils from './utils';
import config from './config';
import TileSource from './TileSource';
import Canvas from './Canvas.ts';
import LabelBuffer from './LabelBuffer.ts';
import Styler from './Styler.ts';
import utils from './utils.ts';
import config from './config.ts';
import TileSource from './TileSource.ts';
import Tile from './Tile.ts';
export type Feature = unknown;
class Renderer {
private canvas: Canvas | undefined;
@ -87,7 +90,7 @@ class Renderer {
const z = utils.baseZoom(zoom);
center = utils.ll2tile(center.lon, center.lat, z);
const tiles = [];
const tiles: Tile[] = [];
const tileSize = utils.tilesizeAtZoom(zoom);
for (let y = Math.floor(center.y) - 1; y <= Math.floor(center.y) + 1; y++) {
@ -121,12 +124,12 @@ class Renderer {
return tiles;
}
async _getTile(tile) {
async _getTile(tile: Tile): Promise<Tile> {
tile.data = await this.tileSource.getTile(tile.xyz.z, tile.xyz.x, tile.xyz.y);
return tile;
}
_getTileFeatures(tile, zoom) {
_getTileFeatures(tile: Tile, zoom: number): Tile {
const position = tile.position;
const layers = {};
const drawOrder = this._generateDrawOrder(zoom);
@ -152,7 +155,11 @@ class Renderer {
}
_renderTiles(tiles) {
const labels = [];
const labels: {
tile: Tile,
feature: any,
scale: unknown,
}[] = [];
if (tiles.length === 0) return;
const drawOrder = this._generateDrawOrder(tiles[0].xyz.z);
@ -167,7 +174,7 @@ class Renderer {
labels.push({
tile,
feature,
scale: layer.scale
scale: layer.scale,
});
} else {
this._drawFeature(tile, feature, layer.scale);
@ -195,11 +202,11 @@ class Renderer {
return frame;
}
featuresAt(x, y) {
featuresAt(x: number, y: number): Feature[] {
return this.labelBuffer.featuresAt(x, y);
}
_drawFeature(tile, feature, scale) {
_drawFeature(tile, feature, scale: number): boolean {
let points, placed;
if (feature.style.minzoom && tile.zoom < feature.style.minzoom) {
return false;
@ -263,11 +270,11 @@ class Renderer {
return true;
}
_scaleAndReduce(tile, feature, points, scale, filter = true) {
_scaleAndReduce(tile: Tile, feature: Feature, points: {x: number, y: number}[], scale: number, filter = true) {
let lastX;
let lastY;
let outside;
const scaled = [];
const scaled: {x: number, y: number}[] = [];
const minX = -this.tilePadding;
const minY = -this.tilePadding;

View File

@ -4,21 +4,33 @@
Handling of and access to single VectorTiles
*/
import { Buffer } from 'node:buffer';
import zlib from 'node:zlib';
import { VectorTile } from '@mapbox/vector-tile';
import Protobuf from 'pbf';
import zlib from 'zlib';
import RBush from 'rbush';
import x256 from 'x256';
import config from './config';
import utils from './utils';
import Styler from './Styler';
import config from './config.ts';
import utils from './utils.ts';
import Styler from './Styler.ts';
class Tile {
public layers: any[];
private styler: Styler;
private tile: VectorTile;
public position: {
x: number,
y: number,
};
public xyz: {
x: number,
y: number,
z: number,
};
public zoom: number;
constructor(styler: Styler) {
this.styler = styler;
}

View File

@ -6,14 +6,14 @@
* remote TileServer
* local MBTiles and VectorTiles
*/
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
import fetch from 'node-fetch';
import envPaths from 'env-paths';
const paths = envPaths('mapscii');
import Tile from './Tile';
import config from './config';
import Tile from './Tile.ts';
import config from './config.ts';
// https://github.com/mapbox/node-mbtiles has native build dependencies (sqlite3)
// To maximize MapSCIIs compatibility, MBTiles support must be manually added via

View File

@ -4,7 +4,7 @@
methods used all around
*/
import config from './config';
import config from './config.ts';
const constants = {
RADIUS: 6378137,