简体   繁体   English

缩放后如何重置HTML5 canvas?

[英]How to reset HTML5 canvas after zooming?

I'm trying to create an HTML5-canvas based web app with zooming-to-cursor functionality.我正在尝试创建一个基于 HTML5 画布的 web 应用程序,它具有缩放到光标功能。 The idea is simple:这个想法很简单:

  • a user loads an image via standard HTML input;用户通过标准 HTML 输入加载图像;
  • the image gets centered on a canvas;图像以 canvas 为中心;
  • the user can zoom in/out the image to a cursor location;用户可以将图像放大/缩小到 cursor 位置;
  • when the user loads another image, canvas gets cleared and reset;当用户加载另一个图像时,canvas 被清除并重置;

I'm having troubles with the last step.我在最后一步遇到了麻烦。 When I load the first image, zoom in/out and then load the second image, I get the second image already zoomed to the scale of the previous image.当我加载第一张图片,放大/缩小然后加载第二张图片时,我得到的第二张图片已经缩放到前一张图片的比例。 I suppose the problem comes from trackTransforms function (took it there ), but I don't understand how it works, so I can't track down the issue.我想问题出在trackTransforms function(那里拿走了),但我不明白它是如何工作的,所以我无法找到问题所在。

Live code: https://jsfiddle.net/Arkonage/tgkv21s8/22/直播码: https://jsfiddle.net/Arkonage/tgkv21s8/22/

As a quick and dirty solution:作为一个快速而肮脏的解决方案:

  • Add ctx.save();添加ctx.save(); to the beginning of the trackTransforms() function definition.trackTransforms() function 定义的开头。
  • Add ctx.restore(); ctx.save();添加ctx.restore(); ctx.save(); ctx.restore(); ctx.save(); to the beginning of the resetCanvas() function definition.resetCanvas() function 定义的开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM