Created
January 5, 2023 14:56
-
-
Save bruzkovsky/a444af54c9d3715617c1f4855d623764 to your computer and use it in GitHub Desktop.
fabric.js FixedTextbox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// orginally from here: https://github.com/fabricjs/fabric.js/issues/4000#issue-235260862 | |
class FixedTextbox extends fabric.Textbox { | |
_wrapLine(_line, lineIndex, desiredWidth, reservedSpace) { | |
const offset = 0; | |
let textWidth = this._measureWord(_line, lineIndex, offset); | |
if (textWidth > desiredWidth && !this.isEditing) { | |
const ellipsis = '…'; | |
const lengthReducingFactor = desiredWidth / textWidth; | |
let length = Math.ceil(_line.length * lengthReducingFactor); | |
while (textWidth >= desiredWidth && length > 0) { | |
_line = _line.substring(0, length--) + ellipsis; | |
textWidth = this._measureWord(_line, lineIndex, offset); | |
} | |
} | |
return super._wrapLine(_line, lineIndex, desiredWidth, reservedSpace); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment