javascript - Kineticjs 组图像拖动

标签 javascript jquery html5-canvas kineticjs konvajs

我是 Kinetic js 的新手,在弄清楚如何阻止图像移动到边界之外然后创建空白区域时遇到了问题

到目前为止,这是我的代码

//photo layer
var photoGroup = new Kinetic.Group({ x: layout.photoGroup.x, y: layout.photoGroup.y, draggable: true });

var Photolayer = new Kinetic.Layer();

var cropArea = new Kinetic.Rect({
     x: layout.cropAreaRect.x,
     y: layout.cropAreaRect.y,
     width: layout.cropAreaRect.width,
     height: layout.cropAreaRect.height,
     fill: "white",
     listening: false
});

Photolayer.add(cropArea);
Photolayer.add(photoGroup);

stage.add(Photolayer);

最佳答案

这里是一个演示 dragBoundFunc() 的工作代码片段。

金色矩形是我们打算限制拖动的区域。蓝色矩形显示我们计算的区域 - 与金色矩形不同,因为我们使用可拖动对象的左上角作为我们数学的基础,我们必须考虑它自己的宽度和高度。红色 block 可以是图像或任何 Konvajs 元素。

拖动红色方 block !

// add a stage
var s = new Konva.Stage({
  container: 'container',
  width: 800,
  height: 600
});

// add a layer        
var l = new Konva.Layer();
s.add(l);

// Add a green rect to the LAYER just to show boundary of the stage.
var green = new Konva.Rect({stroke: 'lime', width: 800, height: 600, x: 0, y: 0});
l.add(green);


// Add a gold rect to the LAYER just to give some visual feedback on intended drag limits
var gold = new Konva.Rect({stroke: 'gold', width: 400, height: 200, x: 55, y: 55});
l.add(gold);


// Add a red rect to act as our draggable
var red = new Konva.Rect({fill: 'red',  stroke: 'red', width: 40, height: 50, x: 65, y: 65, draggable: true,
        dragBoundFunc: function(pos) {
        
                  var newX = pos.x;
                  if (newX < minX){ newX = minX};
                  if (newX > maxX){ newX = maxX};

                  var newY = pos.y;
                  if (newY < minY){ newY = minY};
                  if (newY > maxY){ newY = maxY};

                  $("#info").html('Info: Pos=(' + newX + ', ' + newY + ') range X=' + minX + ' - ' + maxX + ' Y=' + minY + ' - ' + maxY);
                  
          return {
                x: newX,
                y: newY
            }
        }

});
l.add(red);


// calculate the drag boundary once for performance - we need to have the draggable element size for this !
var goldPos = gold.getAbsolutePosition() 
var minX = goldPos.x;
var maxX = goldPos.x + gold.width() - red.width();
var minY = goldPos.y;
var maxY = goldPos.y + gold.height() - red.height();


// Add a blue rect to the LAYER just show the computed drag limits - note the subtraction of the draggable element dimensions from maxX & Y. Note the 1px adjustment is to avoid overlapping the gold rect in the demo and is not required for live.
var blue = new Konva.Rect({stroke: 'blue', opacity: 0.2, width: 399 - red.width(), height: 199 - red.height(), x: 56, y: 56});
l.add(blue);

// bring red back to top so we can drag it
red.moveToTop();

l.draw(); // redraw the layer it all sits on.
#container {
  border: 1px solid #ccc;
}
#info {
height: 20px;
border-bottom: 1px solid #ccc;
}
#hint {
font-style: italic;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/konvajs/konva/1.6.3/konva.min.js"></script>


<body>
<div id='hint'>Hint: green is stage, gold is intended drag limit, blue is computed drag limit.<br/>Drag the red block!</div>

<div id='info'>Info:</div>

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

关于javascript - Kineticjs 组图像拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44500868/

相关文章:

javascript - apollo graphql 传递参数来解析函数

javascript - jquery 没有在 wordpress/网页上加载

Javascript nth-child 不工作

reactjs - React-konva 上传后如何更改图像?

javascript - Canvas 矩形背景图像

javascript - 在使用 create-react-app 的 React 应用程序中填充 ES6 功能的最佳方法

javascript - 我正在编写一个 Javascript 函数来为表格中的每一行涂上不同的颜色。为什么我的代码不起作用?

javascript - RequireJS, $.Deferred 怎么办?

javascript - Canvas 绘图 : Await for images to load in a recursive method

javascript - 序列化表单以便通过 Ajax 发送它