Skip to content

Add text wrapping function #5146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2021
Merged

Conversation

lawreka
Copy link
Contributor

@lawreka lawreka commented Mar 31, 2021

Resolves #5081
Addresses #4652
Follow-up to #5108

Changes:

Adds textWrap function to specify how lines of text are wrapped within a text box. This requires a max-width set on the text area, specified in text() as parameter x2.

  • WORD wrap style only breaks lines at spaces. A single string without spaces that exceeds the boundaries of the canvas or text area is not truncated, and will overflow the desired area, disappearing at the canvas edge.
  • CHAR wrap style breaks lines wherever needed to stay within the text box.

WORD is the default wrap style, and both styles will still break lines at any line breaks (\n) specified in the original text. The text area max-height parameter (y2) also still applies to wrapped text in both styles, lines of text that do not fit within the text area will not be drawn to the screen.

I may have gone a little overboard adding documentation and examples, specifically the last one with the little screaming stick-man demonstrating the current lack of truncation. He should be removed if additional functionality is added in the future to deal with truncation and hyphenation, but for now I think he shows kind of a fun way to work with #4652.

I also added an example using Chinese characters, since as @dhowe mentioned in discussion, textWrap(CHAR) is essential for displaying text in character-based languages nicely.

Screenshots of the change:
Screenshot 2021-03-31 at 21 56 56

PR Checklist

  • npm run lint passes
  • [Inline documentation] is included / updated
  • [Unit tests] are included / updated

@outofambit outofambit requested a review from dhowe April 2, 2021 02:29
@outofambit
Copy link
Contributor

I may have gone a little overboard adding documentation and examples

there's no such thing! :) thanks again @lawreka.

@dhowe @lmccart thoughts on this approach?

@dhowe
Copy link
Contributor

dhowe commented Apr 2, 2021

Looks good to me, thanks @lawreka !

@limzykenneth
Copy link
Member

Loved the examples and code looks nice and clean as well! Happy to have this merged when ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Difference with text wrapping betwen 1.1.9 -> 1.2.0 and 1.3.0
4 participants