Skip to content

Commit

Permalink
Add set_frame_offset function
Browse files Browse the repository at this point in the history
Allows the user to offset a single frame of the gif an arbitrary
amount in x and y directions
  • Loading branch information
ejegg committed Jun 28, 2015
1 parent 2c0d2f3 commit 8c044e1
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions libgif.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@
var ctx_scaled = false;

var frames = [];
var frameOffsets = []; // elements have .x and .y properties

var gif = options.gif;
if (typeof options.auto_play == 'undefined')
Expand Down Expand Up @@ -498,7 +499,20 @@
tmpCanvas.style.width = w + 'px';
tmpCanvas.style.height = h + 'px';
tmpCanvas.getContext('2d').setTransform(1, 0, 0, 1, 0, 0);
}
};

var setFrameOffset = function(frame, offset) {
if (!frameOffsets[frame]) {
frameOffsets[frame] = offset;
return;
}
if (typeof offset.x !== 'undefined') {
frameOffsets[frame].x = offset.x;
}
if (typeof offset.y !== 'undefined') {
frameOffsets[frame].y = offset.y;
}
};

var doShowProgress = function (pos, length, draw) {
if (draw && showProgressBar) {
Expand Down Expand Up @@ -590,6 +604,7 @@
data: frame.getImageData(0, 0, hdr.width, hdr.height),
delay: delay
});
frameOffsets.push({ x: 0, y: 0 });
};

var doImg = function (img) {
Expand Down Expand Up @@ -722,6 +737,7 @@
}());

var putFrame = function () {
var offset;
i = parseInt(i, 10);

if (i > frames.length - 1){
Expand All @@ -732,7 +748,9 @@
i = 0;
}

tmpCanvas.getContext("2d").putImageData(frames[i].data, 0, 0);
offset = frameOffsets[i];

tmpCanvas.getContext("2d").putImageData(frames[i].data, offset.x, offset.y);
ctx.globalCompositeOperation = "copy";
ctx.drawImage(tmpCanvas, 0, 0);
};
Expand Down Expand Up @@ -922,7 +940,8 @@
if (!initialized) init();
stream = new Stream(arr);
setTimeout(doParse, 0);
}
},
set_frame_offset: setFrameOffset
};
};

Expand Down

0 comments on commit 8c044e1

Please sign in to comment.