coords.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*globals $, svgroot */
  2. /*jslint vars: true, eqeq: true, forin: true*/
  3. /**
  4. * Coords.
  5. *
  6. * Licensed under the MIT License
  7. *
  8. */
  9. // Dependencies:
  10. // 1) jquery.js
  11. // 2) math.js
  12. // 3) browser.js
  13. // 4) svgutils.js
  14. // 5) units.js
  15. // 6) svgtransformlist.js
  16. var svgedit = svgedit || {};
  17. (function() {'use strict';
  18. if (!svgedit.coords) {
  19. svgedit.coords = {};
  20. }
  21. // this is how we map paths to our preferred relative segment types
  22. var pathMap = [0, 'z', 'M', 'm', 'L', 'l', 'C', 'c', 'Q', 'q', 'A', 'a',
  23. 'H', 'h', 'V', 'v', 'S', 's', 'T', 't'];
  24. /**
  25. * @typedef editorContext
  26. * @type {?object}
  27. * @property {function} getGridSnapping
  28. * @property {function} getDrawing
  29. */
  30. var editorContext_ = null;
  31. /**
  32. * @param {editorContext} editorContext
  33. */
  34. svgedit.coords.init = function(editorContext) {
  35. editorContext_ = editorContext;
  36. };
  37. /**
  38. * Applies coordinate changes to an element based on the given matrix
  39. * @param {Element} selected - DOM element to be changed
  40. * @param {object} changes - Object with changes to be remapped
  41. * @param {SVGMatrix} m - Matrix object to use for remapping coordinates
  42. */
  43. svgedit.coords.remapElement = function(selected, changes, m) {
  44. var i, type,
  45. remap = function(x, y) { return svgedit.math.transformPoint(x, y, m); },
  46. scalew = function(w) { return m.a * w; },
  47. scaleh = function(h) { return m.d * h; },
  48. doSnapping = editorContext_.getGridSnapping() && selected.parentNode.parentNode.localName === 'svg',
  49. finishUp = function() {
  50. var o;
  51. if (doSnapping) {
  52. for (o in changes) {
  53. changes[o] = svgedit.utilities.snapToGrid(changes[o]);
  54. }
  55. }
  56. svgedit.utilities.assignAttributes(selected, changes, 1000, true);
  57. },
  58. box = svgedit.utilities.getBBox(selected);
  59. for (i = 0; i < 2; i++) {
  60. type = i === 0 ? 'fill' : 'stroke';
  61. var attrVal = selected.getAttribute(type);
  62. if (attrVal && attrVal.indexOf('url(') === 0) {
  63. if (m.a < 0 || m.d < 0) {
  64. var grad = svgedit.utilities.getRefElem(attrVal);
  65. var newgrad = grad.cloneNode(true);
  66. if (m.a < 0) {
  67. // flip x
  68. var x1 = newgrad.getAttribute('x1');
  69. var x2 = newgrad.getAttribute('x2');
  70. newgrad.setAttribute('x1', -(x1 - 1));
  71. newgrad.setAttribute('x2', -(x2 - 1));
  72. }
  73. if (m.d < 0) {
  74. // flip y
  75. var y1 = newgrad.getAttribute('y1');
  76. var y2 = newgrad.getAttribute('y2');
  77. newgrad.setAttribute('y1', -(y1 - 1));
  78. newgrad.setAttribute('y2', -(y2 - 1));
  79. }
  80. newgrad.id = editorContext_.getDrawing().getNextId();
  81. svgedit.utilities.findDefs().appendChild(newgrad);
  82. selected.setAttribute(type, 'url(#' + newgrad.id + ')');
  83. }
  84. // Not really working :(
  85. // if (selected.tagName === 'path') {
  86. // reorientGrads(selected, m);
  87. // }
  88. }
  89. }
  90. var elName = selected.tagName;
  91. var chlist, mt;
  92. if (elName === 'g' || elName === 'text' || elName == 'tspan' || elName === 'use') {
  93. // if it was a translate, then just update x,y
  94. if (m.a == 1 && m.b == 0 && m.c == 0 && m.d == 1 && (m.e != 0 || m.f != 0) ) {
  95. // [T][M] = [M][T']
  96. // therefore [T'] = [M_inv][T][M]
  97. var existing = svgedit.math.transformListToTransform(selected).matrix,
  98. t_new = svgedit.math.matrixMultiply(existing.inverse(), m, existing);
  99. changes.x = parseFloat(changes.x) + t_new.e;
  100. changes.y = parseFloat(changes.y) + t_new.f;
  101. } else {
  102. // we just absorb all matrices into the element and don't do any remapping
  103. chlist = svgedit.transformlist.getTransformList(selected);
  104. mt = svgroot.createSVGTransform();
  105. mt.setMatrix(svgedit.math.matrixMultiply(svgedit.math.transformListToTransform(chlist).matrix, m));
  106. chlist.clear();
  107. chlist.appendItem(mt);
  108. }
  109. }
  110. var c, pt, pt1, pt2, len;
  111. // now we have a set of changes and an applied reduced transform list
  112. // we apply the changes directly to the DOM
  113. switch (elName) {
  114. case 'foreignObject':
  115. case 'rect':
  116. case 'image':
  117. // Allow images to be inverted (give them matrix when flipped)
  118. if (elName === 'image' && (m.a < 0 || m.d < 0)) {
  119. // Convert to matrix
  120. chlist = svgedit.transformlist.getTransformList(selected);
  121. mt = svgroot.createSVGTransform();
  122. mt.setMatrix(svgedit.math.matrixMultiply(svgedit.math.transformListToTransform(chlist).matrix, m));
  123. chlist.clear();
  124. chlist.appendItem(mt);
  125. } else {
  126. pt1 = remap(changes.x, changes.y);
  127. changes.width = scalew(changes.width);
  128. changes.height = scaleh(changes.height);
  129. changes.x = pt1.x + Math.min(0, changes.width);
  130. changes.y = pt1.y + Math.min(0, changes.height);
  131. changes.width = Math.abs(changes.width);
  132. changes.height = Math.abs(changes.height);
  133. }
  134. finishUp();
  135. break;
  136. case 'ellipse':
  137. c = remap(changes.cx, changes.cy);
  138. changes.cx = c.x;
  139. changes.cy = c.y;
  140. changes.rx = scalew(changes.rx);
  141. changes.ry = scaleh(changes.ry);
  142. changes.rx = Math.abs(changes.rx);
  143. changes.ry = Math.abs(changes.ry);
  144. finishUp();
  145. break;
  146. case 'circle':
  147. c = remap(changes.cx,changes.cy);
  148. changes.cx = c.x;
  149. changes.cy = c.y;
  150. // take the minimum of the new selected box's dimensions for the new circle radius
  151. var tbox = svgedit.math.transformBox(box.x, box.y, box.width, box.height, m);
  152. var w = tbox.tr.x - tbox.tl.x, h = tbox.bl.y - tbox.tl.y;
  153. changes.r = Math.min(w/2, h/2);
  154. if (changes.r) {changes.r = Math.abs(changes.r);}
  155. finishUp();
  156. break;
  157. case 'line':
  158. pt1 = remap(changes.x1, changes.y1);
  159. pt2 = remap(changes.x2, changes.y2);
  160. changes.x1 = pt1.x;
  161. changes.y1 = pt1.y;
  162. changes.x2 = pt2.x;
  163. changes.y2 = pt2.y;
  164. // deliberately fall through here
  165. case 'text':
  166. case 'tspan':
  167. case 'use':
  168. finishUp();
  169. break;
  170. case 'g':
  171. var gsvg = $(selected).data('gsvg');
  172. if (gsvg) {
  173. svgedit.utilities.assignAttributes(gsvg, changes, 1000, true);
  174. }
  175. break;
  176. case 'polyline':
  177. case 'polygon':
  178. len = changes.points.length;
  179. for (i = 0; i < len; ++i) {
  180. pt = changes.points[i];
  181. pt = remap(pt.x, pt.y);
  182. changes.points[i].x = pt.x;
  183. changes.points[i].y = pt.y;
  184. }
  185. len = changes.points.length;
  186. var pstr = '';
  187. for (i = 0; i < len; ++i) {
  188. pt = changes.points[i];
  189. pstr += pt.x + ',' + pt.y + ' ';
  190. }
  191. selected.setAttribute('points', pstr);
  192. break;
  193. case 'path':
  194. var seg;
  195. var segList = selected.pathSegList;
  196. len = segList.numberOfItems;
  197. changes.d = [];
  198. for (i = 0; i < len; ++i) {
  199. seg = segList.getItem(i);
  200. changes.d[i] = {
  201. type: seg.pathSegType,
  202. x: seg.x,
  203. y: seg.y,
  204. x1: seg.x1,
  205. y1: seg.y1,
  206. x2: seg.x2,
  207. y2: seg.y2,
  208. r1: seg.r1,
  209. r2: seg.r2,
  210. angle: seg.angle,
  211. largeArcFlag: seg.largeArcFlag,
  212. sweepFlag: seg.sweepFlag
  213. };
  214. }
  215. len = changes.d.length;
  216. var firstseg = changes.d[0],
  217. currentpt = remap(firstseg.x, firstseg.y);
  218. changes.d[0].x = currentpt.x;
  219. changes.d[0].y = currentpt.y;
  220. for (i = 1; i < len; ++i) {
  221. seg = changes.d[i];
  222. type = seg.type;
  223. // if absolute or first segment, we want to remap x, y, x1, y1, x2, y2
  224. // if relative, we want to scalew, scaleh
  225. if (type % 2 == 0) { // absolute
  226. var thisx = (seg.x != undefined) ? seg.x : currentpt.x, // for V commands
  227. thisy = (seg.y != undefined) ? seg.y : currentpt.y; // for H commands
  228. pt = remap(thisx,thisy);
  229. pt1 = remap(seg.x1, seg.y1);
  230. pt2 = remap(seg.x2, seg.y2);
  231. seg.x = pt.x;
  232. seg.y = pt.y;
  233. seg.x1 = pt1.x;
  234. seg.y1 = pt1.y;
  235. seg.x2 = pt2.x;
  236. seg.y2 = pt2.y;
  237. seg.r1 = scalew(seg.r1);
  238. seg.r2 = scaleh(seg.r2);
  239. }
  240. else { // relative
  241. seg.x = scalew(seg.x);
  242. seg.y = scaleh(seg.y);
  243. seg.x1 = scalew(seg.x1);
  244. seg.y1 = scaleh(seg.y1);
  245. seg.x2 = scalew(seg.x2);
  246. seg.y2 = scaleh(seg.y2);
  247. seg.r1 = scalew(seg.r1);
  248. seg.r2 = scaleh(seg.r2);
  249. }
  250. } // for each segment
  251. var dstr = '';
  252. len = changes.d.length;
  253. for (i = 0; i < len; ++i) {
  254. seg = changes.d[i];
  255. type = seg.type;
  256. dstr += pathMap[type];
  257. switch (type) {
  258. case 13: // relative horizontal line (h)
  259. case 12: // absolute horizontal line (H)
  260. dstr += seg.x + ' ';
  261. break;
  262. case 15: // relative vertical line (v)
  263. case 14: // absolute vertical line (V)
  264. dstr += seg.y + ' ';
  265. break;
  266. case 3: // relative move (m)
  267. case 5: // relative line (l)
  268. case 19: // relative smooth quad (t)
  269. case 2: // absolute move (M)
  270. case 4: // absolute line (L)
  271. case 18: // absolute smooth quad (T)
  272. dstr += seg.x + ',' + seg.y + ' ';
  273. break;
  274. case 7: // relative cubic (c)
  275. case 6: // absolute cubic (C)
  276. dstr += seg.x1 + ',' + seg.y1 + ' ' + seg.x2 + ',' + seg.y2 + ' ' +
  277. seg.x + ',' + seg.y + ' ';
  278. break;
  279. case 9: // relative quad (q)
  280. case 8: // absolute quad (Q)
  281. dstr += seg.x1 + ',' + seg.y1 + ' ' + seg.x + ',' + seg.y + ' ';
  282. break;
  283. case 11: // relative elliptical arc (a)
  284. case 10: // absolute elliptical arc (A)
  285. dstr += seg.r1 + ',' + seg.r2 + ' ' + seg.angle + ' ' + (+seg.largeArcFlag) +
  286. ' ' + (+seg.sweepFlag) + ' ' + seg.x + ',' + seg.y + ' ';
  287. break;
  288. case 17: // relative smooth cubic (s)
  289. case 16: // absolute smooth cubic (S)
  290. dstr += seg.x2 + ',' + seg.y2 + ' ' + seg.x + ',' + seg.y + ' ';
  291. break;
  292. }
  293. }
  294. selected.setAttribute('d', dstr);
  295. break;
  296. }
  297. };
  298. }());