Skip to content
anton2n edited this page Sep 29, 2014 · 7 revisions

I've tossed this projection tweening function in here for lack of a better place to put it:

  projectionTween = (projection0, projection1) ->
    rotation0 = projection0.rotate()[0...2]
    rotation1 = projection1.rotate()[0...2]

    projection0 = _.extend(projection0.bind({}), projection0)
    projection1 = _.extend(projection1.bind({}), projection1)

    return (d) ->
      project = (λ, φ) ->
        λ *= 180 / Math.PI
        φ *= 180 / Math.PI
        p0 = projection0([λ, φ])
        p1 = projection1([λ, φ])
        return [(1 - t) * p0[0] + t * p1[0], (1 - t) * -p0[1] + t * -p1[1]]

      t = 0

      projection = d3.geo.projection(project)
        .scale(1)

      translate = d3.interpolateArray(projection0.translate(), projection1.translate())
      center = d3.geo.interpolate(projection0.center(), projection1.center())
      rotate = d3.geo.interpolate(rotation0,rotation1)

      path = d3.geo.path().projection(projection)

      return (_t) ->
        t = _t
        projection0.rotate([0,0])
        projection1.rotate([0,0])

        projection.translate(translate(t))
        projection.center(center(t))
        projection.rotate(rotate(t))

        toRet = path(d)

        projection0.rotate(rotation0)
        projection1.rotate(rotation1)

        return toRet
Clone this wiki locally