var canvas = document.getElementById("demo") var ctx = canvas.getContext('2d'); ctx.font = "32pt Arial"; ctx.beginPath(); ctx.lineWidth = "1"; ctx.strokeStyle = "blue"; ctx.moveTo(100, 100); ctx.lineTo(100, 400); ctx.lineTo(400, 400); ctx.lineTo(400, 100); ctx.closePath(); ctx.stroke(); ctx.strokeText("hello world hello world hello world hello world hello world", 120, 200); ctx.strokeStyle = "red"; ctx.stroke(); CANVAS让文本自动换行恐怕是不可能的,因为绘制的文本和矩形没关系,
只有自己根据矩形,文本的大小和位置计算后,再绘制文本
计算文本占的宽度来换行
如果担心计算麻烦 可以用等宽的字体
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号