initial
This commit is contained in:
30
utils/TextHelper.js
Normal file
30
utils/TextHelper.js
Normal file
@@ -0,0 +1,30 @@
|
||||
export class TextHelper {
|
||||
static scaleToFit(textObject, size) {
|
||||
if (!size.width) size.width = Infinity;
|
||||
if (!size.height) size.height = Infinity;
|
||||
if (textObject.style.wordWrap) {
|
||||
wordWrapScaleToFit(textObject, size);
|
||||
} else {
|
||||
nonWordWrapScaleToFit(textObject, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function wordWrapScaleToFit(textObject, size) {
|
||||
textObject.scale.set(1);
|
||||
if (!textObject.style.defaultWordWrapWidth) {
|
||||
textObject.style.defaultWordWrapWidth = textObject.style.wordWrapWidth;
|
||||
}
|
||||
textObject.style.wordWrapWidth = textObject.style.defaultWordWrapWidth;
|
||||
const scaleStep = 0.99;
|
||||
while (size.width < textObject.width || size.height < textObject.height) {
|
||||
textObject.scale.set(textObject.scale.x * scaleStep, textObject.scale.y * scaleStep);
|
||||
textObject.style.wordWrapWidth = textObject.style.wordWrapWidth / scaleStep;
|
||||
}
|
||||
}
|
||||
|
||||
function nonWordWrapScaleToFit(textObject, size) {
|
||||
textObject.scale.set(1);
|
||||
const factor = Math.min(1, size.width / textObject.width, size.height / textObject.height);
|
||||
textObject.scale.set(factor);
|
||||
}
|
||||
Reference in New Issue
Block a user