Skip to content

DistortableImageOverlay events not firing reliably in React (image not draggable, load not triggered) #1405

@G4mingEdition

Description

@G4mingEdition

Describe the bug:
When adding images dynamically inside a React application, DistortableImageOverlay behaves inconsistently the image is sometimes not selectable.

Expected behavior:
All events should consistently fire and the image should always be selectable.

Reproduce the behavior:

  1. Use React (18.3.1) + Leaflet (1.9.4) + leaflet-distortableimage (latest).
  2. Dynamically add images using:
this.backgroundImagesLayer = L.distortableCollection();
...

this.map.whenReady(() => {
  const bgImg = L.distortableImageOverlay(url, {
    mode: 'scale',
    actions: [L.ScaleAction, L.RotateAction],
    selected: true,
  });
  
  this.backgroundImagesLayer.addLayer(bgImg); // 
  
  const init = () => {
    const el = bgImg.getElement();
    if (!el) return requestAnimationFrame(init);
  
    L.DomEvent.on(el, 'load', () => { // Working
        L.DomEvent.on(el, 'click', () => console.log('clicked')); // Not working sometimes
        bgImg.on('dragstart', () => console.log('dragstarted')); // Not working sometimes
    });
  };
  
  requestAnimationFrame(init);
});
  1. Reload the page several times.
  2. Observe that:
    • Sometimes the image is selectable.
    • Sometimes it is not.
    • The img fires "load" event.

Browser, versions and operating system:

  • Browser: Chrome (latest)
  • Versions: React 18.3.1, Leaflet 1.9.4
  • OS: Windows 11

Additional notes:
I also tried explicitly importing the files in several ways, including:

import L from 'leaflet';
import 'leaflet-distortableimage';
import 'leaflet-distortableimage/dist/vendor.js';
import 'leaflet-distortableimage/dist/leaflet.distortableimage.css';

and also:

import L from 'leaflet';
import 'leaflet-distortableimage/dist/leaflet.distortableimage.js';
import 'leaflet-distortableimage/dist/vendor.js';
import 'leaflet-distortableimage/dist/leaflet.distortableimage.css';

The behavior remains the same: sometimes images never become selectable and Interaction/editing events fire inconsistently.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions