Professional Documents
Culture Documents
CMA unit 4 Canvas( complete)
CMA unit 4 Canvas( complete)
HTML5 CANVAS
What is HTML Canvas?
• Canvas provides HTML a bitmapped surface to work
with. It is used to draw graphics on the web page
using javascript.
• The HTML <canvas> element is used to draw graphics
using JavaScript.
• The <canvas> element is only a container for graphics.
We must use JavaScript to actually draw the graphics.
• Canvas has several methods for drawing paths, boxes,
circles, text, and adding images.
• The Canvas API provides a means for drawing graphics
via JavaScript and the HTML <canvas> element.
Uses of Canvas
• Canvas can draw colorful text, with or without animation.
• Canvas has great features for graphical data presentation
with an imagery of graphs and charts.
• Canvas objects can move -> from simple bouncing balls
to complex animations.
• Canvas can respond to JavaScript events and user action
like (key clicks, mouse clicks, button clicks, finger
movement).
• Canvas methods offer a lot of possibilities for HTML
gaming applications.
How to create a HTML canvas?
fillRect(x,y,width,height)
1 This method draws a filled rectangle at x, y position of specified
width and height
strokeRect(x,y,width,height)
2 This method draws a rectangular outline.
ctx.clearRect(75,40,60,60);
ctx.strokeWidth="3pt";
ctx.strokeRect(90,55,30,30);
</script>
</body>
</html>
Canvas - Drawing Paths
Cubic
Beizer
Curve
Cubic
Beizer
Curve
Drawing Cubic Bezier
• Example:
moveTo(30,180) // sx, sy
bezierCurveTo(30,40,90,50,200,190); //ctx1, cty1, ctx2, cty2 , ex, ey
Refer program canvas bezier cubic.html
Drawing Quadratic Bezier
• Quadratic Bezier is drawn by specifying the x and y
coordinates for the starting point(sx, sy), ending point(ex, ey)
and one control points(ctx, cty).
• The relationship of the control point to the starting and
ending points establishes the curvature of the line.
• The quadraticCurveTo( ) function draws a quadratic Bezier
curve from one point to another.
• Syntax: quadraticCurveTo(ctx, cty, ex, ey);
• Example:
moveTo(30,180) // sx, sy
quadraticCurveTo(90,50,200,190); //ctx, cty, ex, ey
Refer program canvas bezier quadratic.html
Practice program
Refer prog: canvas bezier heart.html
Canvas Drawing Images
• We can import an external image(png, jpg, gif
etc) into a canvas and then we can also draw on
that image using other canvas drawing methods.
• Importing images into a canvas is a two-step
process:
– Get an external image source ( using html <img> tag
or by using Image() constructor )
– Draw an image on the canvas with the drawImage()
function.
• The drawImage() function takes one of the
forms or syntax:
drawImage(image, x, y),
drawImage(image, x, y, width, height),
• Example:
ctx.drawImage(img,5,5);
ctx.drawImage(img,0,0,200,200);
// get the canvas element using the DOM
var canvas = document.getElementById('mycanvas');
}
Refer program: canvas images.html
Practice program:
refer: canvas images graph.html
Canvas Create Gradients
• Gradients can be used to fill rectangles, circles, lines, text, etc.
Shapes on the canvas are not limited to solid colors.
• There are two different types of gradients:
– linear gradient (createLinearGradient())
– radial/circular gradient (createRadialGradient())
• Once we have a gradient object, we must add two or more
color stops.
• The addColorStop() method specifies the color stops, and its
position along the gradient. color stops takes values between
0 to 1.
• To use the gradient, set the fillStyle or strokeStyle property to
the gradient, then draw the shape (rectangle, text, or a line).
Method Description example
createLinearGradient(x1, Creates a linear gradient const grad=
y1, x2, y2) that paints along the line ctx.createLinearGradient(2
given by the coordinates 0, 0, 220, 0);
starting point (x1,y1) and
end point (x2,y2)