2014-10-17 59 views
0

我想通過自己的ID刪除帆布我試着給它們分配ID可以調用刪除方法,但它不工作:無法刪除帆布和變化SRC通過其ID:面料JS

見鏈接:http://jsfiddle.net/mishragaurav31/kcyKJ/

的html代碼:

刪除itchange它

js代碼

var canvas = new fabric.Canvas('c'); 

     function getObjectById(id) { 
      canvas.forEachObject(function(o) { 
       if (o.id === id) { 
        return o; 
       } 
      }); 
      return null; 
     } 

    var img = "http://edudemic.com/wp-content/uploads/2012/09/jail_photo.jpg"; 

     fabric.Image.fromURL(img, function (source) { 
     img = source.set({ 
      left: 0, 
      top: 0, 
      angle: 0, 
      id:'moveit' 
     }); 
     canvas.add(img); 
     canvas.renderAll(); 
     }); 

     document.getElementById('download').onclick = function() { 
      alert('hii'); 
     var removeit = getObjectById('moveit'); 
      canvas.remove(removeit); 
     }; 

     document.getElementById('changeimage').onclick = function() { 
      alert('hii'); 
     getObjectById('moveit').src="http://www.meandmylaptop.com/uploads/5/8/1/2/5812329/7801574.png"; 
      canvas.renderAll(); 
     }; 

document.getElementById('changeimage').onclick = function() { 
      alert('hii'); 
     getObjectById('moveit').src="http://www.meandmylaptop.com/uploads/5/8/1/2/5812329/7801574.png"; 
      canvas.renderAll(); 
     }; 
+0

'變種removeit = getObjectById( 'moveit');'爲空。所以不知何故,你設置的ID,但ID沒有被應用。 – artm 2014-10-17 09:55:07

+0

如何設置id? – mydeve 2014-10-17 10:13:56

回答

0

Demo

變化

alert('hii'); 
    var removeit = getObjectById('moveit'); 
    canvas.remove(removeit); 

//  alert('hii'); 
canvas.remove(img); 
// var removeit = getObjectById('moveit'); 
//  canvas.remove(removeit); 
+0

如果我想改變src:img.setAttribute(「src」,image); – mydeve 2014-10-17 10:04:22

+0

@mour [** Demo **](http://jsfiddle.net/ianwong/qdwv5mh6/2/)添加一個新的。 – 2014-10-17 10:12:58

+0

是的,但你是添加新的形象不chnage舊的src – mydeve 2014-10-17 10:17:48