在下面的示例中,我需要等待drawFunc
方法完成,然後再繼續添加不透明度並最終將foregroundPath
添加到Kinetic中的圖層。
在將其添加到使用Angular JS $ q服務的圖層之前,「等待」foregroundPath
值的方法是什麼?
var foregroundPath = new Kinetic.Shape({
x: x,
y: y,
drawFunc: function(context){
context._context.fillStyle = graphic.fill;
try{
fabricSVG.render(context);
}
catch (TypeError) {
console.log('Caught TypeError!');
}
},
fill: graphic.fill,
name: 'graphicMainColor',
scale: {x: imageScale, y: imageScale},
rotation: graphic.rotation
});
foregroundPath.opacity(graphicOpactiy);
var imageLayer = new Kinetic.Layer({name: layerName});
imageLayer.add(foregroundPath);
kineticStage.add(imageLayer);
更新#2
@安東尼-C - 無論是success
或error
回調到這兒叫。相反,我使用$q.all()
,因爲我也爲backgroundPath做了同樣的事情(爲了簡潔起見,我沒有包括它)。在這個例子中,success
方法總是被首先調用。當我做console.log()
我可以告訴腳本首先執行success
所有的承諾和然後它執行什麼在drawFunc
內。它應該在執行所有承諾的success
方法之前先執行drawFunc
?
var backgroundPathDeferred = $q.defer();
var foregroundPathDeferred = $q.defer();
// Graphic Shadow Color
var backgroundPath = new Kinetic.Shape({
x: x - 1,
y: y - 1,
drawFunc: function(context){
context._context.fillStyle = shadowFill;
try{
fabricSVG.render(context);
}
catch (TypeError) {
console.log('Caught TypeError!');
backgroundPathDeferred.reject(TypeError);
}
backgroundPathDeferred.resolve();
},
fill: shadowFill,
name: 'graphicShadowColor',
scale: {x: imageScale, y: imageScale},
rotation: graphic.rotation
});
// Graphic Main Color
var foregroundPath = new Kinetic.Shape({
x: x,
y: y,
drawFunc: function(context){
context._context.fillStyle = graphic.fill;
try{
fabricSVG.render(context);
}
catch (TypeError) {
console.log('Caught TypeError!');
foregroundPathDeferred.reject(TypeError);
}
foregroundPathDeferred.resolve();
},
fill: graphic.fill,
name: 'graphicMainColor',
scale: {x: imageScale, y: imageScale},
rotation: graphic.rotation
});
var promises = {
'foreground': foregroundPathDeferred,
'background': backgroundPathDeferred
};
$q.all(promises).then(function(){
console.log('All promises resolved.', backgroundPath, foregroundPath);
backgroundPath.opacity(shadowOpacity);
foregroundPath.opacity(graphicOpactiy);
var imageLayer = new Kinetic.Layer({name: layerName});
imageLayer.add(backgroundPath);
imageLayer.add(foregroundPath);
kineticStage.add(imageLayer);
kineticStage.find('.background').setZIndex(9999);
$('canvas').css({'width': '100%', 'height': '100%'});
}, function(error){
console.log('Caught error!', error, foregroundPath, backgroundPath);
});
應該是'VAR承諾= { '前景':foregroundPathDeferred .promise, 'background':backgroundPathDeferred.promise };' –