简体   繁体   English

如何将DIV文本转换为HTML5画布绘图

[英]How to convert DIV text to HTML5 canvas drawing

I'm working on an application that needs to convert an HTML "workspace" (fixed size and absolutely positioned objects containing text and/or images) into an HTML5 canvas drawing. 我正在开发一个需要将HTML“工作区”(固定大小和包含文本和/或图像的绝对定位对象)转换为HTML5画布图形的应用程序。 Due to the implementation of the canvas drawing methods, such as .fillText(), I'm having a really hard time adding the text of the elements to the canvas. 由于画布绘制方法(例如.fillText())的实现,我很难将元素的文本添加到画布中。 That is, I cannot get the text on the canvas to have the same size as the text within the original workspace objects, despite the fact that I'm retrieving all the used CSS rules for it: 也就是说,尽管我正在为其检索所有使用的CSS规则,但我无法使画布上的文本具有与原始工作空间对象中的文本相同的大小:

c.font = obj.title.fontStyle+' '+obj.title.fontWeight+' '+obj.title.fontSize+'px + obj.title.fontFamily;

// c = drawing context

By the way, the font-size for the elements is provided in pixels. 顺便说一下,元素的字体大小以像素为单位。

The app is located at http://picselbocs.com/projects/goalcandy/ . 该应用程序位于http://picselbocs.com/projects/goalcandy/ You'll need to log in using 'demo@demo.com' for the email input and 'demo' as the password, then add an element that has a title from the left to the workspace, and aftward press "Convert to PNG". 您需要使用“ demo@demo.com”作为电子邮件输入并以“ demo”作为密码登录,然后将一个具有标题的元素添加到工作区中,并向后按下“转换为PNG” 。 You'll notice how the text size increases. 您会注意到文本大小如何增加。

How can I get around this frustrating problem? 我如何解决这个令人沮丧的问题?

As interesting as the problem might sound, it was in fact completely idiotic! 问题听起来可能很有趣,但实际上完全是愚蠢的! Thus, the answer was unbelievably simple once the problem detected and the solution, applicable in 2 seconds (literally). 因此,一旦发现问题和解决方案,答案将非常简单,只需2秒钟(字面意义)。 The problem was not a cross-browser issue, but rather Chrome's zoom feature. 问题不是跨浏览器问题,而是Chrome的缩放功能。 I had previously zoomed in on the window inside which I opened the application, without knowing about it, which in turn rendered the canvas drawings blurry. 以前,我在不知道它的情况下放大了打开应用程序的窗口,这又使画布图纸变得模糊。 So the solution: zoom out! 因此,解决方案:缩小! %#$%# $@~!@ %#$%# $ @〜!@

This is a limitation of the canvas drawing algorithm and it seems you can't really get around this easily. 这是画布绘制算法的局限性,看来您真的无法轻松解决这个问题。

See this question for reference: Subpixel anti-aliased text on HTML5's canvas element 请参阅以下问题以供参考: HTML5的canvas元素上的亚像素抗锯齿文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM