javascript - 将 SVG 路径转换为 ​​canvas html5

标签 javascript html canvas svg

这里我有一些创建 svg 路径的代码:

http://jsbin.com/gazecasagi/1/edit?html,output

 <html>
      <head>

        <script>
    function draw() {
      var polygons = [[{"X":22,"Y":59.45},{"X":136,"Y":66},{"X":170,"Y":99},{"X":171,"Y":114},{"X":183,"Y":125},{"X":218,"Y":144},{"X":218,"Y":165},{"X":226,"Y":193},{"X":254,"Y":195},{"X":283,"Y":195},{"X":292,"Y":202},{"X":325,"Y":213},{"X":341,"Y":134},{"X":397,"Y":245},{"X":417,"Y":548}]]; 
      var scale = 1000;
      reverse_copy(polygons);
      polygons = scaleup(polygons, scale);
      var cpr = new ClipperLib.Clipper();
      var delta = 20;
      var joinType = ClipperLib.JoinType.jtRound;
      var miterLimit = 2;
      var AutoFix = true;
      var svg, offsetted_polygon,
      cont = document.getElementById('svgcontainer');
      offsetted_polygon = cpr.OffsetPolygons(polygons, delta * scale, joinType, miterLimit, AutoFix);
      //console.log(JSON.stringify(offsetted_polygon));

      // Draw red offset polygon
      svg = '<svg style="margin-top:10px;margin-right:10px;margin-bottom:10px;background-color:#dddddd" width="800" height="600">';
      svg += '<path stroke="red" fill="red" stroke-width="2" stroke-opacity="0.6" fill-opacity="0.2" d="' + polys2path(offsetted_polygon, scale) + '"/>';

      //Draw blue polyline
      svg += '<path stroke="blue" stroke-width="1" d="' + polys2path(polygons, scale) + '"/>';
      svg += '</svg>';

          cont.innerHTML += svg;
    }
    // helper function to scale up polygon coordinates
    function scaleup(poly, scale) {
      var i, j;
      if (!scale) scale = 1;
      for(i = 0; i < poly.length; i++) {
        for(j = 0; j < poly[i].length; j++) {
          poly[i][j].X *= scale;
          poly[i][j].Y *= scale;
        }
      }
      return poly;
    }

    // converts polygons to SVG path string
    function polys2path (poly, scale) {
      var path = "", i, j;
      if (!scale) scale = 1;
      for(i = 0; i < poly.length; i++) {
        for(j = 0; j < poly[i].length; j++){
          if (!j) path += "M";
          else path += "L";
          path += (poly[i][j].X / scale) + ", " + (poly[i][j].Y / scale);
        }
        path += "Z";
      }
      return path;
    }

    function reverse_copy(poly) {
        // Make reverse copy of polygons = convert polyline to a 'flat' polygon ...
      var k, klen = poly.length, len, j; 
      for (k = 0; k < klen; k++) {
        len = poly[k].length;
        poly[k].length = len * 2 - 2;
        for (j = 1; j <= len - 2; j++) {
          poly[k][len - 1 + j] = {
            X: poly[k][len - 1 - j].X,
            Y: poly[k][len - 1 - j].Y
          }
        }
      }
    }
        </script>
      </head>
      <body onload="draw()">

        <div id="svgcontainer"></div>
      </body>
    </html>

有没有一种简单的方法可以将 SVG 路径转换为 ​​Canvas。我需要这个,因为我需要在移动设备上显示这个示例,而 Canvas 在移动设备上比 canvas 具有更好的性能。

我需要将这段代码转换为 CANVAS:

 // Draw red offset polygon
      svg = '<svg style="margin-top:10px;margin-right:10px;margin-bottom:10px;background-color:#dddddd" width="800" height="600">';
      svg += '<path stroke="red" fill="red" stroke-width="2" stroke-opacity="0.6" fill-opacity="0.2" d="' + polys2path(offsetted_polygon, scale) + '"/>';

      //Draw blue polyline
      svg += '<path stroke="blue" stroke-width="1" d="' + polys2path(polygons, scale) + '"/>';
      svg += '</svg>';

如何将 SVG 路径转换为简单的 CANVAS 路径?

最佳答案

您可以使用canvg将 svg 转换为 canvas 的库。

您应该将所有必要的 js 文件包含到您的页面中,然后像这样使用它:

canvg(document.getElementById('canvasElement'), '<svg>...</svg>')

关于javascript - 将 SVG 路径转换为 ​​canvas html5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27949573/

相关文章:

JavaScript:在没有 Canvas 的情况下获取 ImageData

javascript - ExtJS:确保用户提交表单

php - 创建数组/json 对象,无法完全正常工作

javascript - knockout.js 更新选择列表不会在第一次更改时触发

javascript - 表单数据内容类型中未定义的 Node.js req.body

javascript - 我无法从 Bootstrap 日历中获取日期值

javascript - 防止用户使用 jquery 或 javascript 重新加载页面

javascript - 选择单选按钮并单击 'add' 按钮时不显示加载指示器

javascript - 使用 Protractor 测试 Chart.js Canvas

canvas - Firefox 4 文本渲染错误