这个svg svg插件有什么问题? [英] What is wrong with this snap svg plugin?
问题描述
我有此代码:
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'));
这篇关于这个svg svg插件有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!