Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow interacting with canvas through other interactive elements #1473

Merged
merged 1 commit into from
Aug 21, 2023

Conversation

axelboc
Copy link
Contributor

@axelboc axelboc commented Aug 11, 2023

Fix #1388. As discussed in the issue, I take advantage of event bubbling by listening for "canvas events" on canvasWrapper instead of on the canvas itself.

Property pointer-events: none has to remain on canvasWrapper for the stacking order to work, so this part doesn't change: any element that needs to respond to pointer events (hover, click, drag, etc.) must set pointer-events back to auto.

However, from now on, children of canvasWrapper with pointer-events: auto no longer "block" events by default. Events are allowed to bubble up to canvasWrapper, where they may contribute to canvas interactions (zoom, pan, select-to-zoom, etc.)

To prevent a canvas interaction from occurring while interacting with an element (like an annotation), consumers must manually listen for the relevant events and stop their propagation explicitly with evt.stopPropagation(). We expect that in most cases, stopping the propagation of pointerdown events should suffice, since this event controls the beginning of all click-and-drag interactions (pan, select-to-zoom, etc.)

The only place where this new behaviour can be observed currently is on the SvgElement story. One of the rectangles is hoverable; using the wheel to zoom or clicking-and-dragging while hovering the rectangle used to have no effect -- now the canvas zooms and pans as expected.

I'm opening this PR as draft, as I'm planning to write a few more stories to test and demonstrate how to make elements interactive -- maybe even a dedicated documentation page.

@axelboc
Copy link
Contributor Author

axelboc commented Aug 18, 2023

I'm opening this PR as draft, as I'm planning to write a few more stories to test and demonstrate how to make elements interactive -- maybe even a dedicated documentation page.

Change of plan: I'm currently working on a useDrag hook (simpler and more adapted to our use case than the one in @visx/drag) which has so far enabled me to make an SVG circle draggable with very little consumer code. This also allowed me to confirm that the new event bubbling model proposed in this PR works as expected and does not interfere with the drag interaction. useDrag will probably simplify the way some interactions are implemented, like SelectionTool, so I'm going to postpone writing more in-depth documentation for now.

@axelboc axelboc marked this pull request as ready for review August 18, 2023 14:21
@axelboc axelboc requested a review from loichuder August 18, 2023 14:21
@axelboc axelboc mentioned this pull request Aug 18, 2023
Base automatically changed from out-of-r3froot to main August 21, 2023 13:48
@axelboc axelboc merged commit 078c1f0 into main Aug 21, 2023
8 checks passed
@axelboc axelboc deleted the event-bubbling branch August 21, 2023 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Event propagation to canvas
2 participants