git ssb

0+

cel / ssb-activity



Commit f97d1056daa9154ab13a44d173700ec4f3e94d1a

Show highlighted id

cel committed on 11/17/2019, 11:12:20 PM
Parent: 6349a39796d35a2159558bb5333e2a3762fd9463

Files changed

common.jschanged
index.htmlchanged
run.jschanged
common.jsView
@@ -1,7 +1,7 @@
11 var common = typeof window !== 'undefined' ? window : exports
22
3-common.colors20 = [
3 +var colors20 = [
44 [255, 106, 152], [255, 98, 188], [250, 98, 219], [231, 107, 243],
55 [199, 124, 255], [149, 144, 255], [53, 162, 255], [0, 176, 246],
66 [0, 186, 224], [0, 191, 196], [0, 193, 163], [0, 191, 125],
77 [0, 187, 78], [57, 182, 0], [124, 174, 0], [163, 165, 0],
@@ -19,4 +19,27 @@
1919 a[2] * m + b[2] * n,
2020 a[3] * m + b[3] * n,
2121 ]
2222 }
23 +
24 +common.computeIdColors = function (ids) {
25 + var idColors = {}
26 + var colorsUsed = {}
27 + for (var i = 0; i < ids.length; i++) {
28 + var id = ids[i]
29 + var x = i / ids.length
30 + var colorI = Math.floor(x*20)
31 + var inbetweenAmount = x*20 - colorI
32 + var color = common.interpolate(colors20[colorI], colors20[colorI+1], inbetweenAmount)
33 + color[0] = Math.floor(color[0])
34 + color[1] = Math.floor(color[1])
35 + color[2] = Math.floor(color[2])
36 + color[3] = 255
37 + // Ensure uniqueness
38 + while (colorsUsed[color]) {
39 + color[3]--
40 + }
41 + colorsUsed[color] = true
42 + idColors[id] = color
43 + }
44 + return idColors
45 +}
index.htmlView
@@ -5,18 +5,27 @@
55 <title>ssb-activity</title>
66 <script src=common.js></script>
77 </head>
88 <body>
9 +
910 <div style="text-align:center">
1011 <h2>Daily active users</h2>
1112 <img id=graph src=day.png alt="Graph of daily active feeds">
1213 </div>
1314
15 +<div id="user-info" style="visibility:hidden">
16 + <div>User: <a id="user-link"><code id="user-id"> </code></a></div>
17 +</div>
18 +
1419 <script>
15-
20 +var feedBase = 'https://viewer.scuttlebot.io/'
21 +var ids, idColors, idsByColor = {}
1622 var img = document.getElementById('graph')
1723 var imgData, origImgData
1824 var width, height
25 +var userInfo = document.getElementById('user-info')
26 +var userLink = document.getElementById('user-link')
27 +var userIdText = document.getElementById('user-id').firstChild
1928
2029 var colors = {
2130 yellow: [255, 255, 0, 255],
2231 red: [255, 0, 0, 255],
@@ -25,8 +34,39 @@
2534 bg: [255, 255, 255, 0],
2635 highlight: [0, 0, 0, 255]
2736 }
2837
38 +function getJSON(url, cb) {
39 + var req = new XMLHttpRequest()
40 + req.open('GET', url, true)
41 + req.onload = function () {
42 + if (req.readyState !== 4) return
43 + if (req.status >= 200 && req.status < 400)
44 + cb(null, JSON.parse(req.responseText))
45 + else
46 + return cb(req)
47 + }
48 + req.onerror = cb
49 + req.send(null)
50 +}
51 +
52 +function printError(err) {
53 + var pre = document.createElement('pre')
54 + document.body.appendChild(pre)
55 + pre.appendChild(document.createTextNode(err.stack))
56 +}
57 +
58 +getJSON('ids.json', function (err, _ids) {
59 + if (err) return printError(err)
60 + ids = _ids
61 + idColors = common.computeIdColors(ids)
62 + for (var i = 0; i < ids.length; i++) {
63 + var id = ids[i]
64 + var color = idColors[id]
65 + idsByColor[color] = id
66 + }
67 +})
68 +
2969 var canvas = document.createElement('canvas')
3070 var ctx
3171 function onImageLoad() {
3272 width = canvas.width = img.width
@@ -50,11 +90,14 @@
5090 }
5191
5292 function colorEquals(a, b) {
5393 return a && b
54- && a[0] === b[0]
55- && a[1] === b[1]
56- && a[2] === b[2]
94 + && a[3] === b[3]
95 + && (a[3] === 0 || (
96 + a[0] === b[0]
97 + && a[1] === b[1]
98 + && a[2] === b[2]
99 + ))
57100 }
58101
59102 function getPixel(x, y) {
60103 var i = (y * width + x) << 2
@@ -75,10 +118,8 @@
75118 data[i+2] = color[2]
76119 data[i+3] = 255
77120 }
78121
79-var prevColor
80-
81122 function replaceColor(oldColor, newColor) {
82123 for (var x = 0; x < width; x++) {
83124 for (var y = height-1; y > 0; y--) {
84125 var c = getPixel(x, y)
@@ -90,24 +131,77 @@
90131 }
91132 }
92133 }
93134
94-canvas.addEventListener('mousemove', function (e) {
95- var point = getPoint(e)
96- var color = getPixel(point[0], point[1])
97- if (colorEquals(color, colors.black)
98- || colorEquals(color, colors.bg)
99- || colorEquals(color, prevColor)) return
100- if (prevColor) replaceColor(prevColor, prevColor)
135 +function pointEquals(a, b) {
136 + return a ? b && a[0] === b[0] && a[1] === b[1] : !b
137 +}
138 +
139 +var hoverColor, hoverPoint
140 +var clickedColor, clickedPoint
141 +
142 +function highlight(color, point) {
143 + if (colorEquals(color, hoverColor)) return
144 + if (!color) {
145 + if (hoverColor) replaceColor(hoverColor, hoverColor)
146 + ctx.putImageData(imgData, 0, 0)
147 + hoverColor = null
148 + userInfo.style.visibility = 'hidden'
149 + return
150 + }
151 + if (colorEquals(color, colors.black)) return
152 + if (colorEquals(color, colors.bg)) return
153 + if (hoverColor) replaceColor(hoverColor, hoverColor)
101154 replaceColor(color, colors.highlight)
102- prevColor = color
155 + hoverColor = color
103156 ctx.putImageData(imgData, 0, 0)
157 + var id = idsByColor[color]
158 + if (!id) {
159 + userInfo.style.visibility = 'hidden'
160 + return
161 + }
162 + userIdText.nodeValue = id
163 + userLink.href = feedBase + encodeURIComponent(id)
164 + userInfo.style.visibility = 'visible'
165 +}
166 +
167 +function clearHoverPoint() {
168 + highlight(clickedColor, clickedPoint)
169 +}
170 +
171 +function setHoverPoint(point) {
172 + if (pointEquals(point, hoverPoint)) return
173 + hoverPoint = point
174 + var color = getPixel(point[0], point[1])
175 + highlight(color, point)
176 +}
177 +
178 +function setClickedPoint(point) {
179 + if (pointEquals(point, clickedPoint)) return
180 + var color
181 + if (point) {
182 + color = getPixel(point[0], point[1])
183 + if (colorEquals(color, colors.bg)) {
184 + color = null
185 + point = null
186 + }
187 + }
188 + clickedPoint = point
189 + clickedColor = color
190 + highlight(color, point)
191 +}
192 +
193 +canvas.addEventListener('mousemove', function (e) {
194 + setHoverPoint(getPoint(e))
104195 }, false)
105196
197 +
198 +canvas.addEventListener('mousedown', function (e) {
199 + setClickedPoint(getPoint(e))
200 +}, false)
201 +
106202 canvas.addEventListener('mouseout', function (e) {
107- if (prevColor) replaceColor(prevColor, prevColor)
108- ctx.putImageData(imgData, 0, 0)
109- prevColor = null
203 + clearHoverPoint()
110204 }, false)
111205
112206 </script>
113207 </body>
run.jsView
@@ -44,29 +44,13 @@
4444 height: height,
4545 colorType: 6
4646 })
4747
48-var userColors = {}
48 +var userColors = common.computeIdColors(ids)
4949 var userNumbers = {}
50-var colorsUsed = {}
51-
5250 for (var i = 0; i < ids.length; i++) {
5351 var id = ids[i]
5452 userNumbers[id] = i
55- var x = i / ids.length
56- var colorI = Math.floor(x*20)
57- var inbetweenAmount = x*20 - colorI
58- var color = common.interpolate(common.colors20[colorI], common.colors20[colorI+1], inbetweenAmount)
59- color[0] = Math.floor(color[0])
60- color[1] = Math.floor(color[1])
61- color[2] = Math.floor(color[2])
62- color[3] = 255
63- // Ensure uniqueness
64- while (colorsUsed[color]) {
65- color[3]--
66- }
67- colorsUsed[color] = true
68- userColors[id] = color
6953 }
7054
7155 function setPixel(data, x, y, color) {
7256 var i = (y * width + x) << 2

Built with git-ssb-web