datature / portal

Portal is the fastest way to load and visualize your deep neural networks on images and videos 🔮
https://datature.io/portal
Apache License 2.0
256 stars 59 forks source link

Implement custom Leaflet layer for exporting bitmap #179

Closed yongjun21 closed 3 months ago

yongjun21 commented 3 months ago

Custom Leaflet layer that extends Polygon layer by exposing a .getBitmap() method for exporting bitmap of drawn polygon.

When the layer draws a polygon to canvas, it stores a Path2D object which can be used to create an offscreen canvas which in turn can be used for exporting bitmap

It works by monkey patching the renderer's canvas context to spy on the path methods.

declare module CustomBitmapLayer {
  interface Bitmap {
    x: number;
    y: number;
    width: number;
    height: number;
    getImageBitmap: () => ImageBitmap;
    getImageData: () => ImageData;
    getImageUrl: () => Promise<string>;
  }

  export class CustomBitmapLayer extends L.Polygon {
    getBitmap(padding?: number): Bitmap | null;
  }

  export function createCustomBitmapLayer(): CustomBitmapLayer;
}