How to create a simple drawing board in Processing with Python Mode?
Last Updated :
17 May, 2022
Processing is a programming language, and a development environment. It is an open-source software to create visual arts, graphics and animation using programming. It supports around 8 different modes, and in this article, we will be using Python Mode.
In this article, we are going to create a simple drawing board using Processing with Python Mode. In case if you have not installed the Processing Software yet, follow this article to Download and Install Processing, and Setup Python Mode.
Approach:
- Firstly open Processing software and choose Python Mode.
- Then in the coding part, we are going to define three functions, setup(), draw(), and mouseDragged()
- setup() function is called before the start of the program. Here we have to set the background color and size of the window.
- In the mouseDragged() function we are simply going to assign 0 to the global variable value. This function is called when the mouse is being dragged i.e. when the user clicks and holds and moves the mouse button.
- draw() method is called throughout the program execution. In this method, we are going to check if the value is 0, (i.e if mouse is being dragged). If it is true, then we are going to draw a circle (using ellipse() function) at the position of the cursor. fill(0) is used to fill the circle with black color. We can get X and Y coordinates of the cursor using the variables mouseX and mouseY. Once the circle is drawn, set the value to 1 (i.e. circle is drawn for the current position of the mouse).
- Whenever the user drags the mouse, the if condition in the draw method turns true, and draws circle repeatedly throughout the movement of the cursor thus creating a drawing effect.
Below is the implementation:
Here is the Python code for the above approach. Now type the following code in the code editor.
Python3
value = 1
def setup():
background( 255 )
size( 1500 , 1200 )
def draw():
global value
if value = = 0 :
r = 10
fill( 0 )
ellipse(mouseX, mouseY, r, r)
value = 1
def mouseDragged():
global value
value = 0
|
Output:
Processing with Python Mode
Share your thoughts in the comments
Please Login to comment...