stop image animation on destroy. improve test.

This commit is contained in:
Christopher Jeffrey 2015-08-25 20:49:59 -07:00
parent 7a1a979632
commit 793646955f
2 changed files with 38 additions and 6 deletions

View File

@ -47,6 +47,10 @@ function ANSIImage(options) {
// prevent image from blending with itself if there are alpha channels
self.screen.clearRegion(lpos.xi, lpos.xl, lpos.yi, lpos.yl);
});
this.on('destroy', function() {
self.stop();
});
}
ANSIImage.prototype.__proto__ = Box.prototype;
@ -119,26 +123,27 @@ ANSIImage.prototype.setImage = function(file) {
}
};
ANSIImage.prototype.play = function(callback) {
ANSIImage.prototype.play = function() {
var self = this;
return this.img.play(callback || function(bmp, cellmap) {
if (!this.img) return;
return this.img.play(function(bmp, cellmap) {
self.cellmap = cellmap;
self.screen.render();
});
};
ANSIImage.prototype.pause = function() {
if (!this.img) return;
return this.img.pause();
};
ANSIImage.prototype.stop = function() {
if (!this.img) return;
return this.img.stop();
};
ANSIImage.prototype.clearImage = function() {
if (this.img) {
this.stop();
}
this.stop();
this.setContent('');
this.img = null;
this.cellmap = null;
@ -148,7 +153,7 @@ ANSIImage.prototype.render = function() {
var coords = this._render();
if (!coords) return;
if (this.img) {
if (this.img && this.cellmap) {
this.img.renderElement(this.cellmap, this);
}

View File

@ -94,14 +94,41 @@ var png = blessed.image({
screen.render();
screen.key('q', function() {
clearInterval(timeout);
screen.destroy();
});
var timeout = setInterval(function() {
if (png.right <= 0) {
clearInterval(timeout);
return;
}
png.left++;
screen.render();
}, 100);
if (timeout.unref) timeout.unref();
screen.key(['h', 'left'], function() {
png.left -= 2;
});
screen.key(['k', 'up'], function() {
png.top -= 2;
});
screen.key(['l', 'right'], function() {
png.left += 2;
});
screen.key(['j', 'down'], function() {
png.top += 2;
});
screen.on('keypress', function() {
clearInterval(timeout);
});
png.on('mousedown', function() {
clearInterval(timeout);
});