Learn JavaScript with p5.js: Coding for Visual Learners Arslan instant download
Learn JavaScript with p5.js: Coding for Visual Learners Arslan instant download
https://textbookfull.com/product/learn-javascript-with-p5-js-
coding-for-visual-learners-arslan/
https://textbookfull.com/product/getting-started-with-p5-js-
making-interactive-graphics-in-javascript-and-processing-1st-
edition-lauren-mccarthy/
https://textbookfull.com/product/full-stack-javascript-learn-
backbone-js-node-js-and-mongodb-mardan-azat/
https://textbookfull.com/product/coding-art-a-guide-to-unlocking-
your-creativity-with-the-processing-language-and-p5-js-in-four-
simple-steps-2nd-edition-mathias-funk/
https://textbookfull.com/product/web-development-for-beginners-
learn-html-css-javascript-step-by-step-with-this-coding-guide-
programming-guide-for-beginners-website-development-1st-edition-
Learning React js Learn React JS From Scratch with
Hands On Projects 2nd Edition Alves
https://textbookfull.com/product/learning-react-js-learn-react-
js-from-scratch-with-hands-on-projects-2nd-edition-alves/
https://textbookfull.com/product/d3-js-in-action-data-
visualization-with-javascript-2nd-edition-elijah-meeks/
https://textbookfull.com/product/d3-js-in-action-data-
visualization-with-javascript-second-edition-elijah-meeks/
https://textbookfull.com/product/learn-python-visually-creative-
coding-with-processing-py-1st-edition-tristan-bunn/
Learn JavaScript
with p5.js
Coding for Visual Learners
—
Engin Arslan
Learn JavaScript
with p5.js
Coding for Visual Learners
Engin Arslan
Learn JavaScript with p5.js: Coding for Visual Learners
Engin Arslan
Toronto, Ontario, Canada
Chapter 1: Introduction������������������������������������������������������������������������1
Why Learn Coding?�����������������������������������������������������������������������������������������������1
Coding vs. Programming���������������������������������������������������������������������������������������2
On HTML and CSS�������������������������������������������������������������������������������������������������3
Why Learn JavaScript?�����������������������������������������������������������������������������������������4
Why Do We Have Different Languages?����������������������������������������������������������������6
Learning JavaScript with p5.js�����������������������������������������������������������������������������8
iii
Table of Contents
Summary������������������������������������������������������������������������������������������������������������42
Practice���������������������������������������������������������������������������������������������������������������42
Chapter 7: Loops���������������������������������������������������������������������������������85
For Loop��������������������������������������������������������������������������������������������������������������85
Random and Noise Functions�����������������������������������������������������������������������������92
Summary������������������������������������������������������������������������������������������������������������99
Practice�������������������������������������������������������������������������������������������������������������100
iv
Table of Contents
Chapter 8: Functions������������������������������������������������������������������������101
Creating Functions��������������������������������������������������������������������������������������������101
Revisiting Setup and Draw Functions���������������������������������������������������������������107
Summary����������������������������������������������������������������������������������������������������������108
Practice�������������������������������������������������������������������������������������������������������������108
Chapter 9: Objects����������������������������������������������������������������������������109
Using Object Initializer��������������������������������������������������������������������������������������109
Using the Constructor Function������������������������������������������������������������������������117
Summary����������������������������������������������������������������������������������������������������������123
v
Table of Contents
Index�������������������������������������������������������������������������������������������������213
vi
About the Author
Engin Arslan is a Software Developer with a Bachelor of Science in
Materials Engineering and a Postgraduate Degree in Visual Effects. Before
becoming a Developer, he worked as a Visual Effects Artist / Technical
Director on films and TV shows including Resident Evil, Tron, Mama,
Pompeii, Vikings, and Strain. He received an Emmy nomination and won a
Canadian Screen Award for his achievements in Visual Effects. During his
time in VFX, he fell in love with Python and with programming in general.
As a result, he changed careers to be able to immerse himself completely
in software development. Engin currently works at a Toronto-based digital
services company, where he helps develop solutions in strategic problem
spaces using emerging technologies. He also works at Seneca College as
a part-time professor and creates online courses for Lynda/LinkedIn and
Pluralsight.
vii
About This Book
The emphasis of this book will be primarily on learning programming
using JavaScript and p5.js and secondarily in creating visuals. The main
focus is to teach you how to program so that you can choose to pursue
whatever field that you would like with your newly established skill set.
The skills that you will acquire from this book are highly transferrable and
can be used with whatever you choose to build: whether web applications,
programmable robots, or generative art. This means that I will provide
you with enough context so that you can build a strong foundation for
programming. But I also won’t hinder your momentum with irrelevant
technical or theoretical points. The aim is to build a strong but a minimum
viable knowledge to get you running with coding. This is the book that I
wished I had available when I was learning coding myself.
If you are an artist or a visual designer, this book is perfect for you as
you might find the examples we will be building to be directly relevant
to your work. If not, this is still a great book for learning programming as
the visual nature of the exercises will help you grasp the fundamentals
of programming more easily and let you build a strong foundation in a
shorter amount of time.
This book will present various JavaScript and p5.js features and
concepts in the following chapters. The knowledge will be reinforced
by building several useful examples like an animation and a data
visualization; and as a final project, we will be building a game that can be
deployed online using what we learned in this book!
ix
About This Book
x
CHAPTER 1
Introduction
At this age and time that we live in, coding is simply invaluable. It has
the power to uplift your career, your future prospects, and even your
intellectual capacity. Computation is driving one of the largest capital
expansions in history, and there has never been a better time to learn
coding than now.
2
Chapter 1 Introduction
3
Chapter 1 Introduction
4
Chapter 1 Introduction
main()
{
cout << "Hello World!" << endl;
return 0;
}
5
Chapter 1 Introduction
class HelloWorld {
static public void main( String args[] ) {
System.out.println( "Hello World!" );
}
}
console.log('Hello World');
• is easier to write;
6
Chapter 1 Introduction
your programs. Others can be much more concise but could be slower to
execute. Some languages are better suited for certain tasks. JavaScript is
perfect for full stack web development, Matlab is great for mathematical
calculations, C++ has dominance in game programming, and Julia is used
for data science. This doesn’t mean you can’t be using other languages
in these domains, though. Unity Game Engine offers C# for game
development. Python can be preferred for data science. And GoLang or
many other languages could be used for back-end web development. It
sometimes boils down to what the developers prefer to use and what they
already know. And sometimes it comes down to the constraints of a given
project.
I used to work in the visual effects industry and the software packages
that we would be using in the field could be automated using Python or
C++. So those were great language choices for that domain given that’s
what the tools that we were using were supporting. Knowing Java in visual
effects would have been largely useless except for the fact that knowing a
programming language actually makes it more likely that you will be able to
pick up another language as they share similar principles among each other.
Choosing which language to learn as your first can sometimes be a
tough choice as there are lots of viable options out there. Sometimes the
choice is dictated by the application domain. If you are seeking to build
a game using the Unreal Engine maybe you should just learn C++. But
then again if it’s your first time interfacing with a programming language,
you might be faced with such a steep learning curve that it might be
discouraging.
JavaScript stands to be a great choice to learn as your first
programming language. As mentioned earlier, it is widely used and has a
vast application domain that would allow you to experiment with lots of
different applications. It has a big and active community behind it and also
has a very concise syntax that makes it closer to human languages.
7
Chapter 1 Introduction
8
Chapter 1 Introduction
• Faker.js (https://github.com/Marak/Faker.js):
Generate massive amounts of fake data.
• jimp (https://github.com/oliver-moran/jimp): An
image processing library.
9
Chapter 1 Introduction
thousands. Since p5.js was built with simplicity in mind, sometimes small
p5.js programs are referred to as sketches. Even though that’s a clever
way to describe it, I am personally not a huge fan of that wording since it
obfuscates the fact that what you are doing is programming after all.
You can find pragmatic applications of p5.js such as creating data
visualizations (Figure 1-1).
10
Chapter 1 Introduction
11
CHAPTER 2
Getting Started
I nstalling p5.js
There are a couple of ways you can start using p5.js and JavaScript. One
option is to visit the p5.js website (https://p5js.org/download) and
download the p5.js source code on to your system (see Figure 2-1).
At the time of the writing of this walkthrough, the download page has a
link called ‘p5.js complete’ that includes the p5.js library and an example
project. Download this archive file and find the folder named empty-
example inside it. In this folder, you will find two files: the sketch.js
file where you can write JavaScript code and an index.html file that can
be launched with a web browser such as Chrome and would execute and
display the result of the JavaScript code inside sketch.js file. You can also
find a copy of these files on my GitHub repository: https://github.com/
hibernationTheory/p5js-complete.
Even though you can change the contents of sketch.js JavaScript file
with a plain text editor like NotePad, you might instead want to use a code
editor such as `Sublime Text` to do so.
A code editor is pretty similar to a text editor, like Notepad or Word, but
it has special features that make coding much easier such as highlighting
of special words for a given programming language, which in this case
that language is JavaScript. Sublime Text is a code editor that you can use,
which can be downloaded and evaluated for free.
Perhaps the easiest way to get started with p5.js is to use an online
editor. An online code editor can be used inside the web browser and
doesn’t require you to install anything on your system. It is my preferred
way of working when I am learning as it makes it really easy to get started.
An easy-to-use Online Code Editor that is available at the time of the
preparation of this book can be found at this link:
[p5.js online editor - alpha](https://alpha.editor.p5js.org/)
If the above link is not accessible for any reason, you can also try the
p5.js template that is hosted on my Codepen account:
[Codepen - p5.js simple template](https://codepen.io/
enginarslan/pen/qpBBXz?editors=0011). CodePen (https://codepen.
io) is a social development platform that allows you to write code in
the browser and share your work with other developers. It is a great
environment for development and experimentation. The difference
between Codepen and the p5.js editor that is mentioned above is that the
p5.js editor only allows you to run p5.js related code inside itself whereas
Codepen can execute any front-end code.
14
Chapter 2 Getting Started
How the online editor works is that, whenever we have some code
ready to be executed, we will press the play button at the top of the page.
This play button will show the results of our code on the right-hand
side panel. The online editor of Codepen is slightly different in that it
automatically executes the code any time you change it. Pressing the Play
button at this point wouldn’t do much as we didn’t write any code that
draws shapes to the screen. We will just see an empty screen get generated.
But as we can see, this editor has some code already written into it. This
code that we see is needed for almost all the p5.js programs that we will be
writing so it is included here for our convenience (Listing 2-1).
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
}
Let’s just delete this code for now. Before we start using p5.js to learn
JavaScript, we will see a couple of things on the fundamentals of JavaScript.
You can find the code examples that we will be using throughout this
book at the GitHub repository: https://github.com/hibernationTheory/
coding-for-visual-learners.
16
Chapter 2 Getting Started
17
Chapter 2 Getting Started
Listing 2-3 shows the results that will be displayed inside the console
once the code in Listing 2-2 is executed.
2
15
13419
663831006420
One takeaway should be that code executes from top to bottom. There
are some programming structures that alter this flow, but we will see them
later on. Another takeaway should be that computers don’t mind working
with large numbers. We can throw hard operations at them that would take
days for a human to perform.
In the last console.log statement from Listing 2-2 we have two
ridiculously large numbers. What if we wanted to use the resulting number
from that operation and subtract 10 from it on the next line? Right now to
be able to do this we have to type that number again:
18
Chapter 2 Getting Started
So we can use a variable name to refer to that value instead of typing the
value again. Here is how that works:
19
Chapter 2 Getting Started
as variable names; we can’t call our variable var as this name is already in
use by JavaScript. If we tried to use var as a variable name; as in var var =
5, JavaScript would throw an error.
This mention of rules might be making you uneasy at this point.
After all, programming is supposed to be fun right? But don’t worry; the
reserved name list is relatively short, so you don’t need to memorize it.
And as you learn more of the language, you would also develop a better
sense as to which names to avoid.
Regarding rules, there is another rule that should be mentioned.
JavaScript needs us to place semicolons after each statement. If we don't
do this, our program can still work but might fail in certain edge conditions
that can be hard to identify later on. So it is a good idea to use semicolons
after every statement even though it means a bit more work on our part.
Previous code should actually be written as shown in Listing 2-4:
console.log(1 + 1);
console.log(5 + 10);
console.log(213 * 63);
var bigNumber = 321314543265 + 342516463155;
console.log(bigNumber);
console.log(bigNumber - 10);
var x = 10;
x + 5;
console.log(x);
20
Random documents with unrelated
content Scribd suggests to you:
PLAN VON DEM ÜBERFALL
eines englischen Postens auf PAULUS HOOK in der Provinz New Jersey um
1/2-2 Uhr in der Nacht vom 18. zum 19. October 1779.
—< Erklärung umseitig. >—
A. Annäherung und Stellung der Rebellen auf den Höhen von
Bergen zur Deckung des Rückzuges.
B. Angriff auf die Brücke und Blockhaus 1, 2 und 3 und auf das
Fort C das mit 7 Sechspfündern armiert war, die aber nicht zu
Schuss kamen.
D. Barracken, in denen die 110 Mann starke englische Besatzung
gefangen genommen wurde.
E. Schanze, welche ein hessischer Hauptmann, 1 Offizier mit 25
Mann besetzt hielt, woraufhin die Rebellen bei Tagesanbruch
mit ihren Gefangenen den Rückzug antraten.
Ka p i t e l X X .
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com