为什么我的HTML5 / JS应用程序不能在我的iPhone上运行

Why Wont My HTML5/JS App Work On My iPhone?

本文关键字:我的 iPhone 运行 不能 应用程序 HTML5 JS 为什么      更新时间:2023-09-26

伙计们怎么样?

最近,在这个网站的帮助下,我学会了如何通过单击按钮在 HTML5 画布上绘制一个矩形......这不是问题:)这是问题...不幸的是,当我尝试在 iPhone 上使用它时,它根本不起作用......为什么:(?


这是我的代码:


JAVASCRIPT:

// "Rectangle" Button
function rect()
{
var canvas = document.getElementById('canvasSignature'),
    ctx = canvas.getContext('2d'),
    rect = {},
    drag = false;

    function init() {
  canvas.addEventListener('mousedown', mouseDown, false);
  canvas.addEventListener('mouseup', mouseUp, false);
  canvas.addEventListener('mousemove', mouseMove, false);
}

function mouseDown(e) {
  rect.startX = e.pageX - this.offsetLeft;
  rect.startY = e.pageY - this.offsetTop;
  drag = true;
}

function mouseUp() {
  drag = false;
}

function mouseMove(e) {
  if (drag) {
    rect.w = (e.pageX - this.offsetLeft) - rect.startX;
    rect.h = (e.pageY - this.offsetTop) - rect.startY ;
    draw();
  }
}

function draw() {
  ctx.fillRect(rect.startX, rect.startY, rect.w, rect.h);
}

init();
}

HTML5:

<div id="canvasDiv">
    <canvas id="canvasSignature" width="580px" height="788px" style="border:2px solid #000; background: #FFF;"></canvas>
</div>
<div id="rect">  
    <p><button onclick="rect();">Rectangle</button></p>
</div>

任何帮助将不胜感激:)

首先你的代码有误你用这个星星

rect() {

并仅在脚本末尾关闭它

在触摸设备上,我认为您需要使用触摸*事件

像这样的东西

var canvas = document.getElementById('canvasSignature'), ctx = canvas.getContext('2d'), rect = {}, drag = false;
function init() {
  canvas.addEventListener("touchstart", touchHandler, false);
  canvas.addEventListener("touchmove", touchHandler, false);
  canvas.addEventListener("touchend", touchHandler, false);
}

function touchHandler(event) {
  if (event.targetTouches.length == 1) { //one finger touche
    var touch = event.targetTouches[0];
    if (event.type == "touchstart") {
      rect.startX = touch.pageX;
      rect.startY = touch.pageY;
      drag = true;
    } else if (event.type == "touchmove") {
      if (drag) {
        rect.w = touch.pageX - rect.startX;
        rect.h = touch.pageY - rect.startY ;
        draw();
      }
    } else if (event.type == "touchend" || event.type == "touchcancel") {
      drag = false;
    }
  }
}
function draw() {
  ctx.fillRect(rect.startX, rect.startY, rect.w, rect.h);
}
init();