# P5.js: Save only part of the canvas

**URL:** https://discourse.processing.org/t/p5-js-save-only-part-of-the-canvas/7286
**Category:** Coding Questions
**Created:** [January 7, 2019, 8:12pm UTC](https://discourse.processing.org/t/p5-js-save-only-part-of-the-canvas/7286 "2019-01-07T20:12:37Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)
#### Post date: [January 7, 2019, 8:12pm UTC](https://discourse.processing.org/t/p5-js-save-only-part-of-the-canvas/7286/1 "2019-01-07T20:12:37Z")

</div>

How can I save/download only a part of the canvas (save a cropped part of the whole image)?

e.g.

```auto
function setup() {
	createCanvas(windowWidth, windowHeight);
	background(255);

}

function draw() {
	ellipse(mouseX, mouseY, 20, 20);
}

function keyPressed(){
	if(key === 's'){
        	saveFrame(frameCount, "png"); 
        }    
}

```

Here the browser will prompt you to (or automatically start to) download an image of the entire sketch–is there any way I can only save a specific part?

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [January 7, 2019, 8:29pm UTC](https://discourse.processing.org/t/p5-js-save-only-part-of-the-canvas/7286/2 "2019-01-07T20:29:19Z")

</div>

Put the part you want to save into a PImage using `get()`, then save it:

```auto
PImage to_save = get( 20, 30, 100, 200 ); // Grab an image of a 100x200 rectangle at (20,30).
to_save.save("saved_name.png");

```

---

<div class="post-metadata">

### Author: ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)
#### Post date: [January 7, 2019, 8:30pm UTC](https://discourse.processing.org/t/p5-js-save-only-part-of-the-canvas/7286/3 "2019-01-07T20:30:52Z")

</div>

Ah! Thanks @TfGuy44.

---

<div class="post-metadata">

### Author: ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)
#### Post date: [January 9, 2019, 5:50pm UTC](https://discourse.processing.org/t/p5-js-save-only-part-of-the-canvas/7286/4 "2019-01-09T17:50:15Z")

</div>

This worked for me:

```auto
var saveCanvas;

function setup(){
  ...
  saveCanvas = createGraphics(height, height);
}

function draw(){
  ...
}

function keyPressed() {
  if (key == 's') {
    let c = get(width/2-height/2,0, height, height);
    saveCanvas.image(c, 0, 0);
    save(saveCanvas, frameCount+".png");
  }
}

```

---

<div class="post-metadata">

### Author: ![TheWizardBear](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/thewizardbear/32/3552_2.png) [@TheWizardBear](https://discourse.processing.org/u/TheWizardBear)
#### Post date: [January 9, 2019, 7:53pm UTC](https://discourse.processing.org/t/p5-js-save-only-part-of-the-canvas/7286/5 "2019-01-09T19:53:15Z")

</div>

Though for some reason the images from this are of lower quality than a regular save()?
