2013-08-27 57 views
0

我正在尋找一種方法來刪除kinetic js上的圖層.. 我認爲這是一個簡單的任務,我嘗試了一些代碼,如node.remove()或layer.remove(node),但真的不知道。Kinetic Js清除一層

這裏的示例代碼:

<!DOCTYPE HTML> 
<html> 
    <head> 

    </head> 
    <body> 
    <div id="container"></div> 
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.6.0.min.js"></script> 
    <script defer="defer"> 
    var stage = new Kinetic.Stage({ 
     container: 'container', 
     width: 578, 
     height: 200 
     }); 

     var layer = new Kinetic.Layer(); 

     var rect = new Kinetic.Rect({ 
     x: 239, 
     y: 75, 
     width: 100, 
     height: 50, 
     fill: 'green', 
     stroke: 'black', 
     strokeWidth: 4 
     }); 

     // add the shape to the layer 
     layer.add(rect); 

     // add the layer to the stage 
     stage.add(layer); 

function delete() 
    { 
    layer.remove(); 

    } 
    </script> 
    <button type="button" onclick="delete()">Delete</button> 
    </body> 
</html> 

我無法想通了爲什麼我刪除()函數將無法正常工作。 有人知道我做錯了什麼? 謝謝

回答

0

其實我從來沒有需要刪除整個圖層。我清除並重用它。

layer.removeChildren();

除去像(組)或(系)或(圖片)或任何只是具體內容...

console.dir(layer); // initial check -- look at (children)... 

    var groups = $('#layerID').find('Group'); 
    groups.each(function(group) { 

     group.destroy(); or.... 
     group.remove(); 

    }); 

console.dir(layer); // verify it worked (look at children again... a bunch were removed)