@@ -19,6 +19,9 @@ withImage = (url, action) ->
19
19
20
20
favicon = $ (' link[rel="icon"]' )
21
21
22
+ if defaultUrl == null
23
+ defaultUrl = favicon .href
24
+
22
25
if urlCache[doc .slug ]
23
26
favicon .href = urlCache[doc .slug ]
24
27
currentSlug = doc .slug
@@ -27,24 +30,31 @@ withImage = (url, action) ->
27
30
styles = window .getComputedStyle ($ (" ._icon-#{ doc .slug } " ), ' :before' )
28
31
29
32
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 ) ))
33
36
34
- withImage (bgUrl, (img ) ->
35
- canvas = document .createElement (' canvas' )
37
+ withImage (bgUrl, (docImg ) ->
38
+ withImage (defaultUrl, (defaultImg ) ->
39
+ size = defaultImg .width
36
40
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' )
40
43
41
- if defaultUrl == null
42
- defaultUrl = favicon .href
44
+ canvas .width = size
45
+ canvas .height = size
46
+ ctx .drawImage (defaultImg, 0 , 0 )
43
47
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)
46
52
47
- currentSlug = doc .slug
53
+ urlCache[doc .slug ] = canvas .toDataURL ()
54
+ favicon .href = urlCache[doc .slug ]
55
+
56
+ currentSlug = doc .slug
57
+ )
48
58
)
49
59
50
60
@ resetFavicon = () ->
0 commit comments