How do I get touch coordinates with respect to canvas after scaling and translating?

后端 未结 3 914
南旧
南旧 2021-01-06 01:44

I need to get the touch x and y with respect to the canvas to check for collisions and things like that after I have moved and scaled the canvas.

I already managed t

3条回答
  •  佛祖请我去吃肉
    2021-01-06 01:57

     private float convertToCanvasXCoordinate(float touchx,float offsetx,float viewportVisibleWidth){
            float newx=(touchx*viewportVisibleWidth)/getWidth()+offsetx;
            return newx;
        }
    
        private float convertToCanvasYCoordinate(float touchy,float offsety,float viewportVisibleHeight){
            float newy=(touchy*viewportVisibleHeight)/getHeight()+offsety;
            return newy;
        }
    

    i just found out there is a function canvas.getClipBound() which is a rectangle representing the visible viewport that includes the offsetx offset y (the left and top of the rectangle respectively) and the viewport width and height

    simply call these functions and it will get you touchx and touchy with respect to canvas

提交回复
热议问题