mirror of
https://github.com/svg/svgo.git
synced 2025-08-07 15:22:54 +03:00
Add ‘ellipseToCircle’ plugin
Converts non-eccentric ellipse elements to circle elements. Adobe Illustrator seems to be fond of using ellipses even if their rx === ry.
This commit is contained in:
committed by
Lev Solntsev
parent
31e6a8cb65
commit
8e45fa2d58
39
plugins/convertEllipseToCircle.js
Normal file
39
plugins/convertEllipseToCircle.js
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
exports.type = 'perItem';
|
||||||
|
|
||||||
|
exports.active = true;
|
||||||
|
|
||||||
|
exports.description = 'converts non-eccentric <ellipse>s to <circle>s';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts non-eccentric <ellipse>s to <circle>s.
|
||||||
|
*
|
||||||
|
* @see http://www.w3.org/TR/SVG/shapes.html
|
||||||
|
*
|
||||||
|
* @param {Object} item current iteration item
|
||||||
|
* @return {Boolean} if false, item will be filtered out
|
||||||
|
*
|
||||||
|
* @author Taylor Hunt
|
||||||
|
*/
|
||||||
|
exports.fn = function(item) {
|
||||||
|
if (item.isElem('ellipse')) {
|
||||||
|
var rx = item.attr('rx').value || 0;
|
||||||
|
var ry = item.attr('ry').value || 0;
|
||||||
|
|
||||||
|
if (rx === ry ||
|
||||||
|
rx === 'auto' || ry === 'auto' // SVG2
|
||||||
|
) {
|
||||||
|
var radius = rx !== 'auto' ? rx : ry;
|
||||||
|
item.renameElem('circle');
|
||||||
|
item.removeAttr(['rx', 'ry']);
|
||||||
|
item.addAttr({
|
||||||
|
name: 'r',
|
||||||
|
value: radius,
|
||||||
|
prefix: '',
|
||||||
|
local: 'r',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
};
|
13
test/plugins/convertEllipseToCircle.01.svg
Normal file
13
test/plugins/convertEllipseToCircle.01.svg
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<ellipse rx="5" ry="5"/>
|
||||||
|
<ellipse rx="auto" ry="5"/>
|
||||||
|
<ellipse rx="5" ry="auto"/>
|
||||||
|
</svg>
|
||||||
|
|
||||||
|
@@@
|
||||||
|
|
||||||
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<circle r="5"/>
|
||||||
|
<circle r="5"/>
|
||||||
|
<circle r="5"/>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 255 B |
Reference in New Issue
Block a user