我尝试在p5.js中添加一个gif到我的“图片”中(基本上是一个带有移动元素的图片)。
然而,即使我认为我是按照不同指南中的说明做的,我还是遇到了错误"Uncaught : 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);
}
}发布于 2019-05-07 21:37:34
位置不是p5.js Image的函数。为了使用位置,你需要用createImg创建一个img dom元素
在这里,我修改了这个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>https://stackoverflow.com/questions/56018372
复制相似问题