Last active
December 30, 2015 20:29
-
-
Save naraga/7881540 to your computer and use it in GitHub Desktop.
AngularJS+SVG. Simple vs scope inheritance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- SVG starter: http://www.w3schools.com/svg/tryit.asp?filename=trysvg_line --> | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml" ng-app> | |
<head> | |
<title>AngularJS is more than boring datagrids - bouncing line</title> | |
</head> | |
<body> | |
<svg height="{{config.height}}" width="{{config.width}}" ng-controller='LineController'> | |
<line x1="{{line.x1}}" y1="{{line.y1}}" x2="{{line.x2}}" y2="{{line.y2}}" style="stroke:rgb(255,0,0);stroke-width:2" /> | |
Sorry, your browser does not support inline SVG. | |
</svg> | |
<script src="Scripts/angular.js"></script> | |
<script> | |
function LineController($scope, $timeout) { | |
$scope.config = { height: 210, width: 500 }; | |
$scope.line = { x1: 0, y1: 0, x2: 0, y2: 0 }; | |
var setNewRandomPosition = function() { | |
setLine( | |
getRandom(0, $scope.config.width), | |
getRandom(0, $scope.config.height), | |
getRandom(0, $scope.config.width), | |
getRandom(0, $scope.config.height)); | |
}; | |
setNewRandomPosition(); | |
var increments = { x1: 1, y1: 1, x2: 1, y2: 1}; | |
var adjustLinePosition = function () { | |
var getNewIncrement = function(v, min, max, actualInc) { | |
return (v == min || v==max) ? -actualInc : actualInc; | |
}; | |
increments.x1 = getNewIncrement($scope.line.x1, 0, $scope.config.width, increments.x1); | |
increments.y1 = getNewIncrement($scope.line.y1, 0, $scope.config.height, increments.y1); | |
increments.x2 = getNewIncrement($scope.line.x2, 0, $scope.config.width, increments.x2); | |
increments.y2 = getNewIncrement($scope.line.y2, 0, $scope.config.height, increments.y2); | |
setLine( | |
$scope.line.x1 + increments.x1, | |
$scope.line.y1 + increments.y1, | |
$scope.line.x2 + increments.x2, | |
$scope.line.y2 + increments.y2); | |
}; | |
var scheduleNewPosition = function() { | |
$timeout(function() { | |
adjustLinePosition(); | |
scheduleNewPosition(); | |
}, 10, true); | |
}; | |
scheduleNewPosition(); | |
function setLine(x1, y1, x2, y2) { | |
$scope.line.x1 = x1; | |
$scope.line.y1 = y1; | |
$scope.line.x2 = x2; | |
$scope.line.y2 = y2; | |
} | |
function getRandom(min, max) { | |
return Math.floor(Math.random() * (max - min + 1) + min); | |
} | |
} | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- SVG starter: http://www.w3schools.com/svg/tryit.asp?filename=trysvg_line --> | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml" ng-app> | |
<head> | |
<title>AngularJS is more than boring datagrids - bouncing lineS</title> | |
</head> | |
<body> | |
<svg height="{{config.height}}" width="{{config.width}}" ng-controller='GameController'> | |
<line x1="{{line.x1}}" y1="{{line.y1}}" x2="{{line.x2}}" y2="{{line.y2}}" style="stroke:rgb(255,0,0);stroke-width:2" ng-controller='LineController'/> | |
<line x1="{{line.x1}}" y1="{{line.y1}}" x2="{{line.x2}}" y2="{{line.y2}}" style="stroke:rgb(255,0,0);stroke-width:2" ng-controller='LineController'/> | |
<line x1="{{line.x1}}" y1="{{line.y1}}" x2="{{line.x2}}" y2="{{line.y2}}" style="stroke:rgb(255,0,0);stroke-width:2" ng-controller='LineController'/> | |
<line x1="{{line.x1}}" y1="{{line.y1}}" x2="{{line.x2}}" y2="{{line.y2}}" style="stroke:rgb(255,0,0);stroke-width:2" ng-controller='LineController'/> | |
<line x1="{{line.x1}}" y1="{{line.y1}}" x2="{{line.x2}}" y2="{{line.y2}}" style="stroke:rgb(255,0,0);stroke-width:2" ng-controller='LineController'/> | |
Sorry, your browser does not support inline SVG. | |
</svg> | |
<script src="angular.js"></script> | |
<script> | |
function GameController($scope) { | |
$scope.config = { height: 210, width: 500 }; | |
} | |
function LineController($scope, $timeout) { | |
$scope.line = { x1: 0, y1: 0, x2: 0, y2: 0 }; | |
var setNewRandomPosition = function() { | |
setLine( | |
getRandom(0, $scope.config.width), | |
getRandom(0, $scope.config.height), | |
getRandom(0, $scope.config.width), | |
getRandom(0, $scope.config.height)); | |
}; | |
setNewRandomPosition(); | |
var increments = { x1: 1, y1: 1, x2: 1, y2: 1}; | |
var adjustLinePosition = function () { | |
var getNewIncrement = function(v, min, max, actualInc) { | |
return (v == min || v==max) ? -actualInc : actualInc; | |
}; | |
increments.x1 = getNewIncrement($scope.line.x1, 0, $scope.config.width, increments.x1); | |
increments.y1 = getNewIncrement($scope.line.y1, 0, $scope.config.height, increments.y1); | |
increments.x2 = getNewIncrement($scope.line.x2, 0, $scope.config.width, increments.x2); | |
increments.y2 = getNewIncrement($scope.line.y2, 0, $scope.config.height, increments.y2); | |
setLine( | |
$scope.line.x1 + increments.x1, | |
$scope.line.y1 + increments.y1, | |
$scope.line.x2 + increments.x2, | |
$scope.line.y2 + increments.y2); | |
}; | |
var scheduleNewPosition = function() { | |
$timeout(function() { | |
adjustLinePosition(); | |
scheduleNewPosition(); | |
}, 10, true); | |
}; | |
scheduleNewPosition(); | |
function setLine(x1, y1, x2, y2) { | |
$scope.line.x1 = x1; | |
$scope.line.y1 = y1; | |
$scope.line.x2 = x2; | |
$scope.line.y2 = y2; | |
} | |
function getRandom(min, max) { | |
return Math.floor(Math.random() * (max - min + 1) + min); | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment