0

我想沿水平线拖动图像。换句话说,我想忽略鼠标移动的 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()

顺便说一句,我raphael.draggable.js通过在第 13 行插入下面的代码来应用补丁。

  /** 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); }
        }
    }
  };

麦克风

4

0 回答 0