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 // prevent image from blending with itself if there are alpha channels
self.screen.clearRegion(lpos.xi, lpos.xl, lpos.yi, lpos.yl); self.screen.clearRegion(lpos.xi, lpos.xl, lpos.yi, lpos.yl);
}); });
this.on('destroy', function() {
self.stop();
});
} }
ANSIImage.prototype.__proto__ = Box.prototype; 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; 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.cellmap = cellmap;
self.screen.render(); self.screen.render();
}); });
}; };
ANSIImage.prototype.pause = function() { ANSIImage.prototype.pause = function() {
if (!this.img) return;
return this.img.pause(); return this.img.pause();
}; };
ANSIImage.prototype.stop = function() { ANSIImage.prototype.stop = function() {
if (!this.img) return;
return this.img.stop(); return this.img.stop();
}; };
ANSIImage.prototype.clearImage = function() { ANSIImage.prototype.clearImage = function() {
if (this.img) { this.stop();
this.stop();
}
this.setContent(''); this.setContent('');
this.img = null; this.img = null;
this.cellmap = null; this.cellmap = null;
@ -148,7 +153,7 @@ ANSIImage.prototype.render = function() {
var coords = this._render(); var coords = this._render();
if (!coords) return; if (!coords) return;
if (this.img) { if (this.img && this.cellmap) {
this.img.renderElement(this.cellmap, this); this.img.renderElement(this.cellmap, this);
} }

View File

@ -94,14 +94,41 @@ var png = blessed.image({
screen.render(); screen.render();
screen.key('q', function() { screen.key('q', function() {
clearInterval(timeout);
screen.destroy(); screen.destroy();
}); });
var timeout = setInterval(function() { var timeout = setInterval(function() {
if (png.right <= 0) {
clearInterval(timeout);
return;
}
png.left++; png.left++;
screen.render(); screen.render();
}, 100); }, 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() { png.on('mousedown', function() {
clearInterval(timeout); clearInterval(timeout);
}); });