UNPKG

3.06 kBJavaScriptView Raw
1
2/* litejs.com/MIT-LICENSE.txt */
3
4
5/* global El */
6!function(SVGElement, document, bindings) {
7
8 //http://stackoverflow.com/questions/5736398/how-to-calculate-the-svg-path-for-an-arc-of-a-circle
9 function polarToCartesian(centerX, centerY, radius, angle/*InDegrees*/) {
10 angle/*InRadians*/ = (angle - 90) * Math.PI / 180.0
11 return {
12 x: centerX + (radius * Math.cos(angle)),
13 y: centerY + (radius * Math.sin(angle))
14 }
15 }
16 function describeArc(x, y, radius, startAngle, endAngle) {
17 var start = polarToCartesian(x, y, radius, endAngle)
18 , end = polarToCartesian(x, y, radius, startAngle)
19 , arcSweep = endAngle - startAngle <= 180 ? 0 : 1
20 , d = [
21 "M", start.x, start.y,
22 "A", radius, radius, 0, arcSweep, 0, end.x, end.y
23 ].join(" ")
24 return d
25 }
26 bindings.arc = function(el, startAngle, endAngle, radius, x, y) {
27 var center = (y && x && radius) || el.viewportElement.viewBox.baseVal.width >> 1
28 // var length = path.getTotalLength();
29 el.setAttribute("d", describeArc(
30 x || center,
31 y || center,
32 radius || center,
33 3.6 * startAngle,
34 3.6 * endAngle
35 ))
36 }
37
38 function drawLine(node) {
39 var length = node.getTotalLength()
40 node.style.transition = node.style.WebkitTransition = "none"
41 node.style.strokeDasharray = length + " " + length
42 node.style.strokeDashoffset = length
43 // Trigger a layout so styles are calculated & the browser
44 // picks up the starting position before animating
45 node.getBoundingClientRect()
46 node.style.transition = node.style.WebkitTransition = "stroke-dashoffset 5s ease .5s"
47 node.style.strokeDashoffset = "0"
48 }
49 var arch1 = "a? ? 0 1 0 ? -?".split("?")
50 , arch2 = "a? ? 0 1 0 ? ?".split("?")
51 bindings.svgLine = function(el, points, opts) {
52 opts = opts || {}
53 var i = 0
54 , dataPoints = []
55 , radius = opts.radius || 0
56 , d = ["M" + (100 - radius), points[0]]
57 if (radius) {
58 d.push(arch1.join(radius), arch2.join(radius))
59 }
60
61 for (; ++i < points.length; ) {
62 if (opts.smooth) {
63 d.push("C" + (i * 100 + 50 - radius), points[i-1] + "," + (i * 100 + 50 + radius), points[i] + "," + (i * 100 + 100 - radius), points[i])
64 } else {
65 d.push("L" + (i * 100 + 100 - radius), points[i])
66 }
67 if (radius) {
68 d.push(arch1.join(radius), arch2.join(radius))
69 }
70 //dataPoints.push(El("circle[r=3][cx=" + (i * 100 + 100) + "][cy=" + points[i] + "]"))
71 }
72 el.setAttribute("d", d.join(" "))
73 drawLine(el)
74 el.parentNode.append(dataPoints)
75 }
76 var svgToLastActive
77 bindings.initChart = function(el) {
78 El.on(el, "mouseout", function(e) {
79 if (svgToLastActive && e.target == el) {
80 El.cls(svgToLastActive, "is-active", svgToLastActive = null)
81 }
82 })
83 }
84 // riseOnHover
85 bindings.svgToLast = function(el) {
86 El.on(el, "mouseover", function() {
87 if (!svgToLastActive || el != el.parentNode.lastChild) {
88 El.cls(svgToLastActive, "is-active", 0)
89 El.append(el.parentNode, el)
90 El.cls(el, "is-active", svgToLastActive = el)
91 }
92 })
93 }
94 bindings.svgToLast.once =
95 bindings.initChart.once = 1
96}(this.SVGElement, document, El.$b) // jshint ignore:line
97