Lab1 Introduction to Computer Graphics Using WebGL
Lab1 Introduction to Computer Graphics Using WebGL
<html>
<head>
<script type = "text/javascript">
function sayHello() {
document.write ("Hello there!");
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "sayHello()" value
= "Say Hello">
</form>
<p>Use different text in write method and then
try...</p>
</body>
</html>
The getElementById() is a DOM method used to return the element that has the
ID attribute with the specified value. This is one of the most common methods in
the HTML DOM and is used almost every time we want to manipulate an
element on our document. This method returns null if no elements with the
specified ID exists. The ID should be unique within a page. However, if more
than one element with the specified ID exists, it returns the last element in the
javascript code.
Example
<html>
<body>
<p id="element">GetElementById</p>
<script>
var s = document.getElementById("element").innerHTML;
document.write(s);
</script>
</body>
</html>
1.4Event Listener
The event listeners are just like event handlers, except that you can assign as
many event listeners as you like to a particular event on particular element.
To understand how event listeners actually works let's check out a simple
example. Suppose that you've created two functions and you try to execute both
of them on click of the button using the onclick event handler, as shown in the
following example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript Assigning Multiple Event Handlers on a
Single Event</title>
</head>
<body>
<button id="myBtn">Click Me</button>
<script>
// Defining custom functions
function firstFunction() {
alert("The first function executed successfully!");
}
function secondFunction() {
alert("The second function executed successfully");
}
HTML5 <canvas> provides an easy and powerful option to draw graphics using
JavaScript. It can be used to draw graphs, make photo compositions, or do simple (and
not so simple) animations.
The syntax of HTML canvas tag is given below. You have to mention the name of the
canvas inside double quotations (“ ”).
<canvas id = "mycanvas" width = "100" height = "100"></canvas>
Canvas Attributes
The canvas tag has three attributes namely, id, width, and height.
Id − Id represents the identifier of the canvas element in the Document Object
Model (DOM).
Width − Width represents the width of the canvas.
Height − Height represents the height of the canvas.
These attributes determine the size of the canvas. If a programmer is not specifying them
under the canvas tag, then browsers such as Firefox, Chrome, and Web Kit, by default,
provide a canvas element of size 300 × 150.
The following code shows how to create a canvas. We have used CSS to give a colored
border to the canvas.
<html>
<head>
<style>
#mycanvas{border:1px solid red;}
</style>
</head>
<body>
<canvas id = "mycanvas" width = "100" height =
"100"></canvas>
</body>
</html>
2.1 Rendering Context
The <canvas> is initially blank. To display something on the canvas element, we have to
use a scripting language. This scripting language should access the rendering context and
draw on it.
The canvas element has a DOM method called getContext(), which is used to obtain the
rendering context and its drawing functions. This method takes one parameter, the type of
context 2d.
The following code is to be written to get the required context. You can write this script
inside the body tag as shown below.
<!DOCTYPE HTML>
<html>
<body>
<canvas id = "mycanvas" width = "600" height =
"200"></canvas>
<script>
var canvas = document.getElementById('mycanvas');
var context = canvas.getContext('2d');
<!DOCTYPE html>
<html>
<canvas id = 'my_canvas'></canvas>
<script>
var canvas = document.getElementById('my_canvas');
var gl = canvas.getContext('experimental-webgl');
gl.clearColor(0.9,0.9,0.8,1);
gl.clear(gl.COLOR_BUFFER_BIT);
</script>
</html>
References