2016-10-17 102 views
1

我有一個應用程序,與fabricjs。它與鼠標滾輪的縮放一起工作,當我做鼠標滾輪時,我的畫布保存狀態,就像我做鼠標滾輪一樣。防止保存狀態懸停在fabricjs

我在畫布上有一個狀態,當我把鼠標放在一個對象上時。 當您懸停一個對象並進行放大和縮小時,畫布的狀態將與懸停狀態一起保存。我不希望發生這種情況。我如何防止這種情況發生?

//click on the rect to see that the color red is saved in the json 
//shown in the console log, but i want the original State, the red fill 
canvas.on("mouse:over", function(event){ 
if("target" in event){ 
    color = event.target.fill; 
    event.target.fill="red"; 
    canvas.renderAll(); 
} 
}); 
canvas.on("mouse:out", function(event){ 
if("target" in event){ 
    event.target.fill=color; 
    canvas.renderAll(); 
    } 
}); 

var rect = new fabric.Rect({ 
    left: 100, 
    top: 100, 
    fill: 'blue', 
    width: 20, 
    height: 20, 
    angle: 45 
}); 
canvas.add(rect); 

您可以查看小提琴: https://jsfiddle.net/samael205/xtcmokuy/9/

回答

2

沒有辦法忽略懸停狀態。您可以將顏色保存在其他字段中,並將該字段傳遞給toObject()調用,並在導出後將其重置。

var canvas = new fabric.Canvas('canvas', { 
 
    perPixelTargetFind: true 
 
}); 
 

 
canvas.on("mouse:down", function(event) { 
 
    var state = canvas.toObject(['trueColor']); 
 
    state.objects.forEach(o => { 
 
    if (o.trueColor) { 
 
     o.fill = o.trueColor; 
 
    } 
 
    }) 
 
    console.log(state); 
 
}); 
 
//click on the rect to see that the color red is saved in the json 
 
//shown in the console log, but i want the original State, the red fill 
 
canvas.on("mouse:over", function(event) { 
 
    if (event.target) { 
 
    event.target.trueColor = event.target.fill; 
 
    event.target.fill = "red"; 
 
    canvas.renderAll(); 
 
    } 
 
}); 
 
canvas.on("mouse:out", function(event) { 
 
    if (event.target) { 
 
    event.target.fill = event.target.trueColor; 
 
    canvas.renderAll(); 
 
    } 
 
}); 
 

 
var rect = new fabric.Rect({ 
 
    left: 100, 
 
    top: 100, 
 
    fill: 'blue', 
 
    width: 20, 
 
    height: 20, 
 
    angle: 45 
 
}); 
 
canvas.add(rect);
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.6.6/fabric.min.js"></script> 
 
<canvas id="canvas"></canvas>

+0

感謝,做工精細。 –

+0

'code' var state = canvas.toObject(propiedadesAdicionales); \t \t \t state.objects.forEach(函數(鄰,索引){ \t \t \t \t如果在鄰( 「originalColor」){ \t \t \t \t \t開關(o.type){ \t \t \t \t \t \t情況下 「線」: \t \t \t \t \t \t \t o.stroke = o.originalColor; \t \t \t \t \t \t break; \t \t \t \t \t \t案 「圖像」: \t \t \t \t \t \t中斷; \t \t \t \t \t \t默認: \t \t \t \t \t \t \t o.fill = o.originalColor; \t \t \t \t \t} \t \t \t \t} \t \t \t \t如果(o.type == 「圖像」){ \t \t \t \t \t如果(matrix.get_filter_Tint(O)){ \t \t \t如果(矩陣。)(\t \t \t if(矩陣。get_filter_Tint_color(O)== 「紅」){ \t \t \t \t \t \t \t matrix.applyFilterTint( 「紅」,0.5,O); \t \t \t \t \t \t} \t \t \t \t \t} \t \t \t \t} \t \t \t}); \t \t return JSON.stringify(state); –

+0

ups。對不起,我不知道如何插入代碼......但是,這工作正常 –