fix right-shift historical view when time selected

This commit is contained in:
HF 2022-04-02 23:48:26 +02:00
parent cf74ca9f32
commit 4d367d5232
3 changed files with 36 additions and 19 deletions

View File

@ -110,7 +110,7 @@ class ChunkLoader {
const incrementialChunkKey = `${historicalDate}${historicalTime}:${cx}:${cy}`; const incrementialChunkKey = `${historicalDate}${historicalTime}:${cx}:${cy}`;
const incrementialChunk = this.chunks.get(incrementialChunkKey); const incrementialChunk = this.chunks.get(incrementialChunkKey);
if (incrementialChunk) { if (incrementialChunk) {
const incrementialColor = incrementialChunk.getColorIndex(px); const incrementialColor = incrementialChunk.getColorIndex(px, false);
incrementialChunk.timestamp = curTime; incrementialChunk.timestamp = curTime;
if (incrementialColor !== null) { if (incrementialColor !== null) {
return incrementialColor; return incrementialColor;

View File

@ -2,12 +2,21 @@ import { TILE_SIZE } from '../core/constants';
class ChunkRGB { class ChunkRGB {
cell: Array; // array of coords [zoom, cx, cy]
image: HTMLCanvasElement; // just an identifier, could be removed
ready: boolean; cell;
timestamp: number; // HTMLCanvasElement of chunk
image;
// boolean if chunk loeaded (request done)
ready;
// boolean if chunk is empty
isEmpty;
// number timestamp of last load for garbage collection
timestamp;
// palette of canvas
palette; palette;
isBasechunk: boolean; // boolean if it is basechunk, rather than zoomtile
isBasechunk;
constructor(palette, zoom = 0, cx = 0, cy = 0) { constructor(palette, zoom = 0, cx = 0, cy = 0) {
// isBasechunk gets set to true by RECEIVE_BIG_CHUNK // isBasechunk gets set to true by RECEIVE_BIG_CHUNK
@ -21,6 +30,7 @@ class ChunkRGB {
this.image.height = TILE_SIZE; this.image.height = TILE_SIZE;
this.cell = [zoom, cx, cy]; this.cell = [zoom, cx, cy];
this.ready = false; this.ready = false;
this.isEmpty = false;
this.timestamp = Date.now(); this.timestamp = Date.now();
} }
@ -29,7 +39,8 @@ class ChunkRGB {
return null; return null;
} }
fromBuffer(chunkBuffer: Uint8Array) { // from Uint8Array
fromBuffer(chunkBuffer) {
const imageData = new ImageData(TILE_SIZE, TILE_SIZE); const imageData = new ImageData(TILE_SIZE, TILE_SIZE);
const imageView = new Uint32Array(imageData.data.buffer); const imageView = new Uint32Array(imageData.data.buffer);
const colors = this.palette.buffer2ABGR(chunkBuffer); const colors = this.palette.buffer2ABGR(chunkBuffer);
@ -41,7 +52,7 @@ class ChunkRGB {
this.ready = true; this.ready = true;
} }
preLoad(img, zoomDiffAbs: number, sx: number, sy: number) { preLoad(img, zoomDiffAbs, sx, sy) {
if (this.ready) { if (this.ready) {
return; return;
} }
@ -57,14 +68,15 @@ class ChunkRGB {
ctx.restore(); ctx.restore();
} }
fromImage(img: Image) { fromImage(img) {
this.ready = true; this.ready = true;
const ctx = this.image.getContext('2d'); const ctx = this.image.getContext('2d');
ctx.drawImage(img, 0, 0); ctx.drawImage(img, 0, 0);
} }
empty(transparent: boolean = false) { empty(transparent = false) {
this.ready = true; this.ready = true;
this.isEmpty = true;
if (!transparent) { if (!transparent) {
const { image, palette } = this; const { image, palette } = this;
const ctx = image.getContext('2d'); const ctx = image.getContext('2d');
@ -74,19 +86,22 @@ class ChunkRGB {
} }
} }
static getIndexFromCell([x, y]): number { static getIndexFromCell([x, y]) {
return x + (TILE_SIZE * y); return x + (TILE_SIZE * y);
} }
getColorIndex(cell) { getColorIndex(cell, nearest = true) {
const [x, y] = cell; const [x, y] = cell;
const ctx = this.image.getContext('2d'); const ctx = this.image.getContext('2d');
const rgb = ctx.getImageData(x, y, 1, 1).data; const rgb = ctx.getImageData(x, y, 1, 1).data;
return this.palette.getClosestIndexOfColor(rgb[0], rgb[1], rgb[2]); const ind = (nearest)
? this.palette.getClosestIndexOfColor(rgb[0], rgb[1], rgb[2])
: this.palette.getIndexOfColor(rgb[0], rgb[1], rgb[2]);
return ind;
} }
hasColorIn(cell, color): boolean { hasColorIn(cell, color) {
const index = ChunkRGB.getIndexFromCell(cell); const index = ChunkRGB.getIndexFromCell(cell);
const ctx = this.image.getContext('2d'); const ctx = this.image.getContext('2d');
@ -96,7 +111,7 @@ class ChunkRGB {
return (intView[index] === this.palette.abgr[color]); return (intView[index] === this.palette.abgr[color]);
} }
setColor(cell, color): boolean { setColor(cell, color) {
const [x, y] = cell; const [x, y] = cell;
const ctx = this.image.getContext('2d'); const ctx = this.image.getContext('2d');
ctx.fillStyle = this.palette.colors[color]; ctx.fillStyle = this.palette.colors[color];

View File

@ -632,9 +632,11 @@ class Renderer {
chunk = this.chunkLoader chunk = this.chunkLoader
.getHistoricalChunk(cx, cy, fetch, historicalDate, historicalTime); .getHistoricalChunk(cx, cy, fetch, historicalDate, historicalTime);
if (chunk) { if (chunk) {
context.drawImage(chunk, x, y); if (!chunk.isEmpty) {
if (fetch) { context.drawImage(chunk, x, y);
chunk.timestamp = curTime; if (fetch) {
chunk.timestamp = curTime;
}
} }
} else if (oldHistoricalTime) { } else if (oldHistoricalTime) {
chunk = this.chunkLoader chunk = this.chunkLoader
@ -645,7 +647,7 @@ class Renderer {
historicalDate, historicalDate,
oldHistoricalTime, oldHistoricalTime,
); );
if (chunk) { if (chunk && !chunk.isEmpty) {
context.drawImage(chunk, x, y); context.drawImage(chunk, x, y);
if (fetch) { if (fetch) {
chunk.timestamp = curTime; chunk.timestamp = curTime;