将 gif 添加到 p5.js "artwork"

Adding a gif to a p5.js "artwork"

我尝试将 p5.js 中的 gif 添加到我的 "artwork"(基本上是一张带有移动元素的图片)。

然而,我遇到了错误 "Uncaught TypeError: gifimage.position is not a function",尽管我认为我正在按照不同指南中的说明进行操作。

我试过这个:https://editor.p5js.org/kjhollen/sketches/S1bVzeF8Z

下面是代码(.......代表不影响错误的部分)。

var gifimage;

function preload() {
    .........
    gifimage = createImage("gifimage.gif");
}

function setup() {
    createCanvas(1920, 1080);
}

function draw() {
    .........
    if (keyIsPressed === true) {
        gifimage.position(500, 800);
    }
}

位置不是 p5.js Image. In order to use position you will need to create a img dom element with createImg

的函数

这里我修改了这个sketch来展示如何通过按键改变img的位置。

var gif_createImg;

function preload() {
  gif_createImg = createImg("vegetables.gif");
}

function setup() {
  createCanvas(500, 700);
  background(0);
}

function draw() {
    background(0);


  // updates animation frames by using an html
  // img element, positioning it over top of
  // the canvas.
  if (keyIsPressed){
    gif_createImg.position(50, 50);
  } else {
    gif_createImg.position(100,100);
  }
}

要使用 dom 功能,您需要包含 p5.js 库和 p5.dom.js 插件。

<script src='https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.min.js'></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.6/addons/p5.dom.js"></script>