如何删除压力敏感笔创建的路径副本?

时间:2013-12-30 21:19:21

标签: javascript svg raphael snap.svg

我是svg和js的新手。我有一些使用压力敏感笔绘制的svg文件,并且它们内部有填充路径并且具有重复路径(包含填充)。在Illustrator中,您可以选择整个路径,然后将笔更改为基本笔(无压力敏感度),这会将路径更改为简单路径(每条线路没有重复路径的路径)。下面的svg示例显示每行有两条并行路径:

http://jsfiddle.net/Y35sV/10/

https://dl.dropboxusercontent.com/u/140225334/face.svg

我正在考虑使用snap svg更改每个路径的d属性。注意小路径已被手动切割为单个路径。

path.attr({

'd' = 'value'

});// Any ideas on how to get the right value for the d?

如何以与Illustrator相同的方式删除每一行的第二条路径,但请以编程方式使用js?

非常感谢任何想法。

****更新:

我做了一些研究并解决了这个问题,这是我的发现:

1-我需要将所有子路径转换为路径,并将所有路径转换为绝对值。(这部分由Ian完成)

此处:http://jsbin.com/fiwofukitegu/2/edit

2-然后我应该计算每个路径段的C的数量,并有一个检查功能来检查C命令的数量是偶数还是奇数,

类似的东西:

for each M 
var cValue =C. count();
function isEven(value) {
    if (value%2 == 0)
        return true;
    else
        return false;
}

3-我几乎手动检查了这个:

如果每个路径段中的C的数量是偶数 比如2,4,6,8,10 ......我先计算它们,然后从2,3,4,5,6 C及其后续数字中删除。

4-如果每个路径段中的C的数量是奇数

如1,3,5,7,9,......我应先计算它们,然后从1,2,3,4,5 C及其后续数字中删除。

然后结果将是只有一行的路径段,而不是重复的行。

我非常感谢任何js专家并愿意帮助完成这项工作!

1 个答案:

答案 0 :(得分:0)

我认为你必须解析你的“d”路径。 为此,您可以看到它是如何在snapjs中完成的 或者使用像这样的代码 https://github.com/jkroso/parse-svg-path

/* jkroso/parse-svg-path */



var parseSvgPath = function(path){

/**
* expected argument lengths
* @type {Object}
*/
    this.length = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0};
    /**
    * segment pattern
    * @type {RegExp}
    */
    this.segment = /([astvzqmhlc])([^astvzqmhlc]*)/ig;

    this.parseValues = function(args){
        args = args.match(/-?[.0-9]+(?:e[-+]?\d+)?/ig);
        return args ? args.map(Number) : [];
    };

    /**
    * parse an svg path data string. Generates an Array
    * of commands where each command is an Array of the
    * form `[command, arg1, arg2, ...]`
    *
    * @param {String} path
    * @return {Array}
    */
this.parse = function(path) {
    var data = [];
    path.replace(this.segment, function(_, command, args){
        var type = command.toLowerCase();
        args = this.parseValues(args);
        // overloaded moveTo
        if (type == 'm' && args.length > 2) {
            data.push([command].concat(args.splice(0, 2)));
            type = 'l';
            command = command == 'm' ? 'l' : 'L';
        }
        while (true) {
            if (args.length == this.length[type]) {
                args.unshift(command);
                return data.push(args);
            }
            if (args.length < this.length[type]) throw new Error('malformed path data');
            data.push([command].concat(args.splice(0, this.length[type])));
        }
    })
    return data;
    };

    return this.parse(path);
};