How can I get all coordinates of shape's edge in ?

后端 未结 1 1432
半阙折子戏
半阙折子戏 2021-01-16 13:07

There is a shape in my canvas, and I need to get all coordinates of its edge. The shape may be regular or irregular.

1条回答
  •  不要未来只要你来
    2021-01-16 13:42

    You can use the "Marching Squares" algorithm to detect the edge path of a closed shape:

    This one by Michael Bostock for his excellent d3 visualization tool is great:

    https://github.com/d3/d3-plugins/tree/master/geom/contour

    enter image description hereenter image description hereenter image description here

    The key to using his version of Marching Squares is defining the function that defines pixels inside your shape vs pixels outside your shape. Here's a function that uses the .getImageData pixel array (data) to fetch non-transparent pixels:

        // This is used by the marching ants algorithm
        // to determine the outline of the non-transparent
        // pixels on the image
    
        var defineNonTransparent=function(x,y){
            var a=data[(y*cw+x)*4+3];
            return(a>20);
        }
    

    Example code and a Demo: http://jsfiddle.net/m1erickson/5v5hmo62/

    
    
    
     
    
    
    
    
    
        Show Image
    Show Outline Path

    0 讨论(0)
提交回复
热议问题