Download full (Ebook) Getting Started with p5.js: Making Interactive Graphics in JavaScript and Processing by Lauren McCarthy, Casey Reas, Ben Fry ISBN 9781457186776, 1457186772 ebook all chapters
Download full (Ebook) Getting Started with p5.js: Making Interactive Graphics in JavaScript and Processing by Lauren McCarthy, Casey Reas, Ben Fry ISBN 9781457186776, 1457186772 ebook all chapters
com
DOWLOAD EBOOK
ebooknice.com
https://ebooknice.com/product/getting-started-with-processing-1471152
ebooknice.com
https://ebooknice.com/product/make-getting-started-with-
processing-11403918
ebooknice.com
ebooknice.com
(Ebook) Biota Grow 2C gather 2C cook by Loucas, Jason;
Viles, James ISBN 9781459699816, 9781743365571,
9781925268492, 1459699815, 1743365578, 1925268497
https://ebooknice.com/product/biota-grow-2c-gather-2c-cook-6661374
ebooknice.com
ebooknice.com
ebooknice.com
https://ebooknice.com/product/sat-ii-success-
math-1c-and-2c-2002-peterson-s-sat-ii-success-1722018
ebooknice.com
https://ebooknice.com/product/learn-javascript-with-p5-js-coding-for-
visual-learners-52953782
ebooknice.com
Computers / Digital Media
Make:
Learn programming the fun way—by sketching with interactive computer
graphics! Getting Started with p5.js contains techniques that can be
applied to creating games, animations, and interfaces.
p5.js is a new interpretation of Processing written in JavaScript that
makes it easy to interact with HTML5 objects, including text, input, video,
Getting
webcam, and sound. Like its older sibling Processing, p5.js makes coding
accessible for artists, designers, educators, and beginners.
Written by the lead p5.js developer and the founders of Processing, this
book provides an introduction to the creative possibilities of today’s Web,
Started with
p5.js
With Getting Started with p5.js, you’ll:
Make:
Learn programming the fun way—by sketching with interactive computer
graphics! Getting Started with p5.js contains techniques that can be
applied to creating games, animations, and interfaces.
p5.js is a new interpretation of Processing written in JavaScript that
makes it easy to interact with HTML5 objects, including text, input, video,
Getting
webcam, and sound. Like its older sibling Processing, p5.js makes coding
accessible for artists, designers, educators, and beginners.
Written by the lead p5.js developer and the founders of Processing, this
book provides an introduction to the creative possibilities of today’s Web,
Started with
p5.js
With Getting Started with p5.js, you’ll:
978-1-457-18677-6
[LSI]
Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
1/Hello. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Sketching and Prototyping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Flexibility. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Giants. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Family Tree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Join In. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2/Starting to Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Environment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Download and File Setup. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Your First Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Example 2-1: Draw an Ellipse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Example 2-2: Make Circles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
The Console. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Making a New Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Examples and Reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3/Draw. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
The Canvas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Example 3-1: Create a Canvas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Example 3-2: Draw a Point. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Basic Shapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Example 3-3: Draw a Line. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Example 3-4: Draw Basic Shapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Example 3-5: Draw a Rectangle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Example 3-6: Draw an Ellipse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Example 3-7: Draw Part of an Ellipse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Example 3-8: Draw with Degrees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Example 3-9: Use angleMode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Drawing Order. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Example 3-10: Control Your Drawing Order. . . . . . . . . . . . . . . . . . . . . . . . 26
iii
Example 3-11: Put It in Reverse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Shape Properties. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Example 3-12: Set Stroke Weight. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Example 3-13: Set Stroke Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Color. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Example 3-14: Paint with Grays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Example 3-15: Control Fill and Stroke. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Example 3-16: Draw with Color. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Example 3-17: Set Transparency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Custom Shapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Example 3-18: Draw an Arrow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Example 3-19: Close the Gap. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Example 3-20: Create Some Creatures. . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Robot 1: Draw. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
4/Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
First Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Example 4-1: Reuse the Same Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Example 4-2: Change Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Making Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
p5.js Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Example 4-3: Adjust the Canvas, See What Follows. . . . . . . . . . . . . . . . . . 44
A Little Math. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Example 4-4: Basic Arithmetic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Repetition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Example 4-5: Do the Same Thing Over and Over. . . . . . . . . . . . . . . . . . . . 47
Example 4-6: Use a for Loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Example 4-7: Flex Your for Loop’s Muscles. . . . . . . . . . . . . . . . . . . . . . . . . 49
Example 4-8: Fanning Out the Lines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Example 4-9: Kinking the Lines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Example 4-10: Embed One for Loop in Another. . . . . . . . . . . . . . . . . . . . . 51
Example 4-11: Rows and Columns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Example 4-12: Pins and Lines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Example 4-13: Halftone Dots. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Robot 2: Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5/Response. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Once and Forever. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Example 5-1: The draw() Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Example 5-2: The setup() Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Example 5-3: setup(), Meet draw(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
iv Contents
Follow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Example 5-4: Track the Mouse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Example 5-5: The Dot Follows You. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Example 5-6: Draw Continuously. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Example 5-7: Set Thickness on the Fly. . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Example 5-8: Easing Does It. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Example 5-9: Smooth Lines with Easing. . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Click. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Example 5-10: Click the Mouse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Example 5-11: Detect When Not Clicked. . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Example 5-12: Multiple Mouse Buttons. . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Location. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Example 5-13: Find the Cursor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Example 5-14: The Bounds of a Circle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Example 5-15: The Bounds of a Rectangle. . . . . . . . . . . . . . . . . . . . . . . . . 75
Type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Example 5-16: Tap a Key. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Example 5-17: Draw Some Letters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Example 5-18: Check for Specific Keys. . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Example 5-19: Move with Arrow Keys. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Touch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Example 5-20: Touch the Screen. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Example 5-21: Track the Finger. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Map. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Example 5-22: Map Values to a Range. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Example 5-23: Map with the map() Function. . . . . . . . . . . . . . . . . . . . . . . . 83
Robot 3: Response. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Contents v
Example 6-10: Isolating Transformations. . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Robot 4: Translate, Rotate, Scale. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
7/Media. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Example 7-1: Load an Image. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Example 7-2: Load More Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Example 7-3: Mousing Around with Images. . . . . . . . . . . . . . . . . . . . . . . . 106
Example 7-4: Transparency with a GIF. . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Example 7-5: Transparency with a PNG. . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Example 7-6: Displaying an SVG Image. . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Asynchronicity. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Example 7-7: Demonstrating Asynchronicity. . . . . . . . . . . . . . . . . . . . . . . 110
Example 7-8: Loading with a Callback. . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Fonts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Example 7-9: Drawing with Fonts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Example 7-10: Use a Webfont. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Example 7-11: Load a Custom Font. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Example 7-12: Set the Text Stroke and Fill. . . . . . . . . . . . . . . . . . . . . . . . 115
Example 7-13: Draw Text in a Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Example 7-13: Store Text in a Variable. . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Robot 5: Media. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
8/Motion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Frames. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Example 8-1: See the Frame Rate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Example 8-2: Set the Frame Rate. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Speed and Direction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Example 8-3: Move a Shape. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Example 8-4: Wrap Around. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Example 8-5: Bounce Off the Wall. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Tweening. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Example 8-6: Calculate Tween Positions. . . . . . . . . . . . . . . . . . . . . . . . . . 126
Random. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Example 8-7: Generate Random Values. . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Example 8-8: Draw Randomly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Example 8-9: Move Shapes Randomly. . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Timers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Example 8-10: Time Passes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Example 8-11: Triggering Timed Events. . . . . . . . . . . . . . . . . . . . . . . . . . 130
Circular. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Example 8-12: Sine Wave Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
vi Contents
Example 8-13: Sine Wave Movement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Example 8-14: Circular Motion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Example 8-15: Spirals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Robot 6: Motion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
9/Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Function Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Example 9-1: Roll the Dice. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Example 9-2: Another Way to Roll. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Make a Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Example 9-3: Draw the Owl. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
Example 9-4: Two’s Company. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Example 9-5: An Owl Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Example 9-6: Increasing the Surplus Population. . . . . . . . . . . . . . . . . . . . 146
Example 9-7: Owls of Different Sizes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Return Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Example 9-8: Return a Value. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Robot 7: Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
10/Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Properties and Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Define a Constructor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Create Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Example 10-1: Make an Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Example 10-2: Make Multiple Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Robot 8: Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
11/Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
From Variables to Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Example 11-1: Many Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Example 11-2: Too Many Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Example 11-3: Arrays, Not Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Make an Array. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Example 11-4: Declare and Assign an Array. . . . . . . . . . . . . . . . . . . . . . . 169
Example 11-5: Assigning to an Array in One Go. . . . . . . . . . . . . . . . . . . . 170
Example 11-6: Revisiting the First Example. . . . . . . . . . . . . . . . . . . . . . . . 170
Repetition and Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Example 11-7: Filling an Array in a for Loop. . . . . . . . . . . . . . . . . . . . . . . 171
Example 11-8: Track Mouse Movements. . . . . . . . . . . . . . . . . . . . . . . . . . 172
Arrays of Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Example 11-9: Managing Many Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Example 11-10: Sequences of Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Contents vii
Robot 9: Arrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
12/Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Data Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Tables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Example 12-1: Read the Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Example 12-2: Draw the Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Example 12-3: 29,740 Cities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
JSON. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Example 12-4: Read a JSON File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Example 12-5: Visualize Data from a JSON File. . . . . . . . . . . . . . . . . . . . 189
Network Data and APIs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Example 12-6: Parsing the Weather Data. . . . . . . . . . . . . . . . . . . . . . . . . 194
Example 12-7: Chaining Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Robot 10: Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
13/Extend. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
p5.sound. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Example 13-1: Play a Sample. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Example 13-2: Listen to a Mic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Example 13-3: Create a Sine Wave. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
p5.dom. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Example 13-4: Access the Webcam. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Example 13-5: Create a Slider. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Example 13-6: Create an Input Box. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
viii Contents
Visit https://ebooknice.com to
discover a wide range of
eBooks across various genres.
Enjoy exclusive deals and
discounts to enhance your
reading experience. Start your
digital reading journey today!
Preface
xi
New users begin by focusing on graphics and interaction rather
than on data structures and text console output.
Over the years, Processing has grown into a huge community. It
is used in classrooms worldwide, in arts, humanities, and com-
puter science curricula, as well as by professionals.
Two years ago, Ben and Casey approached me with a question:
what would Processing look like if it were on the Web? p5.js
starts with the original goal of Processing, to make coding
accessible for artists, designers, educators, and beginners, then
reinterprets it for today’s Web using JavaScript and HTML.
Developing p5.js has felt like bringing different worlds together.
To ease the transition to the Web for the existing community of
Processing users, we adhered to the syntax and conventions of
Processing as much as possible. However, p5.js is built with
JavaScript, while Processing is built with a language called Java.
These two languages have different patterns and affordances,
so at times we had to deviate from familiar Processing syntax. It
was also important that p5.js integrated seamlessly into all the
existing web features, tools, and frameworks in order to draw in
users that might be familiar with the Web but new to creative
coding. Synthesizing all of these factors was a challenge, but the
goal of uniting these frameworks provided a clear path for the
development of p5.js.
A first beta version was launched in August 2014. Since then, it
has been used and integrated into curricula around the world.
There is an official p5.js Editor currently in development, and
progress is underway on many new features and libraries.
p5.js is a community effort—hundreds of people have contrib-
uted core features, bug fixes, examples, documentation, design,
thoughts, and discussion. We aim to carry on the vision and the
spirit of the Processing community as we open it up even more
on the Web.
xii Preface
How This Book Is Organized
The chapters in this book are organized as follows:
Preface xiii
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and
file extensions.
Constant width
Used for program listings, as well as within paragraphs to
refer to program elements such as variable or function
names, databases, data types, environment variables, state-
ments, and keywords.
Constant width italic
Shows text that should be replaced with user-supplied val-
ues or by values determined by context.
xiv Preface
Casey Reas, and Ben Fry. Copyright 2015 Maker Media, Inc.,
978-1-457-18677-6.”
If you feel your use of code examples falls outside fair use or the
permission given here, feel free to contact us at permis-
[email protected].
How to Contact Us
Please address comments and questions concerning this book
to the publisher:
Preface xv
Make: unites, inspires, informs, and entertains a growing com-
munity of resourceful people who undertake amazing projects in
their backyards, basements, and garages. Make: celebrates your
right to tweak, hack, and bend any technology to your will. The
Make: audience continues to be a growing culture and commu-
nity that believes in bettering ourselves, our environment, our
educational system—our entire world. This is much more than
an audience, it’s a worldwide movement that Make: is leading—
we call it the Maker Movement.
For more information about Make:, visit us online:
Acknowledgments
We thank Brian Jepson and Anna Kaziunas France for their great
energy, support, and insight.
We can’t imagine this book without Massimo Banzi’s Getting
Started with Arduino (Maker Media). Massimo’s excellent book
is the prototype.
A small group of individuals has, for years, contributed essential
time and energy to Processing. Dan Shiffman is our partner in
the Processing Foundation, the 501(c)(3) organization that sup-
ports the Processing software. Much of the core code for Pro-
cessing 2.0 and 3.0 has come from the sharp minds of Andres
Colubri and Manindra Moharana. Scott Murray, Jamie Kosoy,
and Jon Gacnik have built a wonderful web infrastructure for the
project. James Grady is rocking the 3.0 user interface. We thank
Florian Jenett for his years of diverse work on the project includ-
ing the forums, website, and design. Elie Zananiri and Andreas
Schlegel have created the infrastructure for building and docu-
menting contributed libraries, and have spent countless hours
curating the lists. Many others have contributed significantly to
the project; the precise data is available at https://github.com/
processing.
xvi Preface
This book grew out of teaching with Processing at UCLA. Chan-
dler McWilliams has been instrumental in defining these
classes. Casey thanks the undergraduate students in the
Department of Design Media Arts at UCLA for their energy and
enthusiasm. His teaching assistants have been great collabora-
tors in defining how Processing is taught. Hats off to Tatsuya
Saito, John Houck, Tyler Adams, Aaron Siegel, Casey Alt,
Andres Colubri, Michael Kontopoulos, David Elliot, Christo Alle-
gra, Pete Hawkes, and Lauren McCarthy.
p5.js is developed by a large community of contributors around
the world. Dan Shiffman, Jason Sigal, Sam Lavigne, K.Adam
White, Chandler McWilliams, Evelyn Eastmond, the members of
the p5 working group at ITP, the attendees of the first p5.js Con-
tributor’s Conference at the Frank-Ratchye STUDIO for Creative
Inquiry at Carnegie Mellon University, and the students and
mentors of Processing Google Summer of Code have been
instrumental in bringing p5.js from its early days to where it is
today. Significant support for the project has been provided by
the Processing Foundation, NYU ITP, RISD, and Bocoup. You can
view the full list of contributors at http://p5js.org/contribute/
#contributors. Lauren also thanks Kyle McDonald for his perpet-
ual support and inspiration.
This book is transformed by Taeyoon Choi’s artful illustrations.
They were developed in part through a residency at the Frank-
Ratchye STUDIO for Creative Inquiry at Carnegie Mellon Univer-
sity, with support from the Art Works program of the National
Endowment for the Arts. Charlotte Stiles helped tremendously
with editing the examples and images for this book.
Preface xvii
Visit https://ebooknice.com to
discover a wide range of
eBooks across various genres.
Enjoy exclusive deals and
discounts to enhance your
reading experience. Start your
digital reading journey today!
1/Hello
1
Sketching and Prototyping
Sketching is a way of thinking; it’s playful and quick. The basic
goal is to explore many ideas in a short amount of time. In our
own work, we usually start by sketching on paper and then mov-
ing the results into code. Ideas for animation and interactions
are usually sketched as storyboards with notations. After mak-
ing some software sketches, the best ideas are selected and
combined into prototypes (Figure 1-1). It’s a cyclical process of
making, testing, and improving that moves back and forth
between paper and screen.
Giants
People have been making pictures with computers since the
1960s, and there’s much to be learned from this history. For
example, before computers could display to CRT or LCD
screens, huge plotter machines (Figure 1-2) were used to draw
images. In life, we all stand on the shoulders of giants, and the
titans for p5.js include thinkers from design, computer graphics,
art, architecture, statistics, and the spaces between. Have a
look at Ivan Sutherland’s Sketchpad (1963), Alan Kay’s Dyna-
book (1968), and the many artists featured in Ruth Leavitt’s
Artist and Computer1 (Harmony Books, 1976). The ACM SIG-
GRAPH and Ars Electronica archives provide fascinating glimp-
ses into the history of graphics and software.
1 http://www.atariarchives.org/artist/
Hello 3
Figure 1-2. Drawing demonstration by Manfred Mohr at Musée
d’Art Moderne de la Ville de Paris using the Benson plotter and a
digital computer on May 11, 1971 (photo by Rainer Mürle, cour-
tesy bitforms gallery, New York)
Figure 1-3. p5.js has a large family of related languages and pro-
gramming environments
Join In
Thousands of people use p5.js every day. Like them, you can
download p5.js for free. You even have the option to modify the
p5.js code to suit your needs. p5.js is a FLOSS project (that is,
free/libre/open source software), and in the spirit of community,
we encourage you to participate by sharing your projects and
knowledge online at http://p5js.org (Figure 1-4).
Hello 5
Figure 1-4. p5.js is fueled by a community of people contribu-
ting through the Internet
9
Random documents with unrelated
content Scribd suggests to you:
olyan meghatottságot érzett, amilyet még soha.
Az asszony a nyakába borult és azt mondta neki:
– Mindent tudok. Megbocsátok neked. Nagyon szeretlek.
– Milyen jóságos vagy te, édes Juliám! – szólt a lovag, egy kissé
elérzékenyedve.
Most tegezte életében először.
Soha se idegenkedett a feleségétől. Őszinte vonzalomból vette el
és nemcsak azért, mert látta, hogy szerelmet keltett szegénykében.
Megbecsülte sok kitünő tulajdonságát, s szeretettel, igaz barátsággal
volt iránta. Elnézte neki, – hiszen nem tehet róla! – hogy nem tudta
fölébreszteni benne azt a szenvedélyt, amelyet nem egészen
félesztendővel a házassága után a szép grófné keltett benne életre.
Talán akkor nem is vette volna el, ha olyannak látja, mint a grófnét…
amaz szeretőnek, ez feleségnek való.
De ha máskor is ragaszkodott hozzá, igy még soha se szerette,
mint ebben a percben, amikor, szegény, formátlanabb volt, mint
valaha.
Vigasztalni kezdte:
– Ej, hat év nem olyan nagy idő! Még fiatalok vagyunk mind a
ketten. Hat év hamar elmulik. És meglásd, milyen hálás leszek azért,
hogy most ilyen jó vagy hozzám!… Majd meglátod, milyen boldogok
leszünk még!
Őszintén gondolta.
– Hat évig nálad nélkül! – sirdogált az asszony. – És hat évig
folyton arra gondolnom, hogy te a börtönben vagy!
– Ej, ez a börtön nem lesz olyan rettenetes, mint képzeled. Csak
hogy éppen nem járhatok szabadon. De itt, a várban, kedvemre
sétálgathatok; a cellám pedig tágas, szép szoba. A derék Le Noir
minden kedvezést megadott, ami megkönnyitheti a fogságomat.
Olvashatok, irhatok; megkapom a könyveimet, levelezhetek
barátaimmal… Sokat fogok irni, még többet tanulni; igyekszem
tökéletességre vinni a latin, a görög és az angol nyelvben. És
munkaközben olyan hamar elmulik a nap, hogy az ember észre se
veszi. Időnkint látni foglak… Ha most találkozhattunk, találkozhatunk
később is. Eljösz, talán minden hónapban és elhozod a
gyermekünket… Hat év! Nem olyan nagy idő az! És a gondolataink
mindennap együtt lesznek néhány óráig, amig levelet irunk
egymásnak. Hat év! Hamar elmulik az! Sokat fogsz foglalkozni a
gyermekünkkel s hamar elröppen a nap. Amikor nem láthatjuk
egymást, szórakozni fogsz. Talán többet fogsz szórakozni, mint ma
gondolod…
Már féltéssel gondolt Choiseul de Meusere.
A kisirt szemü asszony kitalálta ezt a gondolatot s végre
elmosolyodott.
III.
É
A felesége leveleiben egyre több szó volt a gyermekükről. És ő is
mind gyakraban gondolt a kis fiára. Megkapta az arcképét;
mindennap olvasott róla, mit csinál, hogyan erősödik. Tudta, mit
gügyögött először, mikor tanult járni, mivel szeret játszani, hova
vitték, mit mondott. A hatodik esztendő elején már irni tanitották és
az első betüit elküldték az apjának.
A hatodik esztendőben már nem volt egyéb gondolata, csak a kis
fia – és hogy milyen lassan mulnak a napok.
Türelmetlenül számlálta előbb a heteket, aztán a napokat, végül
az órákat.
Egy héttel a kiszabadulása előtt egyszerre csak elmaradtak a
felesége levelei. Moncontour lovag azt hitte, hogy neki meg kell
őrülnie, mert ez a hét soha se mulik el.
IV.
I.
II.
III.
IV.
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.
ebooknice.com