SlideShare a Scribd company logo
1 of 9
JavaScript Canvas
Jagriti Srivastava
Intro..
Used to draw images
Came with HTML5 to draw 2d and 3d graphics
Used for games and visualization
How to start
Must have concept of HTML and basic Javascript
Steps to get started :
 1. Create canvas element with attributes – ID,
 HEIGHT and WIDTH
 2. Add Styles(*Optional)
 3.Get canvas element using ID
4. Get Canvas Context (2D or 3D)
 5. Draw using Context
Steps
1. Create Canvas
// index.html
<canvas id="myCanvas" width="800"
height="1200"></canvas>
Step 2 and 3
Add Styles
// styles.css
body {
background: #111;
color: #f8f8f8;
}
#myCanvas {
background: #f8f8f8;
padding: 0;
margin: 0 auto;
margin-bottom: 1rem;
display: block;
}
Step 3 and 4
Get canvas using Id and canvas context
var canvas =
document.getElementById("myCanvas");
var ctext = canvas.getContext("2d");
Step 5
**Draw using context **
ctext.beginPath();
ctext.moveTo(150,10);//moveTo(x,y) - defines the
starting point of the line
ctext.lineTo(20, 100);//lineTo(x,y) - defines the
ending point of the line
ctext.lineWidth = 5;
ctext.strokeStyle = "blue"; //color of the line
ctext.stroke(); // to provide ink to the line
<!DOCTYPE HTML>
<html>
<head>
<title> Canvas </title>
<style>
body { margin: 0px; padding: 0px; }
#myCanvas { border: 1px solid red; }
</style>
<script>
window.onload = function()
{
var canvas = document.getElementById("mCanvas");
var ctext = canvas.getContext("2d");
ctext.beginPath();
ctext.moveTo(150,10);//moveTo(x,y)
ctext.lineTo(20, 100);//lineTo(x,y)
ctext.lineWidth = 5;
ctext.strokeStyle = "blue"; //color of the line
ctext.stroke(); // to provide ink to the line
};
</script>
</head>
<body>
<canvas id="mCanvas" width="360" height="200"></canvas>
</body>
</html>
How to draw
Set the style -
1)Begin Path
2)Use functions and Coordinates
3)Draw the path to screen

More Related Content

Similar to JavaScript Canvas

Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
Remy Sharp
 
Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02
PL dream
 
#win8acad : Building a Windows 8 Metro Style UI
#win8acad : Building a Windows 8 Metro Style UI#win8acad : Building a Windows 8 Metro Style UI
#win8acad : Building a Windows 8 Metro Style UI
Frederik De Bruyne
 

Similar to JavaScript Canvas (20)

Javascript #10 : canvas
Javascript #10 : canvasJavascript #10 : canvas
Javascript #10 : canvas
 
An Event Apart DC - New CSS Layout meets the Real World
An Event Apart DC - New CSS Layout meets the Real WorldAn Event Apart DC - New CSS Layout meets the Real World
An Event Apart DC - New CSS Layout meets the Real World
 
Css 3 session1
Css 3 session1Css 3 session1
Css 3 session1
 
Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
 
Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02
 
Stupid Canvas Tricks
Stupid Canvas TricksStupid Canvas Tricks
Stupid Canvas Tricks
 
How to make a video game
How to make a video gameHow to make a video game
How to make a video game
 
Advanced Skinning with DotNetNuke 5
Advanced Skinning with DotNetNuke 5Advanced Skinning with DotNetNuke 5
Advanced Skinning with DotNetNuke 5
 
Advanced Skinning With DotNetNuke
Advanced Skinning With DotNetNukeAdvanced Skinning With DotNetNuke
Advanced Skinning With DotNetNuke
 
HTML5, the new buzzword
HTML5, the new buzzwordHTML5, the new buzzword
HTML5, the new buzzword
 
Iml140 cs sbasics_week5
Iml140 cs sbasics_week5Iml140 cs sbasics_week5
Iml140 cs sbasics_week5
 
Html5 canvas
Html5 canvasHtml5 canvas
Html5 canvas
 
Flash Widget Tutorial
Flash Widget TutorialFlash Widget Tutorial
Flash Widget Tutorial
 
New CSS Meets the Real World
New CSS Meets the Real WorldNew CSS Meets the Real World
New CSS Meets the Real World
 
Css3
Css3Css3
Css3
 
