why doesn't d3-geo-zoom pan correctly for more then 1 canvas elements on the page?

Viewed 63

When panning in the first canvas, things work like expected. When panning in the second canvas, it doesn't work like expected. I expected both to work the same. The second globe spins rapidly after a little bit of panning, the first globe keeps the cursor on the same coordinates.

https://codepen.io/tonytrupe/pen/jOqjGvE

class UI {
  constructor(canvas) {
    var width = canvas.width,
      height = canvas.height;
    //set projection type here, geoOrthographic, geoWinkel3
    var projection = d3
      .geoWinkel3()
      //.scale((Math.min(width, height)) / 2)
      .translate([width / 2, height / 2])
      //.rotate([0,0,0])
      .fitExtent(
        [
          [6, 6],
          [width - 6, height - 6]
        ],
        {
          type: "Sphere"
        }
      );

    draw();

    //this.addZoomPan = function () {
      d3
        .geoZoom()
        .northUp(true)
        .projection(projection)
        .onMove(draw)(canvas);
    //};

    function draw() {
      var ctx = canvas.getContext("2d");
      var path = d3.geoPath().context(ctx).projection(projection);
      // Store the current transformation matrix
      ctx.save();
      // Use the identity matrix while clearing the canvas
      ctx.setTransform(1, 0, 0, 1, 0, 0);
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      // Restore the transform
      ctx.restore();

      var border = {
        type: "Sphere"
      };
      ctx.beginPath();

      path(border);

      ctx.strokeStyle = "#000";
      ctx.stroke();

      var lat = 45;
      var lon = 45;
      var graticule = d3.geoGraticule().step([lat, lon]);
      ctx.beginPath();
      path(graticule());
      ctx.strokeStyle = "#000";
      ctx.stroke();
    }
  }
}
//var one = new UI(document.getElementById("one"));
//var two = new UI(document.getElementById("two"));
var three = new UI(document.getElementById("three"));

html

<html>
<script src="//d3js.org/d3.v6.js"></script>
<script src="//d3js.org/d3-geo.v2.min.js"></script>
<script src="//d3js.org/d3-geo-projection.v3.min.js"></script>
<script src="//unpkg.com/d3-geo-zoom"></script>
  
<!--removing all but the last canvas element makes things work as expected-->
<canvas id="one" class="canvas" width="320" height="200"></canvas>
<canvas id="two" class="canvas" width="320" height="200"></canvas>
<canvas id="three" class="canvas" width="320" height="200"></canvas>

</html>

https://github.com/vasturiano/d3-geo-zoom/issues/12

1 Answers
Related