var clamp = function(val, min, max) {
return Math.min(Math.max(val, min), max);
}
// ...
// new position tank
this.pos = function () {
this.x += this.stepX;
this.y += this.stepY;
this.x = clamp(this.x, 0, canvas.width);
this.y = clamp(this.y, 0, canvas.height);
}