【问题标题】:P5 image gallery animationP5图库动画
【发布时间】:2021-09-30 05:43:24
【问题描述】:

我正在尝试动态加载一组图像并以恒定速度沿 Z 方向平移它们,同时为 XY 提供随机值

我正在使用以下代码,但我不知道如何使用图像 texture 属性。

在渲染以下我使用的代码时,我得到一个空白的黑色画布。任何帮助将不胜感激。

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();
  }
}

【问题讨论】:

    标签: javascript processing webgl p5.js


    【解决方案1】:

    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();
      }
    }
    &lt;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"&gt;&lt;/script&gt;

    【讨论】:

    • 非常感谢!虽然这几乎是我想要实现的目标,但我更喜欢图像是 2D 的,而不是包裹在立方体周围的纹理。我该如何实施呢? box 是否有厚度属性?
    • 在这种情况下,你甚至不需要使用纹理,只需使用image(this.img, 0, 0);
    猜你喜欢
    • 2019-11-29
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 2023-01-19
    • 2018-03-30
    相关资源
    最近更新 更多