2012-01-24 6 views
0

맞춤 속성과 기능을 사용하여 맞춤 Raphael 요소를 만들고 싶습니다. 이 오브젝트에는 또한 사전 정의 된 Raphael 오브젝트가 있어야합니다. 예를 들어, 텍스트와 다른 요소가 포함 된 원을 포함하는 노드 클래스가 있습니다. 문제는이 새 객체를 세트에 추가하는 것입니다. Raphael이 아닌 오브젝트는 세트에 추가 할 수 없으므로 이러한 요구가 필요합니다. 따라서 Raphael 객체를 포함 할 수있는 사용자 정의 객체는 사용할 수 없습니다.Raphael.js : 새로운 맞춤 요소 추가하기

var Node = function (paper) { 
    // Coordinates & Dimensions 
    this.x = 0, 
    this.y = 0, 
    this.radius = 0, 
    this.draw = function() { 
     this.entireSet = paper.set(); 
     var circle = paper.circle(this.x, this.y, this.radius); 
     this.circleObj = circle; 
     this.entireSet.push(circle); 
     var text = paper.text(this.x, this.y, this.text); 
     this.entireSet.push(text); 
    } 
    // other functions 
} 

var NodeList = function(paper){ 
    this.nodes = paper.set(), 
    this.populateList = function(){ 
     // in order to add a node to the set 
     // the object must be of type Raphael object 
     // otherwise the set will have no elements 
     this.nodes.push(// new node) 
    } 
    this.nextNode = function(){ 
     // ... 
    } 
    this.previousNode = function(){ 
     // ... 
    } 
} 
+0

는이 같은 뭔가를 찾고 계십니까 : 현재 지원되지 않는 몇 가지 이유입니다 인스턴스 (종이) 이 예는 AG 요소를 감싸는 라파엘 객체를 생성, 수? http://raphaeljs.com/reference.html#Raphael.fn. .arrow() 또는 원하는 것과 같은 함수의 결과로 복잡한 객체를 만들 수있는 사용자 정의 함수를 추가하는 방법이 설명되어 있습니다. – cabreracanal

+0

내가 아는 한 Raphael.fn은 Raphael 개체를 만들지 않습니다. 따라서 생성 된 오브젝트는 Raphael 세트에 추가 될 수 없습니다. 내 클래스는 다음과 같습니다 VAR 노드 = 함수() { // 좌표 및 치수 this.x = 0, this.y = 0, this.radius = 0, this.stroke = 1, // ... 이것을 반환합니다. } – Claudia

+0

작성한 .fn에 첨부하는 기능입니다. 내부에 텍스트가있는 원을 만들려면 (내 질문을 이해하면)이 예제처럼이 방법을 사용합니다 (https://gist.github.com/1043360). 개체가 경로라면 집합에 추가 할 수 있습니다. 또한 텍스트와 서클을 별도로 만들어 세트에 넣을 수 있습니다. 사용자 정의 속성을이 속성에 지정할 수도 있습니다. – cabreracanal

답변

1

이 코드는 사용자가 텍스트 노드를 (그것이 집합을 반환) 만들 수 있도록 당신은 라파엘 객체 (DOM을로드 한 후 방법을 넣어)로 조작 할 수 있습니다 : 코드는 다음과 같을 것이다 :

function loadShape(){ 
    Raphael.fn.node = function(x, y, radius, txt){ 
     this.x = x; 
     this.y = y; 
     this.radius = radius; 
     this.txt = txt; 

     this.drawCircle = function() { 
      return paper.circle(this.x, this.y, radius); 
     }; 
     this.drawText = function() { 
      return paper.text(this.x, this.y, this.txt); 
     }; 

     this.draw = function(){ 
      var group = paper.set(); 
      var circulo = paper.circle(this.x, this.y, radius); 
      var texto = paper.text(this.x, this.y, this.txt); 
      group.push(circulo); 
      group.push(texto); 
      return group; 
     } 
     this.init = function(ox, oy, r, t){ 
      this.x = ox; 
      this.y = oy; 
      this.radius = r; 
      this.txt = t; 
     }; 
     // etc… 
     return this; 
    }; 
    var paper = new Raphael(document.getElementById("wrapper"), "100%", "100%"); 

    //var nodo = paper.node(); 
    //nodo.init(50, 50, 2.0, "soy un nodo"); 
    var nodo = paper.node(250, 150, 50.0, "hola"); 
    nodo.draw(); 
    //circ.attr({"propiedad":"hola"}); 
    //alert(circ.attr("propiedad")); 
} 

이 내용이 유용했는지 알려주세요.

+0

init 메소드는 set()과 비슷하지만 노드의 설정을 변경하려면 이전 세트를 제거하고 다시 그려야합니다! 또한 이것을 할 수있는 내부 함수를 정의 할 수 있습니다;) – cabreracanal

+0

응답 해 주셔서 감사합니다. 더 많은 설명으로 내 질문을 편집했습니다. 내가 제안한 것과 비슷한 구현을했습니다. 불행하게도, 노드를 Raphael 세트에 추가해야하기 때문에 사용할 수 없습니다.노드 객체에 선언 된 속성이 필요하기 때문에 draw 메서드에서 반환 된 집합 만 추가 할 수 없습니다. – Claudia

+0

간단한 배열을 사용하여 노드를 저장하고 Raphael.fn.node를 var Node =로 대체하는 것이 왜 여러분의 질문에 쓴 것처럼? – cabreracanal

2

paper.set(), 에 Raphael 객체 (rect, circle, eclipse, text) 만 추가 할 수 있습니다. 객체는 (Raphael.fn 포함)이 아닙니다. 대신 javascript []의 일반적인 배열 정의를 사용하십시오. fas로서 nodeList는 단순한 목록이지만 nextnode, 이전 노드와 같은 더 많은 옵션이 있다는 것을 이해합니다.

이 데모를보세요, 난 호세 마누엘 카브레라의 코드 ABIT 변경 : 전역 '종이' 가없는 경우 http://jsfiddle.net/Tomen/JNPYN/1/

Raphael.fn.node = function(x, y, radius, txt) { 
    this.x = x; 
    this.y = y; 
    this.radius = radius; 
    this.txt = txt; 
    this.circleObj = paper.circle(this.x, this.y, radius), this.textObj = paper.text(this.x, this.y, this.txt); 
    this.entireSet = paper.set(this.circleObj, this.textObj); 
    return this 
} 
Raphael.fn.nodeList = function() { 
    this.nodes = []; 
    this.push = function(p) { 
     return this.nodes.push(p); 
    }; 

    // this.nextNode = function(){ 
    // ... manipulate this.nodes here 
    // } 
    // this.previousNode = function(){ 
    // ... 
    // } 
    return this 
} 
var ca = paper.node(250, 150, 50.0, "hola"); 
var list = paper.nodeList(); 
list.push(ca); 
+0

안녕하세요. 나는 너와 똑같은 생각을하고 있었다. 왜 노드를 정상 배열에 저장하지 않을까? 하지만, 난 더 쉬운 방법은 var 노드 = 기능을 사용하는 것입니다 ... 그리고 그게 전부라고 생각합니다 :) – cabreracanal

+0

var 노드를 잊어 버리십시오 ... hehe 그것은 .fn에 추가하는 것이 좋습니다. – cabreracanal

+4

당신의 코드 호세 덕분에, 나는 라파엘에게 새로운 사람이다. 나는 다른 질문에 대한 답변을 기다리는 동안 들렀다. 그러나 아무도 지금까지 대답하지 않았다, 하하. – tomen

2

몇 가지 예제가 아래로 떨어질 수 Raphael.fn.yrMethod의 상황에 맞는 것입니다

(function(R){ 

     function g(_paper){ 

      var _canvas = _paper.canvas, 
       _node = document.createElementNS("http://www.w3.org/2000/svg", "g"); 

      _canvas.appendChild(_node); 

      this.add = function(rElement){ 
       _node.appendChild(rElement.node); 
      } 

      this.remove = function(rElement){ 
       _canvas.appendChild(rElement.node); 
      } 

      this.transform = function(tString){ 
       _node.setAttribute('transform', tString); 
      } 

     } 

     R.fn.g = function(){ 
      return new g(this); 
     } 

    })(Raphael);