Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Fractalis
fractal.js
Commits
a1a0fb60
Commit
a1a0fb60
authored
Jul 13, 2017
by
Sascha Herzinger
Browse files
New utilities for text fitting
parent
15b53963
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/vue/mixins/utils.js
0 → 100644
View file @
a1a0fb60
export
default
{
/**
* https://stackoverflow.com/questions/5723154/truncate-a-string-in-the-middle-with-javascript
*/
truncate
({
text
,
strLen
,
separator
})
{
if
(
text
.
length
<=
strLen
)
return
text
separator
=
separator
||
'
...
'
const
sepLen
=
separator
.
length
const
charsToShow
=
strLen
-
sepLen
const
frontChars
=
Math
.
ceil
(
charsToShow
/
2
)
const
backChars
=
Math
.
floor
(
charsToShow
/
2
)
return
text
.
substr
(
0
,
frontChars
)
+
separator
+
text
.
substr
(
text
.
length
-
backChars
)
},
/**
* https://stackoverflow.com/questions/118241/calculate-text-width-with-javascript
*/
getTextWidth
({
text
,
font
})
{
// re-use canvas object for better performance
const
canvas
=
this
.
getTextWidth
.
canvas
||
(
this
.
getTextWidth
.
canvas
=
document
.
createElement
(
'
canvas
'
))
const
context
=
canvas
.
getContext
(
'
2d
'
)
context
.
font
=
font
const
metrics
=
context
.
measureText
(
text
)
return
metrics
.
width
},
truncateTextUntil
({
text
,
font
,
maxWidth
})
{
while
(
this
.
getTextWidth
({
text
,
font
})
>
maxWidth
)
{
text
=
this
.
truncate
({
text
,
strLen
:
text
.
length
-
5
})
}
return
text
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment