SlideShare ist ein Scribd-Unternehmen logo
1 von 6
Downloaden Sie, um offline zu lesen
Plunge into HTML5 Canvas – Let’s
begin
Before the arrival of HTML5 Canvas API it’s always a tough task to build complex graphs and
2D/3D designs in the website. Developers could only use drawing APIs all the way through
plug-ins like SVG (Scalable Vector Graphics) and VML (vector Markup Language) with some
specific web browsers like Internet Explorer. For example, to draw a simple diagonal line
without a canvas element is easy, but it’s a quite difficult task if you don’t have a simple 2D
drawing API at your clearance.
Canvas is just providing that, and that’s why it’s exceptionally useful feature to have in the
browser.
What is Canvas?
The Concept of Canvas is at first developed by APPLE to be used in Mac OS X WebKit to
create dashboard widgets.
In 2012, first draft of HTML5 is published as a working draft for the candidate
recommendation and Canvas (2D and 3D) is the part of the HTML5 specification. HTML5
Canvas is providing a plug-ins free paradigm for browser. It provides native support for many
features which is only possible with 3rd party plug-ins or JavaScript Hacks.
In 2012, first draft of HTML5 is published as a working draft for the candidate
recommendation and Canvas (2D and 3D) is the part of the HTML5 specification. HTML5
Canvas is providing a plug-ins free paradigm for browser. It provides native support for many
features which is only possible with 3rd party plug-ins or JavaScript Hacks.
Getting Started with HTML5 Canvas
When a canvas tag is added into the web page either statically or dynamically, it creates a
rectangular area in the page or by default rectangular area is 150 pixels high and 300 pixels
wide. User can create custom size of canvas by providing specific size. Unlike SVG, which is
vector base, canvas is pixel based.
Canvas Element
1<canvas id=”canvas1”></canvas>
After adding canvas element in your webpage you can manipulate its base on requirement
using JavaScript. User can add lines, graphics, charts, animated text within it.
If you are working with canvas dynamically/programmatically, then you have to first get its
context and perform some actions on context and finally apply those actions on the context.
Following lines of code required to get context of the canvas using the help of standard
document.getElementById Tag.
1
2
3
4
5
<script type="text/javascript">
windows.onload=function() {
var canvas = document.getElementById('canvas1');
var context = canvas=getContext("2d");};
</script>
If you are using JQuery then your code is like this,
1
2
3
4
5
<script type="text/javascript">
$(document).ready(function () {
var canvas = $('#canvas1');
var context = canvas[0].getContext("2d");
<script>
To locate a canvas object, you need to access its 2D drawing context.
W3C defines 2D drawing context in the following way,
“The 2D context represents a flat Cartesian surface whose origin (0, 0) is at the top left corner, with
the coordinate space having x values increasing when going right, and y values increasing when
going down.”
Canvas Coordinates
Coordinates in a canvas start at x=0, y=0 in the upper-left corner – which we refer as the
origin (0, 0). Using fillRect(x,y, width,height) increases the size horizontally over the x-axis
and vertically over the y-axis.
1
2
3
4
5
6
<script type="text/javascript">
windows.onload=function() {
var canvas = document.getElementById('canvas1');
var context = canvas=getContext("2d");
context.fillRect(20,20,150,100)};
</script>
Canvas – Paths
You can render any shape using HTML5 Canvas API. You can draw shapes, lines, text and
many more using HTML5 Canvas. Following are few functions which will help you to draw
shapes using canvas.
moveTo(x,y) Move the current pointer to a specific destination without drawing
lineTo(x,y) Move the current pointer to a specific destination with drawing a
straight line
stroke() Render a line
1
2
3
4
5
6
7
8
<script type="text/javascript">
windows.onload=function() {
var canvas = document.getElementById('canvas1');
var context = canvas=getContext("2d");
context.moveTo(0,0);
context.lineTo(200,100);
context.stroke();}
</script>
arc(x,y,r,start,stop) Use to render a circle with x & y coordinates, radius, starting and
ending angle.
beginPath() Start/Restart the path
1
2
3
4
5
6
7
8
<script type="text/javascript">
windows.onload=function() {
var canvas = document.getElementById('canvas1');
var context = canvas=getContext("2d");
context.beginPath();
context.arc(95,50,40,0,2*Math.PI);
context.stroke();};
</script>
closePath() Close the current path
1
2
3
4
5
6
7
8
9
10
11
<script type="text/javascript">
windows.onload=function() {
var canvas = document.getElementById('canvas1');
var context = canvas=getContext("2d");
context.beginPath();
context.moveTo(20,20);
context.lineTo(20,100);
context.lineTo(70,100);
context.closePath();
context.stroke();}
</script>
fill() Fill a shape with colour
fillStyle FillStyle is property to fill colour or gradient
1
2
3
4
5
6
7
8
9
<script type="text/javascript">
windows.onload=function() {
var canvas = document.getElementById('canvas1');
var context = canvas=getContext("2d");
context.rect(20,20,150,100);
context.fillStyle="blue";
context.fill();
context.stroke();}
</script&gt>
fillText(text,x,y) Draws a filled text
strokeText(text,x,y) Draws a text
font Property defines the font for text
fillText
1
2
3
4
5
6
7
8
<script type="text/javascript">
windows.onload=function() {
var canvas = document.getElementById('canvas1');
var context = canvas=getContext("2d");
context.font = "30px Arial";
context. fillText ("Plunge into HTML5 Canvas ",10,50);
context.stroke();}
</script>
strokeText
1
2
3
4
5
6
7
8
<script type="text/javascript">
windows.onload=function() {
var canvas = document.getElementById('canvas1');
var context = canvas=getContext("2d");
context.font = "30px Arial";
context.strokeText("Plunge into HTML5 Canvas ",10,50);
context.stroke();}
</script>
Browser Support for HTML5 Canvas
After arrival of Internet Explorer 9, almost every browser vendors are trying to provide full
support for the HTML5 Canvas but majorly available browsers are not providing full support
for HTML5 Canvas. Below is the browsers detail which tells you about that how they are
dealing with HTML5 Canvas.
Browser Description
Internet Explorer IE 8 and earlier versions do not support <canvas>
element
Google Chrome Supports <canvas> element through –webkit–
Firefox Firefox may have support via the moz setting
Safari 6 for Mac Supports <canvas> element through –webkit–
Opera Opera 15 has support of few features
If you are working with the <canvas> element than it’s a good practice to check browser
compatibility and in the case where the client’s browser is not supporting <canvas> element
then you can place other alternate text.
1
2
3
4
5
6
7
8
9
&lt;script type=&quot;text/javascript&quot;&gt;
windows.onload=function() {
try{
document.createElement(&quot;canvas1&quot;).getContext(&quot;2d&quot;);
document.getElementById(&quot;support&quot;).innerHTML =&quot;HTML5
Canvas is supported in your browser.&quot;;
} catch (e) {
document.getElementById(&quot;support&quot;).innerHTML = &quot;HTML5
Canvas is not supported in your browser.&quot;;}
};
&lt;/script&gt;
This article provides you the basic overview about the HTML5 Canvas and its different
property and more. In the upcoming article we will discuss more about HTML5 Canvas like
Background Patterns, Canvas Transforms, Canvas Security, Animation, etc. Stay Tuned!
Plunge into HTML5 Canvas – Let’s begin

Weitere ähnliche Inhalte

Andere mochten auch

ANIMALES VERTEBRADOS E INVERTEBRADOS
ANIMALES VERTEBRADOS E INVERTEBRADOSANIMALES VERTEBRADOS E INVERTEBRADOS
ANIMALES VERTEBRADOS E INVERTEBRADOSsjnacimba
 
P2 tajeddine
P2 tajeddineP2 tajeddine
P2 tajeddineAyoub1199
 
2004-10-15 SHAirED: Services for Helping the Air-quality Community use ESE Data
2004-10-15 SHAirED: Services for Helping the Air-quality Community use ESE Data2004-10-15 SHAirED: Services for Helping the Air-quality Community use ESE Data
2004-10-15 SHAirED: Services for Helping the Air-quality Community use ESE DataRudolf Husar
 
рекламный пакет
рекламный пакетрекламный пакет
рекламный пакетMarinarssk
 
гбу ро миац
гбу ро миацгбу ро миац
гбу ро миацMarinarssk
 
Pschology presentation FACTORS AFFECTING EMPLOYEES MOTIVATION ON ORGANIZATION...
Pschology presentation FACTORS AFFECTING EMPLOYEES MOTIVATION ON ORGANIZATION...Pschology presentation FACTORS AFFECTING EMPLOYEES MOTIVATION ON ORGANIZATION...
Pschology presentation FACTORS AFFECTING EMPLOYEES MOTIVATION ON ORGANIZATION...Jamshaid Chudhary
 
6 eboluzioa-sonia
6 eboluzioa-sonia6 eboluzioa-sonia
6 eboluzioa-soniasonri15
 

Andere mochten auch (18)

ANIMALES VERTEBRADOS E INVERTEBRADOS
ANIMALES VERTEBRADOS E INVERTEBRADOSANIMALES VERTEBRADOS E INVERTEBRADOS
ANIMALES VERTEBRADOS E INVERTEBRADOS
 
2016 java9-how-make-qus
2016 java9-how-make-qus2016 java9-how-make-qus
2016 java9-how-make-qus
 
Presentation on Pollution
Presentation on PollutionPresentation on Pollution
Presentation on Pollution
 
P2 tajeddine
P2 tajeddineP2 tajeddine
P2 tajeddine
 
2016 java9-how-make-qus
2016 java9-how-make-qus2016 java9-how-make-qus
2016 java9-how-make-qus
 
2004-10-15 SHAirED: Services for Helping the Air-quality Community use ESE Data
2004-10-15 SHAirED: Services for Helping the Air-quality Community use ESE Data2004-10-15 SHAirED: Services for Helping the Air-quality Community use ESE Data
2004-10-15 SHAirED: Services for Helping the Air-quality Community use ESE Data
 
Kevin auto biografia
Kevin auto biografiaKevin auto biografia
Kevin auto biografia
 
отчет
отчетотчет
отчет
 
Eu escolho
Eu escolhoEu escolho
Eu escolho
 
рекламный пакет
рекламный пакетрекламный пакет
рекламный пакет
 
гбу ро миац
гбу ро миацгбу ро миац
гбу ро миац
 
Word
WordWord
Word
 
Работы
РаботыРаботы
Работы
 
Pschology presentation FACTORS AFFECTING EMPLOYEES MOTIVATION ON ORGANIZATION...
Pschology presentation FACTORS AFFECTING EMPLOYEES MOTIVATION ON ORGANIZATION...Pschology presentation FACTORS AFFECTING EMPLOYEES MOTIVATION ON ORGANIZATION...
Pschology presentation FACTORS AFFECTING EMPLOYEES MOTIVATION ON ORGANIZATION...
 
Ucp presentation
Ucp presentationUcp presentation
Ucp presentation
 
Ogum
OgumOgum
Ogum
 
Badô de osoosi
Badô de osoosiBadô de osoosi
Badô de osoosi
 
6 eboluzioa-sonia
6 eboluzioa-sonia6 eboluzioa-sonia
6 eboluzioa-sonia
 

Ähnlich wie Plunge into HTML5 Canvas – Let’s begin

New Elements & Features in HTML5
New Elements & Features in HTML5New Elements & Features in HTML5
New Elements & Features in HTML5Jamshid Hashimi
 
Academy PRO: HTML5 API graphics
Academy PRO: HTML5 API graphicsAcademy PRO: HTML5 API graphics
Academy PRO: HTML5 API graphicsBinary Studio
 
Introdução ao Microsoft Silverlight 2.0 - Campus Party Brasil 2009
Introdução ao Microsoft Silverlight 2.0 - Campus Party Brasil 2009Introdução ao Microsoft Silverlight 2.0 - Campus Party Brasil 2009
Introdução ao Microsoft Silverlight 2.0 - Campus Party Brasil 2009Ramon Durães
 
Canvas in html5 - TungVD
Canvas in html5 - TungVDCanvas in html5 - TungVD
Canvas in html5 - TungVDFramgia Vietnam
 
Speak the Web 15.02.2010
Speak the Web 15.02.2010Speak the Web 15.02.2010
Speak the Web 15.02.2010Patrick Lauke
 
Drawing with the HTML5 Canvas
Drawing with the HTML5 CanvasDrawing with the HTML5 Canvas
Drawing with the HTML5 CanvasHenry Osborne
 
Rich Media Advertising with SVG and JavaScript
Rich Media Advertising with SVG and JavaScriptRich Media Advertising with SVG and JavaScript
Rich Media Advertising with SVG and JavaScriptGjokica Zafirovski
 
Building a game engine with jQuery
Building a game engine with jQueryBuilding a game engine with jQuery
Building a game engine with jQueryPaul Bakaus
 
Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?Patrick Chanezon
 
Javascript #10 : canvas
Javascript #10 : canvasJavascript #10 : canvas
Javascript #10 : canvasJean Michel
 

Ähnlich wie Plunge into HTML5 Canvas – Let’s begin (20)

Canvas in html5
Canvas in html5Canvas in html5
Canvas in html5
 
New Elements & Features in HTML5
New Elements & Features in HTML5New Elements & Features in HTML5
New Elements & Features in HTML5
 
Web Vector Graphics
Web Vector GraphicsWeb Vector Graphics
Web Vector Graphics
 
Html5
Html5Html5
Html5
 
Html5
Html5Html5
Html5
 
Academy PRO: HTML5 API graphics
Academy PRO: HTML5 API graphicsAcademy PRO: HTML5 API graphics
Academy PRO: HTML5 API graphics
 
Introdução ao Microsoft Silverlight 2.0 - Campus Party Brasil 2009
Introdução ao Microsoft Silverlight 2.0 - Campus Party Brasil 2009Introdução ao Microsoft Silverlight 2.0 - Campus Party Brasil 2009
Introdução ao Microsoft Silverlight 2.0 - Campus Party Brasil 2009
 
HTML 5 Canvas & SVG
HTML 5 Canvas & SVGHTML 5 Canvas & SVG
HTML 5 Canvas & SVG
 
Canvas in html5 - TungVD
Canvas in html5 - TungVDCanvas in html5 - TungVD
Canvas in html5 - TungVD
 
HTML5 canvas
HTML5 canvasHTML5 canvas
HTML5 canvas
 
Html5
Html5Html5
Html5
 
Speak the Web 15.02.2010
Speak the Web 15.02.2010Speak the Web 15.02.2010
Speak the Web 15.02.2010
 
Drawing with the HTML5 Canvas
Drawing with the HTML5 CanvasDrawing with the HTML5 Canvas
Drawing with the HTML5 Canvas
 
Rich Media Advertising with SVG and JavaScript
Rich Media Advertising with SVG and JavaScriptRich Media Advertising with SVG and JavaScript
Rich Media Advertising with SVG and JavaScript
 
Building a game engine with jQuery
Building a game engine with jQueryBuilding a game engine with jQuery
Building a game engine with jQuery
 
Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?
 
Working With Canvas
Working With CanvasWorking With Canvas
Working With Canvas
 
Html 5
Html 5Html 5
Html 5
 
Javascript #10 : canvas
Javascript #10 : canvasJavascript #10 : canvas
Javascript #10 : canvas
 
Html5
Html5Html5
Html5
 

Mehr von Azilen Technologies Pvt. Ltd.

[Step by-step guide] configure document generation functionality in ms dynami...
[Step by-step guide] configure document generation functionality in ms dynami...[Step by-step guide] configure document generation functionality in ms dynami...
[Step by-step guide] configure document generation functionality in ms dynami...Azilen Technologies Pvt. Ltd.
 
How to overcome operational challenges in getting consistent beacon behavior
How to overcome operational challenges in getting consistent beacon behaviorHow to overcome operational challenges in getting consistent beacon behavior
How to overcome operational challenges in getting consistent beacon behaviorAzilen Technologies Pvt. Ltd.
 
Realm mobile platform – explore real time data synchronization capabilities
Realm mobile platform – explore real time data synchronization capabilitiesRealm mobile platform – explore real time data synchronization capabilities
Realm mobile platform – explore real time data synchronization capabilitiesAzilen Technologies Pvt. Ltd.
 
A step by step guide to develop temperature sensor io t application using ibm...
A step by step guide to develop temperature sensor io t application using ibm...A step by step guide to develop temperature sensor io t application using ibm...
A step by step guide to develop temperature sensor io t application using ibm...Azilen Technologies Pvt. Ltd.
 
How to create an angular 2.0 application in liferay dxp to fetch the ootb adv...
How to create an angular 2.0 application in liferay dxp to fetch the ootb adv...How to create an angular 2.0 application in liferay dxp to fetch the ootb adv...
How to create an angular 2.0 application in liferay dxp to fetch the ootb adv...Azilen Technologies Pvt. Ltd.
 
Server driven user interface (sdui) – framework for i os applications!
Server driven user interface (sdui) – framework for i os applications!Server driven user interface (sdui) – framework for i os applications!
Server driven user interface (sdui) – framework for i os applications!Azilen Technologies Pvt. Ltd.
 
How to integrate portlet as widget in liferay to any website application
How to integrate portlet as widget in liferay to any website applicationHow to integrate portlet as widget in liferay to any website application
How to integrate portlet as widget in liferay to any website applicationAzilen Technologies Pvt. Ltd.
 
iPad Application as Return Process Automation Solution for eCommerce Store
iPad Application as Return Process Automation Solution for eCommerce StoreiPad Application as Return Process Automation Solution for eCommerce Store
iPad Application as Return Process Automation Solution for eCommerce StoreAzilen Technologies Pvt. Ltd.
 
[Part 3] automation of home appliances using raspberry pi – all set to automa...
[Part 3] automation of home appliances using raspberry pi – all set to automa...[Part 3] automation of home appliances using raspberry pi – all set to automa...
[Part 3] automation of home appliances using raspberry pi – all set to automa...Azilen Technologies Pvt. Ltd.
 
Rfid systems for asset management — the young technology on its winning path
Rfid systems for asset management — the young technology on its winning pathRfid systems for asset management — the young technology on its winning path
Rfid systems for asset management — the young technology on its winning pathAzilen Technologies Pvt. Ltd.
 
[Part 2] automation of home appliances using raspberry pi – implementation of...
[Part 2] automation of home appliances using raspberry pi – implementation of...[Part 2] automation of home appliances using raspberry pi – implementation of...
[Part 2] automation of home appliances using raspberry pi – implementation of...Azilen Technologies Pvt. Ltd.
 
[Part 1] automation of home appliances using raspberry pi – software installa...
[Part 1] automation of home appliances using raspberry pi – software installa...[Part 1] automation of home appliances using raspberry pi – software installa...
[Part 1] automation of home appliances using raspberry pi – software installa...Azilen Technologies Pvt. Ltd.
 

Mehr von Azilen Technologies Pvt. Ltd. (20)

Software Product Development for Startups.pdf
Software Product Development for Startups.pdfSoftware Product Development for Startups.pdf
Software Product Development for Startups.pdf
 
How Chatbots Empower Healthcare Ecosystem?
How Chatbots Empower Healthcare Ecosystem?How Chatbots Empower Healthcare Ecosystem?
How Chatbots Empower Healthcare Ecosystem?
 
[Step by-step guide] configure document generation functionality in ms dynami...
[Step by-step guide] configure document generation functionality in ms dynami...[Step by-step guide] configure document generation functionality in ms dynami...
[Step by-step guide] configure document generation functionality in ms dynami...
 
How to overcome operational challenges in getting consistent beacon behavior
How to overcome operational challenges in getting consistent beacon behaviorHow to overcome operational challenges in getting consistent beacon behavior
How to overcome operational challenges in getting consistent beacon behavior
 
Liferay dxp – the good, the bad and the ugly
Liferay dxp – the good, the bad and the uglyLiferay dxp – the good, the bad and the ugly
Liferay dxp – the good, the bad and the ugly
 
Realm mobile platform – explore real time data synchronization capabilities
Realm mobile platform – explore real time data synchronization capabilitiesRealm mobile platform – explore real time data synchronization capabilities
Realm mobile platform – explore real time data synchronization capabilities
 
A step by step guide to develop temperature sensor io t application using ibm...
A step by step guide to develop temperature sensor io t application using ibm...A step by step guide to develop temperature sensor io t application using ibm...
A step by step guide to develop temperature sensor io t application using ibm...
 
How to create an angular 2.0 application in liferay dxp to fetch the ootb adv...
How to create an angular 2.0 application in liferay dxp to fetch the ootb adv...How to create an angular 2.0 application in liferay dxp to fetch the ootb adv...
How to create an angular 2.0 application in liferay dxp to fetch the ootb adv...
 
Register Virtual Device and analyze the device data
Register Virtual Device and analyze the device dataRegister Virtual Device and analyze the device data
Register Virtual Device and analyze the device data
 
Analytics and etl based bi solutions
Analytics and etl based bi solutionsAnalytics and etl based bi solutions
Analytics and etl based bi solutions
 
Advanced risk management &amp; mitigation system
Advanced risk management &amp; mitigation systemAdvanced risk management &amp; mitigation system
Advanced risk management &amp; mitigation system
 
Server driven user interface (sdui) – framework for i os applications!
Server driven user interface (sdui) – framework for i os applications!Server driven user interface (sdui) – framework for i os applications!
Server driven user interface (sdui) – framework for i os applications!
 
How to integrate portlet as widget in liferay to any website application
How to integrate portlet as widget in liferay to any website applicationHow to integrate portlet as widget in liferay to any website application
How to integrate portlet as widget in liferay to any website application
 
A walkthrough of recently held wwdc17
A walkthrough of recently held wwdc17A walkthrough of recently held wwdc17
A walkthrough of recently held wwdc17
 
How wearable devices are changing our lives
How wearable devices are changing our livesHow wearable devices are changing our lives
How wearable devices are changing our lives
 
iPad Application as Return Process Automation Solution for eCommerce Store
iPad Application as Return Process Automation Solution for eCommerce StoreiPad Application as Return Process Automation Solution for eCommerce Store
iPad Application as Return Process Automation Solution for eCommerce Store
 
[Part 3] automation of home appliances using raspberry pi – all set to automa...
[Part 3] automation of home appliances using raspberry pi – all set to automa...[Part 3] automation of home appliances using raspberry pi – all set to automa...
[Part 3] automation of home appliances using raspberry pi – all set to automa...
 
Rfid systems for asset management — the young technology on its winning path
Rfid systems for asset management — the young technology on its winning pathRfid systems for asset management — the young technology on its winning path
Rfid systems for asset management — the young technology on its winning path
 
[Part 2] automation of home appliances using raspberry pi – implementation of...
[Part 2] automation of home appliances using raspberry pi – implementation of...[Part 2] automation of home appliances using raspberry pi – implementation of...
[Part 2] automation of home appliances using raspberry pi – implementation of...
 
[Part 1] automation of home appliances using raspberry pi – software installa...
[Part 1] automation of home appliances using raspberry pi – software installa...[Part 1] automation of home appliances using raspberry pi – software installa...
[Part 1] automation of home appliances using raspberry pi – software installa...
 

Kürzlich hochgeladen

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
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 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
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 FresherRemote DBA Services
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
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 2024The Digital Insurer
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
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...Martijn de Jong
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
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 WorkerThousandEyes
 

Kürzlich hochgeladen (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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
 

Plunge into HTML5 Canvas – Let’s begin

  • 1. Plunge into HTML5 Canvas – Let’s begin Before the arrival of HTML5 Canvas API it’s always a tough task to build complex graphs and 2D/3D designs in the website. Developers could only use drawing APIs all the way through plug-ins like SVG (Scalable Vector Graphics) and VML (vector Markup Language) with some specific web browsers like Internet Explorer. For example, to draw a simple diagonal line without a canvas element is easy, but it’s a quite difficult task if you don’t have a simple 2D drawing API at your clearance. Canvas is just providing that, and that’s why it’s exceptionally useful feature to have in the browser. What is Canvas? The Concept of Canvas is at first developed by APPLE to be used in Mac OS X WebKit to create dashboard widgets. In 2012, first draft of HTML5 is published as a working draft for the candidate recommendation and Canvas (2D and 3D) is the part of the HTML5 specification. HTML5 Canvas is providing a plug-ins free paradigm for browser. It provides native support for many features which is only possible with 3rd party plug-ins or JavaScript Hacks.
  • 2. In 2012, first draft of HTML5 is published as a working draft for the candidate recommendation and Canvas (2D and 3D) is the part of the HTML5 specification. HTML5 Canvas is providing a plug-ins free paradigm for browser. It provides native support for many features which is only possible with 3rd party plug-ins or JavaScript Hacks. Getting Started with HTML5 Canvas When a canvas tag is added into the web page either statically or dynamically, it creates a rectangular area in the page or by default rectangular area is 150 pixels high and 300 pixels wide. User can create custom size of canvas by providing specific size. Unlike SVG, which is vector base, canvas is pixel based. Canvas Element 1&lt;canvas id=”canvas1”&gt;&lt;/canvas&gt; After adding canvas element in your webpage you can manipulate its base on requirement using JavaScript. User can add lines, graphics, charts, animated text within it. If you are working with canvas dynamically/programmatically, then you have to first get its context and perform some actions on context and finally apply those actions on the context. Following lines of code required to get context of the canvas using the help of standard document.getElementById Tag. 1 2 3 4 5 &lt;script type=&quot;text/javascript&quot;&gt; windows.onload=function() { var canvas = document.getElementById('canvas1'); var context = canvas=getContext(&quot;2d&quot;);}; &lt;/script&gt; If you are using JQuery then your code is like this, 1 2 3 4 5 &lt;script type=&quot;text/javascript&quot;&gt; $(document).ready(function () { var canvas = $('#canvas1'); var context = canvas[0].getContext(&quot;2d&quot;); &lt;script&gt; To locate a canvas object, you need to access its 2D drawing context. W3C defines 2D drawing context in the following way, “The 2D context represents a flat Cartesian surface whose origin (0, 0) is at the top left corner, with the coordinate space having x values increasing when going right, and y values increasing when going down.” Canvas Coordinates
  • 3. Coordinates in a canvas start at x=0, y=0 in the upper-left corner – which we refer as the origin (0, 0). Using fillRect(x,y, width,height) increases the size horizontally over the x-axis and vertically over the y-axis. 1 2 3 4 5 6 &lt;script type=&quot;text/javascript&quot;&gt; windows.onload=function() { var canvas = document.getElementById('canvas1'); var context = canvas=getContext(&quot;2d&quot;); context.fillRect(20,20,150,100)}; &lt;/script&gt; Canvas – Paths You can render any shape using HTML5 Canvas API. You can draw shapes, lines, text and many more using HTML5 Canvas. Following are few functions which will help you to draw shapes using canvas. moveTo(x,y) Move the current pointer to a specific destination without drawing lineTo(x,y) Move the current pointer to a specific destination with drawing a straight line stroke() Render a line 1 2 3 4 5 6 7 8 &lt;script type=&quot;text/javascript&quot;&gt; windows.onload=function() { var canvas = document.getElementById('canvas1'); var context = canvas=getContext(&quot;2d&quot;); context.moveTo(0,0); context.lineTo(200,100); context.stroke();} &lt;/script&gt; arc(x,y,r,start,stop) Use to render a circle with x & y coordinates, radius, starting and ending angle. beginPath() Start/Restart the path 1 2 3 4 5 6 7 8 &lt;script type=&quot;text/javascript&quot;&gt; windows.onload=function() { var canvas = document.getElementById('canvas1'); var context = canvas=getContext(&quot;2d&quot;); context.beginPath(); context.arc(95,50,40,0,2*Math.PI); context.stroke();}; &lt;/script&gt; closePath() Close the current path
  • 4. 1 2 3 4 5 6 7 8 9 10 11 &lt;script type=&quot;text/javascript&quot;&gt; windows.onload=function() { var canvas = document.getElementById('canvas1'); var context = canvas=getContext(&quot;2d&quot;); context.beginPath(); context.moveTo(20,20); context.lineTo(20,100); context.lineTo(70,100); context.closePath(); context.stroke();} &lt;/script&gt; fill() Fill a shape with colour fillStyle FillStyle is property to fill colour or gradient 1 2 3 4 5 6 7 8 9 &lt;script type=&quot;text/javascript&quot;&gt; windows.onload=function() { var canvas = document.getElementById('canvas1'); var context = canvas=getContext(&quot;2d&quot;); context.rect(20,20,150,100); context.fillStyle=&quot;blue&quot;; context.fill(); context.stroke();} &lt;/script&amp;gt&gt; fillText(text,x,y) Draws a filled text strokeText(text,x,y) Draws a text font Property defines the font for text fillText 1 2 3 4 5 6 7 8 &lt;script type=&quot;text/javascript&quot;&gt; windows.onload=function() { var canvas = document.getElementById('canvas1'); var context = canvas=getContext(&quot;2d&quot;); context.font = &quot;30px Arial&quot;; context. fillText (&quot;Plunge into HTML5 Canvas &quot;,10,50); context.stroke();} &lt;/script&gt; strokeText 1 2 3 4 5 6 7 8 &lt;script type=&quot;text/javascript&quot;&gt; windows.onload=function() { var canvas = document.getElementById('canvas1'); var context = canvas=getContext(&quot;2d&quot;); context.font = &quot;30px Arial&quot;; context.strokeText(&quot;Plunge into HTML5 Canvas &quot;,10,50); context.stroke();} &lt;/script&gt; Browser Support for HTML5 Canvas After arrival of Internet Explorer 9, almost every browser vendors are trying to provide full support for the HTML5 Canvas but majorly available browsers are not providing full support
  • 5. for HTML5 Canvas. Below is the browsers detail which tells you about that how they are dealing with HTML5 Canvas. Browser Description Internet Explorer IE 8 and earlier versions do not support <canvas> element Google Chrome Supports <canvas> element through –webkit– Firefox Firefox may have support via the moz setting Safari 6 for Mac Supports <canvas> element through –webkit– Opera Opera 15 has support of few features If you are working with the <canvas> element than it’s a good practice to check browser compatibility and in the case where the client’s browser is not supporting <canvas> element then you can place other alternate text. 1 2 3 4 5 6 7 8 9 &lt;script type=&quot;text/javascript&quot;&gt; windows.onload=function() { try{ document.createElement(&quot;canvas1&quot;).getContext(&quot;2d&quot;); document.getElementById(&quot;support&quot;).innerHTML =&quot;HTML5 Canvas is supported in your browser.&quot;; } catch (e) { document.getElementById(&quot;support&quot;).innerHTML = &quot;HTML5 Canvas is not supported in your browser.&quot;;} }; &lt;/script&gt; This article provides you the basic overview about the HTML5 Canvas and its different property and more. In the upcoming article we will discuss more about HTML5 Canvas like Background Patterns, Canvas Transforms, Canvas Security, Animation, etc. Stay Tuned!