Comparing version 0.2.1 to 0.2.2
@@ -8,3 +8,4 @@ // create a canvas | ||
// create an atlas with our canvas | ||
var atlas = require('../')(canvas); | ||
var createAtlas = require('../'); | ||
var atlas = createAtlas(canvas); | ||
@@ -61,4 +62,3 @@ function atlasPack(img) { | ||
// handle exporting atlas | ||
var saveas = document.querySelector('#export'); | ||
saveas.onclick = function(e) { | ||
document.querySelector('#export').onclick = function(e) { | ||
e.preventDefault(); | ||
@@ -68,1 +68,19 @@ window.open(canvas.toDataURL()); | ||
}; | ||
// 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(); | ||
if (window.confirm('Are you sure?')) { | ||
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height); | ||
canvas.width = 128; canvas.height = 128; | ||
atlas = createAtlas(canvas); | ||
} | ||
return false; | ||
}; |
28
index.js
@@ -136,3 +136,3 @@ /* | ||
loop(atlas.right); | ||
} else if (atlas.rect.name) { | ||
} else if (typeof atlas.rect.name !== 'undefined') { | ||
self._uvcache[atlas.rect.name] = [ | ||
@@ -157,2 +157,28 @@ [atlas.rect.x, atlas.rect.y], | ||
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 | ||
@@ -159,0 +185,0 @@ Atlas.prototype._ontoCanvas = function(node) { |
{ | ||
"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", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -139,2 +139,15 @@ # atlaspack | ||
#### `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)` | ||
@@ -157,2 +170,3 @@ Creates a rectangle instance. | ||
## release history | ||
* 0.2.2 - Ability to get and set JSON key. | ||
* 0.2.1 - Add uv method for uv coordinates. | ||
@@ -159,0 +173,0 @@ * 0.2.0 - Add expand and index methods. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16715
325
177