We are about to switch to a new forum software. Until then we have removed the registration on this forum.
So, I am working with AngularJS website for medical big data visualization @ UCSF and trying to use p5.js. I hope to bind the canvas only to a specific part like so below but its not working.
What am I missing?
html
<body>
<div id="canvasHolder"></div>
</body>
CSS
#canvasHolder{
width:700px;
height:450px;
position: absolute;
margin: auto;
left: 0;
right: 0;
top:0;
bottom:0;
}
Js
var canvas = createCanvas(width, height);
canvas.parent('canvasHolder');
Answers
Variables width & height are properties of the p5.Renderer created by createCanvas():
It's got nothing to do w/ the corresponding HTMLDivElement's own width & height CSS properties whose id is "canvasHolder":
In order to grab the latter, you're gonna need function select() from library "p5.dom.js":
http://p5js.org/reference/#/p5/select
It's gonna wrap the HTMLElement found as a p5.Element though:
From there, we can directly access its properties like width, height & elt! :-bd
Dunno anything 'bout AngularJS. But here's my attempt w/ regular HTML/CSS/JS: :P
@fkkcloud: I don't think select() is what you're looking for. There's a far simpler solution: look for the 'instance mode' example on this page. This allows you to specify a target element for the p5js canvas. It also has the advantage of limiting the scope of p5's core methods which otherwise are global. That's likely to be of more concern if you're rolling it into a page with other dependencies such as Angular.
If you override the native width/height of the canvas via css I'm guessing it will just stretch, but you might want to test for unforeseen consequences: e.g. Misaligned hit detection etc...
I guess another reason is that, canvasHoldler id can not be selected when its within ng-view since I am using AngularJS for routing pages..
Should I put sketch stuff in controller of that page?
Sorry I see that what you tried is more or less equivalent to my suggestion. I've not used angularJS (on my todo list) but if you're using it to manipulate the DOM you'll obviously have to ensure the target element is in place when you fire up p5...