new function() { // Block // svg query processor // id: svg tag's id // return: svg query processor var svgQuery = function(id) { return new svgQuery.int(id); } // for id generation; var prefix = "d20140106svg"; var currentId = 1000; // int: svgQuery's internal object svgQuery.int = function(id) { this.root = document.getElementById(id); }; // attr: get / set attributes to the svg root // attrs: an array of attribute to set // return: this for further processing svgQuery.int.prototype.attr = function(attrs) { if(typeof attrs == "string") { return this.getAttr(attrs); } for (var i in attrs) { this.root.setAttribute(i, attrs[i]); } return this; } // getAttr: get an attribute of SVG root // return: value of the attribute svgQuery.int.prototype.getAttr = function(attr) { return this.root.getAttribute(attr); } // style: get / set style to the svg root svgQuery.int.prototype.style = function(styles) { if(typeof styles == "string") { return this.getAttr(styles); } for (var i in styles) { this.root.style[i] = styles[i]; } return this; } // getStyle: get style entry of SVG root // return: value of the style entry svgQuery.int.prototype.getStyle = function(style) { return this.root.style[style]; } // data: get / set data // data: { "key": value, "key": value, ... } svgQuery.int.prototype.data = function(data) { if(typeof data == "string") { return this.getData(data); } for (var i in data) { this.root[i] = data[i]; } } svgQuery.int.prototype.getData = function(data) { return this.root[data]; } // newId: generate id // return: new id svgQuery.int.prototype.newId = function() { currentId++; return prefix + currentId; } // createElement: create element and elQuery object // tagName: element's tag name to create // return: new elQuery object svgQuery.int.prototype.createElement = function(tagName) { var ns = 'http://www.w3.org/2000/svg'; var el = document.createElementNS(ns, tagName); var id = this.newId(); el.setAttribute("id", id); this.root.appendChild(el); var eqInstance = new svgQuery.elQuery(id); return eqInstance;; } // circle: create circle // cx, cy, r: SVG circle attributes // return: svgQuery.elQuery object svgQuery.int.prototype.circle = function(cx, cy, r) { var el = this.createElement('circle'); return el.attr({ "cx": cx, "cy": cy, "r": r }); } svgQuery.int.prototype.element = function(id) { return new svgQuery.elQuery(id); } // on: register event handler // event: a kind of event // fun: callback function // return: this for further processing svgQuery.int.prototype.on = function(event, fun) { if(typeof this.root['on' + event] !== 'undefined') { this.root['on' + event] = fun; } else { // console.log("Warning: unknown event specified"); } return this; } //// SVG Element Query Processor // elQuery: svg element query processor // id: element's id // reutrn: nothing svgQuery.elQuery = function(id) { this.el = document.getElementById(id); } // attr: get / set attributes to the element // attrs: an array of attribute to set // return: this for further processing svgQuery.elQuery.prototype.attr = function(attrs) { if(typeof attrs == "string") { return this.getAttr(attrs); } for (var i in attrs) { this.el.setAttribute(i, attrs[i]); } return this; } svgQuery.elQuery.prototype.getAttr = function(attr) { return this.el.getAttribute(attr); } // style: get / set style to the element // styles: an array of styles to set // return: this for further processing svgQuery.elQuery.prototype.style = function(styles) { if(typeof styles == "string") { return this.getAttr(styles); } for (var i in styles) { this.el.style[i] = styles[i]; } return this; } svgQuery.elQuery.prototype.getStyle = function(style) { return this.el.style[style]; } // data: get / set data // data: { "key": value, "key": value, ... } svgQuery.elQuery.prototype.data = function(data) { if(typeof data == "string") { return this.getData(data); } for (var i in data) { this.el[i] = data[i]; } } svgQuery.elQuery.prototype.getData = function(data) { return this.el[data]; } // on: register event handler // event: a kind of event // fun: callback function // return: this for further processing svgQuery.elQuery.prototype.on = function(event, fun) { if(typeof this.el['on' + event] !== 'undefined') { this.el['on' + event] = fun; } else { // console.log("Warning: unknown event: " + event + " specified"); } return this; } var main = function() { var svgId = "d20140106svg"; // svg attributes var svgAttr = { "width": 200, "height": 100, }; // svg style // "style": "border-color: black; border-style: solid; border-width: 1", var svgStyle = { "border-color": "gray", "border-style": "solid", "border-width": 1, } // circle data var c1 = { "cx": "50", "cy": "50", "r": 15 }; // circle attributes var c1Attr = { "fill": "red", "stroke": "blue", "stroke-width": "2" }; // onmousedown var down = function(e) { var root = svgQuery(svgId); var el = svgQuery(svgId).element(this.id); var mx = e.clientX; var my = e.clientY; var cx = Number(el.attr("cx")); var cy = Number(el.attr("cy")); var dx = mx - cx; var dy = my - cy; el.data( { "dx": dx, "dy": dy } ); root.data( { "el": el } ); root.on('mousemove', drag); } var touchstart = function() { var e = event.touches[0]; var root = svgQuery(svgId); var el = svgQuery(svgId).element(this.id); var mx = e.clientX; var my = e.clientY; var cx = Number(el.attr("cx")); var cy = Number(el.attr("cy")); var dx = mx - cx; var dy = my - cy; el.data( { "dx": dx, "dy": dy } ); root.data( { "el": el } ); root.on('touchmove', touchmove); } var drag = function(e) { var root = svgQuery(svgId); var el = root.data("el"); var mx = e.clientX; var my = e.clientY; var dx = el.data("dx"); var dy = el.data("dy"); var x = mx - dx; var y = my - dy; el.attr({ "cx": x, "cy": y }); } var touchmove = function() { var e = event.touches[0]; var root = svgQuery(svgId); var el = root.data("el"); var mx = e.clientX; var my = e.clientY; var dx = el.data("dx"); var dy = el.data("dy"); var x = mx - dx; var y = my - dy; el.attr({ "cx": x, "cy": y }); } var up = function(e) { var root = svgQuery(svgId); root.on('mousemove', null); } var touchend = function() { var root = svgQuery(svgId); root.on('touchmove', null); } var select = function(e) { return false; } svgQuery(svgId).attr(svgAttr).style(svgStyle).on('selectstart', select). circle(c1.cx, c1.cy, c1.r).attr(c1Attr). on('mousedown', down).on('mouseup', up). on('touchstart', touchstart).on('touchend', touchend); } main(); } // Block