Loading new tiles when shift requires...
This commit is contained in:
parent
07551a887e
commit
2362ff2938
|
@ -1,5 +1,10 @@
|
||||||
import { IonContent } from '@ionic/react';
|
import { IonContent, ReactComponentOrElement } from '@ionic/react';
|
||||||
import react, { useState } from 'react';
|
import react, {
|
||||||
|
ReactComponentElement,
|
||||||
|
useEffect,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from 'react';
|
||||||
|
|
||||||
import './Map.css';
|
import './Map.css';
|
||||||
import MouseHandler from './MouseHandler';
|
import MouseHandler from './MouseHandler';
|
||||||
|
@ -32,21 +37,74 @@ const Map: react.FC<MapProperties> = (props: MapProperties) => {
|
||||||
setZoom(zoom * zoomFactor);
|
setZoom(zoom * zoomFactor);
|
||||||
};
|
};
|
||||||
|
|
||||||
var tiledLayer: any[] = [];
|
console.log(`Rendering Map, zoom:${zoom}, shift:${JSON.stringify(shift)}`);
|
||||||
|
|
||||||
|
var initialTiledLayer: any[][] = [];
|
||||||
for (let row = 0; row < nbTiles; row++) {
|
for (let row = 0; row < nbTiles; row++) {
|
||||||
|
let tileRow = [];
|
||||||
for (let col = 0; col < nbTiles; col++) {
|
for (let col = 0; col < nbTiles; col++) {
|
||||||
tiledLayer.push(
|
tileRow.push(<g key={`${row}/${col}`} />);
|
||||||
<Tile
|
|
||||||
key={`${row}/${col}`}
|
|
||||||
href={fakeTile}
|
|
||||||
x={col * 256}
|
|
||||||
y={row * 256}
|
|
||||||
delay={20 * col * row}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
initialTiledLayer.push(tileRow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const [tiledLayer, setTiledLayer] = useState(initialTiledLayer);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const firstVisibleTiles = {
|
||||||
|
x: Math.max(Math.floor(-shift.x / 256), 0),
|
||||||
|
y: Math.max(Math.floor(-shift.y / 256), 0),
|
||||||
|
};
|
||||||
|
|
||||||
|
const lastVisibleTiles = {
|
||||||
|
x: Math.min(
|
||||||
|
Math.ceil(firstVisibleTiles.x + props.width / 256),
|
||||||
|
nbTiles - 1
|
||||||
|
),
|
||||||
|
y: Math.min(
|
||||||
|
Math.ceil(firstVisibleTiles.y + props.height / 256),
|
||||||
|
nbTiles - 1
|
||||||
|
),
|
||||||
|
};
|
||||||
|
console.log(
|
||||||
|
`Adding new tiles if needed for ${JSON.stringify(
|
||||||
|
firstVisibleTiles
|
||||||
|
)}/${JSON.stringify(lastVisibleTiles)}.`
|
||||||
|
);
|
||||||
|
const newTiledLayer: any[][] = [];
|
||||||
|
for (let row = 0; row < nbTiles; row++) {
|
||||||
|
let tileRow = [];
|
||||||
|
for (let col = 0; col < nbTiles; col++) {
|
||||||
|
const key = `${row}/${col}`;
|
||||||
|
if (
|
||||||
|
tiledLayer[row][col].type === 'g' &&
|
||||||
|
row >= firstVisibleTiles.y &&
|
||||||
|
row <= lastVisibleTiles.y &&
|
||||||
|
col >= firstVisibleTiles.x &&
|
||||||
|
col <= lastVisibleTiles.x
|
||||||
|
) {
|
||||||
|
tileRow.push(
|
||||||
|
<Tile
|
||||||
|
key={key}
|
||||||
|
href={fakeTile}
|
||||||
|
x={col * 256}
|
||||||
|
y={row * 256}
|
||||||
|
delay={1000}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// tileRow.push(<g key={key} />);
|
||||||
|
tileRow.push(tiledLayer[row][col]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newTiledLayer.push(tileRow);
|
||||||
|
}
|
||||||
|
|
||||||
|
setTiledLayer(newTiledLayer);
|
||||||
|
}, [shift, zoom, nbTiles]);
|
||||||
|
|
||||||
|
// console.log(`tiledLayer: ${JSON.stringify(tiledLayer)}`);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<IonContent fullscreen={true}>
|
<IonContent fullscreen={true}>
|
||||||
<div
|
<div
|
||||||
|
|
|
@ -4,7 +4,10 @@ interface SlippyBoardProperties {
|
||||||
boardSize: number;
|
boardSize: number;
|
||||||
shift: { x: number; y: number };
|
shift: { x: number; y: number };
|
||||||
zoom: number;
|
zoom: number;
|
||||||
layers?: { key: string; content: ReactElement | ReactElement[] }[];
|
layers?: {
|
||||||
|
key: string;
|
||||||
|
content: ReactElement | ReactElement[] | ReactElement[][];
|
||||||
|
}[];
|
||||||
}
|
}
|
||||||
|
|
||||||
const SlippyBoard: react.FC<SlippyBoardProperties> = (
|
const SlippyBoard: react.FC<SlippyBoardProperties> = (
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import react, { useEffect, useMemo, useRef, useState } from 'react';
|
import react, { useEffect, useMemo, useRef, useState } from 'react';
|
||||||
|
|
||||||
interface TileProperties {
|
interface TileProperties {
|
||||||
href: string;
|
href?: string;
|
||||||
x: number;
|
x: number;
|
||||||
y: number;
|
y: number;
|
||||||
delay?: number;
|
delay?: number;
|
||||||
|
@ -16,32 +16,36 @@ const Tile: react.FC<TileProperties> = (props: TileProperties) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const loadImage = async () => {
|
if (props.href !== undefined) {
|
||||||
console.log(`Pre loading: ${JSON.stringify(props.href)}`);
|
const loadImage = async () => {
|
||||||
const image = new Image(256, 256);
|
console.log(`Pre loading: ${JSON.stringify(props.href)}`);
|
||||||
// const image:SVGImageElement = document.createElement('image') as unknown as SVGImageElement;
|
const image = new Image(256, 256);
|
||||||
// const image = new SVGImageElement();
|
// const image:SVGImageElement = document.createElement('image') as unknown as SVGImageElement;
|
||||||
image.loading = 'eager';
|
// const image = new SVGImageElement();
|
||||||
image.setAttribute('href', props.href);
|
image.loading = 'eager';
|
||||||
if (!image.complete) {
|
// @ts-ignore
|
||||||
await image.decode();
|
image.setAttribute('href', props.href);
|
||||||
}
|
if (!image.complete) {
|
||||||
if (props.delay !== undefined) {
|
await image.decode();
|
||||||
await timeout(props.delay);
|
}
|
||||||
}
|
if (props.delay !== undefined) {
|
||||||
const svgImage = document.createElementNS(
|
await timeout(props.delay);
|
||||||
'http://www.w3.org/2000/svg',
|
}
|
||||||
'image'
|
const svgImage = document.createElementNS(
|
||||||
) as unknown as SVGImageElement;
|
'http://www.w3.org/2000/svg',
|
||||||
svgImage.setAttribute('width', '256');
|
'image'
|
||||||
svgImage.setAttribute('height', '256');
|
) as unknown as SVGImageElement;
|
||||||
svgImage.setAttribute('href', props.href);
|
svgImage.setAttribute('width', '256');
|
||||||
// svgImage.setAttribute('x', '0');
|
svgImage.setAttribute('height', '256');
|
||||||
// svgImage.setAttribute('y', '0');
|
// @ts-ignore
|
||||||
g.current?.replaceChildren(svgImage);
|
svgImage.setAttribute('href', props.href);
|
||||||
};
|
// svgImage.setAttribute('x', '0');
|
||||||
loadImage();
|
// svgImage.setAttribute('y', '0');
|
||||||
}, []);
|
g.current?.replaceChildren(svgImage);
|
||||||
|
};
|
||||||
|
loadImage();
|
||||||
|
}
|
||||||
|
}, [props.href]);
|
||||||
|
|
||||||
// const children = useMemo(
|
// const children = useMemo(
|
||||||
// () => (
|
// () => (
|
||||||
|
@ -52,9 +56,11 @@ const Tile: react.FC<TileProperties> = (props: TileProperties) => {
|
||||||
// []
|
// []
|
||||||
// );
|
// );
|
||||||
|
|
||||||
return <>
|
return (
|
||||||
<g ref={g} transform={`translate(${props.x},${props.y})`} />
|
<>
|
||||||
</>;
|
<g ref={g} transform={`translate(${props.x},${props.y})`} />
|
||||||
|
</>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Tile;
|
export default Tile;
|
||||||
|
|
Loading…
Reference in New Issue