在 iPad 上的网页上做笔记

Taking notes on a webpage on an iPad

提问人:Testa Maxima 提问时间:11/13/2023 最后编辑:Rory McCrossanTesta Maxima 更新时间:11/13/2023 访问量:47

问:

我目前正在编写我的代码,想知道如何创建一个盒子,让 iPad 用户可以用手指/笔在盒子里做笔记。我以为我在某个地方看到了它并试图实现它,但它不起作用,也找不到我看到它的网页。

我正在尝试谷歌并使用来自不同网页的各种帮助。预期的结果应该是,我可以用手指或笔在这个网页上画画并做笔记。

let painting = false;

const canvas = document.getElementById('signatureCanvas');
const ctx = canvas.getContext('2d');

function startPosition(e) {
  painting = true;
  draw(e);
}

function finishedPosition() {
  painting = false;
  ctx.beginPath();
}

function draw(e) {
  if (!painting) return;
  ctx.lineWidth = 2;
  ctx.lineCap = 'round';

  // Adjust for touch events
  let clientX = e.clientX || e.touches[0].clientX;
  let clientY = e.clientY || e.touches[0].clientY;

  ctx.lineTo(clientX - canvas.offsetLeft, clientY - canvas.offsetTop);
  ctx.stroke();
  ctx.beginPath();
  ctx.moveTo(clientX - canvas.offsetLeft, clientY - canvas.offsetTop);
}

function clearCanvas() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
}

canvas.addEventListener('mousedown', startPosition);
canvas.addEventListener('mouseup', finishedPosition);
canvas.addEventListener('mousemove', draw);

// Touch event listeners
canvas.addEventListener('touchstart', startPosition);
canvas.addEventListener('touchend', finishedPosition);
canvas.addEventListener('touchmove', draw);
.canvas-container {
  border: 1px solid #ccc;
  width: 100%;
}
<div class="container">
  <div class="row form-section centered-content" style="background-color:lavender;">
    <div class="col-md-12">
      <div class="canvas-container">
        <canvas id="signatureCanvas" width="300" height="150"></canvas>
      </div>
      <button type="button" class="btn btn-secondary mt-2" onclick="clearCanvas()">Notiz löschen</button>
    </div>
  </div>
</div>

JavaScript HTML CSS 画布

评论

1赞 Rory McCrossan 11/13/2023
你是说你当前的代码不起作用吗?它在浏览器中工作正常,因此我只能假设它是否与您正在使用的事件处理有关,而不是由触摸事件触发
0赞 Kristian 11/13/2023
也许这有帮助?stackoverflow.com/a/67155647/3706717这是我的旧答案之一
1赞 Greenylie 11/13/2023
我可以确认这@RoryMcCrossan所说的,对我来说,无论是在 Windows 上使用触摸和笔,还是在 android 上使用触摸。
0赞 Testa Maxima 11/13/2023
您好,感谢您的帮助,但代码有效。我只是没有更新我的代码,或者可能在我的代码中写了错误的东西。至于现在,它也以我选择的指定方式工作。非常感谢您的帮助!

答:

0赞 AztecCodes 11/13/2023 #1

调整:

  • 事件处理
  • 画布大小调整
  • 高 DPI 显示

代码的增强版本:

let painting = false;

const canvas = document.getElementById('signatureCanvas');
const ctx = canvas.getContext('2d');

// Adjust canvas for high DPI displays
function adjustCanvas() {
  const ratio = window.devicePixelRatio || 1;
  const width = canvas.offsetWidth * ratio;
  const height = canvas.offsetHeight * ratio;
  canvas.width = width;
  canvas.height = height;
  canvas.style.width = `${canvas.offsetWidth}px`;
  canvas.style.height = `${canvas.offsetHeight}px`;
  ctx.scale(ratio, ratio);
}
adjustCanvas();
window.onresize = adjustCanvas;

function startPosition(e) {
  // Prevents scrolling
  e.preventDefault();
  painting = true;
  draw(e);
}

function finishedPosition() {
  painting = false;
  ctx.beginPath();
}

function draw(e) {
  if (!painting) return;
  ctx.lineWidth = 2;
  ctx.lineCap = 'round';

  let clientX = e.clientX || e.touches[0].clientX;
  let clientY = e.clientY || e.touches[0].clientY;

  ctx.lineTo(clientX - canvas.getBoundingClientRect().left, clientY - canvas.getBoundingClientRect().top);
  ctx.stroke();
  ctx.beginPath();
  ctx.moveTo(clientX - canvas.getBoundingClientRect().left, clientY - canvas.getBoundingClientRect().top);
}

function clearCanvas() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
}

canvas.addEventListener('mousedown', startPosition);
canvas.addEventListener('mouseup', finishedPosition);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('touchstart', startPosition);
canvas.addEventListener('touchend', finishedPosition);
canvas.addEventListener('touchmove', draw);
.canvas-container {
  border: 1px solid #ccc;
  width: 100%;
  height: 300px;
  /* Adjust this as needed */
}
<div class="container">
  <div class="row form-section centered-content" style="background-color:lavender;">
    <div class="col-md-12">
      <div class="canvas-container">
        <canvas id="signatureCanvas"></canvas>
      </div>
      <button type="button" class="btn btn-secondary mt-2" onclick="clearCanvas()">Notiz löschen</button>
    </div>
  </div>
</div>

评论

0赞 Testa Maxima 11/13/2023
哇,这比我以前拥有的要好得多,非常感谢!然而,似乎有一个新问题。我不能使用画布的整个宽度,因为它让我只能使用画布的 75%,你知道可能有什么问题吗,这发生在这个和我的代码上。
0赞 Testa Maxima 11/13/2023
此外,绘图不在手指/鼠标本身上,而是有点偏移