objects_MultiLineText.js.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: objects/MultiLineText.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: objects/MultiLineText.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>import {Text} from "./Text.js";
  20. /**
  21. * Multiple line text drawing directly into the canvas.
  22. *
  23. * Has support for basic text indent and alignment.
  24. *
  25. * @class
  26. * @extends {Text}
  27. */
  28. function MultiLineText()
  29. {
  30. Text.call(this);
  31. /**
  32. * Maximum width of the text content. After text reaches the max width a line break is placed.
  33. *
  34. * Can be set to null to be ignored.
  35. *
  36. * @type {number}
  37. */
  38. this.maxWidth = null;
  39. /**
  40. * Height of each line of text, can be smaller or larger than the actual font size.
  41. *
  42. * Can be set to null to be ignored.
  43. *
  44. * @type {number}
  45. */
  46. this.lineHeight = null;
  47. }
  48. MultiLineText.prototype = Object.create(Text.prototype);
  49. MultiLineText.prototype.draw = function(context, viewport, canvas)
  50. {
  51. context.font = this.font;
  52. context.textAlign = this.textAlign;
  53. context.textBaseline = this.textBaseline;
  54. var lineHeight = this.lineHeight || Number.parseFloat(this.font);
  55. var lines = this.text.split("\n");
  56. var offsetY = 0;
  57. // Iterate trough all lines (breakpoints)
  58. for(var i = 0; i &lt; lines.length; i++)
  59. {
  60. var line = lines[i];
  61. var size = context.measureText(line);
  62. var sublines = [];
  63. // Split into multiple sub-lines
  64. if(this.maxWidth !== null &amp;&amp; size.width > this.maxWidth)
  65. {
  66. while(line.length > 0)
  67. {
  68. var subline = "";
  69. var subsize = context.measureText(subline + line[0]);
  70. while(subsize.width &lt; this.maxWidth &amp;&amp; line.length > 0)
  71. {
  72. subline += line[0];
  73. line = line.substr(1);
  74. subsize = context.measureText(subline + line[0]);
  75. }
  76. sublines.push(subline);
  77. }
  78. }
  79. // Fits into a single line
  80. else
  81. {
  82. sublines = [line];
  83. }
  84. for(var j = 0; j &lt; sublines.length; j++)
  85. {
  86. if(this.fillStyle !== null)
  87. {
  88. context.fillStyle = this.fillStyle;
  89. context.fillText(sublines[j], this.position.x, this.position.y + offsetY);
  90. }
  91. if(this.strokeStyle !== null)
  92. {
  93. context.strokeStyle = this.strokeStyle;
  94. context.strokeText(sublines[j], this.position.x, this.position.y + offsetY);
  95. }
  96. offsetY += lineHeight;
  97. }
  98. }
  99. };
  100. export {MultiLineText};
  101. </code></pre>
  102. </article>
  103. </section>
  104. </div>
  105. <nav>
  106. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="BezierCurve.html">BezierCurve</a></li><li><a href="Box.html">Box</a></li><li><a href="Box2.html">Box2</a></li><li><a href="BoxMask.html">BoxMask</a></li><li><a href="Circle.html">Circle</a></li><li><a href="DOM.html">DOM</a></li><li><a href="EventManager.html">EventManager</a></li><li><a href="Graph.html">Graph</a></li><li><a href="Helpers.html">Helpers</a></li><li><a href="Image.html">Image</a></li><li><a href="Key.html">Key</a></li><li><a href="Line.html">Line</a></li><li><a href="Mask.html">Mask</a></li><li><a href="Matrix.html">Matrix</a></li><li><a href="MultiLineText.html">MultiLineText</a></li><li><a href="Node.html">Node</a></li><li><a href="NodeConnector.html">NodeConnector</a></li><li><a href="NodeGraph.html">NodeGraph</a></li><li><a href="NodeSocket.html">NodeSocket</a></li><li><a href="Object2D.html">Object2D</a></li><li><a href="Pattern.html">Pattern</a></li><li><a href="Pointer.html">Pointer</a></li><li><a href="QuadraticCurve.html">QuadraticCurve</a></li><li><a href="Renderer.html">Renderer</a></li><li><a href="RoundedBox.html">RoundedBox</a></li><li><a href="Text.html">Text</a></li><li><a href="UUID.html">UUID</a></li><li><a href="Vector2.html">Vector2</a></li><li><a href="Viewport.html">Viewport</a></li><li><a href="ViewportControls.html">ViewportControls</a></li></ul><h3>Global</h3><ul><li><a href="global.html#connector">connector</a></li><li><a href="global.html#direction">direction</a></li><li><a href="global.html#inputs">inputs</a></li><li><a href="global.html#inputSocket">inputSocket</a></li><li><a href="global.html#name">name</a></li><li><a href="global.html#node">node</a></li><li><a href="global.html#outputs">outputs</a></li><li><a href="global.html#outputSocket">outputSocket</a></li><li><a href="global.html#text">text</a></li><li><a href="global.html#type">type</a></li></ul>
  107. </nav>
  108. <br class="clear">
  109. <footer>
  110. Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.4</a> on Thu May 14 2020 18:22:31 GMT+0100 (Western European Summer Time)
  111. </footer>
  112. <script> prettyPrint(); </script>
  113. <script src="scripts/linenumber.js"> </script>
  114. </body>
  115. </html>