undefined TypeError only on iPhone — JavaScript, HTML5 canvas_context.measureText

本小妞迷上赌 提交于 2019-12-11 11:26:14

问题


I'm having an issue when running a webpage on iOS Safari.

I am using the canvas-text library to render text which works perfectly on the following:

  • Ubuntu/Windows -- chrome, firefox
  • Mac -- Safari
  • Android -- default, firefox, opera, dolphin

However, it does not work on iPhone.

I enabled the javascript console on the iPhone and I get a few errors which read :

Javascript: Error undefined

TypeError: 'undefined' is not an object

The console does not display line numbers, but by placing alert boxes throughout the code I was able to isolate where the problem occured. Here is the code which produces the error:

usedX = context.measureText(textUsername).width + context.measureText(textInput).width + paddingInput*2 + borderInput*2;

The problem exists within context.measureText. I know this because I have tried replacing the previous code with the following:

var usedX = context.measureText("why is this an error?");

and I still got the error on that line.

I know for sure that context and measureText are defined because I have done:

alert(context.measureText);

which alerted:

function measureText(){[native code]}

As I said, the problem seems to be in measureText. I have Googled lot's and could not find a solution.

I am a JavaScript noob, so I won't be all that surprised if there is a simple fix. But it seems all is well with the code, and this is a problem with iPhone's implementation of measureText.

来源:https://stackoverflow.com/questions/13301056/undefined-typeerror-only-on-iphone-javascript-html5-canvas-context-measurete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!