简体   繁体   English

如何使用Snap.svg对单个SVG元素做出独特的响应?

[英]How to make unique responses for individual SVG elements with Snap.svg?

I've been looking for hours and nothing (including related StackOverflow threads) has gotten me any closer. 我一直在寻找时间,但没有任何东西(包括相关的StackOverflow线程)使我更加接近。 This problem seems so simple, but it apparently isn't based on the lack of info I can find. 这个问题看起来很简单,但是显然不是基于我找不到的信息。

I'm trying to make an interactive piano with Snap.svg where you can click each key and it will play its respective note. 我正在尝试使用Snap.svg制作交互式钢琴,您可以在其中单击每个键,它将弹奏各自的音符。 I've tried two methods so far: 到目前为止,我已经尝试了两种方法:

Method 1: A simple FOR loop that makes all the rects and adds an event listener. 方法1:一个简单的FOR循环,使所有区域并添加一个事件侦听器。

<script>
  window.onload=init();
  var S=Snap(500,100);
  var w=15;  var h=100;

  function init() {
    for (i=0;i<10;i++) {
      S.rect(i*w,0,w,h).attr{(id:i)}.click(console.log(this.id));
    }
  }
</script>

'undefined' is what I get when I click. 单击后得到的是“未定义”。

Method 2: I tried wrapping the SVG element in a JS object. 方法2:我尝试将SVG元素包装在JS对象中。

<script>
  window.onload=init();
  S=Snap(500,100);
  var h=15;  var w=100;
  var notes=['A','B','C',...];

  function Key(id, x, note) {
    this.id=id;
    this.x=x;
    this.note=note;
    this.drawSvg=function() {
            var keyImg=S.rect(x,0,w,h);
            keyImg.attr({ ... });
            keyImg.click(callback);
            return keyImg;
          }
    var callback=function() {
           console.log(this.note, "clicked!");
           return 0;
         } 
  }//Key() 

  function init() {
    var keys=[];
    for (i=0;i<10;i++) {
       keys[i]=new Key(i,i*w,notes[i%12]);
     }
    for (j=0;j<keys.length;j++) {
       keys[j].drawSvg();
     }
  }
</script>

This almost works. 这几乎可行。 It draws properly but every key I click I get 'undefined "Clicked!" 它绘制正确,但是我单击的每个键都得到未定义的“单击!” .

I know it's because the 'this' in callback() isn't the same 'this' in the Key constructor. 我知道这是因为callback()中的“ this”与Key构造函数中的“ this”不同。

So my question is: How do I get the callback() function to 'know' what object it's being called from so I can use that object's own attributes within the callback? 所以我的问题是:如何获取callback()函数以“知道”正在从哪个对象调用对象,以便可以在回调中使用该对象自己的属性?

Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

Heres a way that includes 2 methods. 这是一种包括2种方法的方法。 In your first example, you have attr wrong with the parenthesis. 在第一个示例中,括号的属性错误。

Also, a click handler must take a function as an arg, this way 'this' will be correct. 另外,点击处理程序必须将一个函数用作arg,这样“ this”将是正确的。 So something like... 所以像...

jsfiddle 的jsfiddle

Edit: I've added 2 methods that can be used, from different angles. 编辑:我添加了2种可以使用的方法,它们来自不同的角度。

 r.data('key', new Key( i )); 

Assocate an object as a data element to the Snap object. 将对象关联为Snap对象的数据元素。

 r.click( clickHandler.bind(r, key2));

Bind an object to the handler, so it gets passed as an arg. 将对象绑定到处理程序,使其作为arg传递。

Here is the whole lot. 这是全部。 You can shorten it a bit depending on which method works best. 您可以根据最有效的方法将其缩短一点。

var w=15;  var h=100;

function altKey( id ) {
   this.id='anotherkeymethod ' + id;
};

function Key(id ) {
    this.id='key' + id;
};

function init() {
    var r, key2;

    for (i=0;i<10;i++)      {
        key2 = new altKey( i );

        r = S.rect(i*w,0,w,h)
             .attr({ id:i, fill: 'blue' });

        r.data('key', new Key( i ));          // Method 1
        r.click( clickHandler.bind(r, key2)); // Method 2

    }
  }

function clickHandler( myKeyEl ) {
    console.log(this.id, this.data('key'), myKeyEl);
};

init();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM