2017-07-18 20 views

回答

0

在你的情況,我認爲你可以使用mouse:over處理器中setTimeout功能。這樣你可以在執行代碼之前放一些延遲。

所以我做了什麼:

1)使用setTimeoutmouse:over處理

2)保存參考啓動超時在var timeout;

3)timeout變量使用clearTimeoutmouse:out處理程序防止mouse:over中的代碼在延遲完全完成之前如果鼠標沒有被執行

(function() { 
 
    var canvas = this.__canvas = new fabric.Canvas('c'); 
 
    fabric.Object.prototype.transparentCorners = false; 
 
    
 
    var timeout; 
 
    canvas.on('mouse:over', function(e) { 
 
    if(!e.target) return false; 
 
    
 
    timeout = setTimeout(function(){ 
 
     e.target.setFill('red'); 
 
     canvas.renderAll(); 
 
    }, 1000) 
 
    }); 
 

 
    canvas.on('mouse:out', function(e) { 
 
    if(!e.target) return false; 
 
    
 
    /* clear the timeout so we make sure that mouse:over code will not execute if delay is not completed */ 
 
    clearTimeout(timeout); 
 
    
 
    e.target.setFill('green'); 
 
    canvas.renderAll(); 
 
    }); 
 

 
    // add random objects 
 
    for (var i = 15; i--;) { 
 
    var dim = fabric.util.getRandomInt(30, 60); 
 
    var klass = ['Rect', 'Triangle', 'Circle'][fabric.util.getRandomInt(0,2)]; 
 
    var options = { 
 
     top: fabric.util.getRandomInt(0, 300), 
 
     left: fabric.util.getRandomInt(0, 300), 
 
     fill: 'green' 
 
    }; 
 
    if (klass === 'Circle') { 
 
     options.radius = dim; 
 
    } 
 
    else { 
 
     options.width = dim; 
 
     options.height = dim; 
 
    } 
 
    canvas.add(new fabric[klass](options)); 
 
    } 
 
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.16/fabric.js"></script> 
 

 
<canvas id="c" width="300" height="300"></canvas>

我使用在此代碼段當前超時是1000毫秒= 1秒。您可以在setTimeout函數中調整它。我希望這對你有所幫助,如果有些事情不清楚,請告訴我。

+0

太好了,這正是我所需要的。如果我無法實現它,我會執行它並返回。 –

相關問題