gpt4 book ai didi

javascript - P5图库动画

转载 作者:行者123 更新时间:2023-12-05 00:31:56 25 4
gpt4 key购买 nike

我正在尝试动态加载一组图像并以恒定速度在 Z 方向上平移它们,同时为 X 设置随机值。和 Y我正在使用以下代码,但我被困在如何使用图像 texture 上属性(property)。
在渲染我使用的以下代码时,我得到了一个空白的黑色 Canvas 。任何帮助将不胜感激。

let imgs = [];
let imgs_arr = [];
let num = 15;

function preload() {
for (let i = 0; i < num; i++) {
imgs[i] = loadImage("https://picsum.photos/400/400/?image=" + i * 10);
}
}

function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
colorMode(HSB, 360, 100, 100, 100);
for (let i = 0; i < num; i++) {
let x = random(-width / 2, width / 2);
let y = random(-height / 2, height / 2);
let z = random(-width*5, width/2);
let texture = new Type(imgs[i], x, y, z)
imgs_arr.push(texture);
}
}

function draw() {
background(0,0,0);
orbitControl();
for (let i = 0; i < num; i++) {
imgs_arr[i].update();
imgs_arr[i].display();
}
}

class Type {
constructor(_img, _x, _y, _z) {
this.img = _img;
this.x = _x;
this.y = _y;
this.z = _z;
}

update() {
this.z += 10;
if(this.z > width/2){
this.z = -width*5;
}
}


display() {
push();
translate(this.x, this.y, this.z);
texture(this.img)
pop();
}
}

最佳答案

来自 reading the docs看起来好像缺少一个关键组件。假设您想在一个盒子上绘制图像,您需要在纹理之后添加以下内容:

box(width / 2);
这是正在运行的示例:

let imgs = [];
let imgs_arr = [];
let num = 15;

function preload() {
for (let i = 0; i < num; i++) {
imgs[i] = loadImage("https://picsum.photos/400/400/?image=" + i * 10);
}
}

function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
colorMode(HSB, 360, 100, 100, 100);
for (let i = 0; i < num; i++) {
let x = random(-width / 2, width / 2);
let y = random(-height / 2, height / 2);
let z = random(-width*5, width/2);
let texture = new Type(imgs[i], x, y, z)
imgs_arr.push(texture);
}
}

function draw() {
background(0,0,0);
orbitControl();
for (let i = 0; i < num; i++) {
imgs_arr[i].update();
imgs_arr[i].display();
}
}

class Type {
constructor(_img, _x, _y, _z) {
this.img = _img;
this.x = _x;
this.y = _y;
this.z = _z;
}

update() {
this.z += 10;
if(this.z > width/2){
this.z = -width*5;
}
}


display() {
push();
translate(this.x, this.y, this.z);
texture(this.img)
box(width / 2);
pop();
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js" integrity="sha512-N4kV7GkNv7QR7RX9YF/olywyIgIwNvfEe2nZtfyj73HdjCUkAfOBDbcuJ/cTaN04JKRnw1YG1wnUyNKMsNgg3g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

关于javascript - P5图库动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69386745/

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