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?
Rechtschreibfehler im Funktionsnamen Transparent (anstelle von Trasparent) – laiju
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) –