# Faster way to draw part of an image?

**URL:** https://discourse.processing.org/t/faster-way-to-draw-part-of-an-image/38140
**Category:** Coding Questions
**Created:** [July 28, 2022, 9:55pm UTC](https://discourse.processing.org/t/faster-way-to-draw-part-of-an-image/38140 "2022-07-28T21:55:33Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![canslp](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@canslp](https://discourse.processing.org/u/canslp)
#### Post date: [July 28, 2022, 9:55pm UTC](https://discourse.processing.org/t/faster-way-to-draw-part-of-an-image/38140/1 "2022-07-28T21:55:33Z")

</div>

i need to be able to draw the top part of an image, here’s what i have

```auto
void drawImageSplice(PGraphics pg,PImage img,int x,int y,int w,int h,float cut){
  int yp = round(h*cut);
  if(yp>0){
    PGraphics buffer = createGraphics(w,yp);
    buffer.beginDraw();
    buffer.image(img,0,0,w,h);
    buffer.endDraw();
    pg.image(buffer,x,y,w,yp);
  }
}

```

this works great locally but in a live html canvas it’s very slow. is there a faster way to do this? thanks

---

<div class="post-metadata">

### Author: ![Shwaa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shwaa/32/9696_2.png) [@Shwaa](https://discourse.processing.org/u/Shwaa)
#### Post date: [July 29, 2022, 3:28am UTC](https://discourse.processing.org/t/faster-way-to-draw-part-of-an-image/38140/2 "2022-07-29T03:28:04Z")

</div>

The [clip](https://processing.org/reference/clip_.html) function may be of use, since it allows you to temporarily draw only in specific rectangle on screen

---

<div class="post-metadata">

### Author: ![canslp](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@canslp](https://discourse.processing.org/u/canslp)
#### Post date: [July 29, 2022, 4:12pm UTC](https://discourse.processing.org/t/faster-way-to-draw-part-of-an-image/38140/3 "2022-07-29T16:12:09Z")

</div>

oh that’s perfect thanks

---

<div class="post-metadata">

### Author: ![canslp](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@canslp](https://discourse.processing.org/u/canslp)
#### Post date: [July 29, 2022, 4:17pm UTC](https://discourse.processing.org/t/faster-way-to-draw-part-of-an-image/38140/4 "2022-07-29T16:17:03Z")

</div>

wait apparently this doesn’t work in html at all. like it just stops the program immediately

---

<div class="post-metadata">

### Author: ![canslp](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@canslp](https://discourse.processing.org/u/canslp)
#### Post date: [July 29, 2022, 4:18pm UTC](https://discourse.processing.org/t/faster-way-to-draw-part-of-an-image/38140/5 "2022-07-29T16:18:52Z")

</div>

it works great in local processing but in live html with a processing canvas it goes blank immediately. even just calling noClip() breaks the canvas.

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/0/8019a7ecc334a79621b37c65eb4b1494570a18a3.png)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/e/ae608598e31983e4acb6a261772024f9a29fd746.png)

---

<div class="post-metadata">

### Author: ![Shwaa](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shwaa/32/9696_2.png) [@Shwaa](https://discourse.processing.org/u/Shwaa)
#### Post date: [July 30, 2022, 1:41pm UTC](https://discourse.processing.org/t/faster-way-to-draw-part-of-an-image/38140/6 "2022-07-30T13:41:01Z")

</div>

Then an alternative which avoids making new PGraphics is the [copy()](https://processing.org/reference/copy_.html) method, which allows you to copy a certain rectangle of an image using the underlying pixels array. This may be more memory efficient, since you are not creating a new PGraphics every time you draw the image, but it likely isn’t much faster since you are copying pixels rather than drawing an image using image(). I haven’t tested the speed of this function, but it may be worth a shot.

---

<div class="post-metadata">

### Author: ![canslp](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@canslp](https://discourse.processing.org/u/canslp)
#### Post date: [July 30, 2022, 3:27pm UTC](https://discourse.processing.org/t/faster-way-to-draw-part-of-an-image/38140/7 "2022-07-30T15:27:49Z")

</div>

do you know if clip() can be used in html. i still can’t get it to run for some reason
