Skip to content

Commit

Permalink
Rewrote most of it.. to accomplish asciidisco#52
Browse files Browse the repository at this point in the history
(also the old code been a lil dirty imo :))

Returns now a "meta" field instead of two - height and maxheight - fields.
The meta object contains these fields:
```
{
  maxHeight: int,
  maxWidth: int,
  images: [{
      height: int,
      offsetY: int,
      totalHeight: int,
      width: int
    },...]
}
```
  • Loading branch information
alpadev committed Mar 31, 2014
1 parent 78a65ba commit 6416c26
Showing 1 changed file with 44 additions and 28 deletions.
72 changes: 44 additions & 28 deletions lib/phantomspriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,51 @@ page.onConsoleMessage = function (msg) {

page.evaluate(function () {
var testdraw = function () {
var maxwidth = 0;
var maxheight = 0;
var allHeight = 0;
var heights = [];
var canvas = null;

for (var i = 0; i < document.images.length; i++) {
maxheight += document.images[i].height;
if (maxwidth < document.images[i].width) {
maxwidth = document.images[i].width;
}
}
maxheight = maxheight + (document.images.length * parseInt(document.getElementById('test').getAttribute('rel'), 10));
// Create canvas element
canvas = document.getElementById('test');
canvas.setAttribute('width', maxwidth);
canvas.setAttribute('height', maxheight);
// Loop through all images
for (var j = 0; j < document.images.length; j++) {
// Insert before the image
var ctx = canvas.getContext('2d');

// Draw image to canvas
var beforeHeight = (j - 1 === -1) ? 0 : document.images[j - 1].height;
allHeight += (beforeHeight + parseInt(canvas.getAttribute('rel'), 10));
heights.push(allHeight);
ctx.drawImage(document.images[j], 0, allHeight);
var images = document.images,
canvas = document.getElementById('test'),
ctx = canvas.getContext('2d'),
spriteCache = document.createElement('canvas'),
cacheCtx = spriteCache.getContext('2d'),
imgHeight = 0,
imgWidth = 0,
imgTotalHeight = 0,
spriteHeight = 0,
spriteWidth = 0,
spacing = parseInt(canvas.getAttribute('rel'), 10) || 0,
meta = { images: [] };

// loop all images
for (var i = 0, l = images.length; i < l; i += 1) {
imgHeight = images[i].height;
imgWidth = images[i].width;
imgTotalHeight = imgHeight + (i === l - 1 ? 0 : spacing);

spriteHeight += imgTotalHeight;
spriteWidth = spriteWidth < imgWidth ? imgWidth : spriteWidth;

// paint to cache

This comment has been minimized.

Copy link
@alpadev

alpadev Apr 1, 2014

Author Owner

@asciidisco
This is so we can drop the second loop. Don't know about performance, but the cache canvas isn't even added to the dom, is it? I have a guess it will just copy/move ImageData references behind the scene. Have tried a bunch of sprites without any noticeable impact. If you feel any doubts about it guess we have to stick with the two loop tactic..

spriteCache.setAttribute('height', spriteHeight);
spriteCache.setAttribute('width', spriteWidth);
cacheCtx.drawImage(canvas, 0, 0);

// draw the real canvas
canvas.setAttribute('height', spriteHeight);
canvas.setAttribute('width', spriteWidth);
ctx.drawImage(spriteCache, 0, 0);
ctx.drawImage(images[i], 0, spriteHeight - imgTotalHeight);

meta.images.push({
height: imgHeight,
width: imgWidth,
totalHeight: imgTotalHeight,
offsetY: spriteHeight - imgTotalHeight
});
}
return JSON.stringify({image: canvas.toDataURL(), heights: heights, maxheight: maxheight}) + '<<<<ENDIMAGE';

meta.maxHeight = spriteHeight;
meta.maxWidth = spriteWidth;

return JSON.stringify({image: canvas.toDataURL(), meta: meta}) + '<<<<ENDIMAGE';
};
setTimeout(function () {
console.log(testdraw());
Expand Down

0 comments on commit 6416c26

Please sign in to comment.