Merge pull request #582 from SVG-Edit/issues/374

#374 Polygon number-of-sides cannot be edited after placed
This commit is contained in:
JFH
2021-07-07 10:36:22 +02:00
committed by GitHub
3 changed files with 100 additions and 2 deletions

View File

@@ -621,6 +621,7 @@ exports[`use various parts of svg-edit > check tool_star #0`] = `
fill="#ffff00"
strokecolor="#000000"
strokewidth="0"
starradiusmultiplier="3"
points="370,135.33333333333331 383.0618944953883,184.02184456944562 433.40376775301024,181.39886704167017 391.13458925100343,208.86704431944327 409.18568348616486,255.93446629166317 370,224.22222222222223 330.81431651383514,255.93446629166317 348.86541074899657,208.86704431944327 306.59623224698976,181.39886704167017 356.9381055046117,184.02184456944562 370,135.33333333333331 383.0618944953883,184.02184456944562 "
stroke="#000000"
stroke-width="0"
@@ -710,6 +711,7 @@ exports[`use various parts of svg-edit > check tool_polygon #0`] = `
fill="#ffff00"
strokecolor="#000000"
strokewidth="0"
starradiusmultiplier="3"
points="370,135.33333333333331 383.0618944953883,184.02184456944562 433.40376775301024,181.39886704167017 391.13458925100343,208.86704431944327 409.18568348616486,255.93446629166317 370,224.22222222222223 330.81431651383514,255.93446629166317 348.86541074899657,208.86704431944327 306.59623224698976,181.39886704167017 356.9381055046117,184.02184456944562 370,135.33333333333331 383.0618944953883,184.02184456944562 "
stroke="#000000"
stroke-width="0"

View File

