Unit 3 Multimedia Animation
Unit 3 Multimedia Animation
HTMLS - CANVAS:
There are two types of rendering contexts in Canvas: 2D and WebGL. The 2D
context is the default context and provides a powerful and flexible API for creating
2D graphics and animations. The WebGL context, on the other hand, allows for the
creation of 3D graphics using the WebGL API.
TS
Browser Support
Here are a few examples of what can be done with HTML5 Canvas:
1. Drawing shapes and lines: The HTML5 Canvas element can be used to draw
various shapes and lines on a web page. Here's an example of a red rectangle:
<script>
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.fillStyle = "red";
context.fillRect(10, 10, 50, 50);
Tejaswi S, Asst. Prof. Dept. of Computer Science, Seshadripuram Degree College,Mysuru
</script>
2. Creating animations: The HTML5 Canvas element can also be used to create
animations on a web page. Here's an example of a bouncing ball animation:
<script>
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var x = canvas.width / 2;
var y = canvas.height / 2;
var dx = 2;
var dy = -2;
var ballRadius = 10;
function drawBall() {
TS
context.beginPath();
context.arc(x, y, ballRadius, 0, Math.PI * 2);
context.fillStyle = "#0095DD";
context.fill();
context.closePath();
}
function draw() {
context.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
x += dx;
y += dy;
}
setInterval(draw, 10);
</script>
<script>
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.putImageData(imageData, 0, 0);
};
</script>
These are just a few examples of what can be done with HTML5 Canvas. The
possibilities are endless!
Canvas is a powerful HTML element that allows you to draw graphics and create
animations using JavaScript. Here are some basic canvas drawing techniques:
```
// create a canvas element
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
Tejaswi S, Asst. Prof. Dept. of Computer Science, Seshadripuram Degree College,Mysuru
// draw a filled rectangle
ctx.fillRect(10, 10, 100, 50);
2. Drawing Paths: You can draw complex shapes by defining a path on the canvas.
To create a path, use the `beginPath()` method, and then use `moveTo()`, `lineTo()`,
and `arc()` methods to draw the path. For example:
```
// create a canvas element
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
// create a path
ctx.beginPath();
TS
ctx.moveTo(10, 10);
ctx.lineTo(50, 50);
ctx.lineTo(100, 10);
ctx.closePath();
3. Drawing Lines: To draw a straight line on the canvas, use the `moveTo()` and
`lineTo()` methods. For example:
```
// create a canvas element
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
// draw a line
ctx.beginPath();
ctx.moveTo(10, 10);
Tejaswi S, Asst. Prof. Dept. of Computer Science, Seshadripuram Degree College,Mysuru
ctx.lineTo(100, 100);
ctx.strokeStyle = 'black';
ctx.stroke();
```
4. Drawing Bezier Curves: Bezier curves are used to create smooth curves on the
canvas. To draw a Bezier curve, use the `bezierCurveTo()` method. For example:
```
// create a canvas element
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
5. Drawing Quadratic Curves: Quadratic curves are simpler than Bezier curves and
can be used to create smooth curves on the canvas. To draw a quadratic curve, use
the `quadraticCurveTo()` method. For example:
```
// create a canvas element
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
6. Using Images: You can use images on the canvas by creating an `Image` object
and then using the `drawImage()` method. For example:
```
// create a canvas element
var canvas = document.getElementById('myCanvas');
Tejaswi S, Asst. Prof. Dept. of Computer Science, Seshadripuram Degree College,Mysuru
var ctx = canvas.getContext('2d');
In HTML5, styles and colors are used to change the appearance of elements on a
webpage. You can use CSS to define styles for your HTML elements, including font
sizes, font colors, background colors, and more.
Tejaswi S, Asst. Prof. Dept. of Computer Science, Seshadripuram Degree College,Mysuru
Canvas - Text and Fonts:
In HTML5 Canvas, you can use the `fillText()` or `strokeText()` methods to draw text
on the canvas. You can also specify the font size, font family, and font style using the
`font` property.
HTML5 Canvas also supports patterns and shadows. You can create a pattern using
an image or another canvas element, and then use that pattern to fill a shape or
draw a stroke. You can also add shadows to shapes using the `shadowBlur`,
`shadowColor`, and `shadowOffset` properties.
TS
Canvas - Save and Restore States:
When working with the HTML5 Canvas, you can save the current state of the canvas
using the `save()` method. This includes things like the current transformation matrix,
fill color, stroke color, and more. You can later restore this state using the `restore()`
method.
Canvas - Translation:
Canvas - Rotation:
Rotation is the process of rotating the canvas around a specific point. In HTML5
Canvas, you can use the `rotate()` method to rotate the canvas around the origin, or
you can use the `translate()` method to move the canvas to a new location and then
rotate it.
Canvas - Scaling:
Scaling is the process of changing the size of the canvas. In HTML5 Canvas, you
can use the `scale()` method to scale the canvas along the x-axis and y-axis.
Tejaswi S, Asst. Prof. Dept. of Computer Science, Seshadripuram Degree College,Mysuru
Canvas - Transforms:
Canvas composition allows you to combine multiple shapes or images into a single
image. In HTML5 Canvas, you can use the `globalCompositeOperation` property to
specify how the shapes or images should be combined.
Canvas - Animations: