git ssb

0+

cel / ssb-activity



Commit e6771acdf6578c1d07533da3811ea9d6fc8575a7

Add page with user color hover highlighting

cel committed on 11/17/2019, 10:36:39 PM
Parent: c6463c1960dd5fa8a7d2151ff4cd54afe8a04dbc

Files changed

run.jschanged
common.jsadded
index.htmladded
run.jsView
@@ -3,8 +3,9 @@
33 var pull = require('pull-stream')
44 var File = require('pull-file')
55 var Split = require('pull-split')
66 var PNG = require('pngjs').PNG
7 +var common = require('./common')
78
89 var start = new Date('2015-08-05').getTime()
910 var startDay = Math.floor(start / 86400000)
1011 var startWeek = Math.floor(start / 86400000 / 7)
@@ -43,29 +44,8 @@
4344 height: height,
4445 colorType: 6
4546 })
4647
47-var colors20 = [
48- [255, 106, 152], [255, 98, 188], [250, 98, 219], [231, 107, 243],
49- [199, 124, 255], [149, 144, 255], [53, 162, 255], [0, 176, 246],
50- [0, 186, 224], [0, 191, 196], [0, 193, 163], [0, 191, 125],
51- [0, 187, 78], [57, 182, 0], [124, 174, 0], [163, 165, 0],
52- [192, 155, 0], [216, 144, 0], [234, 131, 49], [248, 118, 109],
53- [255, 106, 152]
54-]
55-
56-function interpolate(a, b, n) {
57- if (!a) a = [0, 0, 0, 0]
58- if (!b) b = [0, 0, 0, 0]
59- var m = 1 - n
60- return [
61- a[0] * m + b[0] * n,
62- a[1] * m + b[1] * n,
63- a[2] * m + b[2] * n,
64- a[3] * m + b[3] * n,
65- ]
66-}
67-
6848 var userColors = {}
6949 var userNumbers = {}
7050
7151 for (var i = 0; i < ids.length; i++) {
@@ -73,9 +53,9 @@
7353 userNumbers[id] = i
7454 var x = i / ids.length
7555 var colorI = Math.floor(x*20)
7656 var inbetweenAmount = x*20 - colorI
77- var c = interpolate(colors20[colorI], colors20[colorI+1], inbetweenAmount)
57 + var c = common.interpolate(common.colors20[colorI], common.colors20[colorI+1], inbetweenAmount)
7858 c[3] = 255
7959 userColors[id] = c
8060 }
8161
common.jsView
@@ -1,0 +1,22 @@
1 +var common = typeof window !== 'undefined' ? window : exports
2 +
3 +common.colors20 = [
4 + [255, 106, 152], [255, 98, 188], [250, 98, 219], [231, 107, 243],
5 + [199, 124, 255], [149, 144, 255], [53, 162, 255], [0, 176, 246],
6 + [0, 186, 224], [0, 191, 196], [0, 193, 163], [0, 191, 125],
7 + [0, 187, 78], [57, 182, 0], [124, 174, 0], [163, 165, 0],
8 + [192, 155, 0], [216, 144, 0], [234, 131, 49], [248, 118, 109],
9 + [255, 106, 152]
10 +]
11 +
12 +common.interpolate = function (a, b, n) {
13 + if (!a) a = [0, 0, 0, 0]
14 + if (!b) b = [0, 0, 0, 0]
15 + var m = 1 - n
16 + return [
17 + a[0] * m + b[0] * n,
18 + a[1] * m + b[1] * n,
19 + a[2] * m + b[2] * n,
20 + a[3] * m + b[3] * n,
21 + ]
22 +}
index.htmlView
@@ -1,0 +1,114 @@
1 +<!doctype html>
2 +<html>
3 +<head>
4 +<meta charset=utf-8>
5 +<title>ssb-activity</title>
6 +<script src=common.js></script>
7 +</head>
8 +<body>
9 +<div style="text-align:center">
10 + <h2>Daily active users</h2>
11 + <img id=graph src=day.png alt="Graph of daily active feeds">
12 +</div>
13 +
14 +<script>
15 +
16 +var img = document.getElementById('graph')
17 +var imgData, origImgData
18 +var width, height
19 +
20 +var colors = {
21 + yellow: [255, 255, 0, 255],
22 + red: [255, 0, 0, 255],
23 + black: [0, 0, 0, 255],
24 + grey: [200, 200, 200, 255],
25 + bg: [255, 255, 255, 0],
26 + highlight: [0, 0, 0, 255]
27 +}
28 +
29 +var canvas = document.createElement('canvas')
30 +var ctx
31 +function onImageLoad() {
32 + width = canvas.width = img.width
33 + height = canvas.height = img.height
34 + ctx = canvas.getContext('2d')
35 + ctx.drawImage(img, 0, 0)
36 + imgData = ctx.getImageData(0, 0, width, height)
37 + origImgData = ctx.getImageData(0, 0, width, height)
38 + img.parentNode.insertBefore(canvas, img)
39 + img.parentNode.removeChild(img)
40 + canvas.style.maxWidth = '100%'
41 +}
42 +if (img.complete) onImageLoad()
43 +else img.onload = onImageLoad
44 +
45 +function getPoint(e) {
46 + return [
47 + Math.floor(e.offsetX * width / canvas.offsetWidth),
48 + Math.floor(e.offsetY * height / canvas.offsetHeight)
49 + ]
50 +}
51 +
52 +function colorEquals(a, b) {
53 + return a && b
54 + && a[0] === b[0]
55 + && a[1] === b[1]
56 + && a[2] === b[2]
57 +}
58 +
59 +function getPixel(x, y) {
60 + var i = (y * width + x) << 2
61 + var data = origImgData.data
62 + return [
63 + data[i],
64 + data[i+1],
65 + data[i+2],
66 + data[i+3]
67 + ]
68 +}
69 +
70 +function setPixel(x, y, color) {
71 + var i = (y * width + x) << 2
72 + var data = imgData.data
73 + data[i] = color[0]
74 + data[i+1] = color[1]
75 + data[i+2] = color[2]
76 + data[i+3] = 255
77 +}
78 +
79 +var prevColor
80 +
81 +function replaceColor(oldColor, newColor) {
82 + for (var x = 0; x < width; x++) {
83 + for (var y = height-1; y > 0; y--) {
84 + var c = getPixel(x, y)
85 + if (colorEquals(c, colors.bg)) break
86 + if (colorEquals(c, oldColor)) {
87 + setPixel(x, y, newColor)
88 + break
89 + }
90 + }
91 + }
92 +}
93 +
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)
101 + replaceColor(color, colors.highlight)
102 + prevColor = color
103 + ctx.putImageData(imgData, 0, 0)
104 +}, false)
105 +
106 +canvas.addEventListener('mouseout', function (e) {
107 + if (prevColor) replaceColor(prevColor, prevColor)
108 + ctx.putImageData(imgData, 0, 0)
109 + prevColor = null
110 +}, false)
111 +
112 +</script>
113 +</body>
114 +</html>

Built with git-ssb-web