@@ -29,6 +29,8 @@ export default {
name,
async init(_S) {
const svgEditor = this;
const { ChangeElementCommand } = _S; // , svgcontent,
const addToHistory = function (cmd) { svgCanvas.undoMgr.addCommandToHistory(cmd); };
const { svgCanvas } = svgEditor;
const { $id } = svgCanvas;
let selElems;
@@ -136,6 +138,64 @@ export default {
showPanel(false, "polygon");
$id("starNumPoints").addEventListener("change", (event) => {
setAttr("point", event.target.value);
const orient = 'point';
const point = event.target.value;
let i = selElems.length;
while (i--) {
const elem = selElems[i];
if (elem.hasAttribute('r')) {
const oldPoint = elem.getAttribute('point');
const oldPoints = elem.getAttribute('points');
const radialshift = elem.getAttribute('radialshift');
let xpos = 0;
let ypos = 0;
if (elem.points) {
const list = elem.points;
const len = list.numberOfItems;
for (let i = 0; i < len; ++i) {
const pt = list.getItem(i);
xpos += parseFloat(pt.x);
ypos += parseFloat(pt.y);
}
const cx = xpos / len;
const cy = ypos / len;
const circumradius = elem.getAttribute('r');
const inradius = circumradius / elem.getAttribute('starRadiusMultiplier');
let polyPoints = "";
for (let s = 0; point >= s; s++) {
let angle = 2.0 * Math.PI * (s / point);
if (orient === "point") {
angle -= Math.PI / 2;
} else if (orient === "edge") {
angle = angle + Math.PI / point - Math.PI / 2;
}
let x = circumradius * Math.cos(angle) + cx;
let y = circumradius * Math.sin(angle) + cy;
polyPoints += x + "," + y + " ";
if (!isNaN(inradius)) {
angle = 2.0 * Math.PI * (s / point) + Math.PI / point;
if (orient === "point") {
angle -= Math.PI / 2;
} else if (orient === "edge") {
angle = angle + Math.PI / point - Math.PI / 2;
}
angle += radialshift;
x = inradius * Math.cos(angle) + cx;
y = inradius * Math.sin(angle) + cy;
polyPoints += x + "," + y + " ";
}
}
elem.setAttribute("points", polyPoints);
addToHistory(new ChangeElementCommand(elem, { 'point': oldPoint, 'points': oldPoints }));
}
}
}
});
$id("RadiusMultiplier").addEventListener("change", (event) => {
setAttr("starRadiusMultiplier", event.target.value);
@@ -145,6 +205,40 @@ export default {
});
$id("polySides").addEventListener("change", (event) => {
setAttr("sides", event.target.value);
const sides = event.target.value;
let i = selElems.length;
while (i--) {
const elem = selElems[i];
if (elem.hasAttribute('edge')) {
const oldSides = elem.getAttribute('sides');
const oldPoints = elem.getAttribute('points');
let xpos = 0;
let ypos = 0;
if (elem.points) {
const list = elem.points;
const len = list.numberOfItems;
for (let i = 0; i < len; ++i) {
const pt = list.getItem(i);
xpos += parseFloat(pt.x);
ypos += parseFloat(pt.y);
}
const cx = xpos / len;
const cy = ypos / len;
const edg = elem.getAttribute('edge');
const inradius = (edg / 2) * cot(Math.PI / sides);
const circumradius = inradius * sec(Math.PI / sides);
let points = "";
for (let s = 0; sides >= s; s++) {
const angle = (2.0 * Math.PI * s) / sides;
const x = circumradius * Math.cos(angle) + cx;
const y = circumradius * Math.sin(angle) + cy;
points += x + "," + y + " ";
}
elem.setAttribute("points", points);
addToHistory(new ChangeElementCommand(elem, { 'sides': oldSides, 'points': oldPoints }));
}
}
}
});
},
mouseDown(opts) {
@@ -223,10 +317,12 @@ export default {
const circumradius =
Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy)) / 1.5;
const RadiusMultiplier = document.getElementById("RadiusMultiplier").value;
const inradius =
circumradius / document.getElementById("RadiusMultiplier").value;
circumradius / RadiusMultiplier;
newFO.setAttribute("r", circumradius);
newFO.setAttribute("r2", inradius);
newFO.setAttribute('starRadiusMultiplier', RadiusMultiplier);
let polyPoints = "";
for (let s = 0; point >= s; s++) {

View File

@@ -49,7 +49,7 @@ const svgWhiteList_ = {
metadata: [ 'class', 'id' ],
path: [ 'class', 'clip-path', 'clip-rule', 'd', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'id', 'marker-end', 'marker-mid', 'marker-start', 'mask', 'opacity', 'requiredFeatures', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform' ],
pattern: [ 'class', 'height', 'id', 'patternContentUnits', 'patternTransform', 'patternUnits', 'requiredFeatures', 'style', 'systemLanguage', 'viewBox', 'width', 'x', 'xlink:href', 'y' ],
polygon: [ 'class', 'clip-path', 'clip-rule', 'id', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'id', 'class', 'marker-end', 'marker-mid', 'marker-start', 'mask', 'opacity', 'points', 'requiredFeatures', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform', 'sides', 'shape' ],
polygon: [ 'class', 'clip-path', 'clip-rule', 'id', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'id', 'class', 'marker-end', 'marker-mid', 'marker-start', 'mask', 'opacity', 'points', 'requiredFeatures', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform', 'sides', 'shape', 'edge', 'point', 'starRadiusMultiplier', 'r', 'radialshift' ],
polyline: [ 'class', 'clip-path', 'clip-rule', 'id', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'marker-end', 'marker-mid', 'marker-start', 'mask', 'opacity', 'points', 'requiredFeatures', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform', 'se:connector' ],
radialGradient: [ 'class', 'cx', 'cy', 'fx', 'fy', 'gradientTransform', 'gradientUnits', 'id', 'r', 'requiredFeatures', 'spreadMethod', 'systemLanguage', 'xlink:href' ],
rect: [ 'class', 'clip-path', 'clip-rule', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'height', 'id', 'mask', 'opacity', 'requiredFeatures', 'rx', 'ry', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform', 'width', 'x', 'y' ],