svg.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.remove_element = function(element) {
  42. this.element.removeChild(element.element);
  43. };
  44. svg.element_wrapper = function(canvas_wrapper, element, x, y) {
  45. svg.__ui_element_wrapper.call(this, element);
  46. this.a = 0.0;
  47. this.translation = canvas_wrapper.element.createSVGTransform();
  48. this.rotation = canvas_wrapper.element.createSVGTransform();
  49. this.translation.setTranslate(x,y);
  50. this.rotation.setRotate(this.a, 0.0, 0.0);
  51. /** WORKAROUND Chrome Issue 55010 (1of3) **/
  52. if( window && window.chrome && window.chrome.webstore )
  53. this.__translation = {x:x, y:y};
  54. var t = this.element.transform.baseVal;
  55. t.appendItem(this.translation);
  56. t.appendItem(this.rotation);
  57. };
  58. svg.element_wrapper.prototype = new svg.__ui_element_wrapper();
  59. svg.element_wrapper.prototype.set_position = function(x, y) {
  60. /** WORKAROUND Chrome Issue 55010 (2of3) **/
  61. if( window && window.chrome && window.chrome.webstore )
  62. {
  63. this.__translation.x = x;
  64. this.__translation.y = y;
  65. this.element.setAttribute('transform','translate('+this.__translation.x+' '+this.__translation.y+')');
  66. }
  67. else
  68. this.translation.setTranslate(x,y);
  69. };
  70. svg.element_wrapper.prototype.get_position = function() {
  71. /** WORKAROUND Chrome Issue 55010 (3of3) **/
  72. if( window && window.chrome && window.chrome.webstore )
  73. return {x:this.__translation.x, y:this.__translation.y};
  74. else
  75. return {x: this.translation.matrix.e, y: this.translation.matrix.f};
  76. };
  77. svg.element_wrapper.prototype.move = function(dx, dy) {
  78. var pos = this.get_position();
  79. this.set_position(pos.x + dx, pos.y + dy);
  80. };
  81. svg.element_wrapper.prototype.set_rotation = function(a) {
  82. this.a = a;
  83. this.rotation.setRotate(this.a, 0.0, 0.0);
  84. };
  85. svg.element_wrapper.prototype.rotate = function(a) {
  86. this.a += a;
  87. this.rotation.setRotate(this.a, 0.0, 0.0);
  88. };
  89. svg.element_wrapper.prototype.set_color = function(color) {
  90. this.element.setAttribute('fill', color);
  91. };