iPhone上的某些移动浏览器无法正确执行Javascript
Certain mobile browsers on iPhone not executing Javascript properly
我正在尝试运行从我在UIWebView中加载的URL返回的javascript。该脚本获取图像并在其上绘制一个点以显示用户位置。
在Mobile Safari,UIWebView和Google Chrome Mobile中,图像未正确生成,并且点位于错误的位置。
在Desktop Safari,Chrome,Opera Mini和我的应用程序的Android版本中,它工作正常。
我需要帮助让它在UIWebView/Mobile Safari中工作。
问题 1:在 zoomIn() 方法中调用 context.drawImage() 会在 Mobile Safari 的调试控制台中生成以下错误:"Javascript 错误。定义。INDEX_SIZE_ERR:DOM 异常 1:索引或大小为负数,或大于允许的值。
问题 2:在 zoomOut() 方法中,图像本身绘制得很好,但点不在正确的位置。
更新:从下面的代码中可以看出,我添加了控制台.log语句来打印出原始图像的宽度和高度。在有问题的浏览器中,这些值是它们应该值的一半。我现在正试图弄清楚为什么。
法典:
window.onload = function zoomIn() {
var canvas = document.getElementById("area");
var context = canvas.getContext("2d");
var imageObj = new Image();
var px = 1988;
var py = 734;
imageObj.src = "IMAGE URL GOES HERE";
imageObj.onload = function() {
canvas.width=640;
canvas.height=480;
if(px-canvas.width<0 || py-canvas.height<0){
canvas.width=500;
canvas.height=300;
}
console.log(imageObj.height);
console.log(imageObj.width);
context.drawImage(imageObj, px-canvas.width, py-canvas.height, canvas.width*2, canvas.height*2,0,0,canvas.width,canvas.height);
context.beginPath();
context.arc(canvas.width/2, canvas.height/2, 10, 0, 2 * Math.PI, false);
context.fillStyle = "red";
context.fill();
context.lineWidth = 3;
context.strokeStyle = "black";
context.beginPath();
context.font = "10pt Calibri";
context.fillStyle = "black"
context.textAlign = "center";
context.fillText("You Are Here", canvas.width/2, canvas.height/2+20);
context.stroke();
};
document.form1.button2.style.visibility="hidden"
document.form1.button1.style.visibility="visible";
};
function zoomOut(){
var canvas = document.getElementById("area");
var context = canvas.getContext("2d");
var imageObj = new Image();
var px = 1988;
var py = 734;
imageObj.src = "IMAGE URL GOES HERE";
imageObj.onload = function(){
canvas.width=imageObj.width
canvas.height=imageObj.height
context.drawImage(imageObj,0,0);
context.arc(px, py, 20, 0, 2 * Math.PI, false);
context.fillStyle = "red";
context.fill();
context.lineWidth = 3;
context.strokeStyle = "black";
context.beginPath();
context.rect(0, 0, canvas.width, canvas.height);
context.font = "15pt Calibri";
context.fillStyle = "black";
context.textAlign = "center";
context.fillText("You Are Here",px,py+25);
context.stroke();
};
document.form1.button1.style.visibility="hidden"
document.form1.button2.style.visibility="visible";
}
如果你读过我的整篇文章(不包括代码),你就会知道我最终发现iPhone上原始图像的尺寸是其他浏览器中的一半。
这是由于Apple对可下载的图像大小设置的限制引起的,即3百万像素。我的形象比这更大。但是Javascript引用了图像的某些部分(在iOS上)我们超出了它的界限。
来源:苹果 文档:了解 iOS 资源限制
相关文章:
- 无法在通过jQuery的ajax加载的页面中执行javascript
- 在Safari执行javascript之前对其进行修改
- 如何在执行此特定onclick事件时执行JavaScript函数
- 如何在从浏览缓存加载页面时执行javascript
- 使用Rhino和ASE执行Javascript的区别
- 如何在加载完整页面后严格执行javascript代码
- 在动态加载的对话框中执行Javascript
- 在Sinatra中执行Javascript
- 通过AJAX加载页面并执行javascript和CSS
- 适用于多种浏览器的Selenium-需要能够执行javascript的webDriver
- 如何在seleniumwebdriver中执行javascript提示并等待接受输入
- 在window.open()生成的窗口中执行JavaScript
- AJAX成功回调-执行javascript时出现问题
- 通过指令在控制器中执行javascript函数
- 从React Native Android原生地执行JavaScript代码
- iframe未执行Javascript方法(PHP)
- 执行JavaScript代码,使用selenium webdriver或WatiN从控制台获取日志
- 通过web驱动程序异步执行Javascript
- 如何在长时间执行JavaScript期间显示微调器
- 点击困难时执行javascript函数