使用JavaScript动画属性值|放大SVG viewBox

时间:2017-09-05 17:37:00

标签: javascript html svg coordinates zoom

我们有一个用JavaScript生成的SVG网格。

当用户双击网格上的任何坐标并在前一个缩放状态和当前缩放状态之间进行短动画过渡时,目标是2倍缩放SVG网格。除了一个问题之外,这实际上在我的代码段中几乎100%正常工作:

我可以设置缩放级别的动画,但无法平滑地动画X和Y坐标转换。

查看下面的代码段(最好是全屏),然后双击网格几次。

'use strict'
function zoom( evt ){
  var loc = getCoords( evt ),
      newX = loc.x / 0.8 - 12.5,
      newY = loc.y / 0.8 - 12.5,
      grid = document.getElementById( 'grid' ),
      viewBoxAttr = grid.getAttribute( 'viewBox' ),
      viewBoxAry = viewBoxAttr.split( ' ' ),
      curX = viewBoxAry[ 0 ], curY = viewBoxAry[ 1 ],
      curZm = viewBoxAry[ 2 ], dblZm = curZm / 2,
      tweenZm = curZm, diffX = 0,
      interval = setInterval( 
        function(){
          if( tweenZm >= dblZm ){
            tweenZm = tweenZm / 1.015625;
            diffX = newX - curX;
          }
          else {
            clearInterval( interval );
          }
          zmOnPt( newX, newY, tweenZm );
        },
        10
      ),
      ary = [];
  
  ary.push( curZm );
  ary.push( dblZm );
}

var grid = document.getElementById( 'grid' );
grid.addEventListener( 'dblclick', zoom );

createLines( '.h-lns' ); createLines( '.v-lns' );
createLabels( '.h-num' ); createLabels( '.v-num' );
recalibrate();
<head>
  <link id="main" rel="stylesheet" 
         href="https://codepen.io/basement/pen/brJLLZ.css"
  >
  <link id="animations" rel="stylesheet"
        href="https://codepen.io/basement/pen/zdXRWo.css"
  >
</head>
<body id="body">
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" class="cntr" id="grid">
    <script id="injectGrid" xlink:href="https://codepen.io/basement/pen/brJLLZ.js">
    </script> 
    <g id="drawing">  
      <circle cx="60" cy="40" r="0.5" fill="#0dd" opacity="0.9" />
      <circle cx="70" cy="40" r="0.5" fill="#0dd" opacity="0.9" />
      <path
        fill="none" opacity="0.5" stroke="#0dd" stroke-width="0.5"
        d="
          M60, 40
          A10, 10
           0,
           0, 1
           70, 50
          C70, 55
           65, 60
           60, 60
          Q50, 60
           50, 50
          T55, 35
          T70, 40
        "
      />      
    </g>
  </svg>
  <script id="sidebar" src="https://codepen.io/basement/pen/zdXRWo.js"></script>
  <script id="main" src="https://codepen.io/basement/pen/yorjXq.js"></script>
</body>

注意平滑的缩放动画加上震动的x和y平移? viewBox只是跳到你点击的X和Y坐标,而没有动画到它。然后它放大现在居中的坐标。

目标是让x和y通过缩放平滑过渡。

我隐藏了许多我认为与此代码段链接到codepen的单独文件无关的代码。如果你想看到没有复制和粘贴源代码的那些,这里有一个列表:

主要CSS: https://codepen.io/basement/pen/brJLLZ.css

动画CSS: https://codepen.io/basement/pen/zdXRWo.css

GRID CREATION JS: https://codepen.io/basement/pen/brJLLZ.js

SIDEBAR CODE: https://codepen.io/basement/pen/zdXRWo.js

MAIN JAVASCRIPT: https://codepen.io/basement/pen/yorjXq.js

1 个答案:

答案 0 :(得分:1)

您的缩放功能似乎不必要地复杂化。它看似任意的方程常数,我不明白,你正在以一种我看不到目的的方式操纵坐标。

对于下面的版本,我只是将viewBox的宽度和高度减半,然后将其居中于单击鼠标的坐标上。然后,对于动画,我只是从旧的viewBox值到新的值进行线性插值。

