javascript - HTML5 文本旋转

标签 javascript html rotation html5-canvas

我想旋转图形 x 轴的文本。下面是我正在使用的代码

context.rotate(20*Math.PI/2);
context.fillText(name, startX + (i * barWidth) + i, chartHeight + 10, 200); 
context.rotate(-20*Math.PI/2);

写完文字后,我将其旋转回原来的位置。但不知何故它不起作用。工作代码可以在 http://intercepter.comli.com/example3.html 找到。

我已经浏览过this article并尝试过,但它就是不起作用。请帮帮我

最佳答案

20*Math.PI/2 旋转与不旋转任何东西类似,因为:

20*Math.PI/2 = 10*Math.PI = 2*Math.PI

不确定您想如何旋转文本,但不是

context.rotate(20*Math.PI/2);
context.fillText(name, startX + (i * barWidth) + i, chartHeight + 10, 200); 
context.rotate(-20*Math.PI/2);

您可以尝试使用transformations像这样:

context.save();
context.setTransform(1,0,0,1,0,0);
context.translate(startX + (i * barWidth) + barWidth/2, chartHeight - 10); 
context.rotate(-Math.PI/2);

context.fillText(name, 0, 0, 200);        
context.restore();

查看工作 DEMO :

关于javascript - HTML5 文本旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10969436/

相关文章:

javascript - 如何使用li上的onclick事件显示 block (CSS)

javascript - 如何在滚动条上应用 css?

javascript - 使用 ng-repeat 时无法打开或关闭 Accordion

c++ - 在 C++ 中使用 opengl 旋转魔方面的问题

javascript - jquery 文件未与 html 文件链接

javascript - 使用正则表达式匹配特定语法

android - AsyncTask fragment 轮换

javascript - 通过 Javascript 停止/启动 SVG 旋转

javascript - JQuery 选择器范围

javascript - 如何将类 'active' 添加到 href 在 location.pathname 中的导航项