问题
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