forked from badges/stability-badges
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbadge.js
112 lines (94 loc) · 2.1 KB
/
badge.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
module.exports = badge
function badge(label, category, options) {
options = options || {}
options.labelColor = options.labelColor || '#4b4b4b'
options.categoryColor = options.categoryColor || '#74c614'
options.width = options.width || 100
options.labelWidth = options.labelWidth || 35
var svg = ''
var height = 19
var shadow = 2
var body = height - shadow
// shadow
svg += el('rect', {
x: 0
, y: 0
, width: options.width
, height: height
, style: {
fill: options.labelColor
, opacity: 0.3
}
})
svg += el('rect', {
x: 0
, y: 0
, width: options.width
, height: body
, style: {
fill: options.labelColor
}
})
svg += el('rect', {
x: options.labelWidth
, y: 0
, width: options.width - options.labelWidth
, height: body
, style: {
fill: options.categoryColor
}
})
svg += dropshadowText(5, body / 2, label)
svg += dropshadowText(options.labelWidth + 5, body / 2, category)
return svg
}
function el(el) {
var args = Array.prototype.slice.call(arguments, 1)
var str = '<' + el
args.filter(function(arg) {
return typeof arg !== 'string'
}).forEach(function(opts) {
Object.keys(opts).forEach(function(key) {
var value = opts[key]
if (Array.isArray(value)) value = value.join(' ')
if (typeof value === 'object') {
value = Object.keys(value).reduce(function(str, key) {
str += key
str += ':'
str += value[key]
str += ';'
return str
}, '')
}
str += ' '
str += key
str += '="'
str += String(value).replace(/\"/g, '\\"')
str += '"'
})
})
str += '>'
args.filter(function(arg) {
return typeof arg === 'string'
}).forEach(function(inner) {
str += inner
})
str += '</' + el + '>'
return str
}
function dropshadowText(x, y, label) {
return el('text', label, {
x: x
, y: y + 1
, style: {
'fill': '#000'
, 'opacity': 0.75
}
}) + el('text', label, {
x: x
, y: y
, style: {
'fill': '#fff'
}
})
}