mirror of
https://github.com/svg/svgo.git
synced 2025-08-06 04:22:39 +03:00
Convert convertPathData to visitor
This commit is contained in:
1
input.svg
Normal file
1
input.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg viewBox="0 0 300 300" xmlns="http://www.w3.org/2000/svg"> <g transform="matrix(1 0 0 1 30 40)"> <circle cx="50" cy="50" r="50" /> </g> <g style="transform:matrix(1,0,0,1,20,20)"> <circle cx="50" cy="50" r="50" /> </g> </svg>
|
After Width: | Height: | Size: 230 B |
1
output.svg
Normal file
1
output.svg
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<svg viewBox="0 0 300 300" xmlns="http://www.w3.org/2000/svg"><circle cx="50" cy="50" r="50" transform="translate(30 40)"/><circle cx="50" cy="50" r="50" style="transform:matrix(1,0,0,1,20,20)"/></svg>
|
After Width: | Height: | Size: 201 B |
@@ -6,10 +6,8 @@ const { path2js, js2path } = require('./_path.js');
|
|||||||
const { applyTransforms } = require('./_applyTransforms.js');
|
const { applyTransforms } = require('./_applyTransforms.js');
|
||||||
const { cleanupOutData } = require('../lib/svgo/tools');
|
const { cleanupOutData } = require('../lib/svgo/tools');
|
||||||
|
|
||||||
exports.type = 'perItem';
|
exports.type = 'visitor';
|
||||||
|
|
||||||
exports.active = true;
|
exports.active = true;
|
||||||
|
|
||||||
exports.description =
|
exports.description =
|
||||||
'optimizes path data: writes in shorter form, applies transformations';
|
'optimizes path data: writes in shorter form, applies transformations';
|
||||||
|
|
||||||
@@ -56,16 +54,17 @@ let arcTolerance;
|
|||||||
*
|
*
|
||||||
* @author Kir Belevich
|
* @author Kir Belevich
|
||||||
*/
|
*/
|
||||||
exports.fn = function (item, params) {
|
exports.fn = (root, params) => {
|
||||||
if (
|
return {
|
||||||
item.type === 'element' &&
|
element: {
|
||||||
pathElems.includes(item.name) &&
|
enter: (node) => {
|
||||||
item.attributes.d != null
|
if (pathElems.includes(node.name) && node.attributes.d != null) {
|
||||||
) {
|
const computedStyle = computeStyle(node);
|
||||||
const computedStyle = computeStyle(item);
|
|
||||||
precision = params.floatPrecision;
|
precision = params.floatPrecision;
|
||||||
error =
|
error =
|
||||||
precision !== false ? +Math.pow(0.1, precision).toFixed(precision) : 1e-2;
|
precision !== false
|
||||||
|
? +Math.pow(0.1, precision).toFixed(precision)
|
||||||
|
: 1e-2;
|
||||||
roundData = precision > 0 && precision < 20 ? strongRound : round;
|
roundData = precision > 0 && precision < 20 ? strongRound : round;
|
||||||
if (params.makeArcs) {
|
if (params.makeArcs) {
|
||||||
arcThreshold = params.makeArcs.threshold;
|
arcThreshold = params.makeArcs.threshold;
|
||||||
@@ -83,12 +82,12 @@ exports.fn = function (item, params) {
|
|||||||
computedStyle['stroke-linecap'].value !== 'butt');
|
computedStyle['stroke-linecap'].value !== 'butt');
|
||||||
const maybeHasStrokeAndLinecap = maybeHasStroke && maybeHasLinecap;
|
const maybeHasStrokeAndLinecap = maybeHasStroke && maybeHasLinecap;
|
||||||
|
|
||||||
var data = path2js(item);
|
var data = path2js(node);
|
||||||
|
|
||||||
// TODO: get rid of functions returns
|
// TODO: get rid of functions returns
|
||||||
if (data.length) {
|
if (data.length) {
|
||||||
if (params.applyTransforms) {
|
if (params.applyTransforms) {
|
||||||
applyTransforms(item, data, params);
|
applyTransforms(node, data, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
convertToRelative(data);
|
convertToRelative(data);
|
||||||
@@ -102,9 +101,12 @@ exports.fn = function (item, params) {
|
|||||||
data = convertToMixed(data, params);
|
data = convertToMixed(data, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
js2path(item, data, params);
|
js2path(node, data, params);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user