KonvaJs Jquery Canvas上的图像大小调整问题

sg3maiej  于 2023-10-17  发布在  jQuery
关注(0)|答案(1)|浏览(284)

我正面临着一个问题,图像覆盖图像与背景图像使用KonvaJs JQuery。我想只在图像上设置分辨率,而不是整个图像的大小。请查看随附的屏幕截图。这将帮助你更容易理解。我需要你帮忙解决这个问题。Resize KonvaJs Image我期待的结果是这样的。Expected KonvaJs Resize ImageOverlay ImageBackground Image画布图像-Canvas Resize Image谢谢
我试过这个代码。

// Create a Konva.js stage
        var stage = new Konva.Stage({
          container: 'canvas-container',
          width: 712,
          height: 575,
        });

        // Create a Konva.js layer
        var layer = new Konva.Layer();
        stage.add(layer);

        // Load the background image (non-resizable)
        var backgroundImg = new Image();
        backgroundImg.src = customBgImagePath;

        backgroundImg.onload = function () {
          var background = new Konva.Image({
            image: backgroundImg,
            width: 712,
            height: 575,
          });

          layer.add(background);

          // Load the overlay image (resizable)
          var overlayImg = new Image();
          overlayImg.src = removedBgImagePath;

          overlayImg.onload = function () {
            var overlay = new Konva.Image({
              image: overlayImg,
              x: stage.width() / 2 - overlayImg.width / 2, // Center horizontally
              y: stage.height() / 2 - overlayImg.height / 2, // Center vertically
              draggable: true, // Enable drag-and-drop
            });

            layer.add(overlay);
            stage.draw();

            // Create a Konva.js Transformer for resizing only the overlay image
            var tr = new Konva.Transformer({
              nodes: [overlay],
              //keepRatio: true,
              //ignoreStroke: true
            });

            tr.hide();
            layer.add(tr);

            // When the image is clicked
            overlay.on('click', function () {
              // Toggle the Transformer's visibility and interactivity
              tr.visible(!tr.visible());
              tr.enabled(tr.visible());
              layer.draw();
            });

            stage.on('click', function (event) {
              // Hide the Transformer if the user clicks anywhere else on the stage
              tr.visible(false);
              tr.enabled(false);
              layer.draw();
            });

            // Enable resizing for the overlay image
            tr.on('transform', function () {
              layer.batchDraw(); // Redraw the layer ,
            });

            /* dataURL = stage.toDataURL({ pixelRatio: 3 });*/
            layer.Draw();

            //// Redraw the canvas on drag, drop, and resize
            //overlay.on('dragmove', function () {
            //  layer.draw();
            //});

            //overlay.on('transform', function () {
            //  layer.draw();
            //});

          };
        };
g6baxovj

g6baxovj1#

将代码更新为更现代的JS(const而不是var),并在声明图像加载处理程序之后移动图像src的赋值,这是一个很好的实践。还删除了stage.draw和layer.draw和batchDraw的不必要调用,因为它们现在都内置在konva中。并删除了Transformer.visible的使用--管理Transformer.nodes()列表更容易。
这里有一个语法错误layer.Draw()-即使你需要这个,它也是layer.draw()。
最后添加了cancelBubble上点击的图像。没有这一点,点击传播到阶段,使问题复杂化。
总结-它的工作原理。我误解了这个问题吗?

const customBgImagePath = 'https://assets.codepen.io/255591/bgsvg.svg'

const removedBgImagePath = "https://images.unsplash.com/photo-1588899357441-7f613216191b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2OTU2NTM2NDh8&ixlib=rb-4.0.3&q=80&w=400"

// Create a Konva.js stage
var stage = new Konva.Stage({
  container: 'canvas-container',
  width: 712,
  height: 575,
});

// Create a Konva.js layer
const layer = new Konva.Layer();
stage.add(layer);

// Load the background image (non-resizable)
const backgroundImg = new Image();

backgroundImg.onload = function() {
  const background = new Konva.Image({
    image: backgroundImg,
    width: 712,
    height: 575,
  });

  layer.add(background);

  // Load the overlay image (resizable)
  const overlayImg = new Image();

  overlayImg.onload = function() {
    const overlay = new Konva.Image({
      image: overlayImg,
      x: stage.width() / 2 - overlayImg.width / 2, // Center horizontally
      y: stage.height() / 2 - overlayImg.height / 2, // Center vertically
      draggable: true, // Enable drag-and-drop
    });

    layer.add(overlay);
    stage.draw();

    // Create a Konva.js Transformer for resizing only the overlay image
    var tr = new Konva.Transformer({
      nodes: [overlay],
      keepRatio: true,
      ignoreStroke: true
    });

    //tr.hide();
    layer.add(tr);

    // When the image is clicked
    overlay.on('click', function(evt) {
      if (tr.nodes().length > 0) {
        tr.nodes([])
      } else {
        tr.nodes([overlay])
      }
      // cancel the event from bubbling to the layer and stage
      evt.cancelBubble = true;
    });

    stage.on('click', function(event) {
      tr.nodes([])
    });

  };

  // Set the src after declating the onload handler
  overlayImg.src = removedBgImagePath;

};

// Set the src after declating the onload handler
backgroundImg.src = customBgImagePath;
<head>
  <!-- USE DEVELOPMENT VERSION -->
  <script src="https://unpkg.com/[email protected]/konva.min.js"></script>
  <meta charset="utf-8" />
  <title>o</title>
  <style>
    body {
      margin: 0;
      padding: 0;
      overflow: hidden;
      background-color: #f0f0f0;
    }
  </style>
</head>

<body>
  <div id="canvas-container"></div>

</body>

相关问题