Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
p5.js: Let the computer draw something

I would like to let the computer draw something. It should look like a human is drawing something on a paper, with random values.

This is my try:

function setup() {
  createCanvas(500, 500);
  frameRate(30);
}

function draw() {
  x1 = random(500);
  y1 = random(500);
  x2 = random(500);
  y2 = random(500);
  line(x1, y1, x2, y2);
}
<script src="https://cdn.jsdelivr.net/npm/p5@1.4.0/lib/p5.js"></script>

So at the moment, it just puts random lines on top of each other. But it should result in one long connected scribble with curves, like this:

How is it possible to code it like that?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Hope it helps a little. Still not perfect but possible approach to play with and further advance ;-) Have a fun with

var x1, y1, x2=250, y2=250, maxVal = 500;

function setup() {
  createCanvas(maxVal, maxVal);
  frameRate(30);
}

// just uncomment one line, comment another and do restart please
// var a = 2, b = 1;
 var a = 40, b = 20;
// var a = 100, b = 50;
//var a = 200, b = 100;

function draw() {

  function tryFix(val) {

    let res = val < 0 ? 0 : val > maxVal ? maxVal : val;

    return res;

  }

  dx = random(a)-b;
  dy = random(a)-b;
  x1 = x2;
  y1 = y2;
  x = x2 + dx;
  y = y2 + dy;
  x = tryFix(x);
  y = tryFix(y);
  x2 = x;
  y2 = y;
  line(x1, y1, x2, y2);
}
<script src="https://cdn.jsdelivr.net/npm/p5@1.4.0/lib/p5.js"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!