【问题标题】:RaphaelJS seems to lack shape hierarchiesRaphaelJS 似乎缺乏形状层次结构
【发布时间】:2012-05-19 18:02:26
【问题描述】:

Raphael 似乎缺乏形状层次结构?

我无法创建一个“附加”到较大圆圈的较小圆圈,并且知道当较大圆圈时它会被缩放/平移。

同样,如果我将元素放入集合中,拖动处理程序会单独连接到形状,并且在其回调中我没有集合的句柄。

我是否忽略了什么?

【问题讨论】:

    标签: javascript svg raphael


    【解决方案1】:

    这是当前的行为,因为 Raphael 没有为“集合”创建任何真实元素。

    【讨论】:

      【解决方案2】:

      如果你想在一个集合上启用 Drag'nDrop,你可以使用以下代码:

      Raphael.el.set_drag = function (aSet) {
          // Enable drag'n drop in a Set
          var startAll = function () {
              // storing original coordinates
              for (var i in this.set.items) {
                  var comp = this.set.items[i];
                  try {
                      comp.attr({opacity: 0.3});
                  } catch (ex) {;}
                  if (comp.type == "path") {
                      comp.ox = comp.getBBox().x;
                      comp.oy = comp.getBBox().y;
                  }
                  else {
                      comp.ox = comp.attrs.cx || comp.attrs.x;
                      comp.oy = comp.attrs.cy || comp.attrs.y;
                  }
              }
          },
      
          moveAll = function (dx, dy) {
              for (var i in this.set.items) {
                  var comp = this.set.items[i];
                  if (comp.attrs.cx)             // ellipse
                      comp.attr({cx: comp.ox + dx, cy: comp.oy + dy});
                  else if (comp.attrs.x)
                      comp.attr({x: comp.ox + dx, y: comp.oy + dy});
                  else            // path
                      comp.translate(comp.ox - comp.getBBox().x + dx, comp.oy - comp.getBBox().y + dy);
              }
          },
      
          upAll = function () {
              for (var i in this.set.items) {
                  var comp = this.set.items[i];
                  if (comp.attrs.cx)             // ellipse
                      comp.attr({cx: comp.ox, cy: comp.oy + dy});
                  else if (comp.attrs.x)
                      comp.attr({x: comp.ox, y: comp.oy + dy});
                  else            // path
                      comp.translate(comp.ox , comp.oy - comp.getBBox().y + dy);
                  this.set.items[i].attr({opacity: 1});
              }
          };
      
          this.set = aSet; //create a "set" property on the element
          this.drag(moveAll,startAll,upAll);
          return this;    
      }
      
      
      // Create your elements
      var first_element = paper.rect(10,10,100,50).attr({fill:'#f00'});
      var second_element = paper.rect(30,300,100,50).attr({fill:'#0f0'});
      
      // Add elements to your set
      var myset = paper.set();
      myset.push(first_element);
      myset.push(second_element);
      
      // Add handler on the elements
      first_element.set_drag(myset);
      second_element.set_drag(book_set);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-02
        • 2017-01-03
        • 2019-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多