Skip to content

Commit f2164ef

Browse files
eivers88etsaaron
authored andcommitted
texture packer progress
1 parent 988f35f commit f2164ef

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

assets/js/app/texture-packer.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
const GITHUB_URL = '/*\nResponsive CSS Sprite created using: ' +
2+
'http://responsive-css.us/\n' +
3+
'*/\n\n';
4+
15
function findNode(root, w, h) {
26
if (root.used) {
37
return findNode(root.right, w, h) || findNode(root.down, w, h);
@@ -95,9 +99,17 @@ export default class TexturePacker {
9599
let canvas = this.canvas;
96100
let ctx = canvas.getContext('2d');
97101
let pad = this.root.p;
102+
let prefix = this.prefix;
98103
let width = this.root.w + pad;
99104
let height = this.root.h + pad;
100105

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+
101113
canvas.width = width;
102114
canvas.height = height;
103115

@@ -118,9 +130,21 @@ export default class TexturePacker {
118130
ctx.stroke();
119131

120132
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+
121142
}
122143
}
123144

145+
computedCSS = GITHUB_URL + selectorsString + globalString + spriteString;
146+
147+
console.log(computedCSS);
124148
}
125149

126150
update() {

assets/js/bundle.js

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)