2017-11-30 4 views
0

Ich frage mich, was ich hier vermisse Ich habe einen Code, der überprüft, ob das Bild basierend auf Canvas transparent ist.Callback Returning True/False

function Trasparent(url, npc, clb) { 
 
    var img = new Image(); 
 
    img.src = url; 
 
    img.onload =() => { 
 
     canvas.width = img.width; 
 
     canvas.height = img.height; 
 
     ctx.clearRect(0, 0, canvas.width, canvas.height); 
 
     ctx.drawImage(img, 0, 0); 
 
     var maxlength = Math.sqrt(img.width * img.height) * 5 + 300; 
 
     if (canvas.toDataURL().length < maxlength) { 
 
      clb(false, npc); 
 
     } else { 
 
      clb(true, npc); 
 
     } 
 
    }; 
 
}

Wenn ich es so mache:

 function Trasparent(url, npc, clb) { 
 
      var img = new Image(); 
 
      img.src = url; 
 
      img.onload =() => { 
 
       canvas.width = img.width; 
 
       canvas.height = img.height; 
 
       ctx.clearRect(0, 0, canvas.width, canvas.height); 
 
       ctx.drawImage(img, 0, 0); 
 
       var maxlength = Math.sqrt(img.width * img.height) * 5 + 300; 
 
       if (canvas.toDataURL().length < maxlength) { 
 
        clb(false, npc); 
 
       } else { 
 
        clb(true, npc); 
 
       } 
 
      }; 
 
     } 
 

 
     function callback(success, npc) { 
 
      if (success) { 
 
       console.log("Not trasparent"); 
 
      } else { 
 
       console.log("Trasparent"); 
 
      } 
 
     } 
 
     
 
     
 
     Trasparent(npc.icon, npc, callback);

Es funktioniert ganz gut, aber wenn ich versuche, um diese Funktion zu machen oben wie folgt:

function Trasparent(url, npc) { 
 
    var img = new Image(); 
 
    img.src = url; 
 
    img.onload =() => { 
 
     canvas.width = img.width; 
 
     canvas.height = img.height; 
 
     ctx.clearRect(0, 0, canvas.width, canvas.height); 
 
     ctx.drawImage(img, 0, 0); 
 
     var maxlength = Math.sqrt(img.width * img.height) * 5 + 300; 
 
     if (canvas.toDataURL().length < maxlength) { 
 
      return false; 
 
     } else { 
 
      return true; 
 
     } 
 
    }; 
 
} 
 

 

 
if(Transparent(npc.icon, npc)){ 
 
    console.log("Not transparent"); 
 
} else { 
 
    console.log("Trasparent"); 
 
}

Es funktioniert nicht ...

Auch tho in diesem Beispiel, die ich schrieb es funktioniert:

function check(a, b) { 
 
\t var result = a + b; 
 
\t if (result <= 10) { 
 
\t \t return (false); 
 
\t } else { 
 
\t \t return (true); 
 
\t } 
 
} 
 

 

 
function test() { 
 
\t if (check(5, 4)) { 
 
\t \t console.log(">10"); 
 
\t } else { 
 
\t \t console.log("<10") 
 
\t } 
 
} 
 
test();

Was i vermisse ich?

+1

Rechtschreibfehler im Funktionsnamen Transparent (anstelle von Trasparent) – laiju

+1

Mögliches Duplikat von [Wie gebe ich die Antwort von einem asynchronen Anruf zurück?] (Https://stackoverflow.com/questions/14220321/how-do-i- return-the-response-from-asynchron-call) –

Antwort

0

Ihr Code ist asynchron. Sie können nicht wahr oder falsch zurückgeben. Sie benötigen ein Rückruf das ist zurückzukehren, warum Ihr, wenn nicht

function Trasparent(url, npc,cb) { 
 
    var img = new Image(); 
 
    img.src = url; 
 
    img.onload =() => { 
 
     canvas.width = img.width; 
 
     canvas.height = img.height; 
 
     ctx.clearRect(0, 0, canvas.width, canvas.height); 
 
     ctx.drawImage(img, 0, 0); 
 
     var maxlength = Math.sqrt(img.width * img.height) * 5 + 300; 
 
     if (canvas.toDataURL().length < maxlength) { 
 
      cb(false); 
 
     } else { 
 
      cb(true); 
 
     } 
 
    }; 
 
} 
 

 

 
Transparent(npc.icon, npc,function(result){ 
 
    if(result) 
 
     console.log("Not transparent"); 
 
    } else { 
 
     console.log("Trasparent"); 
 
    } 
 
    }) 
 
)

+1

Rechtschreibfehler in der Funktion genannt und erklärt – laiju

+0

Ich verpasste die Schreibweise cuz ich habe es von meiner Quelle kopiert und nur umbenannt für exapmle hier. Es ist also nur mein Fehler, auf jeden Fall ist sogar die Rechtschreibung korrekt, es funktioniert nicht – Hatchling

1

Die Rück Aussagen sind nicht auf die Funktion Transparent gehört, funktioniert!

Sie erstellen hier eine andere Funktion, die beim Aufruf true oder false zurückgibt, aber sie wird nicht sofort ausgeführt und ihr Rückgabewert wird in Ihrer Funktion Transparent nicht zurückgegeben. Was Sie haben, ist im Wesentlichen dieses Schnipsel:

function Trasparent(url, npc) { 
    var img = new Image(); 
    img.src = url; 
    img.onload = function() { 
     // function body totally unrelated to the Transparent-function 
     // and not executed and not returning anything right now 
    }; 
    return undefined; 
} 

(dies sind nicht eigentlich die gleiche, da Funktionen Fett Pfeil this erfassen, siehe What does "this" refer to in arrow functions in ES6?)

Ihre Lösung mit dem Rückruf ist der Weg zu gehen.