svg.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * Small framework for dynamically creating/deleting shapes on an SVG canvas and responding to events.
  3. */
  4. svg = {};
  5. svg.uiElementWrapper = function (element) {
  6. this.element = element;
  7. };
  8. svg.canvasWrapper = function (element) {
  9. svg.uiElementWrapper.call(this, element);
  10. this.namespace = 'http://www.w3.org/2000/svg';
  11. this.width = parseInt(element.getAttribute('width'));
  12. this.height = parseInt(element.getAttribute('height'));
  13. };
  14. svg.canvasWrapper.prototype = new svg.uiElementWrapper();
  15. svg.canvasWrapper.prototype.addCircle = function (x, y, r, style) {
  16. let newElement = document.createElementNS(this.namespace, 'circle');
  17. newElement.setAttribute('r', r);
  18. newElement.setAttribute('stroke', '#000000');
  19. for (let key in style) {
  20. newElement.setAttribute(key, style[key]);
  21. }
  22. this.element.appendChild(newElement);
  23. return new svg.elementWrapper(this, newElement, x, y);
  24. };
  25. svg.canvasWrapper.prototype.addRectangle = function (x, y, w, h, style) {
  26. let newElement = document.createElementNS(this.namespace, 'rect');
  27. newElement.setAttribute('width', w);
  28. newElement.setAttribute('height', h);
  29. for (let key in style)
  30. newElement.setAttribute(key, style[key]);
  31. this.element.appendChild(newElement);
  32. let wrapper = new svg.elementWrapper(this, newElement, x, y);
  33. let transl = this.element.createSVGTransform();
  34. transl.setTranslate(-w / 2.0, -h / 2.0);
  35. newElement.transform.baseVal.appendItem(transl);
  36. return wrapper;
  37. };
  38. svg.canvasWrapper.prototype.addText = function (x, y, textContent, style) {
  39. let newElement = document.createElementNS(this.namespace, 'text');
  40. let textnode = document.createTextNode(textContent);
  41. newElement.setAttribute('x', x);
  42. newElement.setAttribute('y', y);
  43. for (let key in style) {
  44. newElement.setAttribute(key, style[key]);
  45. }
  46. this.element.appendChild(newElement);
  47. newElement.appendChild(textnode);
  48. let wrapper = new svg.elementWrapper(this, newElement, x, y);
  49. wrapper.textnode = textnode
  50. return wrapper;
  51. }
  52. svg.canvasWrapper.prototype.removeElement = function (element) {
  53. this.element.removeChild(element.element);
  54. };
  55. svg.elementWrapper = function (currentCanvasWrapper, element, x, y) {
  56. svg.uiElementWrapper.call(this, element);
  57. this.angle = 0.0;
  58. this.translation = currentCanvasWrapper.element.createSVGTransform();
  59. this.rotation = currentCanvasWrapper.element.createSVGTransform();
  60. this.translation.setTranslate(x, y);
  61. this.rotation.setRotate(this.angle, 0.0, 0.0);
  62. /** WORKAROUND Chrome Issue 55010 (1of3) **/
  63. if (window && window.chrome && window.chrome.webstore)
  64. this.uiElementWrapper = {x: x, y: y};
  65. let t = this.element.transform.baseVal;
  66. t.appendItem(this.translation);
  67. t.appendItem(this.rotation);
  68. };
  69. svg.elementWrapper.prototype = new svg.uiElementWrapper();
  70. svg.elementWrapper.prototype.setPosition = function (x, y) {
  71. /** WORKAROUND Chrome Issue 55010 (2of3) **/
  72. if (window && window.chrome && window.chrome.webstore) {
  73. this.chromeTranslation.x = x;
  74. this.chromeTranslation.y = y;
  75. this.element.setAttribute('transform', 'translate(' + this.chromeTranslation.x + ' ' + this.chromeTranslation.y + ')');
  76. } else
  77. this.translation.setTranslate(x, y);
  78. };
  79. svg.elementWrapper.prototype.getPosition = function () {
  80. /** WORKAROUND Chrome Issue 55010 (3of3) **/
  81. if (window && window.chrome && window.chrome.webstore)
  82. return {x: this.chromeTranslation.x, y: this.chromeTranslation.y};
  83. else
  84. return {x: this.translation.matrix.e, y: this.translation.matrix.f};
  85. };
  86. svg.elementWrapper.prototype.move = function (dx, dy) {
  87. const pos = this.getPosition();
  88. this.setPosition(pos.x + dx, pos.y + dy);
  89. };
  90. svg.elementWrapper.prototype.setRotation = function (angle) {
  91. this.angle = angle;
  92. this.rotation.setRotate(this.angle, 0.0, 0.0);
  93. };
  94. svg.elementWrapper.prototype.rotate = function (angle) {
  95. this.angle += angle;
  96. this.rotation.setRotate(this.angle, 0.0, 0.0);
  97. };
  98. svg.elementWrapper.prototype.setColor = function (color) {
  99. this.element.setAttribute('fill', color);
  100. };
  101. svg.elementWrapper.prototype.setText = function (textContent) {
  102. this.element.removeChild(this.textnode)
  103. textnode = document.createTextNode(textContent);
  104. this.element.appendChild(textnode)
  105. this.textnode = textnode
  106. };