Skip to content

Commit 14e49f7

Browse files
committed
Use GitHub's parent-child style
1 parent c36478c commit 14e49f7

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

assets/javascripts/lib/favicon.coffee

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ withImage = (url, action) ->
1919

2020
favicon = $('link[rel="icon"]')
2121

22+
if defaultUrl == null
23+
defaultUrl = favicon.href
24+
2225
if urlCache[doc.slug]
2326
favicon.href = urlCache[doc.slug]
2427
currentSlug = doc.slug
@@ -27,24 +30,31 @@ withImage = (url, action) ->
2730
styles = window.getComputedStyle($("._icon-#{doc.slug}"), ':before')
2831

2932
bgUrl = styles['background-image'].slice(5, -2)
30-
bgSize = if bgUrl.includes('@2x') then 32 else 16
31-
bgX = parseInt(styles['background-position-x'].slice(0, -2))
32-
bgY = parseInt(styles['background-position-y'].slice(0, -2))
33+
sourceSize = if bgUrl.includes('@2x') then 32 else 16
34+
sourceX = Math.abs(parseInt(styles['background-position-x'].slice(0, -2)))
35+
sourceY = Math.abs(parseInt(styles['background-position-y'].slice(0, -2)))
3336

34-
withImage(bgUrl, (img) ->
35-
canvas = document.createElement('canvas')
37+
withImage(bgUrl, (docImg) ->
38+
withImage(defaultUrl, (defaultImg) ->
39+
size = defaultImg.width
3640

37-
canvas.width = bgSize
38-
canvas.height = bgSize
39-
canvas.getContext('2d').drawImage(img, bgX, bgY)
41+
canvas = document.createElement('canvas')
42+
ctx = canvas.getContext('2d')
4043

41-
if defaultUrl == null
42-
defaultUrl = favicon.href
44+
canvas.width = size
45+
canvas.height = size
46+
ctx.drawImage(defaultImg, 0, 0)
4347

44-
urlCache[doc.slug] = canvas.toDataURL()
45-
favicon.href = urlCache[doc.slug]
48+
docIconPercentage = 65
49+
destinationCoords = size / 100 * (100 - docIconPercentage)
50+
destinationSize = size / 100 * docIconPercentage
51+
ctx.drawImage(docImg, sourceX, sourceY, sourceSize, sourceSize, destinationCoords, destinationCoords, destinationSize, destinationSize)
4652

47-
currentSlug = doc.slug
53+
urlCache[doc.slug] = canvas.toDataURL()
54+
favicon.href = urlCache[doc.slug]
55+
56+
currentSlug = doc.slug
57+
)
4858
)
4959

5060
@resetFavicon = () ->

0 commit comments

Comments
 (0)