SlideShare a Scribd company logo
4
Most read
11
Most read
15
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

What's hot (20)

PDF
HTML CSS Basics
Mai Moustafa
 
KEY
Web Design 101
T.S. Lim
 
PPT
CSS Basics
WordPress Memphis
 
PPTX
Introduction to JavaScript Basics.
Hassan Ahmed Baig - Web Developer
 
PPTX
Links in Html
sadeenedian08
 
PPSX
Introduction to css
Evolution Network
 
PDF
Introduction to HTML5
Gil Fink
 
PPTX
Html5 tutorial for beginners
Singsys Pte Ltd
 
PPT
Javascript arrays
Hassan Dar
 
PPTX
css.ppt
bhasula
 
PPTX
Html n CSS
Sukrit Gupta
 
PPTX
Static and Dynamic webpage
Aishwarya Pallai
 
PPTX
HTML: Tables and Forms
BG Java EE Course
 
PPTX
Java script
Abhishek Kesharwani
 
PPTX
Cascading Style Sheet (CSS)
AakankshaR
 
PPTX
Html Basic Tags
Richa Singh
 
PPT
Css Ppt
Hema Prasanth
 
HTML CSS Basics
Mai Moustafa
 
Web Design 101
T.S. Lim
 
CSS Basics
WordPress Memphis
 
Introduction to JavaScript Basics.
Hassan Ahmed Baig - Web Developer
 
Links in Html
sadeenedian08
 
Introduction to css
Evolution Network
 
Introduction to HTML5
Gil Fink
 
Html5 tutorial for beginners
Singsys Pte Ltd
 
Javascript arrays
Hassan Dar
 
css.ppt
bhasula
 
Html n CSS
Sukrit Gupta
 
Static and Dynamic webpage
Aishwarya Pallai
 
HTML: Tables and Forms
BG Java EE Course
 
Java script
Abhishek Kesharwani
 
Cascading Style Sheet (CSS)
AakankshaR
 
Html Basic Tags
Richa Singh
 
Css Ppt
Hema Prasanth
 

Similar to HTML5 Canvas - Basics.pptx (20)

PPTX
canvas.pptx
VeenaNaik23
 
PPTX
canvas_1.pptx
RutujRunwal1
 
PPTX
Html5 canvas
Nisa Soomro
 
PPTX
introduction of HTML canvas and styles .pptx
hannahroseline2
 
PPTX
Drawing with the HTML5 Canvas
Henry Osborne
 
PPTX
HTML CANVAS
Light Salt
 
PPTX
HTML 5 Canvas & SVG
Ofir's Fridman
 
PPT
Scmad Chapter06
Marcel Caraciolo
 
PPTX
Css5 canvas
Vadim Spiridenko
 
PPTX
Javascript Canvas API
Samuel Santos
 
PDF
Intro to HTML5 Canvas
Juho Vepsäläinen
 
PPTX
Advanced html5 diving into the canvas tag
David Voyles
 
KEY
Exploring Canvas
Kevin Hoyt
 
PPT
Html5 Canvas Drawing and Animation
Mindfire Solutions
 
PPTX
Html canvas
Rao Mubashar
 
DOCX
On the tomcat drive in folder cosc210 you will find file named Paint.docx
dunhamadell
 
PPTX
Chapter 02 sprite and texture
boybuon205
 
PDF
HTML5 Canvas - The Future of Graphics on the Web
Robin Hawkes
 
PPT
Chapter 13
Terry Yoast
 
PDF
Matlab graphics
pramodkumar1804
 
canvas.pptx
VeenaNaik23
 
canvas_1.pptx
RutujRunwal1
 
Html5 canvas
Nisa Soomro
 
introduction of HTML canvas and styles .pptx
hannahroseline2
 
Drawing with the HTML5 Canvas
Henry Osborne
 
HTML CANVAS
Light Salt
 
HTML 5 Canvas & SVG
Ofir's Fridman
 
Scmad Chapter06
Marcel Caraciolo
 
Css5 canvas
Vadim Spiridenko
 
Javascript Canvas API
Samuel Santos
 
Intro to HTML5 Canvas
Juho Vepsäläinen
 
Advanced html5 diving into the canvas tag
David Voyles
 
Exploring Canvas
Kevin Hoyt
 
Html5 Canvas Drawing and Animation
Mindfire Solutions
 
Html canvas
Rao Mubashar
 
On the tomcat drive in folder cosc210 you will find file named Paint.docx
dunhamadell
 
Chapter 02 sprite and texture
boybuon205
 
HTML5 Canvas - The Future of Graphics on the Web
Robin Hawkes
 
Chapter 13
Terry Yoast
 
Matlab graphics
pramodkumar1804
 
Ad

More from AhmadAbba6 (6)

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

Recently uploaded (20)

PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Français Patch Tuesday - Juillet
Ivanti
 
PPTX
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
Top Managed Service Providers in Los Angeles
Captain IT
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
July Patch Tuesday
Ivanti
 
Français Patch Tuesday - Juillet
Ivanti
 
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 

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);