简体   繁体   English

回调返回true / false

[英]callback returning true/false

I'm wondering what I am missing here I have a code that checks if the image is transparent based on canvas. 我想知道我在这里缺少什么,我有一个代码可以检查基于画布的图像是否透明。

 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); } }; } 

When I'm doing it this way: 当我这样做时:

  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); 

It works just fine, but when I'm trying to make this function above like this: 它工作得很好,但是当我试图像上面这样制​​作这个函数时:

 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"); } 

It doesn't work... 没用...

Even tho in this example which i wrote it works fine: 即使在我写的这个例子中也可以正常工作:

 function check(a, b) { var result = a + b; if (result <= 10) { return (false); } else { return (true); } } function test() { if (check(5, 4)) { console.log(">10"); } else { console.log("<10") } } test(); 

What i am missing? 我想念什么?

The return-statements are not belonging to the function Transparent ! 返回语句不属于函数Transparent

You are creating a different function here which returns true or false when it is called, but it is not executed right away and it's return-value is not returned in your function Transparent . 您将在此处创建一个不同的函数,该函数在调用时返回true或false,但不会立即执行,并且其返回值不会在函数Transparent返回。 What you have is essentially this snippet: 本质上,您所拥有的是以下代码段:

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;
}

(these are not actually the same since fat arrow functions capture this , see What does "this" refer to in arrow functions in ES6? ) (这实际上不是因为胖箭头功能捕获同一this ,看到什么是“这”指的箭头功能ES6? )

Your solution with the callback is the way to go. 您使用回调的解决方案是必经之路。

your code is async. 您的代码是异步的。 you cant return true or false. 您不能返回true或false。 you need to return a callback thats why your if doesn't work 您需要返回一个回调,这就是为什么您的if无法正常工作的原因

 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"); } }) ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM