企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
## 中英文段落 合成 ``` <canvas id="canvas" width=640 height=300></canvas> ``` ```js var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var text = "Techbrood is a l 很舒服 engine 是 on creatives and techniques aw started at the beginning of 2014, founded by senior technical leaders. by senior technical leaders."; // 调用 wrapText(2dCtx, 文本, x轴, y轴, 最大宽度, fontSize) wrapText(ctx, text, 20, 20, 640, 18); function wrapText(context, text, x, y, maxWidth, fontSize, fontFace) { var words = text.split(' '); var line = ''; var lineHeight = fontSize + 10; context.font = fontSize + "px " + fontFace; for (var n = 0; n < words.length; n++) { var testLine = line + words[n] + ' '; var metrics = context.measureText(testLine); var testWidth = metrics.width; if (testWidth > maxWidth - 10) { // 减10得原因在在于英文和中文会丢失 context.fillStyle = "#a3a353"; context.fillText(line, x, y); line = words[n] + ' '; y += lineHeight; } else { line = testLine; } } context.fillType = "#a3a353"; context.fillText(line, x, y); } ```