Famo.us drag&掉落表面?

时间:2014-08-04 21:42:27

标签: drag-and-drop famo.us

我已经通过了方便的Famo.us University教程,并且正在制作一个拖拽和放大器的原型。放下界面。它是典型的用户界面,用户可以拖动图标并将其拖放到目标上以执行某些操作。我已经把拖拽部件弄下来了,但是检测到掉落是非常毛茸茸的。在Famo.us中是否有内置的碰撞检测?

编辑:我已查看了Collision API,但不清楚这是否适用于各种视图。

以下是我组织项目的方式:

AppView (overall container)
 |
 |__ MenuView (sidebar) --> VizView (icons in MenuView)
 |
 |__ PageView (workspace where the drop targets live)

这可能不是解决此问题的最佳方法。我不确定。在视图中连接输入事件似乎很痛苦。

VizView来源:

/*** VizView.js ***/

define(function(require, exports, module) {
    var View          = require('famous/core/View');
    var Surface       = require('famous/core/Surface');
    var Transform     = require('famous/core/Transform');
    var Modifier = require('famous/core/Modifier');
    var ImageSurface  = require('famous/surfaces/ImageSurface');

    var Transitionable = require("famous/transitions/Transitionable");
    var SnapTransition = require("famous/transitions/SnapTransition");
    Transitionable.registerMethod("spring", SnapTransition);

    var GenericSync     = require('famous/inputs/GenericSync');
    var MouseSync       = require('famous/inputs/MouseSync');
    var TouchSync       = require('famous/inputs/TouchSync');
    GenericSync.register({'mouse': MouseSync, 'touch': TouchSync});

    function VizView() {
        View.apply(this, arguments);

        _createIcon.call(this);
    }

    VizView.prototype = Object.create(View.prototype);
    VizView.prototype.constructor = VizView;

    VizView.DEFAULT_OPTIONS = {
        width: 200,
        height: 100,
        angle: -0.2,
        iconSize: 98,
        iconUrl: '',
        title: 'Empty',
        fontSize: 26
    };

    function _createIcon() {
        this.zIndex = 0;
        var me = this;

        var iconSurface = new ImageSurface({
            size: [this.options.iconSize, this.options.iconSize],
            content : this.options.iconUrl,
            properties: {
                cursor: 'pointer'
            }
        });

        var initModifier = new Modifier({
            // places the icon in the proper location
            transform: Transform.translate(24, 2, 0)
        });

        this.position = new Transitionable([0, 0]);
        var positionModifier = new Modifier({
            transform : function(){
                var currentPosition = me.position.get();
                return Transform.translate(currentPosition[0], currentPosition[1], me.zIndex);
            },
        });

        var sync = new GenericSync(
            ['mouse', 'touch']
        );
        sync.on('start', function(data){
            me.zIndex = 1;
        });
        sync.on('update', function(data){
            me.updateIcon(data);
        });
        sync.on('end', function(data){
            var velocity = data.velocity;
            me.position.set([0, 0], {
                method : 'spring',
                period : 150,
                velocity : velocity
            });
            me.zIndex = 0;
        });

        iconSurface.pipe(sync);
        this.add(positionModifier).add(initModifier).add(iconSurface);

        this.updateIcon = function (data) {
            if (this.zIndex == 0) return;
            var currentPosition = this.position.get();
            this.position.set([
                currentPosition[0] + data.delta[0],
                currentPosition[1] + data.delta[1]
            ]);
        }
    }

    module.exports = VizView;
});

VizView在MenuView中实例化:

        var vizView = new VizView({
            iconUrl: "path/to/iconUrl",
            title: "Viz Title"
        });

        var vizModifier = new StateModifier({
            transform: Transform.translate(0, yOffset, 0)
        });

        this.add(vizModifier).add(vizView);

1 个答案:

答案 0 :(得分:1)

draggable中的Surface Famo.us实际上不是DOM draggable元素,但可以设置为使用鼠标在浏览器中工作。我还没有能够GenericSynctouch使用此解决方案。

阅读Famo.us网站上的陷阱,有drag and drop提示表面可拖动是一个问题。

  

How do I find the absolute position of a Surface on the screen?

     

按设计这是不可能的。这是开发人员应该做的事情   不关心。 暂时,这意味着这样的互动   因为拖放更难实现,但这是打算和我们   正在研究这些用例的优雅解决方案。

虽然:使用GenericSync 时,您可以使用带有Famo.us DOM draggable的{​​{1}}个活动在评论中说明并链接到John Traver解决方案。

