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 TODO: params parsing and so on
#*/ #*/
import config from './src/config'; import config from './src/config.ts';
import Mapscii from './src/Mapscii'; import Mapscii from './src/Mapscii.ts';
import yargs from 'yargs/yargs'; import yargs from 'yargs/yargs';
import { hideBin } from 'yargs/helpers'; 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 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 stringWidth from 'string-width';
import config from './config'; import config from './config.ts';
import utils from './utils'; import utils from './utils.ts';
const asciiMap = { const asciiMap = {
// '▬': [2+32, 4+64], // '▬': [2+32, 4+64],

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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