We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am using instance mode to have multiple canvases on a page. Is there a way to have functions that draw shapes available to either canvas? For example, in the code below, how can I make the drawLine() function work when called?
var l = function( p ) {
p.setup = function() {
p.createCanvas(400, 600);
};
p.draw = function() {
drawLine();
};
};
function drawLine(){
line(0,0,100,100);
}
My other solution is to change my functions so they just output numbers, which then get accessed and draw within the draw loop, but if anyone has a solution to the above problem, it would help me a lot! Thanks!
Answers
<meta charset=utf8> <meta name=viewport content=width=device-width,user-scalable=no,initial-scale=1> <style>body { padding: 0; margin: auto; display: block; width: 400px; height: 600; border: 5px solid green; }</style> <script src=http://p5js.org/js/p5.min.js></script> <script> // forum.Processing.org/two/discussion/13348/drawing-functions-in-instance-mode // 2015-Nov-01 const sketch = function (p) { p.setup = function () { p.createCanvas(400, 600); p.background(0350).stroke('red').strokeWeight(5).noLoop(); }; p.draw = function () { drawLine(p); }; }; new p5(sketch); function drawLine(p) { p.line(0, 0, p.width, p.height); } </script>Thanks! I tried a bunch of things to link the function with the canvas to no avail, but this works perfectly.