Skip to main content
100%

rotation drag

✓ Published0🌍 Public
11wheel
Last edited Jun 26, 2016
Created on Jun 24, 2016

Green geometric symbols drift across a black background as they automatically rotate and scale. Users can click and drag any symbol to reposition it, while shift-dragging horizontally rotates it and vertically scales it around its centroid. The visualization uses D3 v3’s `d3.behavior.drag` for interaction and `d3.timer` for continuous animation, rendering SVG paths generated with `d3.svg.symbol`. Transformations are composed manually by multiplying affine matrices via a custom `multAffine` function, since chained `translate`, `scale`, and `rotate` in a single SVG transform string proved unreliable.

AI-generated description

Drag to move. Shift click and drag left/right to rotate.

Using multiple translates in the transform property didn't work:

transform(d){
  return [
    '  translate(', center,
    ') scale(', d.scale,
    ') rotate(', d.rotate,
    ') translate(', negPos(center), ')'
  ].join('')
}

So I multiplied several affine transformation matrices together to compose the transforms. Math in JS isn't super pretty:

var m = [
  a[0]*b[0] + a[2]*b[1], a[0]*b[2] + a[2]*b[3], a[0]*b[4] + a[2]*b[5] + a[4], 
  a[1]*b[0] + a[3]*b[1], a[1]*b[2] + a[3]*b[3], a[1]*b[4] + a[3]*b[5] + a[5], 
]

return [m[0], m[3], m[1], m[4], m[2], m[5]]

But things could always be worse.

Update: Robert Monfera pointed out that the above transform string was using the wrong variable to translate, the following does work:

transform(d){
  return [
    '  translate(', d.offset,
    ') translate(', d.centroid, ')',
    ') scale(', d.scale,
    ') rotate(', d.rotate*180,
    ') translate(', negPos(d.centroid), ')'
  ].join('')
}

Similar vizzes