Comments (7)
And the init event would be dispatched in here?
Lines 39 to 48 in 652167e
from d3-drag.
Another use case (with d3-zoom but it's exactly the same idea), is to help the initialization of the target's zoom factor, and initial draw, in https://observablehq.com/@fil/synchronized-projections
from d3-drag.
test here https://observablehq.com/d/4b48ecd05076e511
from d3-drag.
And another test to verify that we can bind all listeners (start drag end) on init.
https://observablehq.com/d/1999aec3a613880e
from d3-drag.
In this notebook, we implement initialization behavior (setting the __zoom property) by wrapping the zoom behavior in a function:
https://observablehq.com/@d3/versor-zooming
The awkward part is that when wrapping you must re-expose zoom.on so that callers can register event listeners.
While an init event would make this easier, I’m not sure we want to head in this direction. It feels more like inheritance than composition: we’re dependent on the behavior to expose all the events we need to implement our higher-level behavior. For example, what if you want to remove the styles when the element is no longer draggable, would we need a destroy event? (And should the events be called bind and unbind?) And would the drag behavior then need a drag.remove function instead of using selection.on to remove the drag listeners?
This discussion reminded me of an idea I’ve had for a while but apparently never wrote down: adopting native events. #73 That would also make it easier to build higher-level behaviors because you wouldn’t have the awkward aspect of re-exposing zoom.on (or drag.on): listeners are applied to elements rather than behaviors. So instead of:
const zoom = d3.zoom()
.scaleExtent(scaleExtent.map(x => x * scale))
.on("start", zoomstarted)
.on("zoom", zoomed);
…
return Object.assign(selection => selection
.property("__zoom", d3.zoomIdentity.scale(projection.scale()))
.call(zoom), {
on(type, ...options) {
return options.length
? (zoom.on(type, ...options), this)
: zoom.on(type);
}
});
You might write:
const zoom = d3.zoom()
.scaleExtent(scaleExtent.map(x => x * scale));
…
return selection => selection
.property("__zoom", d3.zoomIdentity.scale(projection.scale()))
.on("zoomstart", zoomstarted)
.on("zoom", zoomed)
.call(zoom);
from d3-drag.
The awkward part is that when wrapping you must re-expose zoom.on
yes I had to read that code 10 times to get a sense of how it was constructed :) But this is indeed what I want to facilitate with this "init" event proposal. Maybe trough another technical approach though. https://observablehq.com/compare/[email protected]@319 works well (though I don't like having to set up the cursor at two very different places).
from d3-drag.
Another way of doing some "initial" work with the current code base is by diverting .touchable() :
const touchable = zoom.touchable();
zoom
.touchable(function() {
d3.select(this)
.property("__zoom", d3.zoomIdentity.scale(projection.scale()))
.style("cursor", "grab");
return touchable.apply(this, arguments);
})
.on("start.cursor zoom.cursor end.cursor", function() {
d3.select(this).style("cursor", d3.event.type === "end" ? "grab" : "grabbing");
});
from d3-drag.
Related Issues (20)
- Possible typo in README HOT 2
- What are the perposes of functions yesdrag and nodrag?
- Drag event on Chrome 72 HOT 3
- d3 mouseup event not firing HOT 1
- Drag Event Issue on Windows Chrome 74, related to solution for #47 HOT 1
- The default filter should return false if event.ctrlKey. HOT 5
- drag start event is don't send when Ctrl-key is press HOT 1
- Click event not firing if drag enables with simulation HOT 1
- Non-cancelable event HOT 3
- remove drag.container? HOT 1
- Use native events (*element*.dispatchEvent) instead of d3-dispatch? HOT 1
- remove dragEvent.on?
- d3.event is not available in version 6.1.1 HOT 1
- DragEvent missing event.currentTarget for arrow function node selection HOT 1
- Make event listeners non-passive. HOT 2
- d3.drag() causes error in testing mousedown event via jsdom HOT 4
- d3.pointer() could use event.touches[0] HOT 2
- Add event.currentTarget
- Support d3.drag in testing environments
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from d3-drag.