-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
html2canvas text-overflow not working #2262
Labels
Comments
Is it solved? I had the same problem |
no "text-overflow" property is not supported by html2canvas. |
i resolved this problem by measuring text's width and manual cutting text. tip: it only works on one-row-text case. // step 1: create a function that can get width of the text
const measureText = (function () {
const canvas = document.createElement('canvas');
canvas.id = 'measure-canvas';
canvas.width = 500;
canvas.height = 100;
return (params) => {
const { font, text } = params;
const ctx = canvas.getContext('2d');
ctx.font = `${font} ${['Helvetica Neue', 'Helvetica', 'PingFang SC'].join(' ')}`;
const { width } = ctx.measureText(text);
return {
width,
};
};
}());
// step 2: create the cutString function
const cutString = (str, font, maxWidth) => {
const { width } = measureText({ text: str, font });
if (width <= maxWidth) {
// use the text
return str;
}
let ret = '';
for (let i = str.length - 1; i >= 0; i--) {
const text = `${str.substring(0, i)}...`;
const { width: textWidth } = measureText({
text,
font,
});
if (textWidth <= maxWidth) {
ret = text;
break;
}
}
return ret;
};
// step 3: get the text container's width and invoke cutString
const containerDom = document.querySelector('#id');
const maxWidth = containerDom.offsetWidth;
// cut text
const finalText = cutString('abcdefg and so on', 'bold 14px', maxWidth); |
I also have the same problem. How do I compatible? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have applied following css to limit the title sting. it works in html but not working Generated image using html2canvas.
text-overflow: ellipsis; property is not working... Any alternative way so I can limit the text in generated image?
.content_overlay .container .staycation_title { font-size: 1.5em; line-height: normal; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 95%; margin-left: auto; margin-right: auto; }
The text was updated successfully, but these errors were encountered: