javascript - KineticJS Canvas - 从中​​心点缩放组

标签 javascript canvas html5-canvas kineticjs

我想扩展我的组(图像和其他东西)。

group.setScale(zoom, zoom);

http://jsfiddle.net/K8nK3/

但是当我扩展我的组时,我认为它不是从我的组中心开始扩展的。我认为是这样的
enter image description here
我希望它从中心开始缩放
enter image description here

我尝试了更多,但没有成功。我该怎么做,谢谢。

最佳答案

[已编辑以更好地满足提问者的需求]

以下是如何从中心点缩放动力学组

首先,我们存储原始组的 centerX 和 centerY,以便我们可以使组保持居中:

      group.cx=group.getX()+group.getWidth()/2;
      group.cy=group.getY()+group.getHeight()/2;

然后我们编写一个自定义缩放方法来缩放组并重新居中:

      group.scale=function(x,y){
          group.setScale(x,y);
          group.setPosition(
              group.cx-group.getWidth()/2*group.getScale().x,
              group.cy-group.getHeight()/2*group.getScale().y);
          group.draw();
      }

这是代码和 fiddle :http://jsfiddle.net/m1erickson/dVGGz/

<!DOCTYPE HTML>
<html>
  <head></head>
  <body>
    <button id="larger">Larger</button>
    <div id="container"></div>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.4.0.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <script>
    $(function(){
      var stage = new Kinetic.Stage({
        container: 'container',
        width: 400,
        height: 400
      });
      var layer = new Kinetic.Layer();

      // Be sure to set width and height
      // They are required for this method to work
      var group = new Kinetic.Group({
        x: 100,
        y: 100,
        width:100,
        height:100
      });
      // store the original group center
      // so we can center the group there
      group.cx=group.getX()+group.getWidth()/2;
      group.cy=group.getY()+group.getHeight()/2;
      // custom scale function to both
      // scale the group and center the results
      group.scale=function(x,y){
          group.setScale(x,y);
          group.setPosition(
              group.cx-group.getWidth()/2*group.getScale().x,
              group.cy-group.getHeight()/2*group.getScale().y);
          group.draw();
      }

      var box1 = new Kinetic.Rect({
        x: 0,
        y: 0,
        width: 50,
        height: 50,
        fill: "blue",
        stroke: 'black',
        strokeWidth: 4
      });
      group.add(box1);

      var box2 = new Kinetic.Rect({
        x: 50,
        y: 50,
        width: 50,
        height: 50,
        fill: "green",
        stroke: 'black',
        strokeWidth: 4
      });
      group.add(box2);

      layer.add(group);
      stage.add(layer);

      var scaleFactor=1;
      $("#larger").click(function(){
          scaleFactor+=0.10;
          group.scale(scaleFactor,scaleFactor);
          console.log(scaleFactor);
      });

});

    </script>
  </body>
</html>

关于javascript - KineticJS Canvas - 从中​​心点缩放组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15853630/

相关文章:

javascript - 使用函数将变量(id)从html传递到外部js文件

php - 带有复选框的表单输入字段,反之亦然 - 缺少第一个值

javascript - 我如何使用 WebGL 创建基于 float 的 RGBA 纹理?

javascript - for 循环不执行对象

python - 如何使用canvasvg库

javascript - 在 HTML 5 Canvas 上创建 Reuleaux 多边形的函数

javascript - 如何使图像完全适合 HTML5 Canvas ?

javascript - 我将如何通过嵌套函数将元素作为参数传递?

javascript - 如何以 Angular 8 显示转换后的值?

Javascript - 使用宽度和高度的百分比在 Canvas 上绘制