1
+ const GITHUB_URL = '/*\nResponsive CSS Sprite created using: ' +
2
+ 'http://responsive-css.us/\n' +
3
+ '*/\n\n' ;
4
+
1
5
function findNode ( root , w , h ) {
2
6
if ( root . used ) {
3
7
return findNode ( root . right , w , h ) || findNode ( root . down , w , h ) ;
@@ -95,9 +99,17 @@ export default class TexturePacker {
95
99
let canvas = this . canvas ;
96
100
let ctx = canvas . getContext ( '2d' ) ;
97
101
let pad = this . root . p ;
102
+ let prefix = this . prefix ;
98
103
let width = this . root . w + pad ;
99
104
let height = this . root . h + pad ;
100
105
106
+ let computedCSS = '' ;
107
+ let selectorsString = '' ;
108
+ let globalString = '\n{display:inline-block; overflow:hidden; ' +
109
+ 'background-repeat: no-repeat;\n' +
110
+ `background-image:url(${ this . path } );}\n\n` ;
111
+ let spriteString = '' ;
112
+
101
113
canvas . width = width ;
102
114
canvas . height = height ;
103
115
@@ -118,9 +130,21 @@ export default class TexturePacker {
118
130
ctx . stroke ( ) ;
119
131
120
132
ctx . drawImage ( texture . img , texture . fit . x + pad , texture . fit . y + pad ) ;
133
+
134
+ selectorsString += '.' + prefix + texture . name + ( i === this . textures . length - 1 ? ' ' : ', ' ) ;
135
+
136
+ spriteString += `.${ prefix + texture . name } {width: ${ texture . w } px; ` +
137
+ `height: ${ texture . h } px; ` +
138
+ `background-position: ${ ( ( ( texture . fit . x + pad ) / ( width - texture . w ) ) * 100 ) . toPrecision ( 6 ) } % ` +
139
+ `${ ( ( ( texture . fit . y + pad ) / ( height - texture . h ) ) * 100 ) . toPrecision ( 6 ) } %; ` +
140
+ `background-size: ${ ( ( width / texture . w ) * 100 ) . toPrecision ( 6 ) } %; }\n` ;
141
+
121
142
}
122
143
}
123
144
145
+ computedCSS = GITHUB_URL + selectorsString + globalString + spriteString ;
146
+
147
+ console . log ( computedCSS ) ;
124
148
}
125
149
126
150
update ( ) {
0 commit comments