From f9da8ce72a272dea8745d4e48c3b3c4bf2b4cc40 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Thu, 15 Aug 2019 14:40:44 -0400 Subject: [PATCH] [mapbox-gl] Add CustomLayerInterface (#37651) --- types/mapbox-gl/index.d.ts | 73 +++++++++++++++++++++++++++++- types/mapbox-gl/mapbox-gl-tests.ts | 15 ++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/types/mapbox-gl/index.d.ts b/types/mapbox-gl/index.d.ts index 43b1b9b509..87f39c513d 100644 --- a/types/mapbox-gl/index.d.ts +++ b/types/mapbox-gl/index.d.ts @@ -124,7 +124,7 @@ declare namespace mapboxgl { listImages(): string[]; - addLayer(layer: mapboxgl.Layer, before?: string): this; + addLayer(layer: mapboxgl.Layer | mapboxgl.CustomLayerInterface, before?: string): this; moveLayer(id: string, beforeId?: string): this; @@ -1384,6 +1384,77 @@ declare namespace mapboxgl { paint?: BackgroundPaint | FillPaint | FillExtrusionPaint | LinePaint | SymbolPaint | RasterPaint | CirclePaint | HeatmapPaint | HillshadePaint; } + // See https://docs.mapbox.com/mapbox-gl-js/api/#customlayerinterface + export interface CustomLayerInterface { + /** A unique layer id. */ + id: string; + + /* The layer's type. Must be "custom". */ + type: 'custom'; + + /* Either "2d" or "3d". Defaults to "2d". */ + renderingMode?: '2d' | '3d'; + + /** + * Optional method called when the layer has been removed from the Map with Map#removeLayer. + * This gives the layer a chance to clean up gl resources and event listeners. + * @param map The Map this custom layer was just added to. + * @param gl The gl context for the map. + */ + onRemove?(map: mapboxgl.Map, gl: WebGLRenderingContext): void; + + /** + * Optional method called when the layer has been added to the Map with Map#addLayer. + * This gives the layer a chance to initialize gl resources and register event listeners. + * @param map The Map this custom layer was just added to. + * @param gl The gl context for the map. + */ + onAdd?(map: mapboxgl.Map, gl: WebGLRenderingContext): void; + + /** + * Optional method called during a render frame to allow a layer to prepare resources + * or render into a texture. + * + * The layer cannot make any assumptions about the current GL state and must bind a framebuffer + * before rendering. + * @param gl The map's gl context. + * @param matrix The map's camera matrix. It projects spherical mercator coordinates to gl + * coordinates. The mercator coordinate [0, 0] represents the top left corner of + * the mercator world and [1, 1] represents the bottom right corner. When the + * renderingMode is "3d" , the z coordinate is conformal. A box with identical + * x, y, and z lengths in mercator units would be rendered as a cube. + * MercatorCoordinate .fromLatLng can be used to project a LngLat to a mercator + * coordinate. + */ + prerender?(gl: WebGLRenderingContext, matrix: number[]): void; + + /** + * Called during a render frame allowing the layer to draw into the GL context. + * + * The layer can assume blending and depth state is set to allow the layer to properly blend + * and clip other layers. The layer cannot make any other assumptions about the current GL state. + * + * If the layer needs to render to a texture, it should implement the prerender method to do this + * and only use the render method for drawing directly into the main framebuffer. + * + * The blend function is set to gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA). This expects + * colors to be provided in premultiplied alpha form where the r, g and b values are already + * multiplied by the a value. If you are unable to provide colors in premultiplied form you may + * want to change the blend function to + * gl.blendFuncSeparate(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA). + * + * @param gl The map's gl context. + * @param matrix The map's camera matrix. It projects spherical mercator coordinates to gl + * coordinates. The mercator coordinate [0, 0] represents the top left corner of + * the mercator world and [1, 1] represents the bottom right corner. When the + * renderingMode is "3d" , the z coordinate is conformal. A box with identical + * x, y, and z lengths in mercator units would be rendered as a cube. + * MercatorCoordinate .fromLatLng can be used to project a LngLat to a mercator + * coordinate. + */ + render(gl: WebGLRenderingContext, matrix: number[]): void; + } + export interface StyleFunction { stops?: any[][]; property?: string; diff --git a/types/mapbox-gl/mapbox-gl-tests.ts b/types/mapbox-gl/mapbox-gl-tests.ts index 78d600f500..d9a731c730 100644 --- a/types/mapbox-gl/mapbox-gl-tests.ts +++ b/types/mapbox-gl/mapbox-gl-tests.ts @@ -172,6 +172,21 @@ map.on('load', function() { "line-width": 8 } }); + + // Add a custom layer + map.addLayer({ + id: 'custom', + type: 'custom', + renderingMode: '3d', + onRemove: function(map, gl) { + map; // $ExpectType Map + gl; // $ExpectType WebGLRenderingContext + }, + render: function(gl, matrix) { + gl; // $ExpectType WebGLRenderingContext + matrix; // $ExpectType number[] + }, + }); }); // FlyTo