1

我一直在为 SVG 开发一个图形编辑器,以在线上供我的用户通过他们的网络浏览器访问它。它基于 SVG 编辑并用 Javascript 编写。

目前的应用程序缺少所谓的“布尔运算”,即用户选择 2 个或更多形状并将它们连接在一起的能力。

我找到了一个名为 LIB2GEOM 的 C++ 库,它应该处理这些操作,我相信这也是 Inkscape 使用的。

那么他们是否有可能将这个库与我的应用程序链接起来,因为它不是用 Javascript 编写的?

         <div id="svgcontainer"></div>
    <script>

    function path2poly() 




 {
  var subj_polygons = [[{X:10,Y:10},{X:110,Y:10},{X:110,Y:110},{X:10,Y:110}],
                  [{X:20,Y:20},{X:20,Y:100},{X:100,Y:100},{X:100,Y:20}]]; 
  var clip_polygons = [[{X:50,Y:50},{X:150,Y:50},{X:150,Y:150},{X:50,Y:150}],
                  [{X:60,Y:60},{X:60,Y:140},{X:140,Y:140},{X:140,Y:60}]];
  var scale = 100;
subj_polygons = scaleup(subj_polygons, scale);
 clip_polygons = scaleup(clip_polygons, scale);
var cpr = new ClipperLib.Clipper();
cpr.AddPolygons(subj_polygons, ClipperLib.PolyType.ptSubject);
cpr.AddPolygons(clip_polygons, ClipperLib.PolyType.ptClip);
var subject_fillType = ClipperLib.PolyFillType.pftNonZero;
var clip_fillType = ClipperLib.PolyFillType.pftNonZero;
var clipTypes = [ClipperLib.ClipType.ctUnion];
var clipTypesTexts = "Union";
var solution_polygons, svg, cont = document.getElementById('svgcontainer');
var i;
for(i = 0; i < clipTypes.length; i++) {
solution_polygons = new ClipperLib.Polygons();
cpr.Execute(clipTypes[i], solution_polygons, subject_fillType, clip_fillType);
//console.log(JSON.stringify(solution_polygons));


 alert(polys2path(solution_polygons, scale));
 }

 }

// 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;

}
 </script>
4

1 回答 1

1

您可以尝试使用emscripten将其交叉编译为 JS

于 2013-03-28T05:57:53.520 回答