如何调整p5.Graphic对象的大小?

问题描述:

p5.js resizeCanvas(x, y)是p5对象的函数,但如果我创建了一个p5.Graphics对象,我可以使用类似函数调整它的大小吗?如何调整p5.Graphic对象的大小?

有趣的是,p5.Graphics对象可以运行resizeGraphics()但没有任何反应(包括没有错误),并且控制台中的高度和宽度保持不变。

g = createGraphics(50, 50); //creates p5.Graphics 
g.resizeCanvas(100, 100);  //fails: silently without error 
g.resize(100, 100);   //fails: resize has not been defined 

难道还有其他功能或者我需要确实提取了cooresponding图形画布和调用本机javascript函数呢?

谢谢!

如果您想调整P5.Graphics的大小,您可以创建一个新的,然后将旧的绘制到新的。

Here是一个例子:

var pg; 

function setup() { 
    createCanvas(1000, 1000); 
    pg = createGraphics(100, 100); 
    pg.background(100); 
    pg.noStroke(); 
    pg.ellipse(pg.width/2, pg.height/2, pg.width, pg.height); 
} 

function draw() { 
    background(200); 
    image(pg, 0, 0); 
} 

function mouseClicked(){ 
    var newPG = createGraphics(mouseX, mouseY); 
    newPG.image(pg, 0, 0, newPG.width, newPG.height); 
    pg = newPG; 
} 
+0

检查的元件,之后'的createGraphics()'不实际创建画布。它只是设置为隐形。我可以通过调用'pg.canvas.remove()'来删除它。这很容易做,但我不确定是否有更干净的方法来做到这一点。 – CrazyInfin8