Skip to content

Instantly share code, notes, and snippets.

@bruzkovsky
Created January 5, 2023 14:56
Show Gist options
  • Save bruzkovsky/a444af54c9d3715617c1f4855d623764 to your computer and use it in GitHub Desktop.
Save bruzkovsky/a444af54c9d3715617c1f4855d623764 to your computer and use it in GitHub Desktop.
fabric.js FixedTextbox
// 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