SlideShare a Scribd company logo
3
Most read
4
Most read
7
Most read
A. A. Datti 2018
HTML5 Canvas
Introduction
Drawing
Coordinate System
Gradients
Text
Images
2
The HTML <canvas> element is used to draw graphics, on
the fly, via scripting (usually JavaScript).
The <canvas> element is only a container for graphics.
You must use a script to actually draw the graphics.
Canvas has several methods for drawing paths, boxes,
circles, text, and adding images.
 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. Everything is possible: from simple bouncing
balls to complex animations.
 Canvas can respond to JavaScript events.
 Canvas can respond to any user action (key clicks, mouse clicks, button
clicks, finger movement).
 Canvas' methods for animations, offer a lot of possibilities for HTML
gaming applications.
 In HTML, a <canvas> element looks like this:
<canvas id="myCanvas" width="200" height="100"></canvas>
 The <canvas> element must have an id attribute so it can be referred to by JavaScript.
 The width and height attribute is necessary to define the size of the canvas.
 Tip: You can have multiple <canvas> elements on one HTML page.
 By default, the <canvas> element has no border and no content.
 To add a border, use a style attribute:
<canvas id="myCanvas" width="200" height="100“ style="border:1px solid#000000;"></canvas>
 All drawing on the HTML canvas must be done with JavaScript:
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "#FF0000";
ctx.fillRect(0,0,150,75);
</script>
 Step 1: Find the Canvas Element
 First of all, you must find the <canvas> element.
 This is done by using the HTML DOM method getElementById():
var canvas = document.getElementById("myCanvas");
 Step 2: Create a Drawing Object
 Secondly, you need a drawing object for the canvas.
 The getContext() is a built-in HTML object, with properties and methods for drawing:
var ctx = canvas.getContext("2d");
 Step 3: Draw on the Canvas
 Finally, you can draw on the canvas.
 Set the fill style of the drawing object to the color red:
ctx.fillStyle = "#FF0000";
 The fillStyle property can be a CSS color, a gradient, or a pattern. The default fillStyle is
black.
 The fillRect(x,y,width,height) method draws a rectangle, filled with the fill style, on the
canvas:
ctx.fillRect(0,0,150,75);
 The HTML canvas is a two-dimensional grid.
 The upper-left corner of the canvas has the coordinates (0,0)
 In the previous chapter, you saw this method used: fillRect(0,0,150,75).
 This means: Start at the upper-left corner (0,0) and draw a 150x75 pixels
rectangle.
 Draw a Line
 To draw a straight line on a canvas, use the following methods:
 moveTo(x,y) - defines the starting point of the line
 lineTo(x,y) - defines the ending point of the line
 To actually draw the line, you must use one of the "ink" methods, like stroke().
 Define a starting point in position (0,0), and an ending point in position (200,100). Then
use the stroke() method to actually draw the line:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.moveTo(0,0);
ctx.lineTo(200,100);
ctx.stroke();
 Draw a Circle
 To draw a circle on a canvas, use the following methods:
 beginPath() - begins a path
 arc(x,y,r,startangle,endangle) - creates an arc/curve. To create a circle with arc(): Set
start angle to 0 and end angle to 2*Math.PI. The x and y parameters define the x- and
y-coordinates of the center of the circle. The r parameter defines the radius of the
circle.
 Define a circle with the arc() method. Then use the stroke() method to actually draw
the circle:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();
 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:
 createLinearGradient(x,y,x1,y1) - creates a linear gradient
 createRadialGradient(x,y,r,x1,y1,r1) - creates a radial/circular gradient
 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. Gradient positions can be anywhere 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).
 Using createLinearGradient()
 Create a linear gradient. Fill rectangle with the gradient:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
// Create gradient
var grd=ctx.createLinearGradient(0,0,200,0);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
// Fill with gradient
ctx.fillStyle=grd;
ctx.fillRect(10,10,150,80);
 Using createRadialGradient():
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
// Create gradient
var grd=ctx.createRadialGradient(75,50,5,90,60,100);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
// Fill with gradient
ctx.fillStyle = grd;
ctx.fillRect(10,10,150,80);
 To draw text on a canvas, the most important property and methods are:
 font - defines the font properties for the text
 fillText(text,x,y) - draws "filled" text on the canvas
 strokeText(text,x,y) - draws text on the canvas (no fill)
 Using fillText()
 Set font to 30px "Arial" and write a filled text on the canvas:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.font = "30px Arial";
ctx.fillText("Hello World",10,50);
 Add Color and Center Text
 Set font to 30px "Comic Sans MS" and write a filled red text in the center of the