Intro to HTML5
Intro to HTML5Intro to HTML5
Intro to HTML5
 
Windows8 lightningtalk
Windows8 lightningtalkWindows8 lightningtalk
Windows8 lightningtalk
 
HTML5 - Chances and Pitfalls (Bytro Labs GmbH)
HTML5 - Chances and Pitfalls (Bytro Labs GmbH)HTML5 - Chances and Pitfalls (Bytro Labs GmbH)
HTML5 - Chances and Pitfalls (Bytro Labs GmbH)
 
#win8acad : Building a Windows 8 Metro Style UI
#win8acad : Building a Windows 8 Metro Style UI#win8acad : Building a Windows 8 Metro Style UI
#win8acad : Building a Windows 8 Metro Style UI
 
Level ofdetail
Level ofdetailLevel ofdetail
Level ofdetail
 

More from jagriti srivastava

More from jagriti srivastava (15)

Map reduce with big data
Map reduce with big dataMap reduce with big data
Map reduce with big data
 
Oyo rooms
Oyo roomsOyo rooms
Oyo rooms
 
Information system of amazon
Information system of amazonInformation system of amazon
Information system of amazon
 
Variable and Methods in Java
Variable and Methods in JavaVariable and Methods in Java
Variable and Methods in Java
 
Component diagram and Deployment Diagram
Component diagram and Deployment DiagramComponent diagram and Deployment Diagram
Component diagram and Deployment Diagram
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time API
 
Form validation and animation
Form validation and animationForm validation and animation
Form validation and animation
 
Custom directive and scopes
Custom directive and scopesCustom directive and scopes
Custom directive and scopes
 
Angular directive filter and routing
Angular directive filter and routingAngular directive filter and routing
Angular directive filter and routing
 
Starting with angular js
Starting with angular js Starting with angular js
Starting with angular js
 
Angular introduction basic
Angular introduction basicAngular introduction basic
Angular introduction basic
 
Scannerclass
ScannerclassScannerclass
Scannerclass
 
Programming Workshop
Programming WorkshopProgramming Workshop
Programming Workshop
 
Java Nested class Concept
Java Nested class ConceptJava Nested class Concept
Java Nested class Concept
 
Java , A brief Introduction
Java , A brief Introduction Java , A brief Introduction
Java , A brief Introduction
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

JavaScript Canvas

  • 2. Intro.. Used to draw images Came with HTML5 to draw 2d and 3d graphics Used for games and visualization
  • 3. How to start Must have concept of HTML and basic Javascript Steps to get started :  1. Create canvas element with attributes – ID,  HEIGHT and WIDTH  2. Add Styles(*Optional)  3.Get canvas element using ID 4. Get Canvas Context (2D or 3D)  5. Draw using Context
  • 4. Steps 1. Create Canvas // index.html <canvas id="myCanvas" width="800" height="1200"></canvas>
  • 5. Step 2 and 3 Add Styles // styles.css body { background: #111; color: #f8f8f8; } #myCanvas { background: #f8f8f8; padding: 0; margin: 0 auto; margin-bottom: 1rem; display: block; }
  • 6. Step 3 and 4 Get canvas using Id and canvas context var canvas = document.getElementById("myCanvas"); var ctext = canvas.getContext("2d");
  • 7. Step 5 **Draw using context ** ctext.beginPath(); ctext.moveTo(150,10);//moveTo(x,y) - defines the starting point of the line ctext.lineTo(20, 100);//lineTo(x,y) - defines the ending point of the line ctext.lineWidth = 5; ctext.strokeStyle = "blue"; //color of the line ctext.stroke(); // to provide ink to the line
  • 8. <!DOCTYPE HTML> <html> <head> <title> Canvas </title> <style> body { margin: 0px; padding: 0px; } #myCanvas { border: 1px solid red; } </style> <script> window.onload = function() { var canvas = document.getElementById("mCanvas"); var ctext = canvas.getContext("2d"); ctext.beginPath(); ctext.moveTo(150,10);//moveTo(x,y) ctext.lineTo(20, 100);//lineTo(x,y) ctext.lineWidth = 5; ctext.strokeStyle = "blue"; //color of the line ctext.stroke(); // to provide ink to the line }; </script> </head> <body> <canvas id="mCanvas" width="360" height="200"></canvas> </body> </html>
  • 9. How to draw Set the style - 1)Begin Path 2)Use functions and Coordinates 3)Draw the path to screen