function zoom( evt ) {
  var loc = getCoords( evt ),
      grid = document.getElementById( 'grid' ),
      viewBoxAttr = grid.getAttribute( 'viewBox' ),
      viewBoxAry = viewBoxAttr.split(' ');
  var oldX = parseFloat(viewBoxAry[0]);
  var oldY = parseFloat(viewBoxAry[1]);
  var oldWidth = parseFloat(viewBoxAry[2]);
  var oldHeight = parseFloat(viewBoxAry[3]);
  var newWidth = oldWidth / 2;  // Halving the view width => zoom X2
  var newHeight = oldHeight / 2;
  var newX = loc.x - newWidth / 2;
  var newY = loc.y - newHeight / 2;
  var animProgress = 0;  // Goes from 0 to 1
  var animStep = 0.02;   // Change in animProgress per interval function invocation.

  var interval = setInterval( function() {
    animProgress += animStep;
    if (animProgress > 1)
      animProgress = 1;
    // Calculate a new viewBox corresponding to our animation progress
    var nextViewBox = [
      oldX + animProgress * (newX - oldX),
      oldY + animProgress * (newY - oldY),
      oldWidth + animProgress * (newWidth - oldWidth),
      oldHeight + animProgress * (newHeight - oldHeight)
    ];
    grid.setAttribute("viewBox", nextViewBox.join(' '));
    if (animProgress >= 1)
      clearInterval( interval );
  }, 10);
}

&#13;
&#13;
'use strict'
function zoom( evt ) {
  var loc = getCoords( evt ),
      grid = document.getElementById( 'grid' ),
      viewBoxAttr = grid.getAttribute( 'viewBox' ),
      viewBoxAry = viewBoxAttr.split(' ');
  var oldX = parseFloat(viewBoxAry[0]);
  var oldY = parseFloat(viewBoxAry[1]);
  var oldWidth = parseFloat(viewBoxAry[2]);
  var oldHeight = parseFloat(viewBoxAry[3]);
  var newWidth = oldWidth / 2;
  var newHeight = oldHeight / 2;
  var newX = loc.x - newWidth / 2;
  var newY = loc.y - newHeight / 2;
  var animProgress = 0;  // Goes from 0 to 1
  var animStep = 0.02;   // Change in animProgress per interval function invocation.
  
  var interval = setInterval( function() {
    animProgress += animStep;
    if (animProgress > 1)
      animProgress = 1;
    // Calculate a new viewBox corresponding to our animation progress
    var nextViewBox = [
      oldX + animProgress * (newX - oldX),
      oldY + animProgress * (newY - oldY),
      oldWidth + animProgress * (newWidth - oldWidth),
      oldHeight + animProgress * (newHeight - oldHeight)
    ];
    grid.setAttribute("viewBox", nextViewBox.join(' '));
    if (animProgress >= 1)
      clearInterval( interval );
  }, 10);
}

var grid = document.getElementById( 'grid' );
grid.addEventListener( 'dblclick', zoom );

createLines( '.h-lns' ); createLines( '.v-lns' );
createLabels( '.h-num' ); createLabels( '.v-num' );
recalibrate();
&#13;
<head>
  <link id="main" rel="stylesheet" 
         href="https://codepen.io/basement/pen/brJLLZ.css"
  >
  <link id="animations" rel="stylesheet"
        href="https://codepen.io/basement/pen/zdXRWo.css"
  >
</head>
<body id="body">
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" class="cntr" id="grid">
    <script id="injectGrid" xlink:href="https://codepen.io/basement/pen/brJLLZ.js">
    </script> 
    <g id="drawing">  
      <circle cx="60" cy="40" r="0.5" fill="#0dd" opacity="0.9" />
      <circle cx="70" cy="40" r="0.5" fill="#0dd" opacity="0.9" />
      <path
        fill="none" opacity="0.5" stroke="#0dd" stroke-width="0.5"
        d="
          M60, 40
          A10, 10
           0,
           0, 1
           70, 50
          C70, 55
           65, 60
           60, 60
          Q50, 60
           50, 50
          T55, 35
          T70, 40
        "
      />      
    </g>
  </svg>
  <script id="sidebar" src="https://codepen.io/basement/pen/zdXRWo.js"></script>
  <script id="main" src="https://codepen.io/basement/pen/yorjXq.js"></script>
</body>
&#13;
&#13;
&#13;