내가 캔버스에 기반하여 이미지가 투명한지 확인하는 코드가 여기에 누락되어 있는지 궁금합니다.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);
}
};
}
내가 이런 식으로 일을 해요 :
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);
그것은 잘 작동하지만을 나는이 기능을 만들려고 할 때 위처럼 :
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");
}
그것은 작동하지 않습니다 ...
심지어 내가 쓴이 예에서는 상점잘 작동 :
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();
내가 뭘 실종 됐어?
함수 이름의 철자 실수 Transparent (Trasparent 대신) – laiju
[어떻게 비동기 호출의 응답을 반환합니까?] (https://stackoverflow.com/questions/14220321/how-do-i-) return-from-an-asynchronous-call) –