Permalink
Browse files

Ability to set/get JSON key. Bump v0.2.2

  • Loading branch information...
1 parent eb66fe5 commit 5e804abf3f0ad094598cd78d5a8866a1100bffff @shama committed Apr 17, 2013
Showing with 49 additions and 1 deletion.
  1. +14 −0 README.md
  2. +7 −0 example/example.js
  3. +1 −0 index.html
  4. +26 −0 index.js
  5. +1 −1 package.json
View
@@ -136,6 +136,19 @@ var uvmap = atlas.uv();
} */
```
+#### `atlas.json([input])`
+Exports or imports a JSON key for the ability to save the atlas state and
+restore it:
+
+```js
+var jsonkey = atlas.json();
+// then later
+atlas = atlas.json(jsonkey);
+
+// make sure to set your canvas if using a canvas too
+atlas.canvas = mycanvas;
+```
+
### `atlaspack.Rect(x, y, w, h)`
Creates a rectangle instance.
@@ -153,6 +166,7 @@ npm install atlaspack
```
## release history
+* 0.2.2 - Ability to get and set JSON key.
* 0.2.1 - Add uv method for uv coordinates.
* 0.2.0 - Add expand and index methods.
* 0.1.0 - initial release
View
@@ -64,6 +64,13 @@ document.querySelector('#export').onclick = function(e) {
return false;
};
+// handle exporting atlas key
+document.querySelector('#json').onclick = function(e) {
+ e.preventDefault();
+ window.open('data:application/json,' + escape(atlas.json()));
+ return false;
+};
+
// reset atlas
document.querySelector('#reset').onclick = function(e) {
e.preventDefault();
View
@@ -15,6 +15,7 @@
<ul>
<li>drag and drop textures on the canvas to add to the atlas
<li><a href="#" id="export">export atlas</a>
+ <li><a href="#" id="json">export JSON</a>
<li><a href="#" id="reset">reset atlas</a>
<li><a href="https://github.com/shama/atlaspack">view on github</a>
</ul>
View
@@ -153,6 +153,32 @@ Atlas.prototype.uv = function() {
return self._uvcache;
};
+Atlas.prototype.json = function(input) {
+ var self = this;
+ if (input) {
+ if (typeof input === 'string') input = JSON.parse(input);
+ return (function loop(obj) {
+ if (!obj || !obj.rect) return;
+ var atlas = new Atlas(obj.rect.x, obj.rect.y, obj.rect.w, obj.rect.h);
+ if (obj.left) atlas.left = loop(obj.left);
+ if (obj.right) atlas.right = loop(obj.right);
+ return atlas;
+ }(input));
+ } else {
+ return JSON.stringify(function loop(atlas) {
+ var obj = {
+ left: null, right: null,
+ rect: atlas.rect, filled: atlas.filled
+ };
+ if (atlas.left !== null) {
+ obj.left = loop(atlas.left);
+ obj.right = loop(atlas.right);
+ }
+ return obj;
+ }(self), null, 2);
+ }
+};
+
// if has an image and canvas, draw to the canvas as we go
Atlas.prototype._ontoCanvas = function(node) {
if (node && this.img && this.canvas) {
View
@@ -1,7 +1,7 @@
{
"name": "atlaspack",
"description": "Pack rectangles (or images) into a rectangle (or canvas texture atlas).",
- "version": "0.2.1",
+ "version": "0.2.2",
"homepage": "https://github.com/shama/atlaspack",
"author": {
"name": "Kyle Robinson Young",

0 comments on commit 5e804ab

Please sign in to comment.