-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
199 lines (184 loc) · 8.78 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>SpriteMason by GammaStream</title>
<link rel="stylesheet" type="text/css" media="all" href="css/base.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
</head>
<body>
<div id="sm-toolbar">
<div id="sm-logo">SpriteMason</div>
</div>
<div id="sm-content">
<p>SpriteMason is as simple as, well... one, two, three in fact. Follow the steps below to generate your sprite image and associated CSS.</p>
<div id="sm-step-zone">
<ol id="sm-steps">
<li>
<div>Select image files (png)</div>
<div id="sm-drop-zone">Drop image files here</div>
</li>
<li>
<div>Configure options</div>
<div style="margin: 10px 0px">
<label><span>Padding:</span> <input type="text" id="sm-param-padding" style="width: 32px" placeholder="5"/> pixels</label>
<label><span>Icon Class:</span> <input type="text" id="sm-param-class" style="width: 100px" placeholder=".icon"/></label>
<label><span>CSS Prefix:</span> <input type="text" id="sm-param-prefix" style="width: 100px" placeholder="None"/></label>
</div>
</li>
<li>
<div>Create sprite + CSS</div>
<div>
<input id="sm-build-button" type="submit" value="Build Sprite">
<input id="sm-reload-button" type="submit" value="Reload">
</div>
</li>
</ol>
</div>
<div id="sm-work-zone">
<div id="sm-scratch-pad">
<h2>Selected Images:</h2>
<div id="sm-scratch-pad-images"></div>
</div>
<div id="sm-output-sprite">
<h2>Sprite Image:</h2>
<span class="instructions">(Right-click on image to "Save Image As…")</span>
<img id="sm-output-sprite-img" src="" alt="Output Image">
</div>
<div id="sm-output-css">
<h2>Sprite CSS:</h2>
<span class="instructions">(Click CSS to select, then Cmd+c or Ctrl+c to copy)</span>
<div id="sm-output-css-text"></div>
</div>
</div>
</div>
<div id="sm-footer">
Copyright © 2012-2015 GammaStream Technologies, Inc. All Rights Reserved.
</div>
<script type="text/javascript">
$(function(){
// Lets define our drop zone and hook it up.
var dropZone = document.getElementById('sm-drop-zone');
dropZone.addEventListener('dragover', function(e){
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
}, false);
/**
* On drop we want to create a new img for each of the image files
* and append them to the scratch pad.
*/
dropZone.addEventListener('drop', function(e){
e.stopPropagation();
e.preventDefault();
var files = e.dataTransfer.files;
for (var i = 0, f; f = files[i]; i++) {
if (f.type.match('image.*')) {
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
var span = document.createElement('span');
span.innerHTML = ['<img class="sm-brick" src="', e.target.result, '" title="', escape(theFile.name), '"/>'].join('');
document.getElementById('sm-scratch-pad-images').insertBefore(span, null);
};
})(f);
reader.readAsDataURL(f);
}
}
$('#sm-scratch-pad').show();
}, false);
/**
* Build the sprite.
*/
$('#sm-build-button').click(function(e){
e.preventDefault();
spriteMason();
return false;
});
/**
* Reset the application by simply reloading the page.
*/
$('#sm-reload-button').click(function(e){
e.preventDefault();
document.location.reload();
return false;
});
/**
* Clicking the output pane causes the text to be selected,
* ready for an immediate copy.
*/
$('#sm-output-css').click(function(e){
e.preventDefault();
selectTextForId('sm-output-css-text');
return false;
});
function spriteMason(){
// update the UI
$('.sm-canvas').remove();
$('#sm-scratch-pad').show();
$('#sm-output-sprite').hide();
$('#sm-output-css').show();
// set our options and grab our images
var $images = $('.sm-brick');
var padding = $('#sm-param-padding').val() ? $('#sm-param-padding').val() : 5;
var icon_class = $('#sm-param-class').val() ? $('#sm-param-class').val() : '.icon';
var css_prefix = $('#sm-param-prefix').val() ? $('#sm-param-prefix').val() + ' ' : '';
// calculate the max height and width of the dropped images.
var maxWidth = 0;
var maxHeight = 0;
var count = 0;
$images.each(function(){
count++;
if( $(this).height() > maxHeight ) maxHeight = $(this).height();
if( $(this).width() > maxWidth ) maxWidth = $(this).width();
});
// calculate our canvas and final sprite size... making it as square as possible
var rows = Math.ceil(Math.sqrt(count));
var cols = Math.ceil(count/rows);
var c_width = cols*maxWidth + ((cols-1)*padding);
var c_height = rows*maxHeight + ((rows-1)*padding);
var id = 'sm-canvas-' + Math.floor( Math.random()*99999 );
var c = $('<canvas id="'+ id +'" class="sm-canvas" width="'+c_width+'" height="'+c_height+'"></canvas>');
$('body').append(c);
// draw the images to the canvas one by one.
var canvas = document.getElementById(id);
var ctx = canvas.getContext('2d');
var sprite_file_name = "sprite" + css_prefix.trim() + icon_class.trim() + ".png";
var css = css_prefix + icon_class + " { \n display: inline-block;\n vertical-align: middle;\n background: url(" + sprite_file_name + ") no-repeat;\n}\n\n";
$images.each(function(index){
var x_offset = Math.floor(index / rows);
var y_offset = Math.floor(index % rows);
var x = (x_offset * maxHeight)+(x_offset*padding);
var y = (y_offset * maxHeight)+(y_offset*padding);
var brick_name = $(this).attr('title').replace(/\.[^/.]+$/, "");
css = css + css_prefix + icon_class + icon_class + "-" +brick_name + " { \n background-position:" + x + "px " + y + "px;\n height:"+ $(this).height() +"px;\n width:" + $(this).width() + "px;\n}\n\n";
ctx.drawImage(this, x, y, $(this).width(), $(this).height());
});
// update the UI
$('#sm-scratch-pad').hide();
$('#sm-output-sprite-img').attr('src', canvas.toDataURL());
$('#sm-output-sprite').show();
$('#sm-output-css-text').text(css);
$('#sm-output-css').show();
}
/**
* Selects the text contained within the given element id.
*
* @param {string} containerId The id of the container.
* @returns {undefined}
*/
function selectTextForId(containerId) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(containerId));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(containerId));
window.getSelection().addRange(range);
}
}
});
</script>
</body>
</html>