html2canvas does not work with Google Maps Pan

前端 未结 4 2017
终归单人心
终归单人心 2020-11-27 18:55

I\'m using html2canvas to save my online map as an image (See the Save as Image link). I\'ve tried it in Firefox, Chrome and Opera.

It tends to work more often if

4条回答
  •  眼角桃花
    2020-11-27 19:49

    Apparently, the problem seems to stem from html2canvas not being able to render css transforms, at least in chrome (I could only reproduce the problem in chrome, on OSX). The container that holds the tiles, is translated using -webkit-transform. So what we could do is to grab the values that the container is shifted, remove the transform, assign left and top from the values we got off transform then use html2canvas. Then so the map doesn't break, we reset the map's css values when html2canvas is done.

    So I pasted this into the javascript console at your site and at it seemed to work

    //get transform value
    var transform=$(".gm-style>div:first>div").css("transform")
    var comp=transform.split(",") //split up the transform matrix
    var mapleft=parseFloat(comp[4]) //get left value
    var maptop=parseFloat(comp[5])  //get top value
    $(".gm-style>div:first>div").css({ //get the map container. not sure if stable
      "transform":"none",
      "left":mapleft,
      "top":maptop,
    })
    html2canvas($('#map-canvas'),
    {
      useCORS: true,
      onrendered: function(canvas)
      {
        var dataUrl= canvas.toDataURL('image/png');
        location.href=dataUrl //for testing I never get window.open to work
        $(".gm-style>div:first>div").css({
          left:0,
          top:0,
          "transform":transform
        })
      }
    });
    

提交回复
热议问题