Guide on creating a dynamic line segment in real-time with three.js

Just delving into the world of Three.js and aiming to replicate the drawing technique seen in Microsoft Paint for creating line segments. My goal is to capture the coordinates of a point onMouseDown and continue extending a line with onMouseMove until releasing the mouse. Assistance would be greatly appreciated!

Answer №1

When it comes to drawing in a 3D space, three.js is the go-to tool. However, if your goal is to replicate a 2D drawing application like Paint, utilizing the 2D canvas with canvas.getContext("2d"); might be a more straightforward approach.

If you're set on using three.js for drawing, I've prepared a comprehensive example that you can explore by following these steps:

  1. Click anywhere on the page and drag your mouse to draw a line on the z-plane.
  2. Experiment with the Shapes button to observe how objects positioned on different planes behave - one closer and one further from the viewer based on their z-coordinate.
  3. Try out the Rotate button, which triggers camera zoom-out and rotation around axes, showcasing how all drawing occurs exclusively on the z plane as you rotate through it.

Take a look at the crucial parts of the code:

Firstly, you need to project the mouse click coordinates onto the z-axis plane using the function below:

function get3dPointZAxis(event)
{
    // Code snippet here
}

Then, connect the projected point with the previous one to form a line:

// Line drawing function

Additionally, to avoid projection issues when rotating near the z plane boundary, utilize the following check:

// Check snippet

For detailed information on projecting mouse coordinates, references are availablehere andhere.

Update:

To draw a line between the mousedown and mouseup positions, check out this updated demo:demo link. The modification focuses on executing the draw operation between points solely upon mouse up event.

// Updated draw function

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JQuery.each is causing an issue due to a potential conflict between JSON and string data types

Hey there, I'm trying to loop through each title in this code but encountering an error in the console that says "cannot use 'in' operator." Everything works fine when I provide an ID from the database. However, when I try to pass in a strin ...

Listener for clicking on a marker in Google Maps

Trying to add a click event to Google Map markers in my Cordova app has proven to be quite challenging. The recommended ways mentioned in the documentation don't seem to work, unless I make the marker draggable - which is not an option for me. It seem ...

What is the best way to apply a class to the grandparent div of an element in AngularJS?

Currently, I have a dynamically generated list of tests: <script id="TestsTemplate" type="text/ng-template"> <article class="tests-main"> <section class="tests"> <div class="test" ng-repeat="test in ...

Having trouble with Node.js child_process.exec when the command doesn't return any output?

Recently encountered an issue while using child_process.exec in nodejs. The problem occurs when the command returns nothing. Here is what I did: const {exec} = require('child_process'); const cmd = `ps -ef | grep -v grep | grep abc123.py`; exec ...

How to import a JSON file in AngularJS using Node.js

I am currently working with NodeJS and AngularJS to handle some data. Within my Angular application, I am trying to access a JSON file and distribute its content into separate controllers. Right now, I am utilizing the http.get method within a controller ...

When Infinite Scroll is integrated into another file with HTML tags stacked on top, it will not load additional posts when scrolling down

I have implemented an Infinite Scroll feature that dynamically loads more data from a database as users scroll to the bottom of the page. However, I encountered an issue when trying to include this functionality in another .PHP file. If I insert any HTML ...

Transforming a JSON array from one structure to another with the power of JavaScript

Hello everyone, I'm new to this platform and seeking some advice. Can anyone help me with converting a JSON array to another JSON array while grouping by ID and including only the values of 'ID' and 'SID'? I've attempted usin ...

Executing php class method through ajax with jQuery without requiring a php handler file

How can I trigger a PHP class method using AJAX with jQuery without the need for a separate PHP handler file? Here is my PHP Class animal.php:- <?php class animal { function getName() { return "lion"; } } ?> jQuery code snippet:- ...

What is the best way to dynamically adjust the top CSS of an element when it is positioned at the top or

Is there a way to dynamically set the top CSS when an element is over the top or bottom of a page? First, hover over the first cat image. It's working fine. http://image.ohozaa.com/i/480/7YpWei.jpg When I scroll the page to the bottom and hover ove ...

What is the method for accessing a local XML file with $.ajax?

Currently, I am working on developing a Firefox extension that requires reading a local XML file. However, I have encountered an issue with using $.ajax to read the file. Here is a snippet of my code: $.ajax({ type: "GET", url: "file:/// ...

Tips for linking real-time information to an array using AngularJS

Exploring Angular for the first time, I've embarked on creating a shopping cart web application. I came across a cart plugin online at https://www.codeproject.com/Articles/576246/A-Shopping-Cart-Application-Built-with-AngularJS, but it only supports s ...

Bootstrap form validation issues

Currently, I am utilizing Vue.js along with Bootstrap for the creation of a website. In this process, I have set up a form on which I am implementing my custom validation logic. Everything seems to be functioning correctly until the moment when the user hi ...

Translating SQL to Sequelize Syntax

I have an SQL query that I need to rewrite as a sequelize.js query in node.js. SELECT historyTable1.* FROM table1 historyTable1 WHERE NOT EXISTS ( SELECT * FROM table1 historyTable2 WHERE historyTable2.id=historyTable1.id AND historyTable2.da ...

JavaScript: What's the best way to update the URL in the address bar without triggering a page refresh?

Similar Question: How can I use JavaScript to update the browser URL without reloading the page? I've observed websites like GMail and GrooveShark altering the URL in the address bar without having to refresh the entire page. Based on my understa ...

Reverse a filter within an ng-repeat loop - AngularJS

I have a question that I haven't found the answer to yet. Here is the issue I'm facing: The JSON data I am working with has this structure: [{ "batch_queue_name": "Batch One", "start_date": "12/01/2016 10:18 P.M.", "end_date": "12/03/2016 ...

What could be the reason for AngularJS encoding the URLs for my POST parameters?

Take a look at our service's REST client: self.headers = { Accept: 'application/json', 'Content-Type': 'application/json' }; self.loginClient = $resource(self.baseUrl + '/users/login', { ...

Navigating through nested JSON arrays in JavaScript involves looping through multiple dimensions

I am facing difficulty finding the correct solution to my issue here. I am trying to iterate through the nested Products arrays in order to display each Product name. Can this be achieved with my current code, or should I consider rewriting it to make qu ...

Angular Fire: The $on method is missing and causing an error stating that undefined is not a function

I am currently attempting to log my Firebase data to the console, but I keep encountering an error stating undefined is not a function. Below is the full error message: TypeError: undefined is not a function at Object.childAdded (http://localhost:9000/scr ...

Modify one specific variable within my comprehensive collection on Firebase Firestore

After clicking the button, I need to update a variable. The variable in question is "bagAmount" and it is stored in my firestore collection. Here is a link to view the Firestore Collection: Firestore Collection Currently, I am able to update one of the va ...

Refreshing a webpage with JavaScript directly from the server (2021)

Utilizing Javascript, I have successfully implemented a cookie setting feature that allows users to switch between normal and classic theme versions on my website by clicking a checkbox. The backend of my application is powered by PHP. My goal is to access ...