<canvas id='bart' width='500' height='310'></canvas>
var canvas = document.getElementById('bart'); // canvas var ctx = canvas.getContext('2d'); // canvas var sprite = new Image(); sprite.src = 'images/sprite.png'; //
function draw(text){ marginTop = 36; // line = ''; // line function write(text, n, marginStep){ // text n marginStep for (var i = 0; i < n; i++) { ctx.fillText(text, 250, marginTop); marginTop += marginStep; }; }; line = ''; ctx.fillStyle = '#fff'; ctx.textAlign = 'center'; ctx.font = '18px Flow';
textWidth = ctx.measureText(text).width;
ctx.drawImage(sprite, 0, 0, 500, 250, 0, 0, 500, 250);
if (textWidth > 0 && textWidth <= 220){ // 0-220 (tc ) textCount = Math.floor(450/(textWidth + 5)); // ( (+)) for (var tc = 0; tc < textCount; tc++){ // line += text + ' '; }; line = line.slice(0, -1); // write(line, 10, 20); // }else if(textWidth > 220 && textWidth <= 225){ // 221-225 (2 ) ctx.font = '15px Flow'; // line = text + ' ' + text; // write(line, 10, 20); // }else if(textWidth > 225 && textWidth <= 360 ){ // 226-360 (1 ) ctx.font = '20px Flow'; // if (textWidth > 445){ // , , ctx.font = '18px Flow'; textWidth = ctx.measureText(text).width; }; write(text, 10, 20); // }else if(textWidth > 360 && textWidth <= 450 ){ // 361-450 , (1 ) write(text, 10, 20); // }else if(textWidth > 450 && textWidth <= 500){ // 451-500 (1 ) ctx.font = '15px Flow'; // write(text, 10, 20); }
else if(textWidth > 500 && textWidth <= 700){ // 501-700 2! var words = text.split(' '); // words var countWords = words.length; // if(countWords > 4){ for (var n = 0; n < countWords; n++) { // countWords 1 , line var testLine = line + words[n] + ' '; // var testWidth = ctx.measureText(testLine).width;// if (testWidth > 450) { // write(line, 5, 40); // 5 40 ( ) 36 line = words[n] + ' '; // line }else { line = testLine; }; }; marginTop = 56; // 2 write(line, 5, 40); // 5 40( ) }
ctx.drawImage(sprite, 498, 128, 80, 180, 406, 118, 80, 180); //
<form name='form_1' onsubmit='return false;' id='form_1'> <input name='inputText' id='inputText' type='text' maxlength='150' autocomplete='off' autofocus placeholder=' -, !'> <input id='writeIt' type='button' value='!'> <input id='downloadIt' type='button' class='center' value='!' > </form>
document.getElementById('writeIt').onclick=function(e){draw(document.forms.form_1.elements.inputText.value)}; document.getElementById('form_1').onsubmit=function(e){ // Enter draw(document.forms.form_1.elements.inputText.value); return false // };
document.getElementById('downloadIt').onclick=function(e){ ctx.drawImage(sprite, 9, 250, 142, 25, 17, 222, 142, 25); // window.open(canvas.toDataURL("image/png"), " Bart's Chalkoard", "resizable=no,toolbar=no,menubar=no,location=no,scrollbars=no,status=no,width=530,height=340, left=400, top=120,"); // . ctx.drawImage(sprite, 16, 226, 134, 18, 16, 226, 134, 18); // };
Source: https://habr.com/ru/post/149606/
All Articles