这个svg svg插件有什么问题? [英] What is wrong with this snap svg plugin?

查看:123
本文介绍了这个svg svg插件有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

 el.replace(/[0-9]*\.[0-9]*/g, function(x){ return (+x).toFixed(2)  }) };

在单个svg路径上可以很好地修剪其小数位.现在我希望能够修剪纸张(svg)的所有路径.为此,我尝试在此处制作一个Snap svg插件:

It works great on an individual svg path to trim its decimal places. Now I want to be able to trim all the paths from a paper(svg).To do that I am trying to make a snap svg plugin here:

Snap.plugin(function(Snap, Element, Paper, glob){

     Paper.prototype.trimPthDec = function(){

    this.paper.selectAll('path').forEach( function(el) { el.trim() } ) ;

      function  trim(){

     el.replace(/[0-9]*\.[0-9]*/g, function(x){ return (+x).toFixed(2)  }) };

        //return paper;

});

但是当我这样称呼它时,它似乎不起作用:

But it doesn't seem to work when I call it like:

// And use it like this to update the paper with new paths?!:
trimPthDec(); // I get this error: Uncaught ReferenceError: trimPthDec is not defined

有人可以告诉我为什么它不起作用吗?

Could anybody tell me why it is not working please?

推荐答案

插件和代码很少出错...主要是在纸上运行,因此您需要在代码中正确引用该纸,然后调用该功能作为原型,而不是独立的func(因此paper.func()而不是func())

Few bits wrong with the plugin and the code...mainly its run on the paper, so you need to reference that paper correctly in the code, and call the function as a prototype, rather than a standalone func (so paper.func() rather than func() )

Snap.plugin(function(Snap, Element, Paper, glob){

     Paper.prototype.trim = function(){

         this.selectAll('path').forEach( function(el) { 
             el.attr('d') && el.attr({ 'd':  el.attr('d').replace(/[0-9]*\.[0-9]*/g, function(x){ return (+x).toFixed(2)  }) });

          });
       };
});

var s = Snap("#svg");

var block = s.path('M200.123345,43.2432');

s.trim();
alert(block.attr('d'));

jsfiddle

这篇关于这个svg svg插件有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