【问题标题】:Image is draggable only on x Axis图像只能在 x 轴上拖动
【发布时间】:2015-03-01 23:31:18
【问题描述】:

使用 kinetic-v4.7.2.min.js 库如何让图像只能在 X 轴上拖动?谁能帮我? 在这个fiddle,我想要水平拖动的房子图像。

HTML 代码:

<h5>Drag the icon from the toolbar to the Kinetic.Stage<br>This creates new Kinetic.Images</br>The images on the stage are draggable.</h5>
<div id="toolbar">
    <img id="house" width=32 height=32 src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house32x32transparent.png">
    <br>
</div>
<div id="container"></div>

JS代码:

// get a reference to the house icon in the toolbar
// hide the icon until its image has loaded
var $house = $("#house");
$house.hide();

// get the offset position of the kinetic container
var $stageContainer = $("#container");
var stageOffset = $stageContainer.offset();
var offsetX = stageOffset.left;
var offsetY = stageOffset.top;

// create the Kinetic.Stage and layer
var stage = new Kinetic.Stage({
    container: 'container',
    width: 350,
    height: 350
});
var layer = new Kinetic.Layer();
stage.add(layer);

// start loading the image used in the draggable toolbar element
// this image will be used in a new Kinetic.Image
var image1 = new Image();
image1.onload = function () {
    $house.show();
}
image1.src = "https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house32x32transparent.png";

// make the toolbar image draggable
$house.draggable({
    helper: 'clone',
});

// set the data payload
$house.data("url", "house.png"); // key-value pair
$house.data("width", "32"); // key-value pair
$house.data("height", "33"); // key-value pair
$house.data("image", image1); // key-value pair

// make the Kinetic Container a dropzone
$stageContainer.droppable({
    drop: dragDrop,
});

// hangle a drop into the Kinetic container
function dragDrop(e, ui) {

    // get the drop point
    var x = parseInt(ui.offset.left - offsetX);
    var y = parseInt(ui.offset.top - offsetY);

    // get the drop payload (here the payload is the image)
    var element = ui.draggable;
    var data = element.data("url");
    var theImage = element.data("image");

    // create a new Kinetic.Image at the drop point
    // be sure to adjust for any border width (here border==1)
    var image = new Kinetic.Image({
        name: data,
        x: x,
        y: y,
        image: theImage,
        draggable: true
    });
    layer.add(image);
    layer.draw();
}

【问题讨论】:

  • 如果只允许水平拖动,怎么能在自己下面的框中得到图像?
  • 不,我希望图像在放入框中后可以水平拖动
  • 你使用 'dragBoundFunc' 来限制拖动:dragBoundFunc:function(pos){ return { x: pos.x, y: this.getAbsolutePosition().y } }
  • @markE 为什么不将其发布为答案?这是解决方案...
  • @loxxy,感谢您的支持! :-) 通常我会在演示中发布完整的答案,但今晚我很累,所以我会留下评论。

标签: javascript html5-canvas kineticjs


【解决方案1】:

dragboundFunc可用于约束用户的拖动(例如,水平约束)

dragboundFunc 在对象内部声明:http://jsfiddle.net/m1erickson/LuZbV/

var image = new Kinetic.Image({
    name: data,
    x: x,
    y: y,
    image: theImage,
    draggable: true,

    // restrict to allow horizontal dragging only
    dragBoundFunc: function(pos) {
        return {
          x: pos.x,
          y: this.getAbsolutePosition().y
        }
    }        
});

【讨论】:

  • 感谢@markE...在,jsfiddle.net/gkefk/32 多个图像可以拖动...但是当我放下该图像时,我希望多个图像中只有一个图像被放置在特定位置。我该怎么做?帮帮我先生...
  • @BirunthaG,我不确定你现在在问什么,但我很确定这与水平拖动约束无关。您可能想提出一个新问题... ;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
  • 2019-12-06
  • 2019-09-15
  • 2023-04-05
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
相关资源
最近更新 更多