How to create more than one canvas with p5?

南楼画角 提交于 2021-01-29 06:32:34

问题


I want to try some things with stereoscopy in 3 dimensions and I need 2 canvas but I find nothing.

I've tried to use canvas1.background(200);

function setup() {
    let canvas1 = createCanvas(100, 100);
    canvas1.position(0,0);
}
function draw() {
//for canvas 1
    background(100);
    rotateX(frameCount * 0.01);
    rotateZ(frameCount * 0.01);
    cone(30, 50);
}
function setup() {
    let canvas2 = createCanvas(100, 100);
    canvas2.position(100,0);
}
function draw() {
//for canvas 2
    background(100);
    rotateX(frameCount * 0.01);
    rotateZ(frameCount * 0.02);
    cone(30, 50);
}

回答1:


To use multiple canvases you will need to use instance mode

The main difference from your code is that each set of p5.js methods will be inside of a function and all calls to p5.js methods will need to be called by the instance of p5 that is passed into the function.

var s1 = function( sketch ) {
  sketch.setup = function() {
    let canvas1 = sketch.createCanvas(100, 100, sketch.WEBGL);
    canvas1.position(0,0);
  }
  sketch.draw = function() {
    //for canvas 1
    sketch.background(100);
    sketch.rotateX(sketch.frameCount * 0.01);
    sketch.rotateZ(sketch.frameCount * 0.01);
    sketch.cone(30, 50);
  }
};

// create a new instance of p5 and pass in the function for sketch 1
new p5(s1);

var s2 = function( sketch ) {

   sketch.setup = function() {
    let canvas2 = sketch.createCanvas(100, 100, sketch.WEBGL);
    canvas2.position(100,0);
  }
  sketch.draw = function() {
    //for canvas 2
    sketch.background(100);
    sketch.rotateX(sketch.frameCount * 0.01);
    sketch.rotateZ(sketch.frameCount * 0.02);
    sketch.cone(30, 50);
  }
};

// create the second instance of p5 and pass in the function for sketch 2
new p5(s2);
<script src='https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.min.js'></script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/addons/p5.dom.min.js"></script>


来源:https://stackoverflow.com/questions/55879820/how-to-create-more-than-one-canvas-with-p5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!