将图像拖入rect?

时间:2013-01-20 01:51:56

标签: drag-and-drop kineticjs

在Kineticjs中,我想将img拖动成矩形。

我想知道是否有人能告诉我该怎么做? 我知道如何拖动形状或图像,但我想将img拖动到形状中。

谢谢,

1 个答案:

答案 0 :(得分:1)

http://jsfiddle.net/Ps3TU/5/是朝着你想做的事情迈出的一步。

  darthVaderImg.on('dragmove', function () {
      var userPos = stage.getUserPosition();
      if(rectShape.intersects(userPos)){
            rectShape.setFillPatternImage(this.getImage());
            this.hide();
            rectShape.setFillPatternOffset(this.getWidth(), 70);
      }
  });

但你真正想做的事情是:http://jsfiddle.net/8Vdht/3/

darthVaderImg.on('dragmove', function () {
 var userPos = stage.getUserPosition();
  if(rectShape.intersects(userPos)){
    this.setX(rectShape.getX());
    this.setY(rectShape.getY());
    this.setWidth(rectShape.getWidth());
    this.setHeight(rectShape.getHeight());
  }
  else {
    this.setWidth(savedWidth);
    this.setHeight(savedHeight);
    this.setImage(imageObj);
  }
  layer.draw();
});

问题是,您不能(仅)将图像(内部)放置在矩形中,然后轻松地将其全部提取出来。所以你需要求助于一个技巧,它会调整图像的大小并使其看起来像是在矩形中。这会修复图像的位置,然后将其移动到矩形之外。

相关问题