-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve calc-text-width method reusing the Konva canvas
- Loading branch information
1 parent
f3f7bef
commit b88a23b
Showing
4 changed files
with
63 additions
and
12 deletions.
There are no files selected for viewing
42 changes: 38 additions & 4 deletions
42
...mmon/components/mock-components/front-rich-components/tabsbar/business/calc-text-width.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,50 @@ | ||
import { Layer } from 'konva/lib/Layer'; | ||
|
||
/** | ||
* Virtually calculates the width that a text will occupy, by using a canvas. | ||
* If a Konva Layer is provided, it will reuse the already existing canvas. | ||
* Otherwise, it will create a canvas within the document, on the fly, to perform the measurement. | ||
* Finaly, as a safety net, a very generic calculation is provided in case the other options are not available. | ||
*/ | ||
export const calcTextWidth = ( | ||
inputText: string, | ||
fontSize: number, | ||
fontfamily: string, | ||
konvaLayer?: Layer | ||
) => { | ||
if (konvaLayer) | ||
return _getTextWidthByKonvaMethod( | ||
konvaLayer, | ||
inputText, | ||
fontSize, | ||
fontfamily | ||
); | ||
|
||
return _getTextCreatingNewCanvas(inputText, fontSize, fontfamily); | ||
}; | ||
|
||
const _getTextWidthByKonvaMethod = ( | ||
konvaLayer: Layer, | ||
text: string, | ||
fontSize: number, | ||
fontfamily: string | ||
) => { | ||
const context = konvaLayer.getContext(); | ||
context.font = `${fontSize}px ${fontfamily}`; | ||
return context.measureText(text).width; | ||
}; | ||
|
||
const _getTextCreatingNewCanvas = ( | ||
text: string, | ||
fontSize: number, | ||
fontfamily: string | ||
) => { | ||
// Creates an invisible canvas to perform the measurement | ||
let canvas = document.createElement('canvas'); | ||
const context = canvas.getContext('2d'); | ||
|
||
if (context) { | ||
context.font = `${fontSize}px ${fontfamily}`; | ||
return context.measureText(inputText).width; | ||
return context.measureText(text).width; | ||
} | ||
const charAverageWidth = fontSize * 0.7; | ||
return inputText.length * charAverageWidth + charAverageWidth * 0.8; | ||
return text.length * charAverageWidth + charAverageWidth * 0.8; | ||
}; |
This file contains 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
This file contains 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
This file contains 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