summaryrefslogtreecommitdiffstats
path: root/src/svg.js
blob: e7f43637187b0ef8551373c7802cee5c293354e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Use the `svg()` function to create a SVG document within a given html element. The first argument can either be an id of the element or the selected element itself.
//
//     var draw = svg('paper').size(300, 300)
//     var rect = draw.rect(100, 100).attr({ fill: '#f06' })



// Shortcut for creating a svg document
this.svg = function(element) {
  if (SVG.supported)
    return new SVG.Doc(element)
}

// The main wrapping element
this.SVG = {
  /* default namespaces */
  ns:    'http://www.w3.org/2000/svg'
, xlink: 'http://www.w3.org/1999/xlink'
  
  /* element id sequence */
, did: 1000

  // Get next named element id
, eid: function(name) {
    return 'Svgjs' + name.charAt(0).toUpperCase() + name.slice(1) + 'Element' + (SVG.did++)
  }
  // Method for element creation
, create: function(name) {
    /* create element */
    var element = document.createElementNS(this.ns, name)
    
    /* apply unique id */
    element.setAttribute('id', this.eid(name))
    
    return element
  }
  // Method for extending objects
, extend: function() {
    var modules, methods, key, i
    
    /* get list of modules */
    modules = Array.prototype.slice.call(arguments)
    
    /* get object with extensions */
    methods = modules.pop()
    
    for (i = modules.length - 1; i >= 0; i--)
      if (modules[i])
        for (key in methods)
          modules[i].prototype[key] = methods[key]
  }
  
}

// svg support test
SVG.supported = (function() {
  return !! document.createElementNS &&
         !! document.createElementNS(SVG.ns,'svg').createSVGRect
})()

if (!SVG.supported) return false;