31 lines
1.0 KiB
JavaScript
31 lines
1.0 KiB
JavaScript
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);
|
|
}
|