Size of canvas controlP5

Hi everyone,

I have been trying to change the size of the canvas in the controlP5 canvas example. The method addcanvas seems to have a setPositions() but not a setSize(). I wonder if anyone knows how to change the size?
I did look into putting the canvas in a group which seems to support setSize but I do not like the extendable and retractable nature of the group.

here is the code from controlP5 canvas example ( don’t mind the plot stuff in the update function):

/**
 * ControlP5 Canvas
 * The ControlWindowCanvas allow you to add custom graphics to 
 * the default controlP5 renderer or a controlWindow rednerer.
 *
 * find a list of public methods available for the Canvas Controller 
 * at the bottom of this sketch's source code
 *
 * by Andreas Schlegel, 2011
 * www.sojamo.de/libraries/controlp5
 * 
 */


import controlP5.*;
import grafica.*;

ControlP5 cp5;

ControlWindow controlWindow;

Canvas cc;

// MyCanvas, your Canvas render class
class MyCanvas extends Canvas {

  int y;
  int mx = 0;
  int my = 0;
  GPlot plot;
  public void setup(PGraphics pg) {
    y = 200;
  }  

  public void update(PApplet p) {
    plot = new GPlot(p);
    plot.setPos(mx, my);
    plot.setDim(100, 100);
    // Set the plot title and the axis labels
    plot.setTitleText("Exponential law");
    plot.getXAxis().setAxisLabelText("x");
    plot.getYAxis().setAxisLabelText("y");

    //plot.setPoints(points);
    plot.setLineColor(color(0, 0, 0));
    plot.setPointColor(color(100, 100, 255));
    plot.activatePointLabels();
    //plot.activatePanning();
    //plot.activateZooming(1.1, CENTER, CENTER);
    
    plot.beginDraw();
    plot.drawBackground();
    plot.drawBox();
    plot.drawXAxis();
    plot.drawYAxis();
    plot.drawTopAxis();
    plot.drawRightAxis();
    plot.drawTitle();
    plot.drawLines();
    plot.drawLabels();
    plot.drawGridLines(GPlot.BOTH);
    plot.drawPoints();
    plot.endDraw();
    
    //mx = p.mouseX;
    //my = p.mouseY;
  }

  public void draw(PGraphics pg) {
    // renders a square with randomly changing colors
    // make changes here.
    //pg.background(0);
    //pg.fill(0);
    //pg.rect(20, 20, 240, 30);
    //pg.fill(255);
    //pg.text("This text is drawn by MyCanvas", 0,0);
  }
}


void setup() {
  size(600, 600);
  frameRate(30);
  cp5 = new ControlP5(this);

  // create a control window canvas and add it to
  // the previously created control window.  
  cc = new MyCanvas();
  cc.pre(); // use cc.post(); to draw on top of existing controllers.
  cp5.addCanvas(cc) // add the canvas to cp5
     .setPosition(100,400)
     .setSize(100,100); // Does not work
}

void draw() {
  background(255);
  //fill(60);
  //rect(100, 100, 200, 200);
}


/*
a list of all methods available for the Canvas Controller
use ControlP5.printPublicMethodsFor(Canvas.class);
to print the following list into the console.

You can find further details about class Canvas in the javadoc.

Format:
ClassName : returnType methodName(parameter type)


controlP5.Canvas : void moveTo(ControlWindow) 
controlP5.Canvas : void setup(PGraphics) 
controlP5.Canvas : void update(PApplet) 
java.lang.Object : String toString() 
java.lang.Object : boolean equals(Object) 

created: 2015/03/24 12:20:53

*/

It seems this is setting your size. The source code is not complicated for this one. CHeck it out: controlp5/src/controlP5/Canvas.java at master · sojamo/controlp5 · GitHub

Kf

1 Like

Thanks for that @kfrajer