2012-12-27 25 views
1

我使用filltext()在画布上书写了文本。现在在另一个函数中,我想以字符串的形式检索该值。如果任何人都可以帮助我。 在此先感谢。如何检索作为字符串在画布上书写的文本

+0

你到目前为止试过的东西? – Hkachhia

+0

我不知道该怎么做。 –

+2

为什么?这就像是说:“我在打字机上打印了一页,并通过了复印机,现在我怎么把它读回文本文件?”。你不应该这样做。 –

回答

1

This w3c article might be of interest to you:

当作者呈现与fillText方法或strokeText, 在画布上的文字,他们还必须添加一个HTML元素(DIV或跨度)与相同的文本, 样式和位置的画布subdom。在 文字的边框应与setElementPath方法设置(见 http://www.w3.org/wiki/Canvas_hit_testing

这使得用户代理使用subdom文本提供的 访问体验,为subdom文本充当 代理在位图中呈现文本。

And here's an example built for accessibility:

<canvas id="example" width="260" height="200" role="img"> <p>A <a href="http://en.wikipedia.org/wiki/Rectangle">rectangle</a> with a black border. In the background is a pink circle. Partially overlaying the circle is a green square and a purple triangle, both of which are semi-opaque, so the full circle can be seen underneath.</p> </canvas> 

因此,所有你需要做的是,当用户输入文本时,将其存储在canvas元素内一个div内。

3

将文本写入画布时,还要将自定义属性添加到包含文本本身的画布对象。当你想检查它时,只需查询该属性即可。所以:

myCanvas.getContext("2d").filltext(theText,x,y,maxWidth); 
myCanvas.addedText = theText; 

后来,

var textToCheck = myCanvas.addedText;