gab*_*iel 51 html javascript css canvas css3
我正在尝试使用CSS3,HTML Canvas甚至SVG 制作弯曲文本效果(例如,见下图)?这可能吗?如果是这样,我怎样才能达到这个效果?
更新:澄清:将以这种方式设置样式的文本将是动态的.

Phr*_*ogz 33
SVG直接支持文本路径,但它不会沿路径"弯曲"各个字形.以下是您如何创建它的示例:
...
<defs>
<path id="textPath" d="M10 50 C10 0 90 0 90 50"/>
</defs>
<text fill="red">
<textPath xlink:href="#textPath">Text on a Path</textPath>
</text>
...
Run Code Online (Sandbox Code Playgroud)
Jak*_*mpl 21
你当然可以用canvas做到这一点,试试这个代码作为例子:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing min-width and max-width</title>
<style type="text/css">
</style>
</head>
<body>
<canvas id="cnv"></canvas>
<script type="text/javascript" charset="utf-8">
cnv = document.getElementById("cnv");
cnv.width = 500;
cnv.height = 300;
ctx = cnv.getContext("2d");
ctx.font = "bold 12px sans-serif";
text = "abcdefghijklm"
for (i = 0; i < text.length; i++) {
ctx.fillText(text[i], 300, 100);
ctx.rotate(0.1);
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
它并没有完全正确,但我确信你会设法将它调整为你的比较;)
Jak*_*mpl 13
或者你可以使用一些CSS来做,但是我相信你不会很快就能在IE上运行它.另一方面,很酷的是文本是可选择的:D
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing min-width and max-width</title>
<style type="text/css">
.num1 {
-webkit-transform: translate(0px, 30px) rotate(-35deg);
}
.num2 {
-webkit-transform: translate(0px, 25px) rotate(-25deg);
}
.num3 {
-webkit-transform: translate(0px, 23px) rotate(0deg);
}
.num4 {
-webkit-transform: translate(0px, 25px) rotate(25deg);
}
.num5 {
-webkit-transform: translate(0px, 30px) rotate(35deg);
}
span {display: inline-block; margin: 1px;}
</style>
</head>
<body>
<div style="width: 300px; height: 300px; margin: 50px auto">
<span class="num1">a</span><span class="num2">b</span><span class="num3">c</span><span class="num4">d</span><span class="num5">e</span>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)