Raphael / CoffeeScript尝试在拖动时约束Y坐标

时间:2012-04-09 23:16:16

标签: coffeescript raphael

我想沿水平线拖动图像。换句话说,我想忽略鼠标移动的Y值。我还想限制X值的范围。这是一个用于拖动图像的CoffeeScript类,但尝试不成功地约束图像的Y值。这段代码的另一个问题是图像的X值似乎是应该的两倍。

class TripleSlider
  circle = ""
  # (@x,@y) is coordinate of upper left corner of component bounding box
  constructor: (@editArea, @x, @y, @tsLabel, @showLimits=false) ->

  dragger: (x, y) =>
    x2 = Math.min(Math.max(x, 0), 127)
    circle.attr({x: x2, y:0}) # does not do anything; I hoped it would constrain Y

  drawRaphael: () =>
    paper = Raphael(10, 50, 320, 200)
    paper.fixNS()
    paper.draggable.enable()
    circle = paper.image("/assets/images/sliderTipDef.png", 0, 0, 13, 16).draggable.enable()
    circle.drag(@dragger)
$ ->
  tripleSlider = new TripleSlider($('#editArea'), 50, 100, "Attribute", true)
  tripleSlider.draw()

顺便说一句,我通过在第13行插入以下代码将补丁应用于raphael.draggable.js

  /** Fix from https://github.com/DmitryBaranovskiy/raphael/issues/409
   *  Just call it once after constructing paper:
   *
   * var paper = Raphael(0, 0, 300, 300);
   * paper.fixNS();
   * paper.draggable.enable();
   */
  Raphael.fn.fixNS = function() {
    var r = this;
    for (var ns_name in Raphael.fn) {
        var ns = Raphael.fn[ns_name];
        if (typeof ns == 'object') for (var fn in ns) {
            var f = ns[fn];
            ns[fn] = function(){ return f.apply(r, arguments); }
        }
    }
  };

麦克

0 个答案:

没有答案