iPhone上的某些移动浏览器无法正确执行Javascript

Certain mobile browsers on iPhone not executing Javascript properly

本文关键字:执行 Javascript 浏览器 移动 iPhone      更新时间:2023-09-26

我正在尝试运行从我在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 资源限制