# Save png function keyPressed not working!

**URL:** https://discourse.processing.org/t/save-png-function-keypressed-not-working/32834
**Category:** Libraries
**Created:** [October 14, 2021, 7:50pm UTC](https://discourse.processing.org/t/save-png-function-keypressed-not-working/32834 "2021-10-14T19:50:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![u\_ro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/u_ro/32/14713_2.png) [@u\_ro](https://discourse.processing.org/u/u_ro)
#### Post date: [October 14, 2021, 7:50pm UTC](https://discourse.processing.org/t/save-png-function-keypressed-not-working/32834/1 "2021-10-14T19:50:13Z")

</div>

hey guys,  
silly question: I am not able to save a png in this sketch, I cannot understand why…

```auto
saveCount = 0;
var mic, fft, spectrum = [];
var X,Y;
var spectrumBuffer = [];
var depth = 20;
var length = 40;
var spacing = 10;
var girth = 30;

function setup() {
  pixelDensity(5);
  X = windowWidth;
  Y = windowHeight;
  mic = new p5.AudioIn();
  mic.start();
  fft = new p5.FFT();
  fft.setInput(mic);
  createCanvas(X, Y,WEBGL);
  background(0);
  setFrameRate(60);
  spectrum = fft.analyze();
  
  //Nested array declaration
  for(var k=0; k<depth;k++){
   		 spectrumBuffer[k] = [];
  }
 ambientLight(100);
 pointLight(250, 250, 250, 100, 100, 0);
}

function draw() {
  orbitControl();
  spectrum = fft.analyze();
  spectrumBuffer[0] = subset(spectrum, 0, length);
  //Array shift
  for(var k=depth-1; k>0;k--){
    spectrumBuffer[k] = spectrumBuffer[k-1];
  }
  translate(spacing*length/2,0);
  rotateX(.5);
  rotateY(0);
  for(var k=0; k<depth;k++){
    rotateX(-.0005);
    translate(-1* spacing * length, 0, -spacing);
      for (var i = 0; i<length; i++)
      {
          rotateZ(.0000003*(X/2-mouseX));
          rotateX(.0000003*(Y/2-mouseY));
          var curr = spectrumBuffer[k][i];
          ambientMaterial(exp(curr/25),exp(curr/25),curr);
          translate(0,-1*curr/2);
          //box(girth, curr, 0);
          box(exp(curr/150));
          translate(spacing, curr/2);
      }
    }
}

function mouseWheel(event) {
  if(length+event.delta/10<=500 && length+event.delta/10>0){
   length += floor(event.delta/10);
  }
}
function keyPressed() {
  if (key == 'z') {
    save("screenshot" + saveCount + ".png");
    saveCount++;
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [October 14, 2021, 8:49pm UTC](https://discourse.processing.org/t/save-png-function-keypressed-not-working/32834/2 "2021-10-14T20:49:16Z")

</div>

Where do run this, in the p5 editor?

Not sure about save() here

---

<div class="post-metadata">

### Author: ![u\_ro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/u_ro/32/14713_2.png) [@u\_ro](https://discourse.processing.org/u/u_ro)
#### Post date: [October 14, 2021, 9:49pm UTC](https://discourse.processing.org/t/save-png-function-keypressed-not-working/32834/3 "2021-10-14T21:49:57Z")

</div>

I run it on Firefox, it usually works on other sketches!

---

<div class="post-metadata">

### Author: ![dethe](https://avatars.discourse-cdn.com/v4/letter/d/439d5e/32.png) [@dethe](https://discourse.processing.org/u/dethe)
#### Post date: [November 4, 2021, 1:33am UTC](https://discourse.processing.org/t/save-png-function-keypressed-not-working/32834/4 "2021-11-04T01:33:16Z")

</div>

Reading the docs on `save()` it works on DOM elements, images, JSON, tables, and sounds. I would expect it to work on a 2D Canvas, which is basically a drawable image, but would be kind of surprised if it worked on WEBGL content (or at least, not surprised if it doesn’t). Were your sketches that worked with `save()` also using WEBGL?
