Working with Elements

Categories:Common workflows

Using each() to arrange elements

Overview

Remember that ElementCollection extends Collection, so all the Collection methods are available on ElementCollection as well.

In the example, each() is used to arrange elements in a circular manner:

It's some basic trigonometry, a couple of custom parameters, and a bit of looping.

Code

function makeCircular(element, index, elements, radius, scale)
{
	radius = radius || 100;

	var angle = 360 * (index / elements.length);
	var radians = angle * (Math.PI / 180) + Math.PI;

	var x = Math.cos(radians) * radius;
	var y = Math.sin(radians) * radius;

	if(scale)
	{
		element.rotation = 0;
		element.scaleX = Math.PI * (radius * 2) / element.width / elements.length;
	}

	element.x = x;
	element.y = y;
	element.rotation = angle - 90;
}

$('*').each(makeCircular, [50, true]);

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>