但是:在此回答时,使用Surface的移动触摸设备无法使用此解决方案。如果陷阱中所述,将此问题与Famo.us一起使用可能会更加困难。我们希望在0.3.5或touch

之后的版本中解决这个问题

MixedMode (WebGL and DOM)
define('main', function(require, exports, module) {
  var Engine = require('famous/core/Engine');
  var Surface = require('famous/core/Surface');
  var ImageSurface = require('famous/surfaces/ImageSurface');
  var Transform = require('famous/core/Transform');
  var Modifier = require('famous/core/Modifier');
  var StateModifier = require('famous/modifiers/StateModifier');
  var Draggable = require('famous/modifiers/Draggable');
  var TransitionableTransform = require('famous/transitions/TransitionableTransform');

  var mainContext = Engine.createContext();

  var transTransform = new TransitionableTransform();
  transTransform.set(Transform.translate(100, 0, 0));

  var captureSurface = new Surface({
    content: 'Drag to Here',
    size: [300, 300],
    properties: {
      textAlign: 'center',
      lineHeight: '300px',
      backgroundColor: 'rgba(255,255,0,0.4)',
      cursor: 'pointer'
    },
    attributes: {
      dropzone: 'copy file:image/png file:image/gif file:image/jpeg'
    }
  });
  captureSurface.on('dragenter', function(evt) {
    console.log('dragenter', evt);
    evt.preventDefault();
    return false;
  });

  captureSurface.on('dragleave', function(evt) {
    console.log('dragleave', evt);
    captureSurface.setProperties({
      border: 'none'
    });
    evt.preventDefault();
    return false;
  });

  captureSurface.on('dragover', function(evt) {
    console.log('dragover', evt);
    captureSurface.setProperties({
      border: '4px dashed black'
    });
    evt.preventDefault();
    return false;
  });

  captureSurface.on('drop', function(evt) {
    console.log('drop', evt);

    evt.preventDefault();
    evt.stopPropagation();

    captureSurface.setProperties({
      border: '4px solid red'
    });

    files = evt.dataTransfer.files;
    console.log(files);
  });
  mainContext.add(new Modifier({
    origin: [0.5, 0.5],
    align: [0.5, 0.5]
  })).add(captureSurface);

  var surface = new Surface({
    content: 'DOM Draggable',
    size: [300, 100],
    properties: {
      backgroundColor: 'rgba(255,0,0,0.4)',
      cursor: 'move'
    },
    attributes: {
      draggable: 'true'
    }
  });
  surface.on('drag', function(evt) {
    console.log('surface drag', evt)
  });
  var imageSurface = new ImageSurface({
    content: 'http://i.imgur.com/NGOwZeT.png',
    size: [100, 100],
    properties: {
      cursor: 'copy'
    },
    attributes: {
      draggable: 'true'
    }
  });
  imageSurface.on('drag', function(evt) {
    console.log('imageSurface drag', evt)
  });
  imageSurface.on('dragend', function(evt) {
    console.log('imageSurface dragend', evt)
  });

  var dragSurface = new Surface({
    content: 'Drag Me',
    size: [100, 100],
    properties: {
      backgroundColor: 'rgba(0,0,0,0.1)',
      cursor: 'move'
    },
    attributes: {
      draggable: 'true'
    }
  });
  dragSurface.on('dragstart', function(evt) {
    console.log('dragSurface dragstart', event, evt);
  });
  dragSurface.on('drag', function(evt) {
    console.log('dragSurface dragstart', event, evt);
  });

  var modifier = new Modifier({
    origin: [0, 0],
    align: [0, 0],
    transform: transTransform
  });
  var imageModifier = new Modifier({
    origin: [0, 0.5],
    align: [0, 0.5]
  });

  var draggable = new Draggable();

  draggable.subscribe(dragSurface);

  mainContext.add(modifier).add(surface);
  mainContext.add(imageModifier).add(imageSurface);
  mainContext.add(draggable).add(dragSurface);

  draggable.on('update', function(e) {
    console.log('draggable update', e, event);

    var pos = e.position;
    surface.setContent('Draggable Position is ' + pos);
    transTransform.set(Transform.translate(pos[0] + 100, pos[1], 0));
  });

  draggable.on('end', function(e) {
    var pos = e.position;
    surface.setContent('Draggable End Position is ' + pos);
    transTransform.set(Transform.translate(pos[0] + 100, pos[1], 0));
  });

  //draggable.deactivate();

});
require(['main']);