Created
July 28, 2015 01:45
-
-
Save SIRHAMY/146f9069ccbe951efb1d to your computer and use it in GitHub Desktop.
P5.js in Angular Application Example
This file contains hidden or 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
angular.module('sirhamy', ['ngRoute', 'projects']); | |
angular.module('sirhamy').config( function($routeProvider) { | |
$routeProvider | |
.when('/main', { | |
templateUrl: 'views/main.html', | |
controller: 'MainCtrl', | |
controllerAs: 'control' | |
}) | |
.when('/phantsaver', { | |
templateUrl: 'modules/phantsaver-p5/phantsaver.html', | |
controller: 'PhantSaverCtrl', | |
}) | |
.otherwise({ | |
redirectTo: '/main' | |
}); | |
}) | |
angular.module('projects', []); |
This file contains hidden or 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
<html ng-app="sirhamy"> | |
<head> | |
<!--Stylesheets--> | |
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"> | |
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css"> | |
<link rel="stylesheet" type="text/css" href="css/main.css"> | |
<!--Begin Page Elements--> | |
<title>SIRHAMY</title> | |
</head> | |
<body> | |
<!--<div class="collapse navbar-collapse"></div>--> | |
<!--<div class="navbar navbar-default navbar-fixed-top" role="navigation"></div>--> | |
<div ng-view style="padding: 0; margin: 0;"></div> | |
<!--Libraries--> | |
<script src="lib/angular.min.js"></script> | |
<script src="lib/angular-route.min.js"></script> | |
<script src="lib/jquery.min.js"></script> | |
<script src="lib/bootstrap.min.js"></script> | |
<script src="lib/p5.min.js"></script> | |
<script src="lib/p5.dom.js"></script> | |
<!--Code--> | |
<script src="js/app.js"></script> | |
<!--Controllers--> | |
<script src="js/controllers/MainCtrl.js"></script> | |
<!--Modules--> | |
<script src="modules/phantsaver-p5/phantsaver.js"></script> | |
</body> | |
</html> |
This file contains hidden or 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
<div class="fix-top-left desc-text-white"> | |
<p>PhantSaver</p> | |
<p>Built using <a href="http://p5js.org/">P5.JS</a> and embedded in Angular page.</p> | |
<p>Original image credit: <a href="http://www.mindzai.net/elephant-gas-mask-mens-tee/"> Mindzai.net</a></p> | |
<p>Click to start/stop</p> | |
</div> | |
<a href="#/main"><img ng-src="rsrc/img/SIRHAMY-W-M.png" | |
class="img-responsive logo-bot-left"></a> | |
<div id="myCanvas"></div> |
This file contains hidden or 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
angular.module('projects').controller('PhantSaverCtrl', function($scope) { | |
//Start PHantSaver | |
var sketch = function(phant) { | |
var url; | |
var myCanvas; | |
var counter = 0; | |
var startingAngle=7; | |
var goRight = true; | |
var startUp = true; | |
var myFrameRate = 30; | |
var running = true; | |
phant.preload = function() { | |
elephantHead = phant.loadImage("modules/phantsaver-p5/Resources/ElephantHead.png"); | |
elephantTrunk = phant.loadImage("modules/phantsaver-p5/Resources/TrunkPart.png"); | |
HucciSymbol = phant.loadImage("modules/phantsaver-p5/Resources/HucciRoseGold.png"); | |
} | |
phant.setup = function() { | |
myCanvas = phant.createCanvas(screen.width, screen.height); | |
phant.frameRate(myFrameRate); | |
} | |
phant.draw = function() { | |
phant.background('#000000'); | |
phant.translate(screen.width/2, screen.height/2); //Set origin to bottom of elephant head | |
phant.image(elephantHead, -elephantHead.width/2, -elephantHead.height/2); //Place elephantHead in center of screen | |
phant.swing(10); | |
//Check if we should continue looping | |
url = phant.getURL(); | |
if(url.indexOf('phantsaver') === -1) { | |
phant.stopDraw(); | |
console.log(document.getElementById("defaultCanvas")); | |
document.getElementById("defaultCanvas").remove(); | |
console.log("URLChange"); | |
} | |
} | |
phant.swing = function(travelTime) | |
{ | |
var xVal = - (elephantTrunk.width/2) + elephantTrunk.width/8; | |
var yVal = (elephantHead.height/20); | |
if(counter===0 && startUp===true) | |
{ | |
phant.rotate(phant.radians(startingAngle)); | |
startUp = false; | |
} | |
//Place trunk on screen and determine rotation degree | |
for(var i = 0; i< 11; i++) | |
{ | |
if(goRight) phant.rotate(phant.radians(startingAngle- 2*startingAngle*( counter/(travelTime*myFrameRate)))); | |
else phant.rotate(phant.radians(-startingAngle+2*startingAngle*( counter/(travelTime*myFrameRate)))); | |
if(i<10) phant.image(elephantTrunk, xVal, yVal + elephantTrunk.height*i * 1.2); | |
else phant.image(HucciSymbol, xVal - 10, yVal + elephantTrunk.height*i * 1.3); | |
} | |
counter++; | |
//End condition for trunk travel | |
if(counter=== (travelTime*myFrameRate)) | |
{ | |
counter = 0; | |
goRight = !goRight; | |
} | |
} | |
phant.mouseClicked = function() { | |
if(running) phant.stopDraw(); | |
else phant.startDraw(); | |
} | |
phant.stopDraw = function() { | |
phant.noLoop(); | |
running = false; | |
} | |
phant.startDraw = function() { | |
phant.loop(); | |
running = true; | |
} | |
} | |
var myP5 = new p5(sketch); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: This is for AngularJS, not Angular.