Skip to content

Commit

Permalink
Dynamic coloured favicons
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpete committed Mar 2, 2016
1 parent fb2ec0e commit 66a8c85
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>rplayr · create playlists from any reddit url</title>
<link href='/app.css' rel='stylesheet'>
<link rel='icon' type='image/png' />
</head>
<body>
<div id='app'></div>
Expand Down
21 changes: 21 additions & 0 deletions src/pages/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { Component, PropTypes } from 'react'
import { Link } from 'react-router'
import randomcolor from 'randomcolor'

import 'normalize.css/normalize.css'
import '../styles/defaults.scss'
Expand All @@ -12,10 +13,30 @@ export default class App extends Component {
children: PropTypes.node
};
componentDidMount () {
const { params, location } = this.props
this.updateFavicon(params, location)
if (window.performance) {
trackTiming('react', 'firstrender', Math.round(window.performance.now()))
}
}
componentWillReceiveProps ({ params, location }) {
this.updateFavicon(params, location.pathname)
}
updateFavicon (params, pathname) {
const seed = params.post_id || params.subreddit || params.multi || pathname
const canvas = document.createElement('canvas')
canvas.width = 32
canvas.height = 32
if (canvas.getContext) {
const ctx = canvas.getContext('2d')
ctx.beginPath()
ctx.arc(16, 16, 16, 0, 360)
ctx.fillStyle = randomcolor({ seed, luminosity: 'light' })
ctx.fill()
const url = canvas.toDataURL('image/png')
document.querySelector('link[rel="icon"]').setAttribute('href', url)
}
}
render () {
return (
<div className={classNames.app}>
Expand Down

0 comments on commit 66a8c85

Please sign in to comment.