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

后端 未结 1 1511
轻奢々
轻奢々 2020-12-12 06:49

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(

相关标签:
1条回答
  • 2020-12-12 07:21

    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);
    };
    
    0 讨论(0)
提交回复
热议问题