'HTML5 canvas ctx.fillText cannot achieve line wrapping'

WBOY
Release: 2023-09-03 10:53:02
forward
1370 people have browsed it

"HTML5 canvas ctx.fillText无法实现换行"

The fillText() method draws filled text on the canvas. If you want to wrap the text, you can do this by splitting the text at a new line and calling filltext() multiple times. By doing this, you split the text into multiple lines and draw each line separately.

You can try running 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) );
Copy after login
// for canvas
<canvas id="c" width="200" height="200"></canvas>
Copy after login

// CSS

canvas {
   background-color: #FFCE9E;
}
Copy after login

The above is the detailed content of 'HTML5 canvas ctx.fillText cannot achieve line wrapping'. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!