简体   繁体   English

如何使用JS库对SVG文件进行动画处理-盆景是否理想?

[英]How Can I Animate my SVG files with a JS Library - Is Bonsai Ideal for this?

I would like to find and learn a js library that will allow me to animate SVG files that I've created in Illustrator. 我想找到并学习一个js库,该库使我可以对在Illustrator中创建的SVG文件进行动画处理。 I have a large background in After Effects and motion graphics, so I'm looking for a JS library that will give me a lot of animation functionality. 我在After Effects和动态图形方面具有丰富的背景知识,因此我正在寻找一个可以提供很多动画功能的JS库。

I'm checking out BonsaiJS right now, and I'm wondering if someone could advise me as to whether or not Bondsai will allow me to animate SVG files, and if so, show me how to get started doing this. 我现在正在检查BonsaiJS,我想知道是否有人可以向我建议Bondsai是否允许我对SVG文件进行动画处理,如果可以,请告诉我如何开始执行此操作。

Right now, I have SVGs embedded in image tags throughout my site. 现在,我在整个网站的图像标签中都嵌入了SVG。 I've quickly wrote some sample code to illustrate my thinking on how to go about animating my SVGs with Bonsai. 我已经快速编写了一些示例代码来说明我对如何使用Bonsai动画SVG的想法。 But, are there any special requirements for dealing with existing SVG files rather than shapes? 但是,处理现有的SVG文件而不是形状是否有特殊要求? Sorry for sounding vague the SVG format and JS animation libraries are new to me. 很抱歉,我对SVG格式和JS动画库的了解不太清楚。 Simply pointing me towards a tutorial for animating SVGs with a js library would be a tremendous help. 只需将我指向使用js库对SVG进行动画处理的教程,将大有帮助。

<img src="graphic.svg" id="graphic">

bonsai.run(document.getElementById('graphic'), {
code: function() {

},

});

Bonsai exposes API for both, embedding images and animating them. 盆景公开了两者的API,即嵌入图像并对其进行动画处理。

bonsai.run(document.body, {
  width: 300,
  height: 300,
  code: function() {
    // url that points to the image
    var url = 'https://upload.wikimedia.org/wikipedia/commons/7/72/Variable_Resistor.svg';
    new Bitmap(url).on('load', function() {
      // adjust size
      this.attr('width', 200);
      // add it to the stage
      this.addTo(stage);
      // animate arbitrary attributes
      this.animate('3s', {
        x: 200
      });
    });
  }
});

I would suggest playing around with your assets and Bonsai's Animation API on http://orbit.bonsaijs.org for getting used to it. 我建议您在http://orbit.bonsaijs.org上玩弄您的资产和Bonsai的Animation API,以习惯它。

I'd recommend you start out with Raphael rather than Bonsai for getting started with SVG. 我建议您从Raphael而不是Bonsai开始,开始使用SVG。 It's more popular and you will find more people who can answer your questions. 它更受欢迎,您会发现更多可以回答您问题的人。 Also, Debugging JS that you use with Bonsai is very difficult. 另外,调试与Bonsai一起使用的JS非常困难。

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

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