Playing with multiple tiled layers.
This commit is contained in:
parent
ca2907d886
commit
268d2378bd
|
@ -1,5 +1,5 @@
|
||||||
import { IonContent } from '@ionic/react';
|
import { IonContent } from '@ionic/react';
|
||||||
import react, { useEffect, useState } from 'react';
|
import react, { useState } from 'react';
|
||||||
|
|
||||||
import './Map.css';
|
import './Map.css';
|
||||||
import MouseHandler from './MouseHandler';
|
import MouseHandler from './MouseHandler';
|
||||||
|
@ -86,13 +86,31 @@ const Map: react.FC<MapProperties> = (props: MapProperties) => {
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'tiles',
|
key: 'tiles1',
|
||||||
content: (
|
content: (
|
||||||
<TiledLayer
|
<TiledLayer
|
||||||
height={props.height}
|
height={props.height}
|
||||||
width={props.width}
|
width={props.width}
|
||||||
shift={shift}
|
shift={shift}
|
||||||
zoom={zoom}
|
zoom={zoom}
|
||||||
|
tileSize={256}
|
||||||
|
nbTiles={nbTiles}
|
||||||
|
active={zoom < 2}
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'tiles2',
|
||||||
|
transform: 'scale(.5)',
|
||||||
|
content: (
|
||||||
|
<TiledLayer
|
||||||
|
height={props.height * 2}
|
||||||
|
width={props.width * 2}
|
||||||
|
shift={{ x: shift.x * 2, y: shift.y * 2 }}
|
||||||
|
zoom={zoom}
|
||||||
|
tileSize={256}
|
||||||
|
nbTiles={nbTiles * 2}
|
||||||
|
active={zoom >= 2}
|
||||||
/>
|
/>
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import { ReactComponentOrElement } from '@ionic/react';
|
import react, { ReactElement, ReactNode } from 'react';
|
||||||
import react, { ReactComponentElement, ReactElement, ReactNode } from 'react';
|
|
||||||
|
|
||||||
interface SlippyBoardProperties {
|
interface SlippyBoardProperties {
|
||||||
boardSize: number;
|
boardSize: number;
|
||||||
|
@ -7,7 +6,8 @@ interface SlippyBoardProperties {
|
||||||
zoom: number;
|
zoom: number;
|
||||||
layers?: {
|
layers?: {
|
||||||
key: string;
|
key: string;
|
||||||
content: ReactElement|ReactNode | ReactElement[] | ReactElement[][];
|
transform?: string;
|
||||||
|
content: ReactElement | ReactNode | ReactElement[] | ReactElement[][];
|
||||||
}[];
|
}[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,15 @@ const SlippyBoard: react.FC<SlippyBoardProperties> = (
|
||||||
transform={`translate(${props.shift.x}, ${props.shift.y}) scale(${props.zoom})`}
|
transform={`translate(${props.shift.x}, ${props.shift.y}) scale(${props.zoom})`}
|
||||||
>
|
>
|
||||||
{props.layers
|
{props.layers
|
||||||
? props.layers.map((layer) => <g key={layer.key}>{layer.content}</g>)
|
? props.layers.map((layer) => (
|
||||||
|
<g
|
||||||
|
className='layer'
|
||||||
|
key={layer.key}
|
||||||
|
transform={layer.transform === undefined ? '' : layer.transform}
|
||||||
|
>
|
||||||
|
{layer.content}
|
||||||
|
</g>
|
||||||
|
))
|
||||||
: null}
|
: null}
|
||||||
</g>
|
</g>
|
||||||
</svg>
|
</svg>
|
||||||
|
|
|
@ -4,6 +4,7 @@ interface TileProperties {
|
||||||
href?: string;
|
href?: string;
|
||||||
x: number;
|
x: number;
|
||||||
y: number;
|
y: number;
|
||||||
|
tileSize: number;
|
||||||
delay?: number;
|
delay?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +20,7 @@ const Tile: react.FC<TileProperties> = (props: TileProperties) => {
|
||||||
if (props.href !== undefined) {
|
if (props.href !== undefined) {
|
||||||
const loadImage = async () => {
|
const loadImage = async () => {
|
||||||
console.log(`Pre loading: ${JSON.stringify(props.href)}`);
|
console.log(`Pre loading: ${JSON.stringify(props.href)}`);
|
||||||
const image = new Image(256, 256);
|
const image = new Image(props.tileSize, props.tileSize);
|
||||||
// const image:SVGImageElement = document.createElement('image') as unknown as SVGImageElement;
|
// const image:SVGImageElement = document.createElement('image') as unknown as SVGImageElement;
|
||||||
// const image = new SVGImageElement();
|
// const image = new SVGImageElement();
|
||||||
image.loading = 'eager';
|
image.loading = 'eager';
|
||||||
|
@ -35,8 +36,8 @@ const Tile: react.FC<TileProperties> = (props: TileProperties) => {
|
||||||
'http://www.w3.org/2000/svg',
|
'http://www.w3.org/2000/svg',
|
||||||
'image'
|
'image'
|
||||||
) as unknown as SVGImageElement;
|
) as unknown as SVGImageElement;
|
||||||
svgImage.setAttribute('width', '256');
|
svgImage.setAttribute('width', props.tileSize.toString());
|
||||||
svgImage.setAttribute('height', '256');
|
svgImage.setAttribute('height', props.tileSize.toString());
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
svgImage.setAttribute('href', props.href);
|
svgImage.setAttribute('href', props.href);
|
||||||
// svgImage.setAttribute('x', '0');
|
// svgImage.setAttribute('x', '0');
|
||||||
|
@ -45,7 +46,7 @@ const Tile: react.FC<TileProperties> = (props: TileProperties) => {
|
||||||
};
|
};
|
||||||
loadImage();
|
loadImage();
|
||||||
}
|
}
|
||||||
}, [props.href, props.delay]);
|
}, [props.href, props.delay, props.tileSize]);
|
||||||
|
|
||||||
// const children = useMemo(
|
// const children = useMemo(
|
||||||
// () => (
|
// () => (
|
||||||
|
|
|
@ -12,18 +12,18 @@ interface TiledLayerProperties {
|
||||||
width: number;
|
width: number;
|
||||||
shift: Point;
|
shift: Point;
|
||||||
zoom: number;
|
zoom: number;
|
||||||
|
tileSize: number;
|
||||||
|
nbTiles: number;
|
||||||
|
active: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const TiledLayer: react.FC<TiledLayerProperties> = (
|
const TiledLayer: react.FC<TiledLayerProperties> = (
|
||||||
props: TiledLayerProperties
|
props: TiledLayerProperties
|
||||||
) => {
|
) => {
|
||||||
const nbTiles =
|
|
||||||
Math.ceil((Math.max(props.width, props.height) * 2) / 256) + 2;
|
|
||||||
|
|
||||||
var initialTiledLayer: any[][] = [];
|
var initialTiledLayer: any[][] = [];
|
||||||
for (let row = 0; row < nbTiles; row++) {
|
for (let row = 0; row < props.nbTiles; row++) {
|
||||||
let tileRow = [];
|
let tileRow = [];
|
||||||
for (let col = 0; col < nbTiles; col++) {
|
for (let col = 0; col < props.nbTiles; col++) {
|
||||||
tileRow.push(<g key={`${row}/${col}`} />);
|
tileRow.push(<g key={`${row}/${col}`} />);
|
||||||
}
|
}
|
||||||
initialTiledLayer.push(tileRow);
|
initialTiledLayer.push(tileRow);
|
||||||
|
@ -32,19 +32,30 @@ const TiledLayer: react.FC<TiledLayerProperties> = (
|
||||||
const [tiledLayer, setTiledLayer] = useState(initialTiledLayer);
|
const [tiledLayer, setTiledLayer] = useState(initialTiledLayer);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (props.active) {
|
||||||
const firstVisibleTiles = {
|
const firstVisibleTiles = {
|
||||||
x: Math.max(Math.floor(-props.shift.x / 256 / props.zoom), 0),
|
x: Math.max(
|
||||||
y: Math.max(Math.floor(-props.shift.y / 256 / props.zoom), 0),
|
Math.floor(-props.shift.x / props.tileSize / props.zoom),
|
||||||
|
0
|
||||||
|
),
|
||||||
|
y: Math.max(
|
||||||
|
Math.floor(-props.shift.y / props.tileSize / props.zoom),
|
||||||
|
0
|
||||||
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
const lastVisibleTiles = {
|
const lastVisibleTiles = {
|
||||||
x: Math.min(
|
x: Math.min(
|
||||||
Math.ceil(firstVisibleTiles.x + props.width / 256 / props.zoom),
|
Math.ceil(
|
||||||
nbTiles - 1
|
firstVisibleTiles.x + props.width / props.tileSize / props.zoom
|
||||||
|
),
|
||||||
|
props.nbTiles - 1
|
||||||
),
|
),
|
||||||
y: Math.min(
|
y: Math.min(
|
||||||
Math.ceil(firstVisibleTiles.y + props.height / 256 / props.zoom),
|
Math.ceil(
|
||||||
nbTiles - 1
|
firstVisibleTiles.y + props.height / props.tileSize / props.zoom
|
||||||
|
),
|
||||||
|
props.nbTiles - 1
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
console.log(
|
console.log(
|
||||||
|
@ -53,9 +64,9 @@ const TiledLayer: react.FC<TiledLayerProperties> = (
|
||||||
)}/${JSON.stringify(lastVisibleTiles)}.`
|
)}/${JSON.stringify(lastVisibleTiles)}.`
|
||||||
);
|
);
|
||||||
const newTiledLayer: any[][] = [];
|
const newTiledLayer: any[][] = [];
|
||||||
for (let row = 0; row < nbTiles; row++) {
|
for (let row = 0; row < props.nbTiles; row++) {
|
||||||
let tileRow = [];
|
let tileRow = [];
|
||||||
for (let col = 0; col < nbTiles; col++) {
|
for (let col = 0; col < props.nbTiles; col++) {
|
||||||
const key = `${row}/${col}`;
|
const key = `${row}/${col}`;
|
||||||
if (
|
if (
|
||||||
tiledLayer[row][col].type === 'g' &&
|
tiledLayer[row][col].type === 'g' &&
|
||||||
|
@ -69,8 +80,9 @@ const TiledLayer: react.FC<TiledLayerProperties> = (
|
||||||
<Tile
|
<Tile
|
||||||
key={key}
|
key={key}
|
||||||
href={fakeTile}
|
href={fakeTile}
|
||||||
x={col * 256}
|
x={col * props.tileSize}
|
||||||
y={row * 256}
|
y={row * props.tileSize}
|
||||||
|
tileSize={props.tileSize}
|
||||||
delay={1000}
|
delay={1000}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
@ -82,7 +94,16 @@ const TiledLayer: react.FC<TiledLayerProperties> = (
|
||||||
}
|
}
|
||||||
|
|
||||||
setTiledLayer(newTiledLayer);
|
setTiledLayer(newTiledLayer);
|
||||||
}, [props.shift, props.zoom, nbTiles, props.height, props.width]);
|
}
|
||||||
|
}, [
|
||||||
|
props.shift,
|
||||||
|
props.zoom,
|
||||||
|
props.nbTiles,
|
||||||
|
props.height,
|
||||||
|
props.width,
|
||||||
|
props.active,
|
||||||
|
props.tileSize,
|
||||||
|
]);
|
||||||
|
|
||||||
return <>{tiledLayer}</>;
|
return <>{tiledLayer}</>;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue