svg.js 4.1 KB

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