在单个div中堆叠两个嵌入式SVG

时间:2019-04-10 19:05:49

标签: javascript html css svgpanzoom

我有一对以编程方式生成的复杂的黑白svg文件。 FWIW代表电路板设计的两个修订版。

这是两个例子

https://pastebin.com/uh6TtP0m

https://pastebin.com/MqareMLv

我试图在视觉上进行比较,如果将feMatrix过滤器应用于这些文件中的每一个并覆盖它们,则可以了解这两个文件的不同之处。

screenshot of filtered and superimposed example

我已经使用pan-zoom-svg使用这种策略以 separate div的步长缩放/平移两个版本,以便可以将两个图像缩放到相同的关注区域。

我希望能够平移和缩放组合的图像。为了达到这个目的,我尝试将两个具有固定属性的svg放置在样式表中,以使其对齐。封闭的div具有相对属性。图像位于嵌入标签中(我也尝试过对象标记。pan-zoom-svg不支持处理图像标记的svgs)。

组合后的图像完全符合我的要求-我可以根据需要平移和缩放它,但是由于溢出,我无法将其放置在最终网页的div中。

我不清楚这是我的代码中的缺陷还是svg-pan-zoom的限制。对单个svg而不是两个副本使用相同的技术非常有效。

我正在以编程方式为多个修订生成比较页面。

两个经过过滤的图像都对齐并且可以正确平移和缩放,但是,我似乎无法将它们放置在div中以用我网页的其余部分进行样式设置。

[编辑以删除隐藏某些文本的错误标记格式]

window.onload = function() {
  // Expose variable to use for testing
  window.zoomBoard = svgPanZoom('#diff', {
    zoomEnabled: true,
    controlIconsEnabled: true,
  });

  // Expose variable to use for testing
  window.zoomBoard2 = svgPanZoom('#diff2', {
    zoomEnabled: true,
    controlIconsEnabled: true,
  });

  zoomBoard.setOnZoom(function(level) {
    zoomBoard2.zoom(level)
    zoomBoard2.pan(zoomBoard.getPan())
  })

  zoomBoard.setOnPan(function(point) {
    zoomBoard2.pan(point)
  })

  zoomBoard2.setOnZoom(function(level) {
    zoomBoard.zoom(level)
    zoomBoard.pan(zoomBoard2.getPan())
  })

  zoomBoard2.setOnPan(function(point) {
    zoomBoard.pan(point)
  })
};
.responsivefull {
  padding: 5 5px;
  width: 100%;
  margin: 3px 0;
  position: relative;
}

.diff1filter {
  filter: url(#f1);
}

.diff2filter {
  filter: url(#f2);
}

.lock {
  position: fixed;
  top: 1px;
  right: 3px;
  outline: #ddd;
}
<script src="https://cdn.jsdelivr.net/npm/svg-pan-zoom@3.6.0/dist/svg-pan-zoom.min.js"></script>

<body>
  <div class="title">ThermocoupleLogger</div>
  <div class="subtitle">F_Cu</div>


  <svg width="0" height="0" viewBox="0 0 600 400" xmlns="http://www.w3.org/2000/svg" version="1.1">
        <defs>
            <filter id="f1" x="0%" y="0%" width="100%" height="100%">
                <feColorMatrix result="original" id="c1" type="matrix" values="1   0   0   0   0
                                                0   1   0   1   0
                                                0   0   1   1   0
                                                0   0   0   1  0 " />
            </filter>
            <filter id="f2" x="0%" y="0%" width="100%" height="100%">
                <feColorMatrix result="original" id="c2" type="matrix" values="1   0   0   1   0
                                        0   1   0   0   0
                                        0   0   1   0   0
                                        0   0   0   0.5   0" />
            </filter>
        </defs>
    </svg>

  <div class="responsivefull">
    <embed id="diff" class="diff1filter lock" type="image/svg+xml" src="https://pastebin.com/raw/uh6TtP0m" style="position:absolute;" />
    <embed id="diff2" class="diff2filter lock" type="image/svg+xml" src="https://pastebin.com/raw/MqareMLv" style="position:absolute;" />
  </div>

1 个答案:

答案 0 :(得分:0)

通过混合使用filterblend-mode可以很容易地发现差异。

<!doctype html>

<html>

<head>
  <title>Spot differences in SVGs</title>

  <style>
    :root {
      --zoom-factor: 300%;
      --pan: 0px;
      --tilt: 0px;
    }

    html, body {
      margin: 0;
      padding: 0;
    }

    .overlapping-images-container {
      position: relative;
      padding: 1rem;
      height: 100vh;
      width: 100%;
      box-sizing: border-box;
    }

    .overlapping-images-container::before,
    .overlapping-images-container::after {
      position: absolute;
      content: '';
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      background-repeat: no-repeat;

      background-size: var(--zoom-factor);
      background-position-y: var(--tilt);
      background-position-x: var(--pan);
    }

    .overlapping-images-container::before {
      background-image: url('F_Cu_3c9fec.svg');
      filter: invert(1);
      z-index: 1;
    }
    .overlapping-images-container::after {
      background-image: url('F_Cu_340edd.svg');
      mix-blend-mode: exclusion;
      z-index: 2;
    }

  </style>
</head>

<body>

  <div class="overlapping-images-container"></div>

</body>

</html>

http://erebaltor.se/rickard/stackoverflow/overlappingSVGs.html

我添加了CSS变量,您可以通过javascript或控制台进行操作,以便您可以缩放图像,以及左右左右移动图像。

Example of how it can look like