# Demo sketches are stuttering / hanging up

**URL:** <https://discourse.processing.org/t/demo-sketches-are-stuttering-hanging-up/41268>\
**Category:** Beginners\
**Created:** [March 13, 2023, 6:28pm UTC](https://discourse.processing.org/t/demo-sketches-are-stuttering-hanging-up/41268 "2023-03-13T18:28:13Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 15, 2023, 1:38am UTC](https://discourse.processing.org/t/demo-sketches-are-stuttering-hanging-up/41268/4 "2023-03-15T01:38:34Z")

</div>

Hello @User442B,

Try this code with and without P2D (or P3D):

```auto
/**
 * Bounce. 
 * 
 * When the shape hits the edge of the window, it reverses its direction. 
 */
 
 import processing.javafx.*;
 
int rad = 60; // Width of the shape
float xpos, ypos; // Starting position of shape    

float xspeed = 1.4; // Speed of the shape
float yspeed = 1.1; // Speed of the shape

int xdirection = 1; // Left or Right
int ydirection = 1; // Top to Bottom

int tlast;

boolean toggle = false;

void setup() 
  {
  //size(640, 760, P2D);
  size(640, 760);
  println("Renderer: " + g.getClass().getName());  

  frameRate(30);
  ellipseMode(RADIUS);
  // Set the starting position of the shape
  xpos = width/2;
  ypos = height/4;
  tlast = millis();
  background(102);
}

void draw() 
{
  //background(102);
  fill(102);
  noStroke();
  rect(0, 0, width, height-400);
  int tnow = millis();
  int tdif = tnow-tlast;
  int y = height-5*(tdif-33)-200;
  int tmp = frameCount%width;
  int x = tmp;
  if (tmp == 0)
    background(102);
  tlast = tnow;
  //println(x, tdif);
  stroke(255, 255, 0);
  strokeWeight(abs(tdif-33));
  point(x, y);
  
  // Update the position of the shape
  xpos = xpos + ( xspeed * xdirection );
  ypos = ypos + ( yspeed * ydirection );
  
  // Test to see if the shape exceeds the boundaries of the screen
  // If it does, reverse its direction by multiplying by -1
  if (xpos > width-rad || xpos < rad) 
    {
    xdirection *= -1;
    }
  if (ypos > height-rad-400 || ypos < rad) 
    {
    ydirection *= -1;
    }

  // Draw the shape
  fill(255);
  noStroke();
  ellipse(xpos, ypos, rad, rad);
  }

```

For frameRate(30) the expected period T = 1/30 = 33.33 ms  
The above code plots the deviation from 33 ms.

size(640, 760) output:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/9/5949981533082ae71629e91e83fa2c94f0e3de20.png)

size(640, 760, P3D) output:

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

Another exploration of this I did in the past:

> [@FrameRate hiccups](https://discourse.processing.org/t/framerate-hiccups/11947/20):
>
> I’d be interested what you see with framerate set to 120, or 240 or higher, or maybe even not multiples of 60. JOGL should lock to vsync so should end up with a similar output framerate on a 60Hz display. I think there may be some clock drift going on between the timer and GPU though. I wonder what this would be like on a 75Hz display too?!

Just an exploration…

P2D and P3D use OPENGL which can use hardware rendering on GPU.

References:

> **[Render Techniques](https://processing.org/tutorials/rendering/)**
>
> Tools for rendering geometries in Processing.

> **[OpenGL](https://en.wikipedia.org/wiki/OpenGL)**
>
> OpenGL (Open Graphics Library) is a cross-language, cross-platform application programming interface (API) for rendering 2D and 3D vector graphics. The API is typically used to interact with a graphics processing unit (GPU), to achieve hardware-accelerated rendering.
> Silicon Graphics, Inc. (SGI) began developing OpenGL in 1991 and released it on June 30, 1992; applications use it extensively in the fields of computer-aided design (CAD), virtual reality, scientific visualization, information vi...

`:)`

---

_[View the full topic](https://discourse.processing.org/t/demo-sketches-are-stuttering-hanging-up/41268)._
