2013-04-18 44 views
0

我正在使用enyo应用程序。我想借鉴一个SVG。 这是我到目前为止的代码:使用javascript操作时SVG为空

enyo.kind({ 
    name:"draw", 
    kind:enyo.Control, 
    tag:"svg", 
    attributes:{ 
     xmlns:"http://www.w3.org/2000/svg", 
     version:"1.1" 
    }, 
    classes:"drawSvg", 
    published:{ 
     drawSize:5, 
     selectedColor:"333333", 
     paths:0 
    }, 
    handlers:{ 
     ondown:"onDownHandler", 
     onmove:"onMoveHandler" 
    }, 

    create:function(){ 
     this.inherited(arguments); 
     this.render(); 
    }, 
    rendered:function(){ 
     this.inherited(arguments); 
    }, 

    onDownHandler:function(sender,target){ 
     this.iPaths++; 
     var p = this.getPositionInSvg(target); 
     var path = document.createElementNS("http://www.w3.org/2000/svg", "path"); 
     path.setAttribute('class', 'drawpath'+ this.iPaths); 
     path.setAttribute('fill', 'none'); 
     path.setAttribute('stroke', '#'+this.getSelectedColor()); 
     path.setAttribute('stroke-width', this.getDrawSize()); 
     path.setAttribute('d', "M " + p.x + "," + p.y); 

     $(this.hasNode()).append(path); 
    }, 

    onMoveHandler:function(sender,target){ 
     var p = this.getPositionInSvg(target); 
     $('. drawpath'+ this.iPaths).attr("d", $('. drawpath'+ this.iPaths).attr("d") + " L "+ p.x+","+ p.y); 
    }, 

    getPositionInSvg:function(target){ 
     var pos = new Object(); 
     pos.x = ((target.pageX - this.hasNode().offsetLeft)/window.settings.SCALE) + 0.5>>0; 
     pos.y = ((target.pageY - $(this.hasNode()).offset().top)/window.settings.SCALE) + 0.5>>0; 
     return pos; 
    } 
}); 

当我看到在萤火生成的HTML代码。这是代码:

<svg id="frame_view1_drawSvg" class="drawSvg" xmlns="http://www.w3.org/2000/svg"  
version="1.1" style="left: 62px; top: 0px; height: 877px; width: 1403px; 
pointer-events: auto; cursor: none;"> 
<path class="drawpath1" fill="none" stroke="#333333" stroke-width="5" d="M 795,597 
L 795,599 L 795,601 L 795,603 L 793,606 L 785,609 L 767,617 L 742,623 L 713,629 
L 640,634 L 616,636 L 579,634 L 549,630 L 530,622 L 513,611 L 495,597 L 485,588 
L 477,581 L 474,577 L 473,576 L 472,575 L 466,572 L 452,566 L 436,557 L 420,543 
L 410,526 L 404,502 L 404,491 L 416,487 L 452,484 L 499,494 L 537,513 L 573,538 
L 588,554 L 591,557 L 591,556 L 583,551 L 570,542 L 553,525 L 531,504 L 523,496 
L 521,495 L 521,494 L 521,494 L 522,494"> 
<path class="drawpath2" fill="none" stroke="#333333" stroke-width="5" d="M 1166,136 
L 1166,137 L 1164,135 L 1162,135 L 1157,134 L 1148,131 L 1128,129 L 1117,124 
L 1107,122 L 1099,119 L 1096,119 L 1094,117 L 1092,117 L 1092,118 L 1092,119 
L 1093,120 L 1095,125 L 1096,129 L 1096,133 L 1096,139 L 1096,141 L 1237,139 
L 1234,140 L 1227,142 L 1219,145 L 1203,151 L 1190,154 L 1172,160 L 1157,162 
L 1148,164 L 1145,164 L 1142,164 L 1139,165 L 1138,167 L 1138,168 L 1138,169 
L 1138,169 L 1139,169 L 1144,169 L 1148,170 L 1149,170 L 901,317 L 901,317 
L 901,317 L 899,326 L 899,327 L 898,331 L 896,336"> 
</svg> 

我想生成的代码看起来不错。 但我在svg中没有看到任何东西。什么都没有呈现,并且svg是空的。

回答

0

我通过每晚Enyo验证我可以做一个非常简单的SVG测试。

http://jsfiddle.net/sugardave/4wXWv/

在应用一种声明

{name: "svg", tag: "svg", attributes: {xmlns: "http://www.w3.org/2000/svg", version: "1.1"}} 

工作得很好,我没有尝试,作为一个独立的一种。

+0

谢谢,我做了enyo.kind一个div而不是svg en作为一个组件添加了你的代码。 – Dimitri

0

在你的SVG,path tag必须是自关闭标签。您可以使用在线工具(如w3c)来查找SVG标记有效性中的错误。

我没有完整的代码来检查,但我建议你onDownHandler:功能,尝试使用$(this.hasNode()).appendChild(path);

下面是一个小提琴产生在你的SVG第一行:http://jsfiddle.net/e9RkY/1/

+0

'.appendChild'不是一个jquery函数。所以我尝试了 'this.hasNode()。appendChild(path);' 但是仍然没有结束标签。 – Dimitri

0

我还没有时间进行调查,但我会在此时更新此答案。但是我从本书中的SVG示例中回想起来,您不能只是在呈现SVG对象后修改其节点。您可能需要将函数设置到SVG中,或者使用不同的方法来修改节点。看看这个,看看它是否有帮助:update SVG dynamically