/** * Small framework for dynamically creating/deleting shapes on an SVG canvas and responding to events. */ svg = {}; svg.uiElementWrapper = function (element) { this.element = element; }; svg.canvasWrapper = function (element) { svg.uiElementWrapper.call(this, element); this.namespace = 'http://www.w3.org/2000/svg'; this.width = parseInt(element.getAttribute('width')); this.height = parseInt(element.getAttribute('height')); }; svg.canvasWrapper.prototype = new svg.uiElementWrapper(); svg.canvasWrapper.prototype.addCircle = function (x, y, r, style) { let newElement = document.createElementNS(this.namespace, 'circle'); newElement.setAttribute('r', r); newElement.setAttribute('stroke', '#000000'); for (let key in style) { newElement.setAttribute(key, style[key]); } this.element.appendChild(newElement); return new svg.elementWrapper(this, newElement, x, y); }; svg.canvasWrapper.prototype.addRectangle = function (x, y, w, h, style) { let newElement = document.createElementNS(this.namespace, 'rect'); newElement.setAttribute('width', w); newElement.setAttribute('height', h); for (let key in style) newElement.setAttribute(key, style[key]); this.element.appendChild(newElement); let wrapper = new svg.elementWrapper(this, newElement, x, y); let transl = this.element.createSVGTransform(); transl.setTranslate(-w / 2.0, -h / 2.0); newElement.transform.baseVal.appendItem(transl); return wrapper; }; svg.canvasWrapper.prototype.addText = function (x, y, textContent, style) { let newElement = document.createElementNS(this.namespace, 'text'); let textnode = document.createTextNode(textContent); newElement.setAttribute('x', x); newElement.setAttribute('y', y); for (let key in style) { newElement.setAttribute(key, style[key]); } this.element.appendChild(newElement); newElement.appendChild(textnode); let wrapper = new svg.elementWrapper(this, newElement, x, y); wrapper.textnode = textnode return wrapper; } svg.canvasWrapper.prototype.removeElement = function (element) { this.element.removeChild(element.element); }; svg.elementWrapper = function (currentCanvasWrapper, element, x, y) { svg.uiElementWrapper.call(this, element); this.angle = 0.0; this.translation = currentCanvasWrapper.element.createSVGTransform(); this.rotation = currentCanvasWrapper.element.createSVGTransform(); this.translation.setTranslate(x, y); this.rotation.setRotate(this.angle, 0.0, 0.0); /** WORKAROUND Chrome Issue 55010 (1of3) **/ if (window && window.chrome && window.chrome.webstore) this.uiElementWrapper = {x: x, y: y}; let t = this.element.transform.baseVal; t.appendItem(this.translation); t.appendItem(this.rotation); }; svg.elementWrapper.prototype = new svg.uiElementWrapper(); svg.elementWrapper.prototype.setPosition = function (x, y) { /** WORKAROUND Chrome Issue 55010 (2of3) **/ if (window && window.chrome && window.chrome.webstore) { this.chromeTranslation.x = x; this.chromeTranslation.y = y; this.element.setAttribute('transform', 'translate(' + this.chromeTranslation.x + ' ' + this.chromeTranslation.y + ')'); } else this.translation.setTranslate(x, y); }; svg.elementWrapper.prototype.getPosition = function () { /** WORKAROUND Chrome Issue 55010 (3of3) **/ if (window && window.chrome && window.chrome.webstore) return {x: this.chromeTranslation.x, y: this.chromeTranslation.y}; else return {x: this.translation.matrix.e, y: this.translation.matrix.f}; }; svg.elementWrapper.prototype.move = function (dx, dy) { const pos = this.getPosition(); this.setPosition(pos.x + dx, pos.y + dy); }; svg.elementWrapper.prototype.setRotation = function (angle) { this.angle = angle; this.rotation.setRotate(this.angle, 0.0, 0.0); }; svg.elementWrapper.prototype.rotate = function (angle) { this.angle += angle; this.rotation.setRotate(this.angle, 0.0, 0.0); }; svg.elementWrapper.prototype.setColor = function (color) { this.element.setAttribute('fill', color); }; svg.elementWrapper.prototype.setText = function (textContent) { this.element.removeChild(this.textnode) textnode = document.createTextNode(textContent); this.element.appendChild(textnode) this.textnode = textnode };