-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
327 lines (301 loc) · 11.7 KB
/
index.html
File metadata and controls
327 lines (301 loc) · 11.7 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<!DOCTYPE html>
<html>
<head>
<title>Draw HTML5</title>
<script type="text/javascript">
function setPixel(imageData, x, y, r=0, g=0, b=0, a=255){
index = (x + y * imageData.width) * 4;
imageData.data[index + 0] = r;
imageData.data[index + 1] = g;
imageData.data[index + 2] = b;
imageData.data[index + 3] = a;
}
function execPixel(){
var x1 = parseInt(document.getElementById("xcoor").value);
var y1 = parseInt(document.getElementById("ycoor").value);
var canvas = document.getElementById("myCanvas");
drawPixel(canvas, x1, y1);
}
function drawPixel(canvas, x, y, r=0, g=0, b=0, a=255){
var w = canvas.width;
var h = canvas.height;
context = canvas.getContext("2d");
var imageData = context.getImageData(0, 0, w, h);
setPixel(imageData, x, y);
context.putImageData(imageData, 0, 0);
}
function execLine(){
var x1 = parseInt(document.getElementById("x1coor").value);
var y1 = parseInt(document.getElementById("y1coor").value);
var x2 = parseInt(document.getElementById("x2coor").value);
var y2 = parseInt(document.getElementById("y2coor").value);
var canvas = document.getElementById("myCanvas2");
drawLine(canvas, x1, y1, x2, y2);
}
function drawLine(canvas, x1, y1, x2, y2, r=0, g=0, b=0, a=255){
var w = canvas.width;
var h = canvas.height;
context = canvas.getContext("2d");
var imageData = context.getImageData(0, 0, w, h);
// y = mx + c
//Bresenham's Algorithm
var dx = Math.abs(x2 - x1);
var sx = x1 < x2 ? 1 : -1;
var dy = Math.abs(y2 - y1);
var sy = y1 < y2 ? 1 : -1;
var err = (dx > dy ? dx : -dy)/2;
while (true) {
drawPixel(canvas, x1, y1);
if (x1 === x2 && y1 === y2) break;
var e2 = err;
if (e2 > -dx) { err -= dy; x1 += sx; }
if (e2 < dy) { err += dx; y1 += sy; }
}
}
window.onload=function(){
//drawDot, cvs4 = canvas 4
var cvs4 = document.getElementById("drawDotCanvas");
drawPixel(cvs4, 100, 100, 1, 1);
//drawLine, cvs5 = canvas 5
var cvs5 = document.getElementById("drawLineCanvas");
drawLine(cvs5, 10, 10, 190, 190);
//Assignment Program
var cvs6 = document.getElementById("assProgramCanvas");
drawLine(cvs6, 50, 100, 50, 50);
drawLine(cvs6, 50, 50, 100, 50);
drawLine(cvs6, 100, 50, 100, 100);
drawLine(cvs6, 100, 100, 50, 100);
drawPixel(cvs6, 75, 75);
}
function changeColor(){
let color = document.getElementById('colorInputColor').value;
document.querySelector("#numofColorsCanvas").style.backgroundColor = color;
document.getElementById('colorInputText').value = color;
}
</script>
<style type="text/css">
canvas{
background-color: lightgreen;
}
q, blockquote{
background-color: lightgrey;
}
a:visited {
color: blue;
}
</style>
</head>
<body>
<h3 style="text-align:center;" id="theTitle">Homework I</h3>
<h4>Explain</h4>
<ul>
<li><a href="#displayScreen">How to display a screen</a></li>
<li><a href="#theResolution">The resolution of the screen</a></li>
<li><a href="#numofColors">Number of colors that can be displayed on the screen</a></li>
<li><a href="#drawDot">How to display a dot at a certain coordinate on the screen</a></li>
<li><a href="#drawLine">How to draw a line on the screen</a></li>
</ul>
<h4>Program</h4>
<ul>
<li><a href="#assProgram">Draw an image as given in the assignment</a></li>
<li><a href="#demo">Try out features in our demo</a></li>
</ul>
<hr>
<div>
<h3>Introduction</h3>
<p>
In HTML5, we can draw on the screen using <b>canvas</b> feature in HTML5, in this page we'll be covering the assignments for our CGA class in the first week. Let's dive right in to the answers. I'm not really sure whether we should use self created functions in this assignment or starting in programming assignments only so I've decided to use default library from HTML5. The code for this page is also available at my <a href="https://github.com/kennysusanto/drawHTML5.git" target="blank">github repository</a> if you're considering to check it out, please do.
</p>
</div>
<div id="displayScreen">
<h3>How to display a screen (canvas) in HTML5</h3>
<p>To use canvas in HTML5, here's a few step by step:</p>
<ol>
<li>
Type in
<q>
<canvas id="myCanvas" width="200" height="200">Canvas not supported by this browser.</canvas>
</q>
on the body of your html document. Result as seen below.
</li>
<li>
(Optional) To color the background of the canvas you'll need to use a little bit of CSS.
</li>
</ol>
<canvas id="displayScreenCanvas" width="200" height="200">Canvas not supported by this browser.</canvas>
</div>
<div id="theResolution">
<h3>The resolution of the screen (canvas) in HTML5</h3>
<p>The resolution of this canvas is as can be seen in the code <q><canvas id="myCanvas" width="200" height="200">Canvas not supported by this browser.</canvas></q> is <b>width = 200</b> and <b>height = 200</b>.</p>
<canvas id="theResolutionCanvas" width="200" height="200">Canvas not supported by this browser.</canvas>
</div>
<div id="numofColors">
<h3>Number of colors that can be displayed on the screen (canvas) in HTML5</h3>
<p>HTML color codes are hexadecimal triplets representing the colors red, green, and blue (#RRGGBB) each of which can be the range of:</p>
<li>0 to 255 (in decimal)</li>
<li>00 to FF (in hexadecimal)</li>
<p>Because each of the three colors can have values from 0 to 255 (256 possible values), there are:
256 × 256 × 256 = 256³
= <b>16,777,216 possible color combinations</b>
</p>
<h3 style="color: blue;">COLOR PICKER</h3>
<div>
<input type="text" id="colorInputText">
<input type="color" id="colorInputColor">
</div>
<br>
<input type="button" id="colorButton" value="Fill" onclick="changeColor()">
<br><br>
<canvas id="numofColorsCanvas" width="200" height="200"></canvas>
</div>
<div id="drawDot">
<h3>How to display a dot at a certain coordinate on the screen (canvas) in HTML5</h3>
<p>Now we will attempt to create a dot in the center of the canvas which will be on the coordinate of <b>100, 100</b> and follow this step by step:</p>
<ol>
<li>
Type in
<q>
<canvas id="myCanvas" width="200" height="200">Canvas not supported by this browser.</canvas>
</q>
on the body of your html document.
</li>
<li>
Create a <b>javascript</b> script tag in your html document.
</li>
<li>
Type in these functions in the script tag.
<blockquote>
function setPixel(imageData, x, y, r=0, g=0, b=0, a=255){<br>
 index = (x + y * imageData.width) * 4;<br>
 imageData.data[index + 0] = r;<br>
 imageData.data[index + 1] = g;<br>
 imageData.data[index + 2] = b;<br>
 imageData.data[index + 3] = a;<br>
}<br>
function drawPixel(canvas, x, y, r=0, g=0, b=0, a=255){<br>
 var w = canvas.width;<br>
 var h = canvas.height;<br>
 context = canvas.getContext("2d");<br>
 var imageData = context.getImageData(0, 0, w, h);<br>
 setPixel(imageData, x, y);<br>
 context.putImageData(imageData, 0, 0);<br>
}
</blockquote>
</li>
<li>
Then type in these execution code
<blockquote>
window.onload=function(){<br>
 var canvas = document.getElementById("myCanvas");<br>
 drawPixel(canvas, 100, 100);<br>
}
</blockquote>
in the script tag.
</li>
<li>As you can see in the script we changed the pixel color to black (r = 0, g = 0, b = 0)</b> on coordinate <b>100, 100</b>.</li>
<li>Voila! (you need to <b>zoom in</b> to see the pixel 'coz it's really small).</li>
</ol>
<canvas id="drawDotCanvas" width="200" height="200">Canvas not supported by this browser.</canvas>
</div>
<div id="drawLine">
<h3>How to draw a line on the screen (canvas) in HTML5</h3>
<p>Now we will attempt to create a line in the canvas which will be kinda similar which creating a dot in the canvas, follow this step by step:</p>
<ol>
<li>
Type in
<q>
<canvas id="myCanvas" width="200" height="200">Canvas not supported by this browser.</canvas>
</q>
on the body of your html document.
</li>
<li>
Add in these functions in to the script tag with existing codes.
<blockquote>
function drawLine(canvas, x1, y1, x2, y2, r=0, g=0, b=0, a=255){<br>
 var w = canvas.width;<br>
 var h = canvas.height;<br>
 context = canvas.getContext("2d");<br>
 var imageData = context.getImageData(0, 0, w, h);<br><br>
 // y = mx + c<br><br>
 //Bresenham's Algorithm<br>
 var dx = Math.abs(x2 - x1);<br>
 var sx = x1 < x2 ? 1 : -1;<br>
 var dy = Math.abs(y2 - y1);<br>
 var sy = y1 < y2 ? 1 : -1; <br>
 var err = (dx > dy ? dx : -dy)/2;<br>
 while (true) {<br>
  drawPixel(canvas, x1, y1);<br>
  if (x1 === x2 && y1 === y2) break;<br>
  var e2 = err;<br>
  if (e2 > -dx) { err -= dy; x1 += sx; }<br>
  if (e2 < dy) { err += dx; y1 += sy; }<br>
 }<br>
}
</blockquote>
</li>
<li>
Then type in these execution code
<blockquote>
window.onload=function(){<br>
 var canvas = document.getElementById("myCanvas");<br>
 drawLine(canvas, 10, 10, 190, 190);<br>
}
</blockquote>
in the script tag inside <b>window.onload=function</b> if it already exists.
</li>
<li>As you can see in the script we will create a line starting from coordinate <b>10, 10</b> until <b>190, 190</b>.</li>
</ol>
<canvas id="drawLineCanvas" width="200" height="200">Canvas not supported by this browser.</canvas>
</div>
<hr>
<div id="assProgram">
<h3>Program to draw as given in the assignment</h3>
<p>This time we will solve the program assignment given in this task, follow this step by step:</p>
<ol>
<li>
Type in
<q>
<canvas id="myCanvas" width="200" height="200">Canvas not supported by this browser.</canvas>
</q>
on the body of your html document.
</li>
<li>
Type in
<blockquote>
window.onload=function(){<br>
 var canvas = document.getElementById("myCanvas");<br>
 drawLine(canvas, 50, 100, 50, 50);<br>
 drawLine(canvas, 50, 50, 100, 50);<br>
 drawLine(canvas, 100, 50, 100, 100);<br>
 drawLine(canvas, 100, 100, 50, 100);<br><br>
 drawPixel(canvas, 75, 75);<br>
}
</blockquote>
in the script tag inside <b>window.onload=function</b> if it already exists.
</li>
<li>
As you can probably see, in the script we created a rectangle with lines starting from <b>50, 100</b> continuing to <b>50, 50</b> then <b>100, 50</b> then <b>100, 100</b> and finally returning to the starting point <b>50, 100</b> also we added a point/pixel at the center of the rectangle specifically at <b>75, 75</b>.
</li>
</ol>
<canvas id="assProgramCanvas" width="200" height="200">Canvas not supported by this browser.</canvas>
</div>
<div id="demo">
<h3>Demo</h3>
<h2>draw pixel</h2>
x:<input type="text" id="xcoor" size="3">
y:<input type="text" id="ycoor" size="3">
<button onclick="execPixel()">draw</button>
<p id="res">width:200 height:200</p>
<canvas id="myCanvas" width="200" height="200"></canvas>
<h2>draw line</h2>
x1:<input type="text" id="x1coor" size="3">
y1:<input type="text" id="y1coor" size="3"><br>
x2:<input type="text" id="x2coor" size="3">
y2:<input type="text" id="y2coor" size="3">
<button onclick="execLine()">draw</button>
<p id="res">width:200 height:200</p>
<canvas id="myCanvas2" width="200" height="200"></canvas>
</div>
<h4 style="text-align: center;"><a href="#theTitle">return top</a></h4>
</body>
</html>