AngularJS Directives - Dynamic

by Varun Krishna P

HTML

<script src="http://code.angularjs.org/angular-1.0.0rc5.min.js"></script>
<body>

    <div ng-app="animateApp" ng-controller="AnimateCtrl">

            <div class="boundingBox">

               <div class="circle"
                    ng-repeat="shape in shapes"
                    ng-style="{ 'backgroundColor':shape.color, 'left':shape.x+'px', 'top':shape.y+'px' }"
                    />

            </div>

    </div>

    <script type="text/javascript">


    </script>

</body>

CSS

.boundingBox {
    width: 600px;
    height: 600px;
    background-color: #333333;
    margin:20px;
}


.circle {
    display:block;
    position:absolute;
    height: 20px;
    width: 20px;
    background-color: #999;
    -moz-border-radius: 15px;
    -webkit-border-radius: 15px;
    border-radius: 15px;
}

.box {
    display:block;
    position:absolute;
    height: 20px;
    width: 20px;

}

#controls {
    position: absolute;
    top: 620px;
}

JavaScript

var module = angular.module('animateApp', []);

function AnimateCtrl($scope, $defer) {

    function buildShape () {
        return {
            color     : '#' + (Math.random() * 0xFFFFFF << 0).toString(16),
            x         : Math.min(380,Math.max(20,(Math.random() * 380))),
            y         : Math.min(180,Math.max(20,(Math.random() * 180)))
        };
    };

    // Publish list of shapes on the $scope/presentationModel
    $scope.shapes   = [];

    // Create shapes
    for (i = 0; i < 100; i++) {
        $scope.shapes.push( buildShape() );
    }
}