2018-05-16 08:32:44 +00:00
|
|
|
/* eslint-disable no-var */
|
2018-05-16 00:53:27 +00:00
|
|
|
/* globals $, svgedit, svgroot */
|
2011-02-04 08:02:46 +00:00
|
|
|
/**
|
|
|
|
* Package: svgedit.path
|
|
|
|
*
|
2012-09-16 18:53:27 +00:00
|
|
|
* Licensed under the MIT License
|
2011-02-04 08:02:46 +00:00
|
|
|
*
|
|
|
|
* Copyright(c) 2011 Alexis Deveria
|
|
|
|
* Copyright(c) 2011 Jeff Schiller
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Dependencies:
|
|
|
|
// 1) jQuery
|
|
|
|
// 2) browser.js
|
|
|
|
// 3) math.js
|
|
|
|
// 4) svgutils.js
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
2011-02-04 08:02:46 +00:00
|
|
|
|
|
|
|
if (!svgedit.path) {
|
|
|
|
svgedit.path = {};
|
|
|
|
}
|
|
|
|
|
2013-02-16 15:02:26 +00:00
|
|
|
var NS = svgedit.NS;
|
2011-02-04 08:02:46 +00:00
|
|
|
var uiStrings = {
|
2013-02-15 17:02:24 +00:00
|
|
|
'pathNodeTooltip': 'Drag node to move it. Double-click node to change segment type',
|
|
|
|
'pathCtrlPtTooltip': 'Drag control point to adjust curve properties'
|
2011-02-04 08:02:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var segData = {
|
2013-02-15 16:51:48 +00:00
|
|
|
2: ['x', 'y'],
|
|
|
|
4: ['x', 'y'],
|
|
|
|
6: ['x', 'y', 'x1', 'y1', 'x2', 'y2'],
|
|
|
|
8: ['x', 'y', 'x1', 'y1'],
|
|
|
|
10: ['x', 'y', 'r1', 'r2', 'angle', 'largeArcFlag', 'sweepFlag'],
|
2011-02-04 08:02:46 +00:00
|
|
|
12: ['x'],
|
|
|
|
14: ['y'],
|
2013-02-15 16:51:48 +00:00
|
|
|
16: ['x', 'y', 'x2', 'y2'],
|
|
|
|
18: ['x', 'y']
|
2011-02-04 08:02:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var pathFuncs = [];
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
var linkControlPts = true;
|
2011-02-04 16:06:25 +00:00
|
|
|
|
2011-02-23 16:24:07 +00:00
|
|
|
// Stores references to paths via IDs.
|
|
|
|
// TODO: Make this cross-document happy.
|
|
|
|
var pathData = {};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.setLinkControlPoints = function (lcp) {
|
|
|
|
linkControlPts = lcp;
|
2011-02-04 16:06:25 +00:00
|
|
|
};
|
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.path = null;
|
|
|
|
|
|
|
|
var editorContext_ = null;
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.init = function (editorContext) {
|
2011-02-04 08:02:46 +00:00
|
|
|
editorContext_ = editorContext;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2013-02-15 16:51:48 +00:00
|
|
|
pathFuncs = [0, 'ClosePath'];
|
2011-02-04 08:02:46 +00:00
|
|
|
var pathFuncsStrs = ['Moveto', 'Lineto', 'CurvetoCubic', 'CurvetoQuadratic', 'Arc',
|
2013-02-15 16:51:48 +00:00
|
|
|
'LinetoHorizontal', 'LinetoVertical', 'CurvetoCubicSmooth', 'CurvetoQuadraticSmooth'];
|
2018-05-16 00:53:27 +00:00
|
|
|
$.each(pathFuncsStrs, function (i, s) {
|
|
|
|
pathFuncs.push(s + 'Abs');
|
|
|
|
pathFuncs.push(s + 'Rel');
|
2011-02-04 08:02:46 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.insertItemBefore = function (elem, newseg, index) {
|
2011-02-04 08:02:46 +00:00
|
|
|
// Support insertItemBefore on paths for FF2
|
|
|
|
var list = elem.pathSegList;
|
|
|
|
|
2013-02-14 13:11:01 +00:00
|
|
|
if (svgedit.browser.supportsPathInsertItemBefore()) {
|
2011-02-04 08:02:46 +00:00
|
|
|
list.insertItemBefore(newseg, index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var len = list.numberOfItems;
|
|
|
|
var arr = [];
|
2014-01-31 01:05:03 +00:00
|
|
|
var i;
|
2018-05-16 00:53:27 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
var curSeg = list.getItem(i);
|
|
|
|
arr.push(curSeg);
|
2011-02-04 08:02:46 +00:00
|
|
|
}
|
|
|
|
list.clear();
|
2018-05-16 00:53:27 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2018-05-16 08:32:44 +00:00
|
|
|
if (i === index) { // index + 1
|
2011-02-04 08:02:46 +00:00
|
|
|
list.appendItem(newseg);
|
|
|
|
}
|
|
|
|
list.appendItem(arr[i]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: See if this should just live in replacePathSeg
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.ptObjToArr = function (type, segItem) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var arr = segData[type], len = arr.length;
|
2014-02-12 01:07:26 +00:00
|
|
|
var i, out = [];
|
2014-01-31 01:05:03 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2018-05-16 00:53:27 +00:00
|
|
|
out[i] = segItem[arr[i]];
|
2011-02-04 08:02:46 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.getGripPt = function (seg, altPt) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var out = {
|
2018-05-16 00:53:27 +00:00
|
|
|
x: altPt ? altPt.x : seg.item.x,
|
|
|
|
y: altPt ? altPt.y : seg.item.y
|
|
|
|
}, path = seg.path;
|
2011-02-04 08:02:46 +00:00
|
|
|
|
2013-02-14 13:11:01 +00:00
|
|
|
if (path.matrix) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var pt = svgedit.math.transformPoint(out.x, out.y, path.matrix);
|
|
|
|
out = pt;
|
|
|
|
}
|
|
|
|
|
2011-02-09 06:14:47 +00:00
|
|
|
out.x *= editorContext_.getCurrentZoom();
|
|
|
|
out.y *= editorContext_.getCurrentZoom();
|
2011-02-04 08:02:46 +00:00
|
|
|
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.getPointFromGrip = function (pt, path) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var out = {
|
|
|
|
x: pt.x,
|
|
|
|
y: pt.y
|
2013-02-14 13:11:01 +00:00
|
|
|
};
|
2011-02-04 08:02:46 +00:00
|
|
|
|
2013-02-14 13:11:01 +00:00
|
|
|
if (path.matrix) {
|
2014-01-31 01:05:03 +00:00
|
|
|
pt = svgedit.math.transformPoint(out.x, out.y, path.imatrix);
|
2011-02-04 08:02:46 +00:00
|
|
|
out.x = pt.x;
|
|
|
|
out.y = pt.y;
|
|
|
|
}
|
|
|
|
|
2011-02-09 06:14:47 +00:00
|
|
|
out.x /= editorContext_.getCurrentZoom();
|
|
|
|
out.y /= editorContext_.getCurrentZoom();
|
2011-02-04 08:02:46 +00:00
|
|
|
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.addPointGrip = function (index, x, y) {
|
2011-02-04 08:02:46 +00:00
|
|
|
// create the container of all the point grips
|
|
|
|
var pointGripContainer = svgedit.path.getGripContainer();
|
2015-07-06 10:42:25 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
var pointGrip = svgedit.utilities.getElem('pathpointgrip_' + index);
|
2011-02-04 08:02:46 +00:00
|
|
|
// create it
|
|
|
|
if (!pointGrip) {
|
2013-02-16 15:02:26 +00:00
|
|
|
pointGrip = document.createElementNS(NS.SVG, 'circle');
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.utilities.assignAttributes(pointGrip, {
|
2013-02-15 17:02:24 +00:00
|
|
|
'id': 'pathpointgrip_' + index,
|
|
|
|
'display': 'none',
|
2011-02-04 08:02:46 +00:00
|
|
|
'r': 4,
|
2013-02-15 17:02:24 +00:00
|
|
|
'fill': '#0FF',
|
|
|
|
'stroke': '#00F',
|
2011-02-04 08:02:46 +00:00
|
|
|
'stroke-width': 2,
|
|
|
|
'cursor': 'move',
|
|
|
|
'style': 'pointer-events:all',
|
|
|
|
'xlink:title': uiStrings.pathNodeTooltip
|
|
|
|
});
|
|
|
|
pointGrip = pointGripContainer.appendChild(pointGrip);
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
var grip = $('#pathpointgrip_' + index);
|
|
|
|
grip.dblclick(function () {
|
2014-01-31 01:05:03 +00:00
|
|
|
if (svgedit.path.path) {
|
|
|
|
svgedit.path.path.setSegType();
|
|
|
|
}
|
2011-02-04 08:02:46 +00:00
|
|
|
});
|
|
|
|
}
|
2013-02-14 13:11:01 +00:00
|
|
|
if (x && y) {
|
2011-02-04 08:02:46 +00:00
|
|
|
// set up the point grip element and display it
|
|
|
|
svgedit.utilities.assignAttributes(pointGrip, {
|
|
|
|
'cx': x,
|
|
|
|
'cy': y,
|
2013-02-15 17:02:24 +00:00
|
|
|
'display': 'inline'
|
2011-02-04 08:02:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return pointGrip;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.getGripContainer = function () {
|
2013-02-15 17:02:24 +00:00
|
|
|
var c = svgedit.utilities.getElem('pathpointgrip_container');
|
2011-02-04 08:02:46 +00:00
|
|
|
if (!c) {
|
2013-02-15 17:02:24 +00:00
|
|
|
var parent = svgedit.utilities.getElem('selectorParentGroup');
|
2013-02-16 15:02:26 +00:00
|
|
|
c = parent.appendChild(document.createElementNS(NS.SVG, 'g'));
|
2013-02-15 17:02:24 +00:00
|
|
|
c.id = 'pathpointgrip_container';
|
2011-02-04 08:02:46 +00:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.addCtrlGrip = function (id) {
|
|
|
|
var pointGrip = svgedit.utilities.getElem('ctrlpointgrip_' + id);
|
|
|
|
if (pointGrip) { return pointGrip; }
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2013-02-16 15:02:26 +00:00
|
|
|
pointGrip = document.createElementNS(NS.SVG, 'circle');
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.utilities.assignAttributes(pointGrip, {
|
2013-02-15 17:02:24 +00:00
|
|
|
'id': 'ctrlpointgrip_' + id,
|
|
|
|
'display': 'none',
|
2011-02-04 08:02:46 +00:00
|
|
|
'r': 4,
|
2013-02-15 17:02:24 +00:00
|
|
|
'fill': '#0FF',
|
|
|
|
'stroke': '#55F',
|
2011-02-04 08:02:46 +00:00
|
|
|
'stroke-width': 1,
|
|
|
|
'cursor': 'move',
|
|
|
|
'style': 'pointer-events:all',
|
|
|
|
'xlink:title': uiStrings.pathCtrlPtTooltip
|
|
|
|
});
|
|
|
|
svgedit.path.getGripContainer().appendChild(pointGrip);
|
|
|
|
return pointGrip;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.getCtrlLine = function (id) {
|
|
|
|
var ctrlLine = svgedit.utilities.getElem('ctrlLine_' + id);
|
|
|
|
if (ctrlLine) { return ctrlLine; }
|
2015-07-06 10:42:25 +00:00
|
|
|
|
2013-02-16 15:02:26 +00:00
|
|
|
ctrlLine = document.createElementNS(NS.SVG, 'line');
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.utilities.assignAttributes(ctrlLine, {
|
2018-05-16 00:53:27 +00:00
|
|
|
'id': 'ctrlLine_' + id,
|
2013-02-15 17:02:24 +00:00
|
|
|
'stroke': '#555',
|
2011-02-04 08:02:46 +00:00
|
|
|
'stroke-width': 1,
|
2013-02-15 17:02:24 +00:00
|
|
|
'style': 'pointer-events:none'
|
2011-02-04 08:02:46 +00:00
|
|
|
});
|
|
|
|
svgedit.path.getGripContainer().appendChild(ctrlLine);
|
|
|
|
return ctrlLine;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.getPointGrip = function (seg, update) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var index = seg.index;
|
|
|
|
var pointGrip = svgedit.path.addPointGrip(index);
|
|
|
|
|
2013-02-14 13:11:01 +00:00
|
|
|
if (update) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var pt = svgedit.path.getGripPt(seg);
|
|
|
|
svgedit.utilities.assignAttributes(pointGrip, {
|
|
|
|
'cx': pt.x,
|
|
|
|
'cy': pt.y,
|
2013-02-15 17:02:24 +00:00
|
|
|
'display': 'inline'
|
2011-02-04 08:02:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return pointGrip;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.getControlPoints = function (seg) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var item = seg.item;
|
|
|
|
var index = seg.index;
|
2018-05-16 00:53:27 +00:00
|
|
|
if (!('x1' in item) || !('x2' in item)) { return null; }
|
2013-02-14 13:11:01 +00:00
|
|
|
var cpt = {};
|
2018-05-16 00:53:27 +00:00
|
|
|
/* var pointGripContainer = */ svgedit.path.getGripContainer();
|
2011-02-04 08:02:46 +00:00
|
|
|
|
|
|
|
// Note that this is intentionally not seg.prev.item
|
2018-05-16 00:53:27 +00:00
|
|
|
var prev = svgedit.path.path.segs[index - 1].item;
|
2011-02-04 08:02:46 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
var segItems = [prev, item];
|
2011-02-04 08:02:46 +00:00
|
|
|
|
2014-01-31 01:05:03 +00:00
|
|
|
var i;
|
|
|
|
for (i = 1; i < 3; i++) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var id = index + 'c' + i;
|
2015-07-06 10:42:25 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
var ctrlLine = cpt['c' + i + '_line'] = svgedit.path.getCtrlLine(id);
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
var pt = svgedit.path.getGripPt(seg, {x: item['x' + i], y: item['y' + i]});
|
|
|
|
var gpt = svgedit.path.getGripPt(seg, {x: segItems[i - 1].x, y: segItems[i - 1].y});
|
2011-02-04 08:02:46 +00:00
|
|
|
|
|
|
|
svgedit.utilities.assignAttributes(ctrlLine, {
|
|
|
|
'x1': pt.x,
|
|
|
|
'y1': pt.y,
|
|
|
|
'x2': gpt.x,
|
|
|
|
'y2': gpt.y,
|
2013-02-15 17:02:24 +00:00
|
|
|
'display': 'inline'
|
2011-02-04 08:02:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
cpt['c' + i + '_line'] = ctrlLine;
|
|
|
|
|
|
|
|
// create it
|
2014-02-12 01:07:26 +00:00
|
|
|
var pointGrip = cpt['c' + i] = svgedit.path.addCtrlGrip(id);
|
2011-02-04 08:02:46 +00:00
|
|
|
|
|
|
|
svgedit.utilities.assignAttributes(pointGrip, {
|
|
|
|
'cx': pt.x,
|
|
|
|
'cy': pt.y,
|
2013-02-15 17:02:24 +00:00
|
|
|
'display': 'inline'
|
2011-02-04 08:02:46 +00:00
|
|
|
});
|
|
|
|
cpt['c' + i] = pointGrip;
|
|
|
|
}
|
|
|
|
return cpt;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This replaces the segment at the given index. Type is given as number.
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.replacePathSeg = function (type, index, pts, elem) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var path = elem || svgedit.path.path.elem;
|
2015-07-06 10:21:37 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
var func = 'createSVGPathSeg' + pathFuncs[type];
|
|
|
|
var seg = path[func].apply(path, pts);
|
|
|
|
|
2013-02-14 13:11:01 +00:00
|
|
|
if (svgedit.browser.supportsPathReplaceItem()) {
|
2011-02-04 08:02:46 +00:00
|
|
|
path.pathSegList.replaceItem(seg, index);
|
|
|
|
} else {
|
|
|
|
var segList = path.pathSegList;
|
|
|
|
var len = segList.numberOfItems;
|
|
|
|
var arr = [];
|
2014-01-31 01:05:03 +00:00
|
|
|
var i;
|
|
|
|
for (i = 0; i < len; i++) {
|
2018-05-16 00:53:27 +00:00
|
|
|
var curSeg = segList.getItem(i);
|
|
|
|
arr.push(curSeg);
|
2011-02-04 08:02:46 +00:00
|
|
|
}
|
|
|
|
segList.clear();
|
2014-01-31 01:05:03 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2018-05-16 08:32:44 +00:00
|
|
|
if (i === index) {
|
2011-02-04 08:02:46 +00:00
|
|
|
segList.appendItem(seg);
|
|
|
|
} else {
|
|
|
|
segList.appendItem(arr[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.getSegSelector = function (seg, update) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var index = seg.index;
|
2013-02-15 17:02:24 +00:00
|
|
|
var segLine = svgedit.utilities.getElem('segline_' + index);
|
2013-02-14 13:11:01 +00:00
|
|
|
if (!segLine) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var pointGripContainer = svgedit.path.getGripContainer();
|
|
|
|
// create segline
|
2013-02-16 15:02:26 +00:00
|
|
|
segLine = document.createElementNS(NS.SVG, 'path');
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.utilities.assignAttributes(segLine, {
|
2013-02-15 17:02:24 +00:00
|
|
|
'id': 'segline_' + index,
|
2011-02-04 08:02:46 +00:00
|
|
|
'display': 'none',
|
2013-02-15 17:02:24 +00:00
|
|
|
'fill': 'none',
|
|
|
|
'stroke': '#0FF',
|
2011-02-04 08:02:46 +00:00
|
|
|
'stroke-width': 2,
|
2018-05-16 00:53:27 +00:00
|
|
|
'style': 'pointer-events:none',
|
2011-02-04 08:02:46 +00:00
|
|
|
'd': 'M0,0 0,0'
|
|
|
|
});
|
|
|
|
pointGripContainer.appendChild(segLine);
|
2013-02-14 13:11:01 +00:00
|
|
|
}
|
2011-02-04 08:02:46 +00:00
|
|
|
|
2013-02-14 13:11:01 +00:00
|
|
|
if (update) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var prev = seg.prev;
|
2013-02-14 13:11:01 +00:00
|
|
|
if (!prev) {
|
2013-02-15 17:02:24 +00:00
|
|
|
segLine.setAttribute('display', 'none');
|
2011-02-04 08:02:46 +00:00
|
|
|
return segLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
var pt = svgedit.path.getGripPt(prev);
|
|
|
|
// Set start point
|
|
|
|
svgedit.path.replacePathSeg(2, 0, [pt.x, pt.y], segLine);
|
|
|
|
|
|
|
|
var pts = svgedit.path.ptObjToArr(seg.type, seg.item, true);
|
2014-01-31 01:05:03 +00:00
|
|
|
var i;
|
|
|
|
for (i = 0; i < pts.length; i += 2) {
|
2018-05-16 00:53:27 +00:00
|
|
|
pt = svgedit.path.getGripPt(seg, {x: pts[i], y: pts[i + 1]});
|
2011-02-04 08:02:46 +00:00
|
|
|
pts[i] = pt.x;
|
2018-05-16 00:53:27 +00:00
|
|
|
pts[i + 1] = pt.y;
|
2011-02-04 08:02:46 +00:00
|
|
|
}
|
2015-07-06 10:42:25 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
svgedit.path.replacePathSeg(seg.type, 1, pts, segLine);
|
|
|
|
}
|
|
|
|
return segLine;
|
|
|
|
};
|
|
|
|
|
2011-02-09 06:14:47 +00:00
|
|
|
// Function: smoothControlPoints
|
|
|
|
// Takes three points and creates a smoother line based on them
|
2018-05-16 00:53:27 +00:00
|
|
|
//
|
|
|
|
// Parameters:
|
2011-02-09 06:14:47 +00:00
|
|
|
// ct1 - Object with x and y values (first control point)
|
|
|
|
// ct2 - Object with x and y values (second control point)
|
|
|
|
// pt - Object with x and y values (third point)
|
|
|
|
//
|
2018-05-16 00:53:27 +00:00
|
|
|
// Returns:
|
2011-02-09 06:14:47 +00:00
|
|
|
// Array of two "smoothed" point objects
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.smoothControlPoints = function (ct1, ct2, pt) {
|
2011-02-09 06:14:47 +00:00
|
|
|
// each point must not be the origin
|
|
|
|
var x1 = ct1.x - pt.x,
|
|
|
|
y1 = ct1.y - pt.y,
|
|
|
|
x2 = ct2.x - pt.x,
|
|
|
|
y2 = ct2.y - pt.y;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2018-05-16 08:32:44 +00:00
|
|
|
if ((x1 !== 0 || y1 !== 0) && (x2 !== 0 || y2 !== 0)) {
|
2013-02-15 16:51:48 +00:00
|
|
|
var anglea = Math.atan2(y1, x1),
|
|
|
|
angleb = Math.atan2(y2, x2),
|
2018-05-16 00:53:27 +00:00
|
|
|
r1 = Math.sqrt(x1 * x1 + y1 * y1),
|
|
|
|
r2 = Math.sqrt(x2 * x2 + y2 * y2),
|
2011-02-09 06:14:47 +00:00
|
|
|
nct1 = editorContext_.getSVGRoot().createSVGPoint(),
|
2013-02-14 13:11:01 +00:00
|
|
|
nct2 = editorContext_.getSVGRoot().createSVGPoint();
|
2018-05-16 00:53:27 +00:00
|
|
|
if (anglea < 0) { anglea += 2 * Math.PI; }
|
|
|
|
if (angleb < 0) { angleb += 2 * Math.PI; }
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-09 06:14:47 +00:00
|
|
|
var angleBetween = Math.abs(anglea - angleb),
|
2018-05-16 00:53:27 +00:00
|
|
|
angleDiff = Math.abs(Math.PI - angleBetween) / 2;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
var newAnglea, newAngleb;
|
2011-02-09 06:14:47 +00:00
|
|
|
if (anglea - angleb > 0) {
|
2018-05-16 00:53:27 +00:00
|
|
|
newAnglea = angleBetween < Math.PI ? (anglea + angleDiff) : (anglea - angleDiff);
|
|
|
|
newAngleb = angleBetween < Math.PI ? (angleb - angleDiff) : (angleb + angleDiff);
|
|
|
|
} else {
|
|
|
|
newAnglea = angleBetween < Math.PI ? (anglea - angleDiff) : (anglea + angleDiff);
|
|
|
|
newAngleb = angleBetween < Math.PI ? (angleb + angleDiff) : (angleb - angleDiff);
|
2011-02-09 06:14:47 +00:00
|
|
|
}
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-09 06:14:47 +00:00
|
|
|
// rotate the points
|
2018-05-16 00:53:27 +00:00
|
|
|
nct1.x = r1 * Math.cos(newAnglea) + pt.x;
|
|
|
|
nct1.y = r1 * Math.sin(newAnglea) + pt.y;
|
|
|
|
nct2.x = r2 * Math.cos(newAngleb) + pt.x;
|
|
|
|
nct2.y = r2 * Math.sin(newAngleb) + pt.y;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-09 06:14:47 +00:00
|
|
|
return [nct1, nct2];
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Segment = function (index, item) {
|
2011-02-04 08:02:46 +00:00
|
|
|
this.selected = false;
|
|
|
|
this.index = index;
|
|
|
|
this.item = item;
|
|
|
|
this.type = item.pathSegType;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-04 08:02:46 +00:00
|
|
|
this.ctrlpts = [];
|
|
|
|
this.ptgrip = null;
|
|
|
|
this.segsel = null;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Segment.prototype.showCtrlPts = function (y) {
|
2014-01-31 01:05:03 +00:00
|
|
|
var i;
|
|
|
|
for (i in this.ctrlpts) {
|
2014-02-12 01:07:26 +00:00
|
|
|
if (this.ctrlpts.hasOwnProperty(i)) {
|
|
|
|
this.ctrlpts[i].setAttribute('display', y ? 'inline' : 'none');
|
|
|
|
}
|
2011-02-04 08:02:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Segment.prototype.selectCtrls = function (y) {
|
|
|
|
$('#ctrlpointgrip_' + this.index + 'c1, #ctrlpointgrip_' + this.index + 'c2')
|
|
|
|
.attr('fill', y ? '#0FF' : '#EEE');
|
2011-02-04 08:02:46 +00:00
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Segment.prototype.show = function (y) {
|
2013-02-14 13:11:01 +00:00
|
|
|
if (this.ptgrip) {
|
2013-02-15 17:02:24 +00:00
|
|
|
this.ptgrip.setAttribute('display', y ? 'inline' : 'none');
|
|
|
|
this.segsel.setAttribute('display', y ? 'inline' : 'none');
|
2011-02-04 08:02:46 +00:00
|
|
|
// Show/hide all control points if available
|
|
|
|
this.showCtrlPts(y);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Segment.prototype.select = function (y) {
|
2013-02-14 13:11:01 +00:00
|
|
|
if (this.ptgrip) {
|
2013-02-15 17:02:24 +00:00
|
|
|
this.ptgrip.setAttribute('stroke', y ? '#0FF' : '#00F');
|
|
|
|
this.segsel.setAttribute('display', y ? 'inline' : 'none');
|
2013-02-14 13:11:01 +00:00
|
|
|
if (this.ctrlpts) {
|
2011-02-04 08:02:46 +00:00
|
|
|
this.selectCtrls(y);
|
|
|
|
}
|
|
|
|
this.selected = y;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Segment.prototype.addGrip = function () {
|
2011-02-04 08:02:46 +00:00
|
|
|
this.ptgrip = svgedit.path.getPointGrip(this, true);
|
|
|
|
this.ctrlpts = svgedit.path.getControlPoints(this, true);
|
|
|
|
this.segsel = svgedit.path.getSegSelector(this, true);
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Segment.prototype.update = function (full) {
|
2013-02-14 13:11:01 +00:00
|
|
|
if (this.ptgrip) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var pt = svgedit.path.getGripPt(this);
|
|
|
|
svgedit.utilities.assignAttributes(this.ptgrip, {
|
|
|
|
'cx': pt.x,
|
|
|
|
'cy': pt.y
|
|
|
|
});
|
|
|
|
|
|
|
|
svgedit.path.getSegSelector(this, true);
|
|
|
|
|
2013-02-14 13:11:01 +00:00
|
|
|
if (this.ctrlpts) {
|
|
|
|
if (full) {
|
2011-02-04 08:02:46 +00:00
|
|
|
this.item = svgedit.path.path.elem.pathSegList.getItem(this.index);
|
|
|
|
this.type = this.item.pathSegType;
|
|
|
|
}
|
|
|
|
svgedit.path.getControlPoints(this);
|
2013-02-14 13:11:01 +00:00
|
|
|
}
|
2013-02-15 17:02:24 +00:00
|
|
|
// this.segsel.setAttribute('display', y?'inline':'none');
|
2011-02-04 08:02:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Segment.prototype.move = function (dx, dy) {
|
|
|
|
var curPts, item = this.item;
|
2011-02-04 08:02:46 +00:00
|
|
|
|
2016-02-22 01:13:40 +00:00
|
|
|
if (this.ctrlpts) {
|
2018-05-16 00:53:27 +00:00
|
|
|
curPts = [item.x += dx, item.y += dy,
|
2016-02-22 01:13:40 +00:00
|
|
|
item.x1, item.y1, item.x2 += dx, item.y2 += dy];
|
|
|
|
} else {
|
2018-05-16 00:53:27 +00:00
|
|
|
curPts = [item.x += dx, item.y += dy];
|
2016-02-22 01:13:40 +00:00
|
|
|
}
|
2015-07-06 10:21:37 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.replacePathSeg(this.type, this.index, curPts);
|
2011-02-04 08:02:46 +00:00
|
|
|
|
2013-02-14 13:11:01 +00:00
|
|
|
if (this.next && this.next.ctrlpts) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var next = this.next.item;
|
2018-05-16 00:53:27 +00:00
|
|
|
var nextPts = [next.x, next.y,
|
2011-02-04 08:02:46 +00:00
|
|
|
next.x1 += dx, next.y1 += dy, next.x2, next.y2];
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.replacePathSeg(this.next.type, this.next.index, nextPts);
|
2011-02-04 08:02:46 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:11:01 +00:00
|
|
|
if (this.mate) {
|
2013-02-15 17:02:24 +00:00
|
|
|
// The last point of a closed subpath has a 'mate',
|
|
|
|
// which is the 'M' segment of the subpath
|
2014-01-31 01:05:03 +00:00
|
|
|
item = this.mate.item;
|
2011-02-04 08:02:46 +00:00
|
|
|
var pts = [item.x += dx, item.y += dy];
|
|
|
|
svgedit.path.replacePathSeg(this.mate.type, this.mate.index, pts);
|
2013-02-15 17:02:24 +00:00
|
|
|
// Has no grip, so does not need 'updating'?
|
2011-02-04 08:02:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.update(true);
|
2018-05-16 00:53:27 +00:00
|
|
|
if (this.next) { this.next.update(true); }
|
2011-02-04 08:02:46 +00:00
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Segment.prototype.setLinked = function (num) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var seg, anum, pt;
|
2018-05-16 08:32:44 +00:00
|
|
|
if (num === 2) {
|
2011-02-04 08:02:46 +00:00
|
|
|
anum = 1;
|
|
|
|
seg = this.next;
|
2018-05-16 00:53:27 +00:00
|
|
|
if (!seg) { return; }
|
2011-02-04 08:02:46 +00:00
|
|
|
pt = this.item;
|
|
|
|
} else {
|
|
|
|
anum = 2;
|
|
|
|
seg = this.prev;
|
2018-05-16 00:53:27 +00:00
|
|
|
if (!seg) { return; }
|
2011-02-04 08:02:46 +00:00
|
|
|
pt = seg.item;
|
|
|
|
}
|
|
|
|
|
|
|
|
var item = seg.item;
|
2018-05-16 00:53:27 +00:00
|
|
|
item['x' + anum] = pt.x + (pt.x - this.item['x' + num]);
|
|
|
|
item['y' + anum] = pt.y + (pt.y - this.item['y' + num]);
|
2011-02-04 08:02:46 +00:00
|
|
|
|
2016-02-22 01:13:40 +00:00
|
|
|
var pts = [item.x, item.y,
|
|
|
|
item.x1, item.y1,
|
|
|
|
item.x2, item.y2];
|
2011-02-04 08:02:46 +00:00
|
|
|
|
|
|
|
svgedit.path.replacePathSeg(seg.type, seg.index, pts);
|
|
|
|
seg.update(true);
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Segment.prototype.moveCtrl = function (num, dx, dy) {
|
2011-02-04 08:02:46 +00:00
|
|
|
var item = this.item;
|
2016-02-22 01:13:40 +00:00
|
|
|
item['x' + num] += dx;
|
|
|
|
item['y' + num] += dy;
|
2011-02-04 08:02:46 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
var pts = [item.x, item.y,
|
|
|
|
item.x1, item.y1, item.x2, item.y2];
|
2011-02-04 08:02:46 +00:00
|
|
|
|
|
|
|
svgedit.path.replacePathSeg(this.type, this.index, pts);
|
|
|
|
this.update(true);
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Segment.prototype.setType = function (newType, pts) {
|
|
|
|
svgedit.path.replacePathSeg(newType, this.index, pts);
|
|
|
|
this.type = newType;
|
2011-02-04 08:02:46 +00:00
|
|
|
this.item = svgedit.path.path.elem.pathSegList.getItem(this.index);
|
2018-05-16 00:53:27 +00:00
|
|
|
this.showCtrlPts(newType === 6);
|
2011-02-04 08:02:46 +00:00
|
|
|
this.ctrlpts = svgedit.path.getControlPoints(this);
|
|
|
|
this.update(true);
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path = function (elem) {
|
2013-02-15 17:02:24 +00:00
|
|
|
if (!elem || elem.tagName !== 'path') {
|
2018-05-16 00:53:27 +00:00
|
|
|
throw new Error('svgedit.path.Path constructed without a <path> element');
|
2011-02-04 16:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.elem = elem;
|
|
|
|
this.segs = [];
|
|
|
|
this.selected_pts = [];
|
|
|
|
svgedit.path.path = this;
|
|
|
|
|
|
|
|
this.init();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Reset path data
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.init = function () {
|
2011-02-04 16:06:25 +00:00
|
|
|
// Hide all grips, etc
|
2015-07-06 10:21:37 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
// fixed, needed to work on all found elements, not just first
|
|
|
|
$(svgedit.path.getGripContainer()).find('*').each(function () {
|
|
|
|
$(this).attr('display', 'none');
|
|
|
|
});
|
2015-07-06 10:21:37 +00:00
|
|
|
|
2011-02-04 16:06:25 +00:00
|
|
|
var segList = this.elem.pathSegList;
|
|
|
|
var len = segList.numberOfItems;
|
|
|
|
this.segs = [];
|
|
|
|
this.selected_pts = [];
|
|
|
|
this.first_seg = null;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-04 16:06:25 +00:00
|
|
|
// Set up segs array
|
2014-01-31 01:05:03 +00:00
|
|
|
var i;
|
|
|
|
for (i = 0; i < len; i++) {
|
2011-02-04 16:06:25 +00:00
|
|
|
var item = segList.getItem(i);
|
|
|
|
var segment = new svgedit.path.Segment(i, item);
|
|
|
|
segment.path = this;
|
|
|
|
this.segs.push(segment);
|
2013-02-14 13:11:01 +00:00
|
|
|
}
|
|
|
|
|
2011-02-04 16:06:25 +00:00
|
|
|
var segs = this.segs;
|
2018-05-16 00:53:27 +00:00
|
|
|
var startI = null;
|
2011-02-04 16:06:25 +00:00
|
|
|
|
2014-01-31 01:05:03 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2013-02-14 13:11:01 +00:00
|
|
|
var seg = segs[i];
|
2018-05-16 00:53:27 +00:00
|
|
|
var nextSeg = (i + 1) >= len ? null : segs[i + 1];
|
|
|
|
var prevSeg = (i - 1) < 0 ? null : segs[i - 1];
|
|
|
|
var startSeg;
|
2013-02-14 13:11:01 +00:00
|
|
|
if (seg.type === 2) {
|
2018-05-16 00:53:27 +00:00
|
|
|
if (prevSeg && prevSeg.type !== 1) {
|
2011-02-04 16:06:25 +00:00
|
|
|
// New sub-path, last one is open,
|
|
|
|
// so add a grip to last sub-path's first point
|
2018-05-16 00:53:27 +00:00
|
|
|
startSeg = segs[startI];
|
|
|
|
startSeg.next = segs[startI + 1];
|
|
|
|
startSeg.next.prev = startSeg;
|
|
|
|
startSeg.addGrip();
|
2011-02-04 16:06:25 +00:00
|
|
|
}
|
|
|
|
// Remember that this is a starter seg
|
2018-05-16 00:53:27 +00:00
|
|
|
startI = i;
|
|
|
|
} else if (nextSeg && nextSeg.type === 1) {
|
2011-02-04 16:06:25 +00:00
|
|
|
// This is the last real segment of a closed sub-path
|
|
|
|
// Next is first seg after "M"
|
2018-05-16 00:53:27 +00:00
|
|
|
seg.next = segs[startI + 1];
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-04 16:06:25 +00:00
|
|
|
// First seg after "M"'s prev is this
|
|
|
|
seg.next.prev = seg;
|
2018-05-16 00:53:27 +00:00
|
|
|
seg.mate = segs[startI];
|
2011-02-04 16:06:25 +00:00
|
|
|
seg.addGrip();
|
2013-02-14 13:11:01 +00:00
|
|
|
if (this.first_seg == null) {
|
2011-02-04 16:06:25 +00:00
|
|
|
this.first_seg = seg;
|
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
} else if (!nextSeg) {
|
2013-02-14 13:11:01 +00:00
|
|
|
if (seg.type !== 1) {
|
2011-02-04 16:06:25 +00:00
|
|
|
// Last seg, doesn't close so add a grip
|
|
|
|
// to last sub-path's first point
|
2018-05-16 00:53:27 +00:00
|
|
|
startSeg = segs[startI];
|
|
|
|
startSeg.next = segs[startI + 1];
|
|
|
|
startSeg.next.prev = startSeg;
|
|
|
|
startSeg.addGrip();
|
2011-02-04 16:06:25 +00:00
|
|
|
seg.addGrip();
|
|
|
|
|
2013-02-14 13:11:01 +00:00
|
|
|
if (!this.first_seg) {
|
2011-02-04 16:06:25 +00:00
|
|
|
// Open path, so set first as real first and add grip
|
2018-05-16 00:53:27 +00:00
|
|
|
this.first_seg = segs[startI];
|
2011-02-04 16:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-16 00:53:27 +00:00
|
|
|
} else if (seg.type !== 1) {
|
2011-02-04 16:06:25 +00:00
|
|
|
// Regular segment, so add grip and its "next"
|
|
|
|
seg.addGrip();
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-04 16:06:25 +00:00
|
|
|
// Don't set its "next" if it's an "M"
|
2018-05-16 00:53:27 +00:00
|
|
|
if (nextSeg && nextSeg.type !== 2) {
|
|
|
|
seg.next = nextSeg;
|
2011-02-04 16:06:25 +00:00
|
|
|
seg.next.prev = seg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.eachSeg = function (fn) {
|
2014-01-31 01:05:03 +00:00
|
|
|
var i;
|
2013-02-14 13:11:01 +00:00
|
|
|
var len = this.segs.length;
|
2014-01-31 01:05:03 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2011-02-04 16:06:25 +00:00
|
|
|
var ret = fn.call(this.segs[i], i);
|
2018-05-16 00:53:27 +00:00
|
|
|
if (ret === false) { break; }
|
2011-02-04 16:06:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.addSeg = function (index) {
|
2011-02-04 16:06:25 +00:00
|
|
|
// Adds a new segment
|
|
|
|
var seg = this.segs[index];
|
2018-05-16 00:53:27 +00:00
|
|
|
if (!seg.prev) { return; }
|
2011-02-04 16:06:25 +00:00
|
|
|
|
|
|
|
var prev = seg.prev;
|
2018-05-16 00:53:27 +00:00
|
|
|
var newseg, newX, newY;
|
|
|
|
switch (seg.item.pathSegType) {
|
2011-02-04 16:06:25 +00:00
|
|
|
case 4:
|
2018-05-16 00:53:27 +00:00
|
|
|
newX = (seg.item.x + prev.item.x) / 2;
|
|
|
|
newY = (seg.item.y + prev.item.y) / 2;
|
|
|
|
newseg = this.elem.createSVGPathSegLinetoAbs(newX, newY);
|
2011-02-04 16:06:25 +00:00
|
|
|
break;
|
2018-05-16 00:53:27 +00:00
|
|
|
case 6: // make it a curved segment to preserve the shape (WRS)
|
2011-02-04 16:06:25 +00:00
|
|
|
// http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm#Geometric_interpretation
|
2018-05-16 00:53:27 +00:00
|
|
|
var p0x = (prev.item.x + seg.item.x1) / 2;
|
|
|
|
var p1x = (seg.item.x1 + seg.item.x2) / 2;
|
|
|
|
var p2x = (seg.item.x2 + seg.item.x) / 2;
|
|
|
|
var p01x = (p0x + p1x) / 2;
|
|
|
|
var p12x = (p1x + p2x) / 2;
|
|
|
|
newX = (p01x + p12x) / 2;
|
|
|
|
var p0y = (prev.item.y + seg.item.y1) / 2;
|
|
|
|
var p1y = (seg.item.y1 + seg.item.y2) / 2;
|
|
|
|
var p2y = (seg.item.y2 + seg.item.y) / 2;
|
|
|
|
var p01y = (p0y + p1y) / 2;
|
|
|
|
var p12y = (p1y + p2y) / 2;
|
|
|
|
newY = (p01y + p12y) / 2;
|
|
|
|
newseg = this.elem.createSVGPathSegCurvetoCubicAbs(newX, newY, p0x, p0y, p01x, p01y);
|
|
|
|
var pts = [seg.item.x, seg.item.y, p12x, p12y, p2x, p2y];
|
2013-02-15 16:51:48 +00:00
|
|
|
svgedit.path.replacePathSeg(seg.type, index, pts);
|
2011-02-04 16:06:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
svgedit.path.insertItemBefore(this.elem, newseg, index);
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.deleteSeg = function (index) {
|
2011-02-04 16:06:25 +00:00
|
|
|
var seg = this.segs[index];
|
|
|
|
var list = this.elem.pathSegList;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-04 16:06:25 +00:00
|
|
|
seg.show(false);
|
|
|
|
var next = seg.next;
|
2014-01-31 01:05:03 +00:00
|
|
|
var pt;
|
2013-02-14 13:11:01 +00:00
|
|
|
if (seg.mate) {
|
2011-02-04 16:06:25 +00:00
|
|
|
// Make the next point be the "M" point
|
2014-01-31 01:05:03 +00:00
|
|
|
pt = [next.item.x, next.item.y];
|
2011-02-04 16:06:25 +00:00
|
|
|
svgedit.path.replacePathSeg(2, next.index, pt);
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-04 16:06:25 +00:00
|
|
|
// Reposition last node
|
|
|
|
svgedit.path.replacePathSeg(4, seg.index, pt);
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-04 16:06:25 +00:00
|
|
|
list.removeItem(seg.mate.index);
|
2013-02-14 13:11:01 +00:00
|
|
|
} else if (!seg.prev) {
|
2011-02-04 16:06:25 +00:00
|
|
|
// First node of open path, make next point the M
|
2018-05-16 00:53:27 +00:00
|
|
|
// var item = seg.item;
|
2014-01-31 01:05:03 +00:00
|
|
|
pt = [next.item.x, next.item.y];
|
2011-02-04 16:06:25 +00:00
|
|
|
svgedit.path.replacePathSeg(2, seg.next.index, pt);
|
|
|
|
list.removeItem(index);
|
|
|
|
} else {
|
|
|
|
list.removeItem(index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.subpathIsClosed = function (index) {
|
2011-02-04 16:06:25 +00:00
|
|
|
var closed = false;
|
|
|
|
// Check if subpath is already open
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.path.eachSeg(function (i) {
|
|
|
|
if (i <= index) { return true; }
|
2013-02-14 13:11:01 +00:00
|
|
|
if (this.type === 2) {
|
2011-02-04 16:06:25 +00:00
|
|
|
// Found M first, so open
|
|
|
|
return false;
|
2013-02-14 13:11:01 +00:00
|
|
|
}
|
|
|
|
if (this.type === 1) {
|
2011-02-04 16:06:25 +00:00
|
|
|
// Found Z first, so closed
|
|
|
|
closed = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-04 16:06:25 +00:00
|
|
|
return closed;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.removePtFromSelection = function (index) {
|
2011-02-04 16:06:25 +00:00
|
|
|
var pos = this.selected_pts.indexOf(index);
|
2018-05-16 08:32:44 +00:00
|
|
|
if (pos === -1) {
|
2011-02-04 16:06:25 +00:00
|
|
|
return;
|
2013-02-14 13:11:01 +00:00
|
|
|
}
|
2011-02-04 16:06:25 +00:00
|
|
|
this.segs[index].select(false);
|
|
|
|
this.selected_pts.splice(pos, 1);
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.clearSelection = function () {
|
|
|
|
this.eachSeg(function () {
|
2011-02-04 16:06:25 +00:00
|
|
|
// 'this' is the segment here
|
|
|
|
this.select(false);
|
|
|
|
});
|
|
|
|
this.selected_pts = [];
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.storeD = function () {
|
2011-02-04 16:06:25 +00:00
|
|
|
this.last_d = this.elem.getAttribute('d');
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.show = function (y) {
|
2011-02-04 16:06:25 +00:00
|
|
|
// Shows this path's segment grips
|
2018-05-16 00:53:27 +00:00
|
|
|
this.eachSeg(function () {
|
2011-02-04 16:06:25 +00:00
|
|
|
// 'this' is the segment here
|
|
|
|
this.show(y);
|
|
|
|
});
|
2013-02-14 13:11:01 +00:00
|
|
|
if (y) {
|
2011-02-04 16:06:25 +00:00
|
|
|
this.selectPt(this.first_seg.index);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2013-02-14 13:11:01 +00:00
|
|
|
// Move selected points
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.movePts = function (dx, dy) {
|
2011-02-04 16:06:25 +00:00
|
|
|
var i = this.selected_pts.length;
|
2018-05-16 00:53:27 +00:00
|
|
|
while (i--) {
|
2011-02-04 16:06:25 +00:00
|
|
|
var seg = this.segs[this.selected_pts[i]];
|
2018-05-16 00:53:27 +00:00
|
|
|
seg.move(dx, dy);
|
2011-02-04 16:06:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.moveCtrl = function (dx, dy) {
|
2011-02-04 16:06:25 +00:00
|
|
|
var seg = this.segs[this.selected_pts[0]];
|
2018-05-16 00:53:27 +00:00
|
|
|
seg.moveCtrl(this.dragctrl, dx, dy);
|
|
|
|
if (linkControlPts) {
|
2011-02-04 16:06:25 +00:00
|
|
|
seg.setLinked(this.dragctrl);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.setSegType = function (newType) {
|
2011-02-04 16:06:25 +00:00
|
|
|
this.storeD();
|
|
|
|
var i = this.selected_pts.length;
|
|
|
|
var text;
|
2018-05-16 00:53:27 +00:00
|
|
|
while (i--) {
|
|
|
|
var selPt = this.selected_pts[i];
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-04 16:06:25 +00:00
|
|
|
// Selected seg
|
2018-05-16 00:53:27 +00:00
|
|
|
var cur = this.segs[selPt];
|
2011-02-04 16:06:25 +00:00
|
|
|
var prev = cur.prev;
|
2018-05-16 00:53:27 +00:00
|
|
|
if (!prev) { continue; }
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
if (!newType) { // double-click, so just toggle
|
2013-02-15 17:02:24 +00:00
|
|
|
text = 'Toggle Path Segment Type';
|
2011-02-04 16:06:25 +00:00
|
|
|
|
|
|
|
// Toggle segment to curve/straight line
|
2018-05-16 00:53:27 +00:00
|
|
|
var oldType = cur.type;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2018-05-16 08:32:44 +00:00
|
|
|
newType = (oldType === 6) ? 4 : 6;
|
2013-02-14 13:11:01 +00:00
|
|
|
}
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
newType = Number(newType);
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
var curX = cur.item.x;
|
|
|
|
var curY = cur.item.y;
|
|
|
|
var prevX = prev.item.x;
|
|
|
|
var prevY = prev.item.y;
|
2011-02-04 16:06:25 +00:00
|
|
|
var points;
|
2018-05-16 00:53:27 +00:00
|
|
|
switch (newType) {
|
2011-02-04 16:06:25 +00:00
|
|
|
case 6:
|
2013-02-14 13:11:01 +00:00
|
|
|
if (cur.olditem) {
|
2011-02-04 16:06:25 +00:00
|
|
|
var old = cur.olditem;
|
2018-05-16 00:53:27 +00:00
|
|
|
points = [curX, curY, old.x1, old.y1, old.x2, old.y2];
|
2011-02-04 16:06:25 +00:00
|
|
|
} else {
|
2018-05-16 00:53:27 +00:00
|
|
|
var diffX = curX - prevX;
|
|
|
|
var diffY = curY - prevY;
|
2011-02-04 16:06:25 +00:00
|
|
|
// get control points from straight line segment
|
|
|
|
/*
|
2018-05-16 00:53:27 +00:00
|
|
|
var ct1x = (prevX + (diffY/2));
|
|
|
|
var ct1y = (prevY - (diffX/2));
|
|
|
|
var ct2x = (curX + (diffY/2));
|
|
|
|
var ct2y = (curY - (diffX/2));
|
2011-02-04 16:06:25 +00:00
|
|
|
*/
|
2018-05-16 00:53:27 +00:00
|
|
|
// create control points on the line to preserve the shape (WRS)
|
|
|
|
var ct1x = (prevX + (diffX / 3));
|
|
|
|
var ct1y = (prevY + (diffY / 3));
|
|
|
|
var ct2x = (curX - (diffX / 3));
|
|
|
|
var ct2y = (curY - (diffY / 3));
|
|
|
|
points = [curX, curY, ct1x, ct1y, ct2x, ct2y];
|
2011-02-04 16:06:25 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
2018-05-16 00:53:27 +00:00
|
|
|
points = [curX, curY];
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-04 16:06:25 +00:00
|
|
|
// Store original prevve segment nums
|
|
|
|
cur.olditem = cur.item;
|
|
|
|
break;
|
|
|
|
}
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
cur.setType(newType, points);
|
2011-02-04 16:06:25 +00:00
|
|
|
}
|
|
|
|
svgedit.path.path.endChanges(text);
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.selectPt = function (pt, ctrlNum) {
|
2011-02-04 16:06:25 +00:00
|
|
|
this.clearSelection();
|
2013-02-14 13:11:01 +00:00
|
|
|
if (pt == null) {
|
2018-05-16 00:53:27 +00:00
|
|
|
this.eachSeg(function (i) {
|
2011-02-04 16:06:25 +00:00
|
|
|
// 'this' is the segment here.
|
2013-02-14 13:11:01 +00:00
|
|
|
if (this.prev) {
|
2011-02-04 16:06:25 +00:00
|
|
|
pt = i;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.addPtsToSelection(pt);
|
2018-05-16 00:53:27 +00:00
|
|
|
if (ctrlNum) {
|
|
|
|
this.dragctrl = ctrlNum;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
if (linkControlPts) {
|
|
|
|
this.segs[pt].setLinked(ctrlNum);
|
2011-02-04 16:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-02-09 06:14:47 +00:00
|
|
|
// Update position of all points
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.Path.prototype.update = function () {
|
2011-02-09 06:14:47 +00:00
|
|
|
var elem = this.elem;
|
2013-02-14 13:11:01 +00:00
|
|
|
if (svgedit.utilities.getRotationAngle(elem)) {
|
2011-02-09 06:14:47 +00:00
|
|
|
this.matrix = svgedit.math.getMatrix(elem);
|
|
|
|
this.imatrix = this.matrix.inverse();
|
|
|
|
} else {
|
|
|
|
this.matrix = null;
|
|
|
|
this.imatrix = null;
|
|
|
|
}
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
this.eachSeg(function (i) {
|
2011-02-09 06:14:47 +00:00
|
|
|
this.item = elem.pathSegList.getItem(i);
|
|
|
|
this.update();
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.getPath_ = function (elem) {
|
2011-03-07 18:26:12 +00:00
|
|
|
var p = pathData[elem.id];
|
2014-01-31 01:05:03 +00:00
|
|
|
if (!p) {
|
|
|
|
p = pathData[elem.id] = new svgedit.path.Path(elem);
|
|
|
|
}
|
2011-03-07 18:26:12 +00:00
|
|
|
return p;
|
|
|
|
};
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.removePath_ = function (id) {
|
|
|
|
if (id in pathData) { delete pathData[id]; }
|
2011-03-07 18:26:12 +00:00
|
|
|
};
|
2014-02-12 01:07:26 +00:00
|
|
|
var newcx, newcy, oldcx, oldcy, angle;
|
2018-05-16 00:53:27 +00:00
|
|
|
var getRotVals = function (x, y) {
|
2014-02-12 01:07:26 +00:00
|
|
|
var dx = x - oldcx;
|
|
|
|
var dy = y - oldcy;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-24 16:13:26 +00:00
|
|
|
// rotate the point around the old center
|
2018-05-16 00:53:27 +00:00
|
|
|
var r = Math.sqrt(dx * dx + dy * dy);
|
2014-02-12 01:07:26 +00:00
|
|
|
var theta = Math.atan2(dy, dx) + angle;
|
2011-02-24 16:13:26 +00:00
|
|
|
dx = r * Math.cos(theta) + oldcx;
|
|
|
|
dy = r * Math.sin(theta) + oldcy;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-02-24 16:13:26 +00:00
|
|
|
// dx,dy should now hold the actual coordinates of each
|
|
|
|
// point after being rotated
|
|
|
|
|
|
|
|
// now we want to rotate them around the new center in the reverse direction
|
|
|
|
dx -= newcx;
|
|
|
|
dy -= newcy;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
r = Math.sqrt(dx * dx + dy * dy);
|
2013-02-15 16:51:48 +00:00
|
|
|
theta = Math.atan2(dy, dx) - angle;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2014-02-12 01:07:26 +00:00
|
|
|
return {'x': r * Math.cos(theta) + newcx,
|
|
|
|
'y': r * Math.sin(theta) + newcy};
|
2011-02-24 16:13:26 +00:00
|
|
|
};
|
|
|
|
|
2011-03-07 18:26:12 +00:00
|
|
|
// If the path was rotated, we must now pay the piper:
|
2018-05-16 00:53:27 +00:00
|
|
|
// Every path point must be rotated into the rotated coordinate system of
|
2011-03-07 18:26:12 +00:00
|
|
|
// its old center, then determine the new center, then rotate it back
|
|
|
|
// This is because we want the path to remember its rotation
|
|
|
|
|
|
|
|
// TODO: This is still using ye olde transform methods, can probably
|
|
|
|
// be optimized or even taken care of by recalculateDimensions
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.recalcRotatedPath = function () {
|
|
|
|
var currentPath = svgedit.path.path.elem;
|
|
|
|
angle = svgedit.utilities.getRotationAngle(currentPath, true);
|
|
|
|
if (!angle) { return; }
|
|
|
|
// selectedBBoxes[0] = svgedit.path.path.oldbbox;
|
|
|
|
var box = svgedit.utilities.getBBox(currentPath),
|
|
|
|
oldbox = svgedit.path.path.oldbbox; // selectedBBoxes[0],
|
|
|
|
oldcx = oldbox.x + oldbox.width / 2;
|
|
|
|
oldcy = oldbox.y + oldbox.height / 2;
|
|
|
|
newcx = box.x + box.width / 2;
|
|
|
|
newcy = box.y + box.height / 2;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-03-07 18:26:12 +00:00
|
|
|
// un-rotate the new center to the proper position
|
2014-02-12 01:07:26 +00:00
|
|
|
var dx = newcx - oldcx,
|
2011-03-07 18:26:12 +00:00
|
|
|
dy = newcy - oldcy,
|
2018-05-16 00:53:27 +00:00
|
|
|
r = Math.sqrt(dx * dx + dy * dy),
|
2013-02-15 16:51:48 +00:00
|
|
|
theta = Math.atan2(dy, dx) + angle;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-03-07 18:26:12 +00:00
|
|
|
newcx = r * Math.cos(theta) + oldcx;
|
|
|
|
newcy = r * Math.sin(theta) + oldcy;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
var list = currentPath.pathSegList,
|
2011-03-07 18:26:12 +00:00
|
|
|
i = list.numberOfItems;
|
|
|
|
while (i) {
|
|
|
|
i -= 1;
|
|
|
|
var seg = list.getItem(i),
|
|
|
|
type = seg.pathSegType;
|
2018-05-16 08:32:44 +00:00
|
|
|
if (type === 1) { continue; }
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2013-02-15 16:51:48 +00:00
|
|
|
var rvals = getRotVals(seg.x, seg.y),
|
2011-03-07 18:26:12 +00:00
|
|
|
points = [rvals.x, rvals.y];
|
2013-02-14 13:11:01 +00:00
|
|
|
if (seg.x1 != null && seg.x2 != null) {
|
2018-05-16 00:53:27 +00:00
|
|
|
var cVals1 = getRotVals(seg.x1, seg.y1);
|
|
|
|
var cVals2 = getRotVals(seg.x2, seg.y2);
|
|
|
|
points.splice(points.length, 0, cVals1.x, cVals1.y, cVals2.x, cVals2.y);
|
2011-03-07 18:26:12 +00:00
|
|
|
}
|
|
|
|
svgedit.path.replacePathSeg(type, i, points);
|
|
|
|
} // loop for each point
|
2011-02-24 16:13:26 +00:00
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
box = svgedit.utilities.getBBox(currentPath);
|
|
|
|
// selectedBBoxes[0].x = box.x; selectedBBoxes[0].y = box.y;
|
|
|
|
// selectedBBoxes[0].width = box.width; selectedBBoxes[0].height = box.height;
|
2013-02-14 13:11:01 +00:00
|
|
|
|
2011-03-07 18:26:12 +00:00
|
|
|
// now we must set the new transform to be rotated around the new center
|
2018-05-16 00:53:27 +00:00
|
|
|
var Rnc = svgroot.createSVGTransform(),
|
|
|
|
tlist = svgedit.transformlist.getTransformList(currentPath);
|
|
|
|
Rnc.setRotate((angle * 180.0 / Math.PI), newcx, newcy);
|
|
|
|
tlist.replaceItem(Rnc, 0);
|
2011-02-23 16:24:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ====================================
|
|
|
|
// Public API starts here
|
|
|
|
|
2018-05-16 00:53:27 +00:00
|
|
|
svgedit.path.clearData = function () {
|
2011-02-23 16:24:07 +00:00
|
|
|
pathData = {};
|
|
|
|
};
|
2014-01-31 01:05:03 +00:00
|
|
|
}());
|