cocos creator 点击区域是否透明判断(用canva

2020-10-13  本文已影响0人  凡凡的小web

每次都是draw的一个像素,没有效率问题

如果不支持像素则透明区域的判断用Polygon Collider组件来判断

function onLucencyTouch(img, x, y){
            var cvs = document.createElement("canvas");
            var ctx = cvs.getContext('2d');
            cvs.width = 1;
            cvs.height = 1;
            ctx.drawImage(img,x,y,1,1,0,0,1,1);
            var imgdata = ctx.getImageData(0,0,1,1);
            return imgdata.data[3];
        }


let target = e.target
            let pos = target.convertToNodeSpace(e.getLocation())
            let sprite = target.getComponent(cc.Sprite)
            let size = that.node.getContentSize();

            let imgObj = sprite.spriteFrame.getTexture().getHtmlElementObj();
            if(onLucencyTouch(imgObj, pos.x, size.height-pos.y)){
                return true
            }else{
                return false
            }

上一篇下一篇

猜你喜欢

热点阅读