简体   繁体   English

BonsaiJS如何加载外部SVG文件?

[英]How can BonsaiJS load external SVG files?

I am learning to work with BonsaiJS and would like to import an external SVG image on the stage. 我正在学习与BonsaiJS合作,并想在舞台上导入外部SVG图像。

At http://docs.bonsaijs.org/overview/Path.html I learnt that there are three ways to create a new Path() sending SVG paths, path commands or path points as arguments, but for more complex SVG files this is too much of a hassle to make this work. http://docs.bonsaijs.org/overview/Path.html上,我了解到有三种方法可以创建一个new Path()发送SVG路径,路径命令或路径点作为参数,但是对于更复杂的SVG文件,这是太麻烦了,无法完成这项工作。

At How Can I Animate my SVG files with a JS Library - Is Bonsai Ideal for this? 如何使用JS库为SVG文件制作动画-盆景是否理想? I read one can use the new Bitmap() method, but SVG's are turned into... bitmaps. 我读到可以使用new Bitmap()方法,但是SVG变成了...位图。

Am I missing something? 我想念什么吗? Thanks in advance! 提前致谢!

At the time it seems impossible for BonsaiJS to load external SVG's like it is possible to load external bitmap images. 那时,BonsaiJS似乎无法加载外部SVG,就像可以加载外部位图图像一样。 The BonsaiJS docs ( http://docs.bonsaijs.org/overview/Path.html ) do provide three methods to manually handle paths from an SVG file separately. BonsaiJS文档( http://docs.bonsaijs.org/overview/Path.html )确实提供了三种方法来分别手动处理SVG文件中的路径。

Path nodes 路径节点

If your SVG contains <path> nodes (tags), get the value of the d attribute and apply the fill manually: 如果您的SVG包含<path>节点(标记),请获取d属性的值并手动应用填充:

SVG SVG

<path id="pathId" fill="#ff6600" d="M54.651,316.878c9.098,19.302,3.208,46.085,11.193,67.117 c0.687,2.48-4.441-0.06-2.581,5.486c9.171,20.188,10.61,52.755,17.331,79.04l-1.88-0.961c1.652,5.139,7.946,8.864,10.469,13.37">

BonsaiJS 盆景JS

var myShape1 = new Path("M54.651,316.878c9.098,19.302,3.208,46.085,11.193,67.117 c0.687,2.48-4.441-0.06-2.581,5.486c9.171,20.188,10.61,52.755,17.331,79.04l-1.88-0.961c1.652,5.139,7.946,8.864,10.469,13.37");
myShape1.fill('#ff6600');

Polygon nodes 多边形节点

If your SVG ie. 如果你的SVG即。 contains <polygon> nodes, get the string value of the points attribute, split this string into an array and turn all string elements into floats. 包含<polygon>节点,获取points属性的字符串值,将此字符串拆分为数组,然后将所有字符串元素转换为浮点数。 Again also apply the fill manually: 再次也手动应用填充:

SVG SVG

<polygon id="polygonId" fill="#FFFFFF" points="76.5,253.5 94.5,165.5 108.5,125.5 128.5,93.5 164.5,66.5 195.891,44.719 254.5,36.5 299.656,36.5 348.5,41.5 414,66 459,102 488,150.5 505.5,218.5 508,331.5 508,390 504.5,424.5 480,463.5 450,509.5 378,554.5 300,566 226,556.5 168,535.5 109.5,476 87,419.5 76.5,339.5 "/>

BonsaiJS 盆景JS

var points = "76.5,253.5 94.5,165.5 108.5,125.5 128.5,93.5 164.5,66.5 195.891,44.719 254.5,36.5 299.656,36.5 348.5,41.5 414,66 459,102 488,150.5 505.5,218.5 508,331.5 508,390 504.5,424.5 480,463.5 450,509.5 378,554.5 300,566 226,556.5 168,535.5 109.5,476 87,419.5 76.5,339.5";
points = points.split(/[\s,]+/).map(function(point) {
  return parseFloat(point);
});
var myShape2 = new Path(points).fill('#ffffff');

Then... 然后...

After defining all parts, one can create a group and add all parts to 'reconstruct' the original SVG image, ie.: 定义完所有零件后,可以创建一个组并添加所有零件以“重建”原始SVG图像,即:

var myGroup = new Group().addTo(stage);
myShape1.addTo(myGroup);
myShape2.addTo(myGroup);

At http://docs.bonsaijs.org/overview/Path.html I learnt that there are three ways to create a new Path() sending SVG paths, path commands or path points as arguments, but for more complex SVG files this is too much of a hassle to make this work. http://docs.bonsaijs.org/overview/Path.html上,我了解到有三种方法可以创建一个新的Path()来发送SVG路径,路径命令或路径点作为参数,但是对于更复杂的SVG文件,这是太麻烦了,无法完成这项工作。

Yes, Bonsai let you import SVG (like) Paths. 是的,盆景让您导入SVG(例如)路径。

At How Can I Animate my SVG files with a JS Library - Is Bonsai Ideal for this? 如何使用JS库为SVG文件制作动画-盆景是否理想? I read one can use the new Bitmap() method, but SVG's are turned into... bitmaps. 我读到可以使用新的Bitmap()方法,但是SVG变成了...位图。

Bonsai doesn't let you import SVGs in general. 盆景一般不允许您导入SVG。 Only paths. 仅路径。 Which means that Bonsai doesn't give you API for accessing the inner nodes of your SVG file. 这意味着Bonsai没有为您提供用于访问SVG文件的内部节点的API。

Using the Bitmap API is not a way of importing your SVG. 使用位图API并不是导入SVG的方法。 The SVG file is treated as any regular image format. SVG文件被视为任何常规图像格式。 It depends on the platform and renderer whether it is going to be rastered or not. 是否要栅格化取决于平台和渲染器。 It can be compared with using DOM's HTMLImageElement. 可以与使用DOM的HTMLImageElement进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM