问题

Friends, i'm finding rotating a text canvas object a bit tricky. The thing is, I'm drawing a graphic, but sometimes the width of each bar is smaller than the 'value' of that bar. So I have to ratate the 'value' 90 degrees. It will work in most cases.

I am doing the following

a function(x, y, text, maxWidth...)

var context = this.element.getContext('2d');

var metric = context.measureText(text); //metric will receive the measures of the text

if(maxWidth != null){

if(metric.width > maxWidth) context.rotate(Math.PI / 2);

}

context.fillText(text, x, y);

Ok, but it doesn't really work. Problems that I have seen: The text duplicates in different angles. The angles are not what I want (perhaps just a matter of trigonometry).

Well I just don't know what to do. I read something about methods like 'save' and 'restore' but I don't what to do with them. I've made some attempts but no one worked.

Would you help me with this, guys?

回答1:

This is a bit tricky to answer simply because there are a lot of concepts going on, so I've made you an example of what I think you'd like to do here:

http://jsfiddle.net/5UKE3/

The main part of it is this. I've put in a lot of comments to explain whats going on:

function drawSomeText(x, y, text, maxWidth) {

//metric will receive the measures of the text

var metric = ctx.measureText(text);

console.log(metric.width);

ctx.save(); // this will "save" the normal canvas to return to

if(maxWidth != null && metric.width > maxWidth) {

// These two methods will change EVERYTHING

// drawn on the canvas from this point forward

// Since we only want them to apply to this one fillText,

// we use save and restore before and after

// We want to find the center of the text (or whatever point you want) and rotate about it

var tx = x + (metric.width/2);

var ty = y + 5;

// Translate to near the center to rotate about the center

ctx.translate(tx,ty);

// Then rotate...

ctx.rotate(Math.PI / 2);

// Then translate back to draw in the right place!

ctx.translate(-tx,-ty);

}

ctx.fillText(text, x, y);

ctx.restore(); // This will un-translate and un-rotate the canvas

}

To rotate around the right spot you have to translate to that spot, then rotate, then translate back.

Once you rotate the canvas the context is rotated forever, so in order to stop all your new drawing operations from rotating when you dont want them to, you have to use save and restore to "remember" the normal, unrotated context.

If anything else doesn't make sense let me know. Have a good time making canvas apps!

来源:https://stackoverflow.com/questions/7755050/html5-text-canvas-rotate-in-case-text-width-is-larger-than-maximum-width-allowed

html text width,HTML5 Text Canvas rotate in case text width is larger than maximum width allowed相关推荐

  1. html canvas text 居中,HTML5 Canvas Text文本居中实例

    canvas> varcancasOne=document.getElementById('canvasOne');varctx=cancasOne.getContext('2d');varte ...

  2. 使用HTML5的canvas做一个会动的时钟

    这次我们的目标是画一个会和时间同步的时钟,不过没有美学感觉,样子丑的厉害. HTML5支持canvas了,我们可以直接在页面上绘图了,我看了下canvas和GDI+的接口差不多,所以我们先了解些基本的 ...

  3. html5之Canvas坐标变换应用-时钟实例

    在上一篇中我们了解html5的Canvas ,在这一篇中不用多说,写一个html5的时钟应用程序试手.在这里主要设置了坐标变换的平移(translate)和旋转变换(ratate),以及html5 C ...

  4. 前端笔记(Html+CSS+JS+DOM+网页特效+jQuery+HTML5+CSS3+canvas 标签+web开发重难点+面向对象+AJAX)

    第1章Html Html:超级文本标记语言(HyperText Markup Language),在浏览器上运行的一种标记语言. 就是给文本加上含有语义的标签. 接下来应该学习更多具体语义标签: 一. ...

  5. 详细讲解HTML5画布Canvas

    因为在整理画布的知识点,结果发现了这个贼厉害的博客,参考原博客:https://blog.csdn.net/u012468376/article/details/73350998 学习HTML5 Ca ...

  6. html5之canvas绘制

    初探html5之canvas 参考w3与菜鸟教程中canvas介绍,以及一些网上的资料,总算实现了一个时钟,作为新手入门还是可取的. 下文两个部分,一是纯粹的时钟,二是添加了一些颜色插件.bootst ...

  7. HTML5之Canvas

    一.canvas简介 ​ <canvas> 是 HTML5 新增的,一个可以使用脚本(通常为JavaScript)在其中绘制图像的 HTML 元素.它可以用来制作照片集或者制作简单(也不是 ...

  8. 用html5的canvas画布绘制贝塞尔曲线

    查看效果:http://keleyi.com/keleyi/phtml/html5/7.htm 完整代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...

  9. HTML5之Canvas标签简要学习

    HTML5 提供了画布(canvas)标签,通过与Javascript结合使用,可以在网页上绘制图像.Canvas是一个矩形区域,使用Javascript可以控制其每一个像素.本文将对canvas标签 ...

最新文章

  1. strstr函数头文件_C语言(函数)学习之strstr strcasestr
  2. centos7虚拟机网桥模式不通_CentOS7虚拟机桥接设置及问题
  3. 修改串口设备名ttymxc1_Modbus通讯之串口联网设备
  4. [教程]JS从糊涂到明白:一步一步编写计算器3 – 功能扩展
  5. Mysql错误1045解决方法
  6. Python 官方推荐的一款打包工具
  7. Android 自定义WebView弹窗及屏蔽弹窗
  8. 区分大小屏幕_第一个Python程序——在屏幕上输出文本
  9. vue设置多选框默认勾选_Vue实现全选和反选即Vue复选框增加全选功能
  10. android gridview滚动监听,检测GridView滚动速度 – Android
  11. 15 年老程序员自述:8 个影响我职业生涯的重要技能
  12. 计算机解决最小二乘法的过程,线性最小二乘法在计算机化学中的应用.doc
  13. FPGA基础入门【14】开发板VGA视频输出控制
  14. 植物大战僵尸变态辅助开发系列教程(E语言实现和VC6实现)(中)
  15. 有线广电如何转型和发展
  16. 西门子PLC能否实时无线采集多处从站模拟量数据?
  17. 如何用u盘安装win7系统安装步骤教程
  18. 物联网大学生就业培训有哪些?物联网工程师培训机构靠谱吗?
  19. LDA深入精讲(强力推荐)
  20. 欧拉线性筛法求素数(顺便实现欧拉函数的求值)

热门文章

  1. boost::process::cmd相关的测试程序
  2. boost::exception_detail::refcount_ptr的测试程序
  3. boost::describe模块宏BOOST_DESCRIBE_PP_PARSE的测试程序
  4. Boost:异步操作,需要boost :: asio :: async_compose函数的测试程序
  5. VTK:小部件之BorderWidget
  6. VTK:可视化之TensorGlyph
  7. VTK:Utilities之VectorArrayUnknownLength
  8. VTK:Utilities之PCAStatistics
  9. VTK:网格之SimpleElevationFilter
  10. VTK:图片之ImageExport