Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

javascript - How to remove a path duplicate created by pressure sensitive pens?

I am new to svg and js. I have some svg files that have been drawn using pressure sensitive pens, and they have paths with fills inside them and are having duplicated paths( to contain the fills). In Illustrator you can select the entire paths, and then change the pen to basic pen( no pressure sensitivity) and this changes the paths to simple paths(paths with no duplicated paths for each line). The below svg example shows that each line is having 2 paths in parallel:

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

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

I was thinking about changing the d attribute of each path using snap svg.Note that the small path has been manually cut to be a single path.

path.attr({

'd' = 'value'

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

How is it possible to remove the second path for each line the same way that Illustrator would do, but programatically using js please?

Any ideas would be greatly appreciated.

****Update:

I did some research and played around with the problem and here is my findings:

1- I need to turn all the subpaths to paths and also convert all paths toAbsolute values.( this part is being done by Ian already)

here : http://jsbin.com/fiwofukitegu/2/edit

2- Then I should count the number of C's for each path segment and have a check function to check if the number of the C commands are even or odd numbers,

something like this:

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

3- I practically and manually have checked this:

if the the number of the C's in each path segment is even number like 2 ,4, 6,8,10,... I should count them first and then remove from 2, 3, 4,5,6 C's and their following digits.

4- if the the number of the C's in each path segment is odd number

like 1, 3,5,7,9,...I should count them first and then remove from 1,2,3,4,5 C's and their following digits.

then the result will be a path segment with only one line , not duplicated line.

I greatly appreciated Anyone who is a js expert and is willing to help to make this work!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you have to parse your "d" path. for this purpose, you can see how it is done in snapjs or use some code like this one 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);
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...