Unit 4 Animation
Unit 4 Animation
2MARK
1. What is the use of canvas element? Mention its attributes.
The canvas element is used to render simple graphics such as line art, graphs,
and other custom graphical elements on the client side.
Attributes
4.List the parameters of the canvas fillRect() method with their purpose.
fillRect(x, y, width, height)
x:The x-axis coordinate of the rectangle's starting point.
Y:The y-axis coordinate of the rectangle's starting point.
Width:The rectangle's width. Positive values are to the right, and negative to
the left.
Height:The rectangle's height. Positive values are down, and negative are up.
The gradient object can be used to fill rectangles, circles, lines, text, etc.
The gradient object can be used to fill rectangles, circles, lines, text, etc.
<html>
<body>
<h1>HTML5 Canvas</h1>
<canvas id="myCanvas" width="300" height="150" "></canvas>
<script>
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
const grd = ctx.createRadialGradient(75, 50, 5, 90, 60, 100);
grd.addColorStop(0, "red");
grd.addColorStop(1, "white");
// Draw filled Rectangle
ctx.fillStyle = grd;
ctx.fillRect(10, 10, 150, 100);
</script>
</body>
</html> Output:
4.Explain arc() and arcTo() methods of canvas
The arc() method adds an arc (curve) to the path.The arc() method creates a
circle or a part of a circle.Use the stroke() or fill() method to draw the path.
arc(x,y,radius,startAngle,endAngle,counterclockwise)
ctx.beginPath();
ctx.stroke() output:
Arcto:-
The arcTo() method adds an arc/curve between two tangents to the path.
Eg:-
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(100, 20);
ctx.arcTo(150, 20, 150, 70, 50);
ctx.lineTo(150, 120);
ctx.stroke(); output:-
5. Explain quadraticCurveTo() and bezierCurveTo() methods of canvas with
proper code example
The quadraticCurveTo() method adds a curve to the current path by using the
control points that represent a quadratic Bézier curve.
Eg:
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.quadraticCurveTo(20, 100, 200, 20);
ctx.stroke(); output:-
bezierCurveTo:- The bezierCurveTo() method adds a curve to the path by using
the control points that represent a cubic Bézier curve.
Eg:-
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.bezierCurveTo(20, 100, 200, 100, 200, 20);
ctx.stroke(); output:-
6. Explain scale(), rotate(), translate() setTransform() methods of canvas using
code examples.
`scale()` Method:-
The `scale()` method is used to scale the current drawing on the canvas. It takes
two parameters, `scaleX` and `scaleY`, which determine the scaling factors for
the horizontal and vertical directions, respectively.
syntax:- context.scale(x,y);
eg:
ctx.strokeRect(5, 5, 25, 15);
ctx.scale(2, 2);
The lineCap property sets or returns the style of the end caps for a line.
Eg=
//butt
ctx.beginPath();
ctx.lineWidth = 10;
ctx.lineCap = "butt";
ctx.moveTo(20, 20);
ctx.lineTo(200, 20);
ctx.stroke();
//round
ctx.beginPath();
ctx.lineCap = "round";
ctx.moveTo(20, 40);
ctx.lineTo(200, 40);
ctx.stroke();
//square
ctx.beginPath();
ctx.lineCap = "square";
ctx.moveTo(20, 60);
ctx.lineTo(200, 60);
ctx.stroke(); output:-
Linejoin:- The lineJoin property sets or returns the type of corner created,
when two lines meet.
Eg:-
ctx.beginPath();
ctx.lineWidth = 10;
ctx.lineJoin = "round"; //miter //bevel
ctx.moveTo(20, 20);
ctx.lineTo(100, 50);
ctx.lineTo(20, 100);
ctx.stroke();
//round watch the corner it round
output:-