This repository has been archived by the owner. It is now read-only.
Permalink
Please sign in to comment.
Browse files
Client creates favicon on 404, express no longer creates favicons.
- Loading branch information...
Showing
with
158 additions
and 108 deletions.
- +14 −2 client/client.coffee
- +6 −3 client/client.js
- +57 −0 client/plugins/favicon.coffee
- +62 −0 client/plugins/favicon.js
- +0 −91 server/express/lib/favicon.coffee
- +18 −10 server/express/lib/server.coffee
- +0 −1 server/express/package.json
- +1 −1 server/express/views/static.html
@@ -0,0 +1,57 @@ | ||
+# ** favicon.coffee ** | ||
+# This is a brute force port of Ward's favicon.rb, | ||
+# and could do with some refactoring by someone | ||
+# who knows the canvas api, but it would be better | ||
+# to move favicon generation to the client side. This | ||
+# could serve as a basis for that with enough refactoring. | ||
+ | ||
+# Utility functions | ||
+hsltorgb = (h, s, l) -> | ||
+ h = (h % 360) / 360 | ||
+ m2 = l * (s + 1) | ||
+ m1 = ((l * 2) - m2) | ||
+ hue = (num) -> | ||
+ if num < 0 | ||
+ num += 1 | ||
+ else if num > 1 | ||
+ num -= 1 | ||
+ if (num * 6) < 1 | ||
+ m1 + (m2 - m1) * num * 6 | ||
+ else if (num * 2) < 1 | ||
+ m2 | ||
+ else if (num * 3) < 2 | ||
+ m1 + (m2 - m1) * (2/3 - num) * 6 | ||
+ else | ||
+ m1 | ||
+ [(hue(h+1/3)*255), (hue(h) * 255), (hue(h - 1/3) * 255)] | ||
+ | ||
+ | ||
+ | ||
+window.plugins.favicon = | ||
+ create: -> | ||
+ $('body').append( | ||
+ $('<canvas />').attr('width', 32).attr('height', 32).attr('id', 'favmaker').attr('display', 'none').hide() | ||
+ ) | ||
+ canvas = document.getElementById('favmaker') | ||
+ ctx = canvas.getContext('2d') | ||
+ light = hsltorgb(Math.random() * 360, .78, .50) | ||
+ dark = hsltorgb(Math.random() * 360, .78, .25) | ||
+ angle = 2 * (Math.random() - 0.5) | ||
+ sin = Math.sin angle | ||
+ cos = Math.cos angle | ||
+ scale = (Math.abs(sin) + Math.abs(cos)) | ||
+ colprep = (col, p) -> | ||
+ Math.floor(light[col]*p + dark[col]*(1-p))%255 | ||
+ for x in [0..31] | ||
+ for y in [0..31] | ||
+ p = if sin >= 0 then sin * x + cos * y else -sin * (31 - x) + cos * y | ||
+ p = p / 31 / scale | ||
+ ctx.fillStyle = "rgba(#{colprep(0, p)}, #{colprep(1, p)}, #{colprep(2, p)}, 1)" | ||
+ ctx.fillRect(x, y, 1, 1) | ||
+ fav = canvas.toDataURL() | ||
+ $('#favicon').attr('href', fav) | ||
+ $('.favicon').attr('src', fav) | ||
+ $.post('/favicon.png', {image: fav}, (data) -> | ||
+ console.log data | ||
+ ) | ||
+ |
@@ -0,0 +1,62 @@ | ||
+(function() { | ||
+ var hsltorgb; | ||
+ | ||
+ hsltorgb = function(h, s, l) { | ||
+ var hue, m1, m2; | ||
+ h = (h % 360) / 360; | ||
+ m2 = l * (s + 1); | ||
+ m1 = (l * 2) - m2; | ||
+ hue = function(num) { | ||
+ if (num < 0) { | ||
+ num += 1; | ||
+ } else if (num > 1) { | ||
+ num -= 1; | ||
+ } | ||
+ if ((num * 6) < 1) { | ||
+ return m1 + (m2 - m1) * num * 6; | ||
+ } else if ((num * 2) < 1) { | ||
+ return m2; | ||
+ } else if ((num * 3) < 2) { | ||
+ return m1 + (m2 - m1) * (2 / 3 - num) * 6; | ||
+ } else { | ||
+ return m1; | ||
+ } | ||
+ }; | ||
+ return [hue(h + 1 / 3) * 255, hue(h) * 255, hue(h - 1 / 3) * 255]; | ||
+ }; | ||
+ | ||
+ window.plugins.favicon = { | ||
+ create: function() { | ||
+ var angle, canvas, colprep, cos, ctx, dark, fav, light, p, scale, sin, x, y; | ||
+ $('body').append($('<canvas />').attr('width', 32).attr('height', 32).attr('id', 'favmaker').attr('display', 'none').hide()); | ||
+ canvas = document.getElementById('favmaker'); | ||
+ ctx = canvas.getContext('2d'); | ||
+ light = hsltorgb(Math.random() * 360, .78, .50); | ||
+ dark = hsltorgb(Math.random() * 360, .78, .25); | ||
+ angle = 2 * (Math.random() - 0.5); | ||
+ sin = Math.sin(angle); | ||
+ cos = Math.cos(angle); | ||
+ scale = Math.abs(sin) + Math.abs(cos); | ||
+ colprep = function(col, p) { | ||
+ return Math.floor(light[col] * p + dark[col] * (1 - p)) % 255; | ||
+ }; | ||
+ for (x = 0; x <= 31; x++) { | ||
+ for (y = 0; y <= 31; y++) { | ||
+ p = sin >= 0 ? sin * x + cos * y : -sin * (31 - x) + cos * y; | ||
+ p = p / 31 / scale; | ||
+ ctx.fillStyle = "rgba(" + (colprep(0, p)) + ", " + (colprep(1, p)) + ", " + (colprep(2, p)) + ", 1)"; | ||
+ ctx.fillRect(x, y, 1, 1); | ||
+ } | ||
+ } | ||
+ fav = canvas.toDataURL(); | ||
+ $('#favicon').attr('href', fav); | ||
+ $('.favicon').attr('src', fav); | ||
+ return $.post('/favicon.png', { | ||
+ image: fav | ||
+ }, function(data) { | ||
+ return console.log(data); | ||
+ }); | ||
+ } | ||
+ }; | ||
+ | ||
+}).call(this); |
@@ -1,91 +0,0 @@ | ||
-# ** favicon.coffee ** | ||
-# This is a brute force port of Ward's favicon.rb, | ||
-# and could do with some refactoring by someone | ||
-# who knows the canvas api, but it would be better | ||
-# to move favicon generation to the client side. This | ||
-# could serve as a basis for that with enough refactoring. | ||
- | ||
-Canvas = require('canvas') | ||
-fs = require('fs') | ||
-mkdirp = require('mkdirp') | ||
-path = require('path') | ||
- | ||
-itself = {} | ||
- | ||
-# Utility functions | ||
-hsltorgb = (h, s, l) -> | ||
- h = (h % 360) / 360 | ||
- m2 = l * (s + 1) | ||
- m1 = ((l * 2) - m2) | ||
- hue = (num) -> | ||
- if num < 0 | ||
- num += 1 | ||
- else if num > 1 | ||
- num -= 1 | ||
- if (num * 6) < 1 | ||
- m1 + (m2 - m1) * num * 6 | ||
- else if (num * 2) < 1 | ||
- m2 | ||
- else if (num * 3) < 2 | ||
- m1 + (m2 - m1) * (2/3 - num) * 6 | ||
- else | ||
- m1 | ||
- [(hue(h+1/3)*255), (hue(h) * 255), (hue(h - 1/3) * 255)] | ||
- | ||
- | ||
-create = (loc, cb) -> | ||
- canvas = new Canvas(32, 32) | ||
- ctx = canvas.getContext('2d') | ||
- out = fs.createWriteStream(loc) | ||
- stream = canvas.createPNGStream() | ||
- stream.on('data', (chunk) -> | ||
- out.write(chunk) | ||
- ) | ||
- stream.on('end', () -> | ||
- console.log('saved png') | ||
- setTimeout( -> | ||
- cb(loc) | ||
- , 100 | ||
- ) | ||
- ) | ||
- light = hsltorgb(Math.random() * 360, .78, .50) | ||
- dark = hsltorgb(Math.random() * 360, .78, .25) | ||
- angle = 2 * (Math.random() - 0.5) | ||
- sin = Math.sin angle | ||
- cos = Math.cos angle | ||
- scale = (Math.abs(sin) + Math.abs(cos)) | ||
- colprep = (col, p) -> | ||
- Math.floor(light[col]*p + dark[col]*(1-p))%255 | ||
- for x in [0..31] | ||
- for y in [0..31] | ||
- p = if sin >= 0 then sin * x + cos * y else -sin * (31 - x) + cos * y | ||
- p = p / 31 / scale | ||
- ctx.fillStyle = "rgba(#{colprep(0, p)}, #{colprep(1, p)}, #{colprep(2, p)}, 1)" | ||
- ctx.fillRect(x, y, 1, 1) | ||
- | ||
-# Exported functions | ||
- | ||
-itself.get = (loc, cb) -> | ||
- path.exists(loc, (exists) -> | ||
- if exists | ||
- cb(loc) | ||
- else | ||
- locroot = path.dirname(loc) | ||
- path.exists(locroot, (exists) -> | ||
- if exists | ||
- create(loc, cb) | ||
- else | ||
- mkdirp(locroot, 0777, -> | ||
- create(loc, cb) | ||
- ) | ||
- ) | ||
- ) | ||
- | ||
-if path.basename(process.argv[1]) is 'favicon.coffee' | ||
- # If run from the command line, create a favicon.png in provided loc or cwd and exit | ||
- create(process.argv[2] or "#{__dirname}/favicon.png", (loc) -> | ||
- require('child_process').spawn('chromium', ["#{loc}"]) | ||
- ) | ||
- | ||
- | ||
-module.exports = itself |
0 comments on commit
e706fd6