Skip to main content
100%

Understanding of Zoom behavior

✓ Published0🌍 Public
11Cr18Ni9
Last edited May 27, 2019
Created on May 27, 2019

This example demonstrates a zoom behavior on a map populated with 500 randomly placed symbols, each rendered as a distinct path. When the user zooms, the symbols scale their area by the square of the zoom factor, while the stroke width is adjusted to remain visually constant. The code uses d3.zoom to manage the zoom transform, accessing the `d3.event.transform` to read the scale factor `k`. It redraws the `d` attribute of each path to adjust symbol size and sets `stroke-width` to `1/t.k` to maintain consistent border width.

AI-generated description

I'm trying to make a map that have zoom-in and zoom-out functionality.

The problem is when wheeling on the map:

  1. some shape will scale accordingly,
  2. all borders will increase or decrease,
  3. 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.

mit Licensed

Similar vizzes