Skip to main content
anthonyl89052030
Inspiring
Feb 11, 2025 5:21 PM UTC
Answered

Anyway for JSFL to read PNG data or anything else that can define bounds!

Hi all, after importing a PNG into Animate and adding it to the stage, I'm trying to discover if there is anyway I can generate a list of random positions within that shape. To give context, the PNG would act like a mask to determine the bounds of an area I'd like to spawn some VFX in - so for example, if I wanted some dust particles automatically placed on a volcano, I'd create an image matching the shape of the volcano mouth and then some coding wizardry would spawn a list of VFX within that shape.

 

If there's an alternative method which achieves the same goal, I'm all ears!

 

Thanks for any help!

    JoãoCésar17023019
    JoãoCésar17023019Correct answer
    Community Expert
    Feb 11, 2025 6:53 PM UTC

    Thanks for the clarification.

     

    So I guess you'll have to iterate over the vertices of a shape instance and look online for an algorithm that lets you find a random point inside of the polygon defined by them.

     

    var shape = fl.getDocumentDOM().selection[0];
    var points = [];
    var i;
    var total = shape.edges.length;
    
    for (i = 0; i < total; i++)
    {
    	var hEdge = shape.edges[i].getHalfEdge(0);
    	var vertex = hEdge.getVertex();
    	
    	points.push({ x: vertex.x, y: vertex.y });
    }

     

     

    4 replies

    JoãoCésar17023019
    Community Expert
    Community Expert
    Feb 11, 2025 5:55 PM UTC

    Hi.

     

    Maybe I'm not getting the issue correctly, but I think you can just get the x, y, width and height of the PNG instance and create random positions based on the bounds defined by them.

     

    Is this what you want?

    Please let us know.

     

    Regards,

    JC

    Translate
    anthonyl89052030
    Inspiring
    Feb 11, 2025 6:01 PM UTC

    Hey - thanks for the response. So I should have been more specific! I was thinking more non-uniform shapes, not a square or  rectangle (as you say I could get their position/height width and calculate what I require). Thanks!

    Translate
    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    Feb 11, 2025 6:53 PM UTC

    Thanks for the clarification.

     

    So I guess you'll have to iterate over the vertices of a shape instance and look online for an algorithm that lets you find a random point inside of the polygon defined by them.

     

    var shape = fl.getDocumentDOM().selection[0];
    var points = [];
    var i;
    var total = shape.edges.length;
    
    for (i = 0; i < total; i++)
    {
    	var hEdge = shape.edges[i].getHalfEdge(0);
    	var vertex = hEdge.getVertex();
    	
    	points.push({ x: vertex.x, y: vertex.y });
    }

     

     

    Translate