Why I get `undefined` when I console.log `image(img, x, y)` but get a p5 object when i console.log `text(text, x, y)`?

Hello community,
my question is described in the title

let img;
function preload(){
  img = loadImage('https://placehold.co/600x400'); 
}

function setup() {
  createCanvas(600, 400);
  background(220);

  let word = text('this is a text', 0,0);
   console.log(word);  // will get a p5 object
 let picture = image(img, 0,0); 
   console.log(picture);  // will get `undefined `
}

Literally they’re all p5.js elements, why the results are different ?

I believe you’ve meant to log() variable picture, right?
console.log(picture);

When a method returns a reference to its caller object it’s said that method is chainable.

Library p5*js made a commitment to turn all of its API methods chainable if they didn’t return anything.

However, there are many methods still returning undefined due to poor oversight.

3 Likes

right, i d like to say console.log(picture); i modified in my question. Thanks a lot for the explanation!

1 Like

Forgot to answer that part: :face_with_peeking_eye:

Most of p5*js API methods are indeed of datatype p5.

However, there are other datatypes too, such as p5.Renderer returned by createCanvas(), p5.Image by loadImage() or p5.Element by createElement(), etc.

3 Likes