- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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; }
- Related Articles
- MySQL Stored procedure won't fetch the whole table?
- Friendships made in Teenage won't last long. Is it true?
- CSS content: attr() on HTML5 progress doesn't work
- HTML5 meta name = “viewport” doesn't work as expected
- How to either determine SVG text box width or force line breaks after 'x' characters?
- How do I make a transparent canvas in HTML5?
- Translating HTML5 canvas
- HTML5 Canvas distorted
- HTML5 Canvas Transformation
- HTML5 Canvas Transformation Matrix
- HTML5 Canvas Circle Text
- HTML5 Canvas Degree Symbol
- HTML5 Canvas Font Size Based on Canvas Size
- How to Line Breaks to JavaScript Alert?
- Do any browsers yet support HTML5's checkValidity() method?

Advertisements