

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 \nsecond line...'; var a = 30; var b = 30; var lineheight = 15; var lines = str.split('\n'); 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 Questions & Answers
- How to draw a text with fillText() in HTML5?
- How to Line Breaks to JavaScript Alert?
- Translating HTML5 canvas
- HTML5 Canvas distorted
- HTML5 Canvas Transformation
- How do I make a transparent canvas in HTML5?
- What Are Whitespace and Line Breaks in JavaScript?
- C program that won’t compile in C++
- Why a Cheaper iPhone won’t Help Users?
- HTML5 Canvas Transformation Matrix
- HTML5 Canvas Circle Text
- HTML5 Canvas Degree Symbol
- MySQL Stored procedure won't fetch the whole table?
- HTML5 Canvas Font Size Based on Canvas Size
- HTML5 Canvas to PNG File
Advertisements