MCPcopy Index your code
hub / github.com/pythongosssss/ComfyUI-Custom-Scripts / wrapText

Function wrapText

web/js/workflowImage.js:504–537  ·  view source on GitHub ↗
(context, text, x, y, maxWidth, lineHeight)

Source from the content-addressed store, hash-verified

502 init() {
503 // https://codepen.io/peterhry/pen/nbMaYg
504 function wrapText(context, text, x, y, maxWidth, lineHeight) {
505 var words = text.split(" "),
506 line = "",
507 i,
508 test,
509 metrics;
510
511 for (i = 0; i < words.length; i++) {
512 test = words[i];
513 metrics = context.measureText(test);
514 while (metrics.width > maxWidth) {
515 // Determine how much of the word will fit
516 test = test.substring(0, test.length - 1);
517 metrics = context.measureText(test);
518 }
519 if (words[i] != test) {
520 words.splice(i + 1, 0, words[i].substr(test.length));
521 words[i] = test;
522 }
523
524 test = line + words[i] + " ";
525 metrics = context.measureText(test);
526
527 if (metrics.width > maxWidth && i > 0) {
528 context.fillText(line, x, y);
529 line = words[i] + " ";
530 y += lineHeight;
531 } else {
532 line = test;
533 }
534 }
535
536 context.fillText(line, x, y);
537 }
538
539 const stringWidget = ComfyWidgets.STRING;
540 // Override multiline string widgets to draw text using canvas while saving as svg

Callers 1

initFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected