processing / p5.js

p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Processing. http://twitter.com/p5xjs —
http://p5js.org/
GNU Lesser General Public License v2.1
21.06k stars 3.21k forks source link

Should P5.Graphics support set() with another P5.Graphics? #2984

Open KevinWorkman opened 6 years ago

KevinWorkman commented 6 years ago

Nature of issue?

Most appropriate sub-area of p5.js?

Which platform were you using when you encountered this?

Details about the bug:

From this Stack Overflow question.

It appears that the set() function does not work with P5.Graphics values. Is this expected?

let redRect;

function setup() {
  createCanvas(100,100);
  redRect = createGraphics(100,100);
  redRect.fill(255, 0, 0);
  redRect.rect(20,20,40,40);

  const blueRect = createGraphics(20, 20);
  blueRect.background(0, 0, 255);
  redRect.set(30, 30, blueRect);
  redRect.updatePixels();
}

function draw() {
  background(0, 255, 0);
  image(redRect,0,0);
}

I would expect this to create a graphics that contains a red rectangle, and then draw a blue rectangle to that first graphics. Instead, only a single pixel is set, and it's set to transparent?

red rectangle on green background

Ajayneethikannan commented 5 years ago

I think that this feature should be present . The reason why it is setting a single transparent pixel is the current set method checks whether the third parameter imgOrCol is an p5.Image instance or a color value. In case it is neither if them, it sets the value of the pixel mentioned as [0, 0, 0, 0]. We can check its implementation here .

We can check if the imgOrCol parameter is p5.Image instance or p5.Graphics instance, then we can set the pixels of one p5.Graphics instance from another.

Also , if we pass a image as the third parameter set(x, y, image) the image fills the entire canvas from x, y onwards. Is this the intended behavior ?
Should we pass the height and width here as

this.drawingContext.drawImage(imgOrCol.canvas, x, y, imgOrCol.width, imgOrCol.height);

Would love your suggestions , Thank you!