首页 > 解决方案 > Konvajs - 相对放大到指针,但非相对缩小

问题描述

我正在使用 Konva 画布框架,我想用鼠标滚轮实现放大和缩小。放大应该相对于我的光标指针,但缩小应该始终缩小到初始状态。

<!DOCTYPE html>
<html>
  <head>
    <script src="https://unpkg.com/konva@7.2.2/konva.min.js"></script>
    <meta charset="utf-8" />
    <title>Konva Zoom Relative to Stage Demo</title>
    <style>
      body {
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #f0f0f0;
      }
    </style>
  </head>

  <body>
    <div id="container"></div>
    <script>
      var width = window.innerWidth;
      var height = window.innerHeight;

      var stage = new Konva.Stage({
        container: 'container',
        width: width,
        height: height,
      });

      var layer = new Konva.Layer();
      stage.add(layer);

      var circle = new Konva.Circle({
        x: stage.width() / 2,
        y: stage.height() / 2,
        radius: 50,
        fill: 'green',
      });
      layer.add(circle);

      layer.draw();

      var scaleBy = 0.95;
      stage.on('wheel', (e) => {
        e.evt.preventDefault();
        var oldScale = stage.scaleX();

        var pointer = stage.getPointerPosition();

        var mousePointTo = {
          x: (pointer.x - stage.x()) / oldScale,
          y: (pointer.y - stage.y()) / oldScale,
        };

        var newScale =
          e.evt.deltaY > 0 ? oldScale * scaleBy : oldScale / scaleBy;

        stage.scale({ x: newScale, y: newScale });

        var newPos = {
          x: pointer.x - mousePointTo.x * newScale,
          y: pointer.y - mousePointTo.y * newScale,
        };
        stage.position(newPos);
        stage.batchDraw();
      });
    </script>
  </body>
</html>

因此,在示例中,您可以看到中心有一个绿色圆圈。当客户端放大时,我希望缩放相对于指针。但是,在缩小时,缩放不会相对于指针,而只是回到初始状态(中心的绿色圆圈)。

标签: javascriptkonva

解决方案


所以我改变了一些东西,只是为了演示目的——但概念是一样的:

<!DOCTYPE html>
<html>
  <head>
    <script src="https://unpkg.com/konva@7.2.2/konva.min.js"></script>
    <meta charset="utf-8" />
    <title>Konva Zoom Relative to Stage Demo</title>
    <style>
      body {
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #f0f0f0;
      }
    </style>
  </head>

  <body>
    <div id="container"></div>
    <script>
      var width = window.innerWidth;
      var height = window.innerHeight;

      var stage = new Konva.Stage({
        container: 'container',
        width: width,
        height: height,
      });

      var layer = new Konva.Layer();
      stage.add(layer);

      var circle = new Konva.Rect({
x: stage.width()/2,
        y:stage.height()/2,
        width: 50,
        height: 50,
        fill: 'green',
      });
      layer.add(circle);

      layer.draw();

      var scaleBy = 0.99;
      stage.on('wheel', (e) => {
        e.evt.preventDefault();
        var oldScale = stage.scaleX();

        var mousePointTo = {
          x: stage.getPointerPosition().x / oldScale - stage.x() / oldScale,
          y: stage.getPointerPosition().y / oldScale - stage.y() / oldScale
        };

        var newScale =
          e.evt.deltaY > 0 ? oldScale * scaleBy : oldScale / scaleBy;
        newScale = Math.max( newScale, 1 );
        stage.scale({ x: newScale, y: newScale });

        var newPos = {
          x:
            -(mousePointTo.x - stage.getPointerPosition().x / newScale) *
            newScale,
          y:
            -(mousePointTo.y - stage.getPointerPosition().y / newScale) *
            newScale
        };
        newPos.x = Math.min( newPos.x, 0 );
        newPos.x = Math.max( newPos.x, width * ( 1 - newScale ) );
        newPos.y = Math.min( newPos.y, 0 );
        newPos.y = Math.max( newPos.y, height * ( 1 - newScale ) );
        stage.position(newPos);
        stage.batchDraw();
      });
    </script>
  </body>
</html>


推荐阅读