-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppC_Ideas.html
More file actions
232 lines (222 loc) · 9.31 KB
/
AppC_Ideas.html
File metadata and controls
232 lines (222 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript: AppC Ideas</title>
<meta name="title" content="Variations on a Theme: JavaScript: AppC Ideas">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="UTF-8">
<meta name="description" content="An object-oriented Introduction">
<meta name="keywords" content="JavaScript,object orientation,introduction">
<meta name="author" content="Ralph P. Lano">
<meta name="robots" content="index,follow">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="book.css">
</head>
<body>
<center>
<div id="wrap">
<ul class="sidenav">
<p><a href="../index.html">Variations on a Theme</a><a href="index.html">JavaScript</a></p>
<li><a href="Ch1_Karel.html">Karel</a></li>
<li><a href="Ch2_Graphics.html">Graphics</a></li>
<li><a href="Ch3_Console.html">Console</a></li>
<li><a href="Ch4_Agrar.html">Agrar</a></li>
<li><a href="Ch5_MindReader.html">MindReader</a></li>
<li><a href="Ch6_Swing.html">Swing</a></li>
<li><a href="Ch7_Asteroids.html">Asteroids</a></li>
<li><a href="Ch8_Stocks.html">Stocks</a></li>
<li><a href="index.html"> </a></li>
<li><a href="AppA_Primer.html">Primer</a></li>
<li><a href="AppB_Libraries.html">Libraries</a></li>
<li><a href="AppC_Ideas.html">Ideas</a></li>
</ul>
<div class="content">
<h1>
Ideas</h1>
<p>
As we have been indicating throughout this book, JavaScript has become more and more powerful. Also p5.js has a few interesting capabilities worth while exploring. In this final section we want to explore a couple of those, such as</p>
<ul>
<li>
playing videos,</li>
<li>
accessing the webcam,</li>
<li>
accessing the microphone,</li>
<li>
accessing sensors,</li>
<li>
web workers,</li>
<li>
web sockets, and</li>
<li>
3D drawing.</li>
</ul>
<p>
Interestingly, some work very nicely, others barely at all. In addition, access an online database like Back4App might open up a whole new set of applications. And finally, peer-to-peer would be a cool feature.</p>
<p>
.</p>
<div style="display:block; float: right; margin: 10px;">
<a href="./src/tryIt.html?name=PrX_Advanced/drawing" style="display: block; text-align: center;" target="_blank"><img alt="" src="img/drawing.png" style="width: 200px; height: 100px; display: block;" />Try it</a></div>
<h2>
Drawing</h2>
<p>
This is a simple drawing application. Its main point is to work on both desktop and mobile devices. The problem lies in the fact that desktop browsers fire the 'mouse' events, whereas the mobile browsers fire the 'touch' events. The following function solves this problem:</p>
<pre style="margin-left: 40px;">
function addMouseOrTouchEvents() {
const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
if (isMobile) {
window.addEventListener('touchstart', handleStart);
window.addEventListener('touchmove', handleMove);
window.addEventListener('touchend', handleEnd);
} else {
window.addEventListener('mousedown', handleStart);
window.addEventListener('mousemove', handleMove);
window.addEventListener('mouseup', handleEnd);
}
}
function handleStart(evt) {
...
}
function handleMove(evt) {
...
}
function handleEnd(evt) {
...
}</pre>
<p>
.</p>
<div style="display:block; float: right; margin: 10px;">
<a href="./src/tryIt.html?name=PrX_Advanced/htmlTextEditor" style="display: block; text-align: center;" target="_blank"><img alt="" src="img/htmlTextEditor.png" style="width: 200px; height: 200px; display: block;" />Try it</a></div>
<h2>
HtmlEditor</h2>
<p>
This is an example of a simple HTML editor. It basically uses a div-tag for rendering together with the document.execCommand(), which actually is deprecated and should not be used anymore, but still works on all browsers I tested. It is a UI program, and we use the <em>JSHtmlTextArea</em> widget:</p>
<pre style="margin-left: 40px;">
function setup() {
createGUI(300, 300);
frameRate(5);
setLayout('border');
display = new JSHtmlTextArea("Enter text here...", 10, 20);
display.addStyle('width: 99%');
display.addStyle('height: 98%');
display.setHtml('hi <b>there</b>, how are <span style="color:red">you</span>?');
addWidget(display, 'CENTER');
...
}</pre>
<p>
The trick is to mark the div-tag with the attribute <em>contentEditable</em> as true.</p>
<p>
.</p>
<div style="display:block; float: right; margin: 10px;">
<a href="./src/tryIt.html?name=PrX_Advanced/urlReaderTester" style="display: block; text-align: center;" target="_blank"><img alt="" src="img/urlReaderTester.png" style="width: 200px; height: 126px; display: block;" />Try it</a></div>
<h2>
UrlReaderTester</h2>
<p>
This example uses and XMLHttpRequest object to issue an HTTP GET request. It could also be used for POST, PUT or DELETE requests. However, this example does not work on many mobile browsers.</p>
<p>
.</p>
<p>
.</p>
<p>
.</p>
<p>
.</p>
<div style="display:block; float: right; margin: 10px;">
<a href="./src/tryIt.html?name=PrX_Advanced/microphone" style="display: block; text-align: center;" target="_blank"><img alt="" src="img/microphone.png" style="width: 200px; height: 99px; display: block;" />Try it</a></div>
<h2>
Microphone</h2>
<p>
A simple GraphicsProgram that accesses the microphone. You need to grant the browser permission to access the microphone. An interesting application might be to do Fourier transformation, etc. However, I did not get this example to work on mobile browsers.</p>
<p>
.</p>
<p>
.</p>
<p>
.</p>
<p>
.</p>
<div style="display:block; float: right; margin: 10px;">
<a href="./src/tryIt.html?name=PrX_Advanced/video" style="display: block; text-align: center;" target="_blank"><img alt="" src="img/video.png" style="width: 200px; height: 151px; display: block;" />Try it</a></div>
<h2>
Video</h2>
<p>
A simple demonstration on how to play and modify video files. Especially the modifying part has really surprised me: one, that it is relatively easy to do, and two, that the framerate is still decent. And three, this example works on all browsers I tested, even the mobile ones.</p>
<p>
.</p>
<p>
.</p>
<p>
.</p>
<p>
.</p>
<p>
.</p>
<div style="display:block; float: right; margin: 10px;">
<a href="./src/tryIt.html?name=PrX_Advanced/webCam" style="display: block; text-align: center;" target="_blank"><img alt="" src="img/webCam.png" style="width: 200px; height: 150px; display: block;" />Try it</a></div>
<h2>
WebCam</h2>
<p>
A simple demonstration on how to access a webcam, and how to modify the pixels. You need to grant the browser permission to access the webcam, and as expected, it I did not get it to run on any of the mobile browsers.</p>
<p>
.</p>
<p>
.</p>
<p>
.</p>
<p>
.</p>
<p>
.</p>
<div style="display:block; float: right; margin: 10px;">
<a href="./src/tryIt.html?name=PrX_Advanced/sensors" style="display: block; text-align: center;" target="_blank"><img alt="" src="img/sensors.png" style="width: 200px; height: 100px; display: block;" />Try it</a></div>
<h2>
Sensors</h2>
<p>
Now this was a big disappointment: I really was hoping that this would work at least on some mobile devices. I only got it to work on Firefox for Android. It is a simple GraphicsProgram that accesses the sensors. You need to grant the browser permission to access the sensors.</p>
<p>
.</p>
<p>
.</p>
<p>
.</p>
<div style="display:block; float: right; margin: 10px;">
<a href="./src/tryIt.html?name=PrX_Advanced/mandelbrot" style="display: block; text-align: center;" target="_blank"><img alt="" src="img/mandelbrot.png" style="width: 200px; height: 200px; display: block;" />Try it</a></div>
<h2>
WebWorker</h2>
<p>
JavaScript is inherently single threaded. Web workers is a way around this. Surprisingly enough, this worked on all browsers I tested, also the mobile ones. </p>
<p>
[1] https://medium.com/techtrument/multithreading-javascript-46156179cf9a<br />
[2] http://blog.namangoel.com/replacing-eval-with-a-web-worker</p>
<p>
.</p>
<p>
.</p>
<p>
.</p>
<p>
.</p>
<h2>
ToDo</h2>
<p>
There are a few more interesting projects to explore.</p>
<p>
- 3D stuff<br />
- database via Back4App or similar<br />
- websockets<br />
- p2p<br />
<a href="https://github.com/vanevery/p5LiveMedia">https://github.com/vanevery/p5LiveMedia</a><br />
https://peerjs.com/<br />
https://blog.bitsrc.io/simplified-peer-to-peer-communication-with-peerjs-e37244267723<br />
https://medium.com/geekculture/multiplayer-interaction-with-p5js-f04909e13b87<br />
https://github.com/vanevery/p5LiveMedia</p>
<p>
.</p>
<p class="footer">
Copyright © 2016-2023 <a href="http://www.lano.de">Ralph P. Lano</a>. All rights reserved.
</p>
</div>
</div>
</center>
</body>
</html>