gpt4 book ai didi

javascript - 如何使用p5创建多个 Canvas ?

转载 作者:行者123 更新时间:2023-12-02 23:45:27 32 4
gpt4 key购买 nike

我想尝试一些 3 维立体视觉的东西,我需要 2 个 Canvas ,但我什么也没找到。

我尝试使用 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);
}

最佳答案

要使用多个 Canvas ,您需要使用 instance mode

与您的代码的主要区别在于,每组 p5.js 方法都将位于函数内部,并且对 p5.js 方法的所有调用都需要由传递到函数中的 p5 实例来调用。

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>

关于javascript - 如何使用p5创建多个 Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55879820/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com