幾年前,我爲APNGedit製作了一個Javascript腳本來繪製笑男人的標誌。它使用現在已不存在mozTextAlongPath好看的'弧形文字?
我最近重新發現了這個腳本,並使用翻譯,旋轉和fillText()重新編寫它。但是,這並不尊重字符寬度,也不會被縮小(看起來很糟糕)。
原始大約2009年(不完美,但沒關係):
當前版本:
怎樣繪製文本在弧形的HTML5畫布上,讓它看起來不錯?基於Kolink的回答
解決方案代碼:
ctx.fillStyle = primaryColor;
ctx.font = fontSize + 'px ' + fontFamily;
var textWidth = ctx.measureText(text).width,
charRotation = 0,
character, charWidth, nextChar, nextWidth, bothWidth, kern, extraRotation, charSegment;
for (var i=0, l=text.length; i<l; i++) {
character = nextChar || text[i];
charWidth = nextWidth || ctx.measureText(character).width;
// Rotate so the letter base makes a circle segment instead of a tangent
extraRotation = (Math.PI/2) - Math.acos((charWidth/2)/radius);
ctx.save();
ctx.translate(radius, h/2);
ctx.rotate(charRotation);
ctx.translate(0, -textRadius);
ctx.rotate(extraRotation);
ctx.fillText(character,0,0);
ctx.restore();
nextChar = text[i+1] || '';
nextWidth = ctx.measureText(nextChar).width;
bothWidth = ctx.measureText(character+nextChar).width;
kern = bothWidth - charWidth - nextWidth;
charSegment = (charWidth+kern)/textWidth; // percent of total text size this takes up
charRotation += charSegment * (Math.PI*2);
}
看看在這裏提到的raphael.js:http://stackoverflow.com/questions/2612764/attach-text-on-path-in-raphael – DhruvPathak