$(document).ready(function() {
var ctx = $('#bart')[0].getContext("2d");
var sprite = new Image();
var marginTop = 48;
var marginLeft = 0;
var line = '';
var textWidth = 0;
var textCount = 0;
sprite.src = './images/sprite.png';
function draw(text){
text += ' '; //Добавляю пробел
ctx.fillStyle = '#fff';
ctx.textAlign = "left";
ctx.font = " 18px Flow ";
textWidth = ctx.measureText(text).width;
textCount = Math.floor(440/textWidth);
marginLeft = (500 - textWidth*textCount)/2+3; //КОСТЫЛЬ!!! компенсирую пробел в конце строки!
marginTop = 48; // Сбрасываю отступ сверху
sprite.onload = function() {
ctx.drawImage(sprite, 0, 0, 500, 310, 0, 0, 500, 310);
for (var tc = 0; tc < textCount; tc++){
line += text; // Формирую строку
};
for (var i = 0; i < 9; i++) {
ctx.fillText(line, marginLeft, marginTop);
marginTop += 18;
};
ctx.drawImage(sprite, 498, 128, 80, 180, 406, 118, 80, 180);
};
};
draw('test');
})