# Convert 2D line drawing into 3D object in p5.js

**URL:** https://discourse.processing.org/t/convert-2d-line-drawing-into-3d-object-in-p5-js/25108
**Category:** Project Guidance
**Tags:** homework
**Created:** [November 1, 2020, 9:50am UTC](https://discourse.processing.org/t/convert-2d-line-drawing-into-3d-object-in-p5-js/25108 "2020-11-01T09:50:07Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![sarath.jasrin](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sarath.jasrin](https://discourse.processing.org/u/sarath.jasrin)
#### Post date: [November 1, 2020, 9:50am UTC](https://discourse.processing.org/t/convert-2d-line-drawing-into-3d-object-in-p5-js/25108/1 "2020-11-01T09:50:07Z")

</div>

Hi I’m very new to p5.js. I found p5 is very easy compare with threejs. I wanted to allow user to draw 2lines and convert in to 3D object then apply texture to the 3D.

I know the above is more for the forum question. I wants some guidence and also want to know whether it’s possible in p5.js.

Convert 2D drawing in to below image. Due to the new user restriction I couldn’t the line drawing but you can see it the below image.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/dc1408a0f2b196938bb804451b1fd67ff136f08c.png)

Thanks in advance.Cheers!

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [November 1, 2020, 2:50pm UTC](https://discourse.processing.org/t/convert-2d-line-drawing-into-3d-object-in-p5-js/25108/2 "2020-11-01T14:50:41Z")

</div>

Hi,

Welcome to the forum! 🙂

One way to do this is to let the user click and draw points on the canvas in 2d. Each time you store the x and y coordinates and when the user has finished, you construct the 3d geometry with the [`beginShape()`](https://p5js.org/reference/#/p5/beginShape) and `endShape()` functions.

Basically if you have 4 points in 2d space, you need to construct the 3d points to “solidify” your stroke like this :

 ![Capture d’écran de 2020-11-01 15-48-14](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a5e9d6937cf062fcc75fe1e768b0977430ddf418.png) ![Capture d’écran de 2020-11-01 15-47-12](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1544708f236194fb51a39c2b38cf259aad30ae7e.png)

Check out this tutorial for creating custom 3d meshes :

[![](https://img.youtube.com/vi/DZlw-IS5OkI/maxresdefault.jpg "18.8: 3D Custom Shapes - WebGL and p5.js Tutorial") ](https://www.youtube.com/watch?v=DZlw-IS5OkI)

---

<div class="post-metadata">

### Author: ![sarath.jasrin](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sarath.jasrin](https://discourse.processing.org/u/sarath.jasrin)
#### Post date: [November 1, 2020, 4:20pm UTC](https://discourse.processing.org/t/convert-2d-line-drawing-into-3d-object-in-p5-js/25108/3 "2020-11-01T16:20:45Z")

</div>

Hi thanks for the guidance. I will try it and comment here. Thanks.

---

<div class="post-metadata">

### Author: ![sarath.jasrin](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sarath.jasrin](https://discourse.processing.org/u/sarath.jasrin)
#### Post date: [November 2, 2020, 7:22am UTC](https://discourse.processing.org/t/convert-2d-line-drawing-into-3d-object-in-p5-js/25108/4 "2020-11-02T07:22:57Z")

</div>

Hi @josephh, I tried to draw line based on the mouse click. It’s not working as I expected.  
I want similar to this demo [https://jsfiddle.net/blackstrings/nh81deqw/](https://jsfiddle.net/blackstrings/nh81deqw/)

But above demo in three js and looks bit complicated. Can you please help me out?

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [November 2, 2020, 4:07pm UTC](https://discourse.processing.org/t/convert-2d-line-drawing-into-3d-object-in-p5-js/25108/5 "2020-11-02T16:07:51Z")

</div>

Hi @sarath.jasrin,

Here is a small visualization in order to help you to better see how to solve this problem. You have a grid where you can draw points by clicking. When you are finished, you can press enter to toggle the 3d view where you can rotate with your mouse.

 ![Capture d'écran 2020-11-02 17:03:05](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/7/73056b08ffe8cec7ed2e5f7e764061fdd61ee9b6.png) --\> ![Capture d'écran 2020-11-02 17:03:24](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f21c765cc3cd7c39e573db827b412a4a04b1f84e.png)

```auto
// Whether the user finished drawing
boolean finishedDrawing = false;

// The path is a list of points
ArrayList<PVector> path;

void setup() {
  size(500, 500, P3D);
  
  // Initialize the path
  path = new ArrayList<PVector>();
}

void draw() {
  background(0);

  // If the user pressed enter
  if (finishedDrawing) {
    // Move the camera on the z axis (pointing toward the screen)
    camera(0, -100, width * 1.5, 0, 0, 0, 0, 1, 0);

    // Rotate on the y axis with the mouse 
    rotateY(map(mouseX, 0, width, 0, PI));
  } else {
    // Otherwise reset the camera
    // Those values were taken from https://processing.org/reference/camera_.html
    camera(0, 0, (height/2.0) / tan(PI*30.0 / 180.0), 0, 0, 0, 0, 1, 0);

    // Display text
    fill(255);
    textSize(18);
    text("Click to add points\nPress ENTER to see 3d", -width/2 + 50, -height/2 + 75);
  }
  
  drawAxis(100);
  
  pushMatrix();
  // translate to center the grid
  translate(-width / 2, -height / 2);
  
  // Draw the grid and the path
  drawGrid(50);
  drawPath();
  
  popMatrix();
}

void drawAxis(float unit) {
  strokeWeight(5);
  textSize(40);

  // x axis
  stroke(255, 0, 0);
  line(0, 0, 0, unit, 0, 0);
  fill(255, 0, 0);
  text("X", unit + 5, 0, 0);
  
  // y axis
  stroke(0, 255, 0);
  line(0, 0, 0, 0, unit, 0);
  fill(0, 255, 0);
  text("Y", 0, unit + 5, 0);
  
  if (finishedDrawing) {
    // z axis
    stroke(0, 0, 255);
    line(0, 0, 0, 0, 0, unit);
    fill(0, 0, 255);
    text("Z", 0, 0, unit + 5);
  }
}

// Add a point when clicked (note that we didn't specify a Z coordinate)
void mousePressed() {
  if (!finishedDrawing) {
    path.add(new PVector(mouseX, mouseY));
  }
}

// Toggle the 3d view
void keyPressed() {
  if (keyCode == ENTER) {
    finishedDrawing = !finishedDrawing;
  }
}

// Draw the grid
void drawGrid(float cellSize) {
  stroke(255, finishedDrawing? 50 : 255);
  strokeWeight(1);

  //pushMatrix()
  for (int x = 0; x <= width; x += cellSize) {
    line(x, 0, x, height);
  }

  for (int y = 0; y <= height; y += cellSize) {
    line(0, y, width, y);
  }
}

void drawPath() {
  stroke(255, 0, 0);

  // Draw lines
  strokeWeight(3);
  for (int i = 0; i < path.size() - 1; i++) {
    line(path.get(i).x, path.get(i).y, path.get(i+1).x, path.get(i+1).y);
  }

  // Draw points
  strokeWeight(12);
  for (PVector point : path) {
    point(point.x, point.y);
  }
}

```

Again a great tutorial by Daniel Schiffman about P3D (OpenGL in processing) :

> **[P3D \\ Processing.org](https://processing.org/tutorials/p3d/)**

And also useful documentation ressources :

- [`camera()`](https://processing.org/reference/camera_.html)
- [`translate()`](https://processing.org/reference/translate_.html)
- [`rotateY`](https://processing.org/reference/rotateY_.html)
- [`size()`](https://processing.org/reference/size_.html) (for P3D)

_EDIT : sorry I just realized that you were asking for p5js but it’s the same concepts and you can easily transpose the code_

---

<div class="post-metadata">

### Author: ![sarath.jasrin](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sarath.jasrin](https://discourse.processing.org/u/sarath.jasrin)
#### Post date: [November 2, 2020, 4:23pm UTC](https://discourse.processing.org/t/convert-2d-line-drawing-into-3d-object-in-p5-js/25108/6 "2020-11-02T16:23:38Z")

</div>

@josephh Thanks for the reply. Let me try!

---

<div class="post-metadata">

### Author: ![sarath.jasrin](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sarath.jasrin](https://discourse.processing.org/u/sarath.jasrin)
#### Post date: [November 3, 2020, 8:54am UTC](https://discourse.processing.org/t/convert-2d-line-drawing-into-3d-object-in-p5-js/25108/7 "2020-11-03T08:54:38Z")

</div>

@josephh I tried to convert into p5js it doesn’t seem like working…

```auto
// Whether the user finished drawing
let finishedDrawing = false;

// The path is a list of points
let path;

function setup() {
  createCanvas(400, 400,WEBGL);
  
  // Initialize the path
  path = [];
}

function draw() {
  background(0);

  // If the user pressed enter
  if (finishedDrawing) {
    // Move the camera on the z axis (pointing toward the screen)
    camera(0, -100, width * 1.5, 0, 0, 0, 0, 1, 0);

    // Rotate on the y axis with the mouse 
    rotateY(map(mouseX, 0, width, 0, PI));
  } else {
    // Otherwise reset the camera
    // Those values were taken from https://processing.org/reference/camera_.html
    camera(0, 0, (height/2.0) / tan(PI*30.0 / 180.0), 0, 0, 0, 0, 1, 0);

    // Display text
    fill(255);
    textSize(18);
    textFont('Georgia');
    text("Click to add points\nPress ENTER to see 3d", -width/2 + 50, -height/2 + 75);
  }
  
  drawAxis(100);
  
  push();
  // translate to center the grid
  translate(-width / 2, -height / 2);
  
  // Draw the grid and the path
  drawGrid(50);
  drawPath();
  
  pop();
}

function drawAxis(unit) {
  strokeWeight(5);
  textSize(40);
textFont('Georgia');
  // x axis
  stroke(255, 0, 0);
  line(0, 0, 0, unit, 0, 0);
  fill(255, 0, 0);
  text("X", unit + 5, 0, 0);
  
  // y axis
  stroke(0, 255, 0);
  line(0, 0, 0, 0, unit, 0);
  fill(0, 255, 0);
  textFont('Georgia');
  text("Y", 0, unit + 5, 0);
  
  if (finishedDrawing) {
    // z axis
    stroke(0, 0, 255);
    line(0, 0, 0, 0, 0, unit);
    fill(0, 0, 255);
    textFont('Georgia');
    text("Z", 0, 0, unit + 5);
  }
}

// Add a point when clicked (note that we didn't specify a Z coordinate)
function mousePressed() {
  if (!finishedDrawing) {
    path.add(new PVector(mouseX, mouseY));
  }
}

// Toggle the 3d view
function keyPressed() {
  if (keyCode == ENTER) {
    finishedDrawing = !finishedDrawing;
  }
}

// Draw the grid
function drawGrid(cellSize) {
  stroke(255, finishedDrawing? 50 : 255);
  strokeWeight(1);

  //pushMatrix()
  for (let x = 0; x <= width; x += cellSize) {
    line(x, 0, x, height);
  }

  for (let y = 0; y <= height; y += cellSize) {
    line(0, y, width, y);
  }
}

function drawPath() {
  stroke(255, 0, 0);

  // Draw lines
  strokeWeight(3);
  for (let i = 0; i < path.length - 1; i++) {
    line(path.x, path.y, path[i+1].x, path[i+1].y);
  }

  // Draw points
  strokeWeight(12);
  path.forEach(p=>{
    point(p.x, p.y);
  })
 
}

```

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [November 3, 2020, 11:03am UTC](https://discourse.processing.org/t/convert-2d-line-drawing-into-3d-object-in-p5-js/25108/8 "2020-11-03T11:03:57Z")

</div>

Ok so the process of writing code is often linked to debugging and solving errors.

In this case, the console is going to be our friend (access it with F12 on most browsers or use the [p5js web editor](https://editor.p5js.org/)).

The first message is this :

`WEBGL: you must load and set a font before drawing text. See `loadFont` and `textFont` for more details.`

It’s because in WEBGL, you must load a font yourself as mentioned [here](https://p5js.org/reference/#/p5/textFont) :

> **WEBGL** : Only fonts loaded via [loadFont()](https://p5js.org/reference/#/p5/loadFont) are supported.

Next when you click, you have an error :

```auto
ReferenceError: PVector is not defined (sketch: line 77)

```

This is because `PVector` is a class defined in Processing Java not in p5js in JavaScript. Instead use :

- [reference | p5.js](https://p5js.org/reference/#/p5.Vector)