canvas:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.font = "30px Comic Sans MS";
ctx.fillStyle = "red";
ctx.textAlign = "center";
ctx.fillText("Hello World", canvas.width/2, canvas.height/2);

More Related Content

PDF
Javascript
PDF
JavaScript - Chapter 12 - Document Object Model
PPT
Php forms
PPTX
Css animation
PPT
JavaScript Tutorial
PDF
Introduction to CSS3
Javascript
JavaScript - Chapter 12 - Document Object Model
Php forms
Css animation
JavaScript Tutorial
Introduction to CSS3

What's hot (20)

PDF
CSS Day: CSS Grid Layout
PPT
PHP - Introduction to PHP Forms
PDF
Javascript essentials
PDF
Introduction to XHTML
PDF
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
PPTX
C# classes objects
PPT
Javascript
PPT
PDF
JavaScript: Variables and Functions
PPT
Introduction to BOOTSTRAP
PDF
JavaScript - Chapter 14 - Form Handling
PPTX
Html & CSS
PDF
PPTX
Operators and expression in c#
PPTX
An Overview of HTML, CSS & Java Script
PPT
Form validation client side
PPT
Jdbc ppt
PPTX
1 03 - CSS Introduction
PPT
Css Ppt
CSS Day: CSS Grid Layout
PHP - Introduction to PHP Forms
Javascript essentials
Introduction to XHTML
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
C# classes objects
Javascript
JavaScript: Variables and Functions
Introduction to BOOTSTRAP
JavaScript - Chapter 14 - Form Handling
Html & CSS
Operators and expression in c#
An Overview of HTML, CSS & Java Script
Form validation client side
Jdbc ppt
1 03 - CSS Introduction
Css Ppt
Ad

Similar to HTML5 Canvas - Basics.pptx (20)

PPTX
canvas.pptx
PPTX
canvas_1.pptx
PPTX
Html5 canvas
PPTX
introduction of HTML canvas and styles .pptx
PPTX
Drawing with the HTML5 Canvas
PPTX
HTML CANVAS
PPTX
HTML 5 Canvas & SVG
PPT
Scmad Chapter06
PPTX
Css5 canvas
PPTX
Javascript Canvas API
PDF
Intro to HTML5 Canvas
PPTX
Advanced html5 diving into the canvas tag
KEY
Exploring Canvas
PPT
Html5 Canvas Drawing and Animation
PPTX
Html canvas
DOCX
On the tomcat drive in folder cosc210 you will find file named Paint.docx
PPTX
Chapter 02 sprite and texture
PDF
HTML5 Canvas - The Future of Graphics on the Web
PPT
Chapter 13
PDF
Matlab graphics
canvas.pptx
canvas_1.pptx
Html5 canvas
introduction of HTML canvas and styles .pptx
Drawing with the HTML5 Canvas
HTML CANVAS
HTML 5 Canvas & SVG
Scmad Chapter06
Css5 canvas
Javascript Canvas API
Intro to HTML5 Canvas
Advanced html5 diving into the canvas tag
Exploring Canvas
Html5 Canvas Drawing and Animation
Html canvas
On the tomcat drive in folder cosc210 you will find file named Paint.docx
Chapter 02 sprite and texture
HTML5 Canvas - The Future of Graphics on the Web
Chapter 13
Matlab graphics
Ad

More from AhmadAbba6 (6)

PPTX
Transmission Medium.pptx
PPTX
Network Models.pptx
PPTX
Basic Concepts of Networking.pptx
PPTX
Introduction to Computer Graphics.pptx
PPTX
Rasterization.pptx
PPTX
HTML5 Canvas (Wall Clock).pptx
Transmission Medium.pptx
Network Models.pptx
Basic Concepts of Networking.pptx
Introduction to Computer Graphics.pptx
Rasterization.pptx
HTML5 Canvas (Wall Clock).pptx

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Encapsulation theory and applications.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
DOCX
The AUB Centre for AI in Media Proposal.docx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Machine learning based COVID-19 study performance prediction
Per capita expenditure prediction using model stacking based on satellite ima...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation theory and applications.pdf
cuic standard and advanced reporting.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Network Security Unit 5.pdf for BCA BBA.
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Unlocking AI with Model Context Protocol (MCP)
The AUB Centre for AI in Media Proposal.docx
“AI and Expert System Decision Support & Business Intelligence Systems”
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Mobile App Security Testing_ A Comprehensive Guide.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Building Integrated photovoltaic BIPV_UPV.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm

