![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
HTML5 canvas ctx.fillText won't do line breaks
The fillText() method draws filled text on the canvas. If you want to break lines you can do this by splitting the text at the new lines and calling the filltext() multiple times. By doing so, you are splitting the text into lines and drawing each line separately.
You can try to run the following code snippet −
var c = $('#c')[0].getContext('2d'); c.font = '12px Courier'; alert(c); var str = 'first line
second line...'; var a = 30; var b = 30; var lineheight = 15; var lines = str.split('
'); for (var j = 0; j<lines.length; j++) c.fillText(lines[j], a, b + (j*lineheight) );
// for canvas <canvas id="c" width="200" height="200"></canvas>
// CSS
canvas { background-color: #FFCE9E; }
Advertisements