Javascript / HTML Programming
Javascript / HTML Programming
On the following pages you’ll find the source files of some some simple Internet pages:
<!DOCTYPE html>
<html>
<head>
<title>ButtonDemo.html by Kari Laitinen</title>
<!--
This is an HTML comment.
-->
<style>
.centered
{
text-align: center;
margin: 64px auto; /* top and bottom margins are set,
right and left margins are automatic */
}
</style>
<script>
function change_text()
{
var text_element = document.getElementById( "text_on_screen" ) ;
</head>
<body>
<div class=centered>
<button onclick="change_text()">Press</button>
</div>
</body>
</html>
• Usually tags are used in pairs so that each start tag (opening tag) has
a corresponding end tag (closing tag). For example, <p> is a start tag
and </p> is an end tag.
• Tags are used to form HTML elements. The start tag is often used to
name the element. For example, an <html> element is everything
that is is between the tags <html> and </html> including the tags.
• HTML elements can contain other elements that are called nested
elements. Usually, the nested elements of an <html> element are a
<head> element and a <body> element.
<!DOCTYPE html>
<html>
<head>
<title>PictureViewing.html by Kari Laitinen</title>
<style>
.centered
{
text-align: center;
margin: 16px auto; /* top and bottom margins are set,
right and left margins are automatic */
}
</style>
<script>
var picture_index = 0 ;
function show_next_picture()
{
picture_index ++ ;
<body>
<div class=centered>
<button onclick="show_next_picture()">Next</button>
</div>
<div class=centered>
<img id=image_element_id
src="images/yellow_field_by_vincent_van_gogh.jpg" width=800>
</div>
</body>
</html>
PictureViewing.html - 1. Complete source code of the page.
PictureViewing.html – <img> and <button> elements in a page 703
• The array and the variable that are defined outside the function are
global data that keep their values between calls to the function.
Below you’ll find out how to use some The first two parameters for the fill-
of the drawing methods that are available to Rect() method specify the upper left corner
draw or fill various graphical shapes in the of the rectangle in the graphical coordinate
’drawing context’. system. The third parameter specifies the
width and the fourth parameter specifies the
height of the rectangle.
In this case the specified rectangle occu-
pies the entire canvas.
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>DrawingDemoCanvas.html © Kari Laitinen</title>
<script type="text/javascript">
function draw_on_canvas()
{
var canvas = document.getElementById( "canvas_for_drawings" ) ;
var context = canvas.getContext( "2d" ) ;
// We'll fill the entire canvas with light color, which overdraws
// the previous drawings.
context.fillStyle = "LightYellow" ;
context.lineWidth = 3 ;
context.strokeStyle = "Blue" ; // Color for the first line.
context.fillStyle = "Black" ;
context.fillText( "Canvas size is " + canvas.width
+ " x " + canvas.height, 20, 20 ) ;
context.fillStyle = "Cyan" ;
context.fillRect( 64, 192, 128, 128 ) ; // Filled square with size 128x128
context.fillStyle = "LightGray" ;
context.strokeStyle = "Black" ;
context.beginPath() ;
context.moveTo( 704, 256 ) ;
context.arc( 704, 256,
64, 0.25 * Math.PI, 1.75 * Math.PI, false ) ;
context.lineTo( 704, 256 ) ;
context.fill() ; // Fill the defined path with filling color.
context.stroke() ; // Draw the outline of the path.
context.beginPath() ;
context.moveTo( 768, 256 ) ;
context.arc( 768, 256,
64, 0.25 * Math.PI, 1.75 * Math.PI, true ) ;
context.lineTo( 768, 256 ) ;
context.fill() ;
context.stroke() ;
<style type="text/css">
#centered
{
width: 896px; /* 1024 - 128 = 896 */
height: 512px;
margin: 30px auto; /* top and bottom margins are 30p;
right and left margins are automatic */
border: 1px solid black;
}
<body onload="draw_on_canvas()">
<div id=centered>
<canvas id=canvas_for_drawings width=896 height=512>
</canvas>
</div>
</body>
</html>
class Ball
{
constructor( given_center_point_x,
given_center_point_y,
given_color )
{
this.ball_center_point_x = given_center_point_x ;
this.ball_center_point_y = given_center_point_y ;
this.ball_color = given_color ;
this.ball_diameter = 128 ;
With the this
keyword it is possible
this.ball_is_activated = false ; to create data mem-
} bers, or to refer to
data members inside
activate_ball() objects.
{
this.ball_is_activated = true ;
}
deactivate_ball()
{
this.ball_is_activated = false ;
}
get_ball_center_point_x()
{
return this.ball_center_point_x ;
}
get_ball_center_point_y()
{
return this.ball_center_point_y ;
}
get_ball_color()
{
return this.ball_color ;
}
set_ball_color( new_color )
{
this.ball_color = new_color ;
}
set_diameter( new_diameter )
{
if ( new_diameter > 3 )
{
this.ball_diameter = new_diameter ;
}
}
Ball.js - 1: The constructor and some ’getter’ and ’setter’ methods for Ball objects.
708 Luku 18: JavaScript / HTML Programming
When methods are written inside JavaScript classes, no keywords are needed.
You just write the method name, and its formal parameters inside parentheses.
move_right()
{
this.ball_center_point_x += 3 ;
}
move_left()
{
this.ball_center_point_x -= 3 ;
}
move_up()
{
this.ball_center_point_y -= 3 ;
}
move_down()
{
this.ball_center_point_y += 3 ;
}
Objects that represent graphical shapes on the screen should be such that they
’know’ their place on the screen, they know they size, color, and other features, and
they can draw themselves. As Ball objects contain the necessary data members,
they can be asked to draw themselves by calling the draw() method.
With the contains_point() method a Ball object can be ’asked’ whether or
not a certain point belongs to the ball area.
var distance_from_given_point_to_ball_center =
Math.sqrt(
draw( context )
{
// We'll first save the current drawing context so that we'll
// not disturb any drawing operations made by other methods.
context.save() ;
context.fillStyle = this.ball_color ;
context.beginPath() ;
context.arc( this.ball_center_point_x, this.ball_center_point_y,
this.ball_diameter / 2, 0, 2 * Math.PI, true )
context.closePath() ;
context.fill() ;
if ( this.ball_is_activated == true )
{
context.lineWidth = 6 ; // Thick edge for an activated ball.
}
The constructor of class Ball is executed when these statements are processed
by the browser. The supplied parameters become data members inside the Ball
objects, and each object will have its own set of data members, and the objects can
be manipulated with the methods that are written inside Ball.js.
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>MovingBallsWithPointerCanvas.html © Kari Laitinen</title>
<script src="Ball.js"></script>
<script>
var previous_pointer_position_x = 0 ;
var previous_pointer_position_y = 0 ;
// Now we know which point was pointed. We will 'ask' all the
// Ball objects whether or not the pressed point is inside
// the area of the ball in question.
if ( first_ball.contains_point( pointer_position_x,
pointer_position_y ) )
{
ball_being_moved = first_ball ;
ball_being_moved.activate_ball() ;
}
else if ( second_ball.contains_point( pointer_position_x,
pointer_position_y ) )
{
ball_being_moved = second_ball ;
ball_being_moved.activate_ball() ;
}
else if ( third_ball.contains_point( pointer_position_x,
pointer_position_y ) )
{
ball_being_moved = third_ball ;
ball_being_moved.activate_ball() ;
}
previous_pointer_position_x = pointer_position_x ;
previous_pointer_position_y = pointer_position_y ;
draw_on_canvas() ;
}
The value of
ball_being_moved
will remain null if
none of the balls con-
tained the pointed posi-
tion.
on_mouse_move()
will be called many
times when mouse is
moved. Here we calcu-
late how much the Ball
object was moved since
the previous call to this
method. The method
move_this_ball() is
called to actually change
the position of the ball
on the screen.
function on_mouse_move( event )
{
if ( ball_being_moved != null )
{
var new_pointer_position_x = event.offsetX ;
var new_pointer_position_y = event.offsetY ;
previous_pointer_position_x = new_pointer_position_x ;
previous_pointer_position_y = new_pointer_position_y ;
ball_being_moved.move_this_ball( pointer_movement_x,
pointer_movement_y ) ;
draw_on_canvas() ;
}
}
draw_on_canvas() ;
}
}
function draw_on_canvas()
{
var canvas = document.getElementById( "canvas_for_balls" ) ;
var context = canvas.getContext("2d") ;
context.fillStyle = "LightYellow" ;
context.fillRect( 0, 0, canvas.width, canvas.height ) ;
first_ball.draw( context ) ;
second_ball.draw( context ) ;
third_ball.draw( context ) ;
}
</script>
<style type="text/css">
#centered
{
width: 800px;
height: 600px;
margin: 30px auto; /* top and bottom margins are 30p;
right and left margins are automatic */
border: 1px solid black;
}
</style>
</head>
<body onload="draw_on_canvas()">
<div id=centered>
<canvas id=canvas_for_balls
width=800 height=600
onmousedown = "on_mouse_down( event )"
onmousemove = "on_mouse_move( event )"
onmouseup = "on_mouse_up( event )">
</canvas>
</div>
</body>
</html>
Here we specify which JavaScript methods
will be called automatically when mouse-
related events take place inside the <canvas>
element.