-
Notifications
You must be signed in to change notification settings - Fork 361
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
responsive text or text wrapping #97
Comments
Hi @LorisZ no not really. You will need to set the font size for each of the segments so the text fits nicely, please see http://dougtesting.net/winwheel/docs/tut7_colours_lines_fonts for more details. Also note you can include \n in the text to get it to break on to a new line. Regards, |
@zarocknz but the text is dynamically added. Also, the container of the canvas is responsive. It would be very difficult to size the text inside the segment. |
I agree, I actually came to the github to see if there is a way to dynamically edit font size or try my best to hack it. For @shidcordero sake i'm including my idea on how one could possibly make it dynamic, by editing the fontSize when making the segments. If anyone figures the math out, lemme know as I'm only working on this challenge in my free time. let segmentsToBeAdded = [...];
let segments = [];
let angleSize = 360 / segmentsToBeAdded.length;
let margin = 15;
let angleWidth= 400;
let ctx = document.getElementById('canvas').getContext("2d");
function heightOfArc(radius,angle){
return (((angle / 360) * 2) * Math.PI) * radius;
}
for(let i =0;i<segmentsToBeAdded.length;i++){
let fontSize = angleWidth;
let foundSize = false;
while(!foundSize){
ctx.font = fontSize + "px Arial";
let metrics = ctx.measureText(segmentsToBeAdded[i]);
let fontHeight = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
let fontWidth = metrics.width;
/*
Here you have access to the width of the would be text[fontWidth] the height of the text[fontHeight]
and the angle of the arc the text will be in[angleWidth], and you can check whether the font will fit
via heightOfArc to see if the height can fit inside the height of the arc. Tho you'd need to run heightOfArc
several times to check each pixel,
*/
if( ... MATH TO DETECT WHETHER SIZE FITS ...) fontSize--; //Still working on the math, haven't found a solution yet
else foundSize = true;
}
segments.push({
"text":segmentsToBeAdded[i],
"textFontSize": fontSize,
})
}
new winWheel({
"outerRadius": angleWidth,
"margin": 15,
"textAlignment": "outer"
"segments": segments,
"canvasId": "canvas"
}); |
Is there a way to make the text responsive to its canvas?
I currently have this and want to make the text fit on the segment
The text was updated successfully, but these errors were encountered: