I'm trying to make a map that have zoom-in and zoom-out functionality.
The problem is when wheeling on the map:
- some shape will scale accordingly,
- all borders will increase or decrease,
- texts will change it's size either.
Firstly, we need understanding what is Zoom Behavior, as the document suggests:
The zoom behavior stores the zoom state on the element to which the zoom behavior was applied
We have two ways to access this object that applied on some node. One way to access it on the fly is when user trigger the zoom event:
var transform = d3.event.transform
Another way is to utilise d3.zoomTransform(node), node is DomNode which bind this behavior.
transform is some kind of 2d matrix object:
k b tx
d k ty
0 0 1
- transform.x - the translation amount tx along the x-axis.
- transform.y - the translation amount ty along the y-axis.
- transform.k - the scale factor k.
- transform.toString() - return "translate(tx, ty) scale(k)"
- d3.zoomIdentity - identity matrix normally used for reinstates
When scaling I can use d3.event.transform.k
to get scale factor, and descale the stroke-width make it always seems identical width( normalWidth / scale ).
Symbol path area linear scale with k^2, I was trying to utilise with the same trick but failed because all symbole seems more luminous. So I just redraw the d attribute.