tradingview / fancy-canvas

MIT License
39 stars 15 forks source link

Added possibility to filter & ignore suggested size change events #24

Open kirchet opened 1 year ago

kirchet commented 1 year ago

Added possibility to filter & ignore suggested size change events

Before:

const binding = bindCanvasElementBitmapSizeTo(canvas, {
  type: 'device-pixel-content-box',
  transform: (bitmapSize: Size, canvasElementClientSize: Size) => ({
    // no way to ignore `subscribeSuggestedBitmapSizeChanged` listeners 😒
    width: Math.max(bitmapSize.width, canvasElementClientSize.width),
    height: Math.max(bitmapSize.height, canvasElementClientSize.height),
  }),
});

Now:

const binding = bindCanvasElementBitmapSizeTo(canvas, {
  type: 'device-pixel-content-box',
  transform: (bitmapSize: Size, canvasElementClientSize: Size) => {
    if (bitmapSize.width === 0 && bitmapSize.height === 0) {
      // ignore zero size resize events 🎉
      return null;
    }
    return {
      width: Math.max(bitmapSize.width, canvasElementClientSize.width),
      height: Math.max(bitmapSize.height, canvasElementClientSize.height),
    }
  }),
});