Skip to content

Commit fc149b4

Browse files
authored
🤖 Merge PR DefinitelyTyped#72804 feat: Add types for leaflet-pixi-overlay by @ivannemitko
1 parent e093e06 commit fc149b4

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!**/*.d.ts
3+
!**/*.d.cts
4+
!**/*.d.mts
5+
!**/*.d.*.ts
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import * as L from "leaflet";
2+
import * as PIXI from "pixi.js";
3+
4+
declare module "leaflet" {
5+
interface PixiOverlayOptions {
6+
/**
7+
* How much to extend the clip area around the map view (relative to its size)
8+
* e.g. 0.1 would be 10% of map view in each direction
9+
* @default 0.1
10+
*/
11+
padding?: number;
12+
/**
13+
* Force use of a 2d-canvas
14+
* @default false
15+
*/
16+
forceCanvas?: boolean;
17+
/**
18+
* Help to prevent flicker when refreshing display on some devices (e.g. iOS devices)
19+
* It is ignored if rendering is done with 2d-canvas
20+
* @default false
21+
*/
22+
doubleBuffering?: boolean;
23+
/**
24+
* Resolution of the renderer canvas
25+
* @default L.Browser.retina ? 2 : 1
26+
*/
27+
resolution?: number;
28+
/**
29+
* Return the layer projection zoom level
30+
* @default projectionZoom
31+
*/
32+
projectionZoom?: (map: L.Map) => number;
33+
/**
34+
* Destroy PIXI EventSystem
35+
* @default false
36+
*/
37+
destroyInteractionManager?: boolean;
38+
/**
39+
* Customize PIXI EventSystem autoPreventDefault property
40+
* This option is ignored if destroyInteractionManager is set
41+
* @default true
42+
*/
43+
autoPreventDefault?: boolean;
44+
/**
45+
* Enables drawing buffer preservation
46+
* @default false
47+
*/
48+
preserveDrawingBuffer?: boolean;
49+
/**
50+
* Clear the canvas before the new render pass
51+
* @default true
52+
*/
53+
clearBeforeRender?: boolean;
54+
/**
55+
* Filter move events that should trigger a layer redraw
56+
* @default () => false
57+
*/
58+
shouldRedrawOnMove?: (e: L.LeafletEvent) => boolean;
59+
/**
60+
* The pane where the overlay will be added
61+
* @default 'overlayPane'
62+
*/
63+
pane?: string;
64+
}
65+
66+
type LatLngToLayerPointFn = (latLng: L.LatLngExpression, zoom?: number) => L.Point;
67+
type LayerPointToLatLngFn = (point: L.PointExpression, zoom?: number) => L.LatLng;
68+
69+
interface PixiOverlayUtils {
70+
/**
71+
* Convert a LatLng to layer point
72+
* @param latLng The geographical point to convert
73+
* @param zoom Optional zoom level (defaults to initial zoom)
74+
*/
75+
latLngToLayerPoint: LatLngToLayerPointFn;
76+
/**
77+
* Convert a layer point to LatLng
78+
* @param point The point to convert
79+
* @param zoom Optional zoom level (defaults to initial zoom)
80+
*/
81+
layerPointToLatLng: LayerPointToLatLngFn;
82+
/**
83+
* Get the scale factor between current zoom and initial zoom
84+
* @param zoom Optional zoom level to compare with initial zoom
85+
*/
86+
getScale(zoom?: number): number;
87+
/**
88+
* Get the PIXI renderer instance
89+
*/
90+
getRenderer(): PIXI.Renderer;
91+
/**
92+
* Get the PIXI container
93+
*/
94+
getContainer(): PIXI.Container;
95+
/**
96+
* Get the Leaflet map instance
97+
*/
98+
getMap(): L.Map;
99+
}
100+
101+
class PixiOverlay extends L.Layer {
102+
constructor(
103+
drawCallback: (utils: PixiOverlayUtils, event?: L.LeafletEvent) => void,
104+
pixiContainer: PIXI.Container,
105+
options?: PixiOverlayOptions,
106+
);
107+
108+
/**
109+
* Manually trigger a redraw of the overlay
110+
* @param data Optional data to pass to the draw callback
111+
*/
112+
redraw(data?: unknown): this;
113+
/**
114+
* Properly clean up the overlay before removing it
115+
*/
116+
destroy(): void;
117+
}
118+
119+
function pixiOverlay(
120+
drawCallback: (utils: PixiOverlayUtils, event?: L.LeafletEvent) => void,
121+
pixiContainer: PIXI.Container,
122+
options?: PixiOverlayOptions,
123+
): PixiOverlay;
124+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import * as L from "leaflet";
2+
import * as PIXI from "pixi.js";
3+
import "leaflet-pixi-overlay";
4+
5+
const COORDINATES = [51.505, -0.09] as L.LatLngTuple;
6+
7+
// Create mock DOM elements
8+
const mapContainer = document.createElement("div");
9+
document.body.appendChild(mapContainer);
10+
const map = L.map(mapContainer).setView(COORDINATES, 13);
11+
12+
// Basic test
13+
const minimalTest = () => {
14+
const container = new PIXI.Container();
15+
const draw = (utils: L.PixiOverlayUtils) => {
16+
const point = utils.latLngToLayerPoint(COORDINATES);
17+
const graphics = new PIXI.Graphics();
18+
graphics.beginFill(0xff0000);
19+
graphics.drawCircle(point.x, point.y, 10);
20+
graphics.endFill();
21+
utils.getContainer().addChild(graphics);
22+
};
23+
24+
const overlay = L.pixiOverlay(draw, container);
25+
map.addLayer(overlay);
26+
overlay.redraw();
27+
overlay.destroy();
28+
};
29+
30+
// Options test
31+
const optionsTest = () => {
32+
const container = new PIXI.Container();
33+
const overlay = new L.PixiOverlay(
34+
(utils: L.PixiOverlayUtils) => utils.getScale(),
35+
container,
36+
{
37+
padding: 0.2,
38+
forceCanvas: true,
39+
projectionZoom: (m: L.Map) => m.getZoom(),
40+
shouldRedrawOnMove: (e: L.LeafletEvent) => e.type === "zoomend",
41+
},
42+
);
43+
map.addLayer(overlay);
44+
};
45+
46+
// Utils test
47+
const utilsTest = () => {
48+
const container = new PIXI.Container();
49+
const overlay = L.pixiOverlay((utils: L.PixiOverlayUtils) => {
50+
const point = utils.latLngToLayerPoint(COORDINATES, 10);
51+
const latLng = utils.layerPointToLatLng(point, 10);
52+
const scale = utils.getScale(10);
53+
const renderer = utils.getRenderer();
54+
const container = utils.getContainer();
55+
const mapInstance = utils.getMap();
56+
}, container);
57+
};
58+
59+
// Run tests
60+
minimalTest();
61+
optionsTest();
62+
utilsTest();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"private": true,
3+
"name": "@types/leaflet-pixi-overlay",
4+
"version": "1.9.9999",
5+
"projects": [
6+
"https://github.com/manubb/Leaflet.PixiOverlay#readme"
7+
],
8+
"peerDependencies": {
9+
"pixi.js": "4.6 - 7"
10+
},
11+
"dependencies": {
12+
"@types/leaflet": "*"
13+
},
14+
"devDependencies": {
15+
"@types/leaflet-pixi-overlay": "workspace:."
16+
},
17+
"owners": [
18+
{
19+
"name": "Ivan Nemitko",
20+
"githubUsername": "ivannemitko"
21+
}
22+
]
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"module": "node16",
4+
"lib": [
5+
"es6",
6+
"dom"
7+
],
8+
"noImplicitAny": true,
9+
"noImplicitThis": true,
10+
"strictFunctionTypes": true,
11+
"strictNullChecks": true,
12+
"types": [],
13+
"noEmit": true,
14+
"forceConsistentCasingInFileNames": true
15+
},
16+
"files": [
17+
"index.d.ts",
18+
"leaflet-pixi-overlay-tests.ts"
19+
]
20+
}

0 commit comments

Comments
 (0)