如何使用touchmove捕获Y坐标?

huangapple go评论98阅读模式
英文:

How to capture Y coordinates with touchmove?

问题

以下是您提供的 JavaScript 代码的中文翻译部分:

  1. document.addEventListener('touchmove', (event) => {
  2. if (event.pageY < 0) {
  3. zoomLevel /= 1.1; // 缩小10%
  4. } else if (event.pageY > 0) {
  5. zoomLevel *= 1.1; // 放大10%
  6. }
  7. if (zoomLevel < 1) {
  8. zoomLevel = 1;
  9. } else if (zoomLevel > 5) {
  10. zoomLevel = 5;
  11. }
  12. drawImage();
  13. })
  14. function drawImage() {
  15. ctx.clearRect(0, 0, canvasWidth, canvasHeight);
  16. // 绘制缩放区域
  17. const imgWidth = img.width * zoomLevel;
  18. const imgHeight = img.height * zoomLevel;
  19. const imgX = canvasWidth / 2 - imgWidth / 2;
  20. const imgY = canvasHeight / 2 - imgHeight / 2;
  21. ctx.drawImage(img, imgX, imgY, imgWidth, imgHeight);
  22. }

希望这对您有所帮助。如果您需要进一步的帮助,请随时提问。

英文:

I'm trying to make a mobile application with html, javascript with canvas, basically scrolling down increases the zoom of an image and scrolling up decreases the zoom of the image, but I can only decrease the zoom because when doing scroll up keeps increasing the size and i want it to decrease the size.

Part of the javascript code:

  1. document.addEventListener(&#39;touchmove&#39;, (event)=&gt;{
  2. if (event.pageY &lt; 0) {
  3. zoomLevel /= 1.1; // zoom out by 10%
  4. } else if (event.pageY &gt; 0) {
  5. zoomLevel *= 1.1; // zoom in by 10%
  6. }
  7. if (zoomLevel &lt; 1) {
  8. zoomLevel = 1;
  9. } else if (zoomLevel &gt; 5) {
  10. zoomLevel = 5;
  11. }
  12. drawImage();
  13. })
  14. function drawImage() {
  15. ctx.clearRect(0, 0, canvasWidth, canvasHeight);
  16. // draw zoom area
  17. const imgWidth = img.width * zoomLevel;
  18. const imgHeight = img.height * zoomLevel;
  19. const imgX = canvasWidth / 2 - imgWidth / 2;
  20. const imgY = canvasHeight / 2 - imgHeight / 2;
  21. ctx.drawImage(img, imgX, imgY, imgWidth, imgHeight);
  22. }

答案1

得分: 1

要获取Delta Y,请使用以下代码进行解决,该代码会存储触摸位置。

  1. let zoomLevel = 1;
  2. document.addEventListener('touchstart', (event) => {
  3. y = event.touches[0].clientY;
  4. });
  5. document.addEventListener('touchmove', (event) => {
  6. const deltaY = event.touches[0].clientY - y;
  7. y = event.touches[0].clientY;
  8. zoomLevel = Math.min(5, Math.max(1, zoomLevel + deltaY * 0.01));
  9. drawImage();
  10. });
英文:

To get the Delta Y solve it with the following code, which stores the touch position.

  1. let zoomLevel = 1;
  2. document.addEventListener(&#39;touchstart&#39;, (event) =&gt; {
  3. y = event.touches[0].clientY;
  4. });
  5. document.addEventListener(&#39;touchmove&#39;, (event) =&gt; {
  6. const deltaY = event.touches[0].clientY - y;
  7. y = event.touches[0].clientY;
  8. zoomLevel = Math.min(5, Math.max(1, zoomLevel + deltaY * 0.01));
  9. drawImage();
  10. });

huangapple
  • 本文由 发表于 2023年5月7日 08:59:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定