HTML5 Canvas - Basics.pptx

  • 1. A. A. Datti 2018
  • 3. The HTML <canvas> element is used to draw graphics, on the fly, via scripting (usually JavaScript). The <canvas> element is only a container for graphics. You must use a script to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.
  • 4.  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. Everything is possible: from simple bouncing balls to complex animations.  Canvas can respond to JavaScript events.  Canvas can respond to any user action (key clicks, mouse clicks, button clicks, finger movement).  Canvas' methods for animations, offer a lot of possibilities for HTML gaming applications.
  • 5.  In HTML, a <canvas> element looks like this: <canvas id="myCanvas" width="200" height="100"></canvas>  The <canvas> element must have an id attribute so it can be referred to by JavaScript.  The width and height attribute is necessary to define the size of the canvas.  Tip: You can have multiple <canvas> elements on one HTML page.  By default, the <canvas> element has no border and no content.  To add a border, use a style attribute: <canvas id="myCanvas" width="200" height="100“ style="border:1px solid#000000;"></canvas>
  • 6.  All drawing on the HTML canvas must be done with JavaScript: <script> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); ctx.fillStyle = "#FF0000"; ctx.fillRect(0,0,150,75); </script>  Step 1: Find the Canvas Element  First of all, you must find the <canvas> element.  This is done by using the HTML DOM method getElementById(): var canvas = document.getElementById("myCanvas");
  • 7.  Step 2: Create a Drawing Object  Secondly, you need a drawing object for the canvas.  The getContext() is a built-in HTML object, with properties and methods for drawing: var ctx = canvas.getContext("2d");  Step 3: Draw on the Canvas  Finally, you can draw on the canvas.  Set the fill style of the drawing object to the color red: ctx.fillStyle = "#FF0000";  The fillStyle property can be a CSS color, a gradient, or a pattern. The default fillStyle is black.  The fillRect(x,y,width,height) method draws a rectangle, filled with the fill style, on the canvas: ctx.fillRect(0,0,150,75);
  • 8.  The HTML canvas is a two-dimensional grid.  The upper-left corner of the canvas has the coordinates (0,0)  In the previous chapter, you saw this method used: fillRect(0,0,150,75).  This means: Start at the upper-left corner (0,0) and draw a 150x75 pixels rectangle.
  • 9.  Draw a Line  To draw a straight line on a canvas, use the following methods:  moveTo(x,y) - defines the starting point of the line  lineTo(x,y) - defines the ending point of the line  To actually draw the line, you must use one of the "ink" methods, like stroke().  Define a starting point in position (0,0), and an ending point in position (200,100). Then use the stroke() method to actually draw the line: var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); ctx.moveTo(0,0); ctx.lineTo(200,100); ctx.stroke();
  • 10.  Draw a Circle  To draw a circle on a canvas, use the following methods:  beginPath() - begins a path  arc(x,y,r,startangle,endangle) - creates an arc/curve. To create a circle with arc(): Set start angle to 0 and end angle to 2*Math.PI. The x and y parameters define the x- and y-coordinates of the center of the circle. The r parameter defines the radius of the circle.  Define a circle with the arc() method. Then use the stroke() method to actually draw the circle: var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); ctx.beginPath(); ctx.arc(95,50,40,0,2*Math.PI); ctx.stroke();
  • 11.  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:  createLinearGradient(x,y,x1,y1) - creates a linear gradient  createRadialGradient(x,y,r,x1,y1,r1) - creates a radial/circular gradient  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. Gradient positions can be anywhere 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).
  • 12.  Using createLinearGradient()  Create a linear gradient. Fill rectangle with the gradient: var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); // Create gradient var grd=ctx.createLinearGradient(0,0,200,0); grd.addColorStop(0,"red"); grd.addColorStop(1,"white"); // Fill with gradient ctx.fillStyle=grd; ctx.fillRect(10,10,150,80);
  • 13.  Using createRadialGradient(): var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); // Create gradient var grd=ctx.createRadialGradient(75,50,5,90,60,100); grd.addColorStop(0,"red"); grd.addColorStop(1,"white"); // Fill with gradient ctx.fillStyle = grd; ctx.fillRect(10,10,150,80);
  • 14.  To draw text on a canvas, the most important property and methods are:  font - defines the font properties for the text  fillText(text,x,y) - draws "filled" text on the canvas  strokeText(text,x,y) - draws text on the canvas (no fill)  Using fillText()  Set font to 30px "Arial" and write a filled text on the canvas: var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); ctx.font = "30px Arial"; ctx.fillText("Hello World",10,50);
  • 15.  Add Color and Center Text  Set font to 30px "Comic Sans MS" and write a filled red text in the center of the canvas: var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); ctx.font = "30px Comic Sans MS"; ctx.fillStyle = "red"; ctx.textAlign = "center"; ctx.fillText("Hello World", canvas.width/2, canvas.height/2);