SlideShare una empresa de Scribd logo
1 de 18
Descargar para leer sin conexión
DRAWING ON CANVAS

     chapter 6
Javascript + canvas

    - Drawing a Logo



    - Graphing Statistics with RGraph
 

The Canvas Element

<canvas id="my_canvas" width="150" height="150"> 
 Fallback content here 
</canvas>

can't use css
 

var canvas = document.getElementById('my_canvas');
if (canvas.getContext){
    var context = canvas.getContext('2d');
}else{

}
canvas_simple_drawing.html

var canvas = document.getElementById('my_canvas'); 
 if (canvas.getContext){ 
 var context = canvas.getContext('2d'); 
  context.fillStyle = "rgb(200,0,0)"; 
  context.fillRect (10, 10, 100, 100); 
  context.fillStyle = "rgb(0,0,200)"; 
  context.fillRect (30, 30, 100, 100); 
  context.strokeStyle = "rgb(200,0,0)"; 
  context.lineWidth = 2; context.beginPath(); context.moveTo
(0, 0); context.lineTo(100, 100); context.stroke(); context.
closePath();}........
Drawing the logo

string of text, an angeld path, a squeare, a triangle

Adding Text 
   c o n t e x t . f o n t = ' i t a l i c 4 0 p x s a n s - s e r i f ' ;
   c o n t e x t . t e x t B a s e l i n e = ' t o p ' ;
   c o n t e x t . f i l l T e x t ( ' A w e s o m e C o ' , 6 0 , 0 ) ;
Drawing the Logo

Drawing lines
    c o n t e x t   .   lineWidth=2;
    c o n t e x t   .   beginPath();
    c o n t e x t   .   moveTo(0,40);
    c o n t e x t   .   lineTo(30,0);
    c o n t e x t   .   lineTo(60,40);
    c o n t e x t   .   lineTo(285,40);
    c o n t e x t   .   stroke();
    c o n t e x t   .   c l o s e P a t h ( ) 
Drawing the Logo

Moving the Origin
    c o n t e x t . s a v e ( ) ;
    c o n t e x t . t r a n s l a t e ( 2 0 , 2 0 ) ;
    c o n t e x t . f i l l R e c t ( 0 , 0 , 2 0 , 2 0 ) ;
Drawing the Logo

Drawing a triangle
    c o n t e x t . f i l l S t y l e = ' # f f f '
    c o n t e x t . s t r o k e S t y l e = ' # f f f ' ;
    c o n t e x t . l i n e W i d t h = 2 ;
    c o n t e x t . b e g i n P a t h ( ) ;
    c o n t e x t . m o v e T o ( 0 , 2 0 ) ;
    c o n t e x t . l i n e T o ( 1 0 , 0 ) ;
    c o n t e x t . l i n e T o ( 2 0 , 2 0 ) ;
    c o n t e x t . l i n e T o ( 0 , 2 0 ) ;
    c o n t e x t . f i l l ( ) ;
    c o n t e x t . c l o s e P a t h ( ) ;
    c o n t e x t . r e s t o r e ( ) ;
Drawing the Logo

AddingColors
// context.fillStyle = "#f00";
// context.strokeStyle = "#f00";
var gradient = context.createLinearGradient(0, 0, 0, 40);
gradient.addColorStop(0, '#a00'); // red
gradient.addColorStop(1, '#f00'); // red
context.fillStyle = gradient;
context.strokeStyle = gradient;
Falling back

Google released a library called ExplorerCanvas

<scriptsrc="javascripts/excanvas.js">
</script>
Graphing Statistics with RGraph




<canvas width="500" height="250" id="test">[no canvas
support]</canvas> 

<script type="text/javascript" charset="utf-8"> 
 var bar = new RGraph.Bar('test', [50,25,15,10]); bar.Set
('chart.gutter', 50); bar.Set('chart.colors', ['red']); bar.Set
('chart.title', "A bar graph of my favorite pies"); bar.Set('chart.
labels', ["Banana Creme", "Pumpkin", "Apple", "Cherry"]); 
 bar.Draw(); 
 </script>
 

Describing Data with HTML

<div id="graph_data"> <h1>Browser share for this site</h1>
<ul> <li> <p data-name="Safari 4" data-percent="15"> Safari 4 -
15% </p> </li> <li> <p data-name="Internet Explorer" data-
percent="55"> Internet Explorer - 55% </p> </li> <li> <p data-
name="Firefox" data-percent="14"> Firefox - 14% </p> </li>
<li> <p data-name="Google Chrome" data-percent="16">
Google Chrome - 16% </p> </li> </ul> </div>
 

load libraries
<script type="text/javascript" charset="utf-8" src="http:
//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
> </script> 
<script src="javascripts/RGraph.common.js" ></script> <script
src="javascripts/RGraph.bar.js" ></script>
 

Turning Our HTML into a Bar Graph

function canvasGraph(){ var title = $('#graph_data h1').text();
var labels = $("#graph_data>ul>li>p[data-name]").map
(function(){ return $(this).attr("data-name"); });
var percents = $("#graph_data>ul>li>p[data-percent]").map
(function(){ return parseInt($(this).attr("data-percent")); });
var bar = new RGraph.Bar('browsers', percents); bar.Set
('chart.gutter', 50); bar.Set('chart.colors', ['red']); bar.Set
('chart.title', title); bar.Set('chart.labels', labels); bar.Draw();
}
Displaying Alternative Content


var canvas = document.getElementById('browsers'); 
 if (canvas.getContext)
{ 
 $('#graph_data').hide(); 
 canvasGraph(); 
 }
Falling back

function divGraph(barColor, textColor, width, spacer,
lblHeight)
{ 
 $('#graph_data ul').hide(); 
 var container = $("#graph_data"); 
 container.css( { "display" : "block", "position" : "relative",
"height": "300px"} );
$("#graph_data>ul>li>p").each(function(i){ 
    var bar = $("<div>" + $(this).attr("data-percent") + "%
</div>"); 
   var label = $("<div>" + $(this).attr("data-name") +
"</div>");               var commonCSS = { "width": width + "px",
"position" : "absolute", "left" : i * (width + spacer) + "px"}; var
barCSS = { "background-color" : barColor, "color" : textColor,
The Future

 

Más contenido relacionado

La actualidad más candente

Programa expresiones regulares
Programa expresiones regularesPrograma expresiones regulares
Programa expresiones regularesAnel Sosa
 
Worth the hype - styled components
Worth the hype - styled componentsWorth the hype - styled components
Worth the hype - styled componentskathrinholzmann
 
Perlで実装されたLINE NEWSの裏側
Perlで実装されたLINE NEWSの裏側Perlで実装されたLINE NEWSの裏側
Perlで実装されたLINE NEWSの裏側LINE Corporation
 
Paperjs presentation
Paperjs presentationPaperjs presentation
Paperjs presentationsharp-blade
 
Random. Kinda.
Random. Kinda.Random. Kinda.
Random. Kinda.awwaiid
 
Plot3D Package and Example in R.-Data visualizat,on
Plot3D Package and Example in R.-Data visualizat,onPlot3D Package and Example in R.-Data visualizat,on
Plot3D Package and Example in R.-Data visualizat,onDr. Volkan OBAN
 
Paperjs presentation
Paperjs presentationPaperjs presentation
Paperjs presentationsharp-blade
 
Processing資料(8) 文字
Processing資料(8) 文字Processing資料(8) 文字
Processing資料(8) 文字reona396
 
Tablas relaciones proyecto Laravel
Tablas relaciones proyecto LaravelTablas relaciones proyecto Laravel
Tablas relaciones proyecto LaravelFUNDET ECUADOR
 
Exploring Canvas
Exploring CanvasExploring Canvas
Exploring CanvasKevin Hoyt
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207Syed Tanveer
 
Oh, you’re the NY times guy
Oh, you’re the NY times guyOh, you’re the NY times guy
Oh, you’re the NY times guyDavid Hayes
 
Perl使いの国のRubyist
Perl使いの国のRubyistPerl使いの国のRubyist
Perl使いの国のRubyistTakafumi ONAKA
 

La actualidad más candente (19)

Programa expresiones regulares
Programa expresiones regularesPrograma expresiones regulares
Programa expresiones regulares
 
Worth the hype - styled components
Worth the hype - styled componentsWorth the hype - styled components
Worth the hype - styled components
 
Perlで実装されたLINE NEWSの裏側
Perlで実装されたLINE NEWSの裏側Perlで実装されたLINE NEWSの裏側
Perlで実装されたLINE NEWSの裏側
 
C code
C codeC code
C code
 
Paperjs presentation
Paperjs presentationPaperjs presentation
Paperjs presentation
 
Random. Kinda.
Random. Kinda.Random. Kinda.
Random. Kinda.
 
Plot3D Package and Example in R.-Data visualizat,on
Plot3D Package and Example in R.-Data visualizat,onPlot3D Package and Example in R.-Data visualizat,on
Plot3D Package and Example in R.-Data visualizat,on
 
Zebra
ZebraZebra
Zebra
 
Paperjs presentation
Paperjs presentationPaperjs presentation
Paperjs presentation
 
Processing資料(8) 文字
Processing資料(8) 文字Processing資料(8) 文字
Processing資料(8) 文字
 
Pointer level 2
Pointer   level 2Pointer   level 2
Pointer level 2
 
Tablas relaciones proyecto Laravel
Tablas relaciones proyecto LaravelTablas relaciones proyecto Laravel
Tablas relaciones proyecto Laravel
 
Exploring Canvas
Exploring CanvasExploring Canvas
Exploring Canvas
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207
 
Oh, you’re the NY times guy
Oh, you’re the NY times guyOh, you’re the NY times guy
Oh, you’re the NY times guy
 
Formato encuesta
Formato encuestaFormato encuesta
Formato encuesta
 
Include
IncludeInclude
Include
 
Perl使いの国のRubyist
Perl使いの国のRubyistPerl使いの国のRubyist
Perl使いの国のRubyist
 
画像Hacks
画像Hacks画像Hacks
画像Hacks
 

Destacado

2.2.4 순환목록
2.2.4 순환목록2.2.4 순환목록
2.2.4 순환목록suitzero
 
1.4.2 코루틴연습문제
1.4.2 코루틴연습문제1.4.2 코루틴연습문제
1.4.2 코루틴연습문제suitzero
 
Clojure in the Wild
Clojure in the WildClojure in the Wild
Clojure in the Wildsuitzero
 
클로저
클로저클로저
클로저dagri82
 

Destacado (7)

7장매크로
7장매크로7장매크로
7장매크로
 
2.2.4 순환목록
2.2.4 순환목록2.2.4 순환목록
2.2.4 순환목록
 
1.4.2 코루틴연습문제
1.4.2 코루틴연습문제1.4.2 코루틴연습문제
1.4.2 코루틴연습문제
 
Clojure in the Wild
Clojure in the WildClojure in the Wild
Clojure in the Wild
 
클로저
클로저클로저
클로저
 
8.다중메서드
8.다중메서드8.다중메서드
8.다중메서드
 
Clojure Chapter.6
Clojure Chapter.6Clojure Chapter.6
Clojure Chapter.6
 

Similar a Drawing on canvas

Exploring Canvas
Exploring CanvasExploring Canvas
Exploring CanvasKevin Hoyt
 
Bubble archery game(c program)
Bubble archery game(c program)Bubble archery game(c program)
Bubble archery game(c program)SETYA HADI
 
Bubble archery game(c program)
Bubble archery game(c program)Bubble archery game(c program)
Bubble archery game(c program)SETYA HADI
 
How to build a html5 websites.v1
How to build a html5 websites.v1How to build a html5 websites.v1
How to build a html5 websites.v1Bitla Software
 
Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...
Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...
Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...Axway Appcelerator
 
JavaOne 2009 - 2d Vector Graphics in the browser with Canvas and SVG
JavaOne 2009 -  2d Vector Graphics in the browser with Canvas and SVGJavaOne 2009 -  2d Vector Graphics in the browser with Canvas and SVG
JavaOne 2009 - 2d Vector Graphics in the browser with Canvas and SVGPatrick Chanezon
 
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdfbfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdfshehabhamad_90
 
Introduction to d3js (and SVG)
Introduction to d3js (and SVG)Introduction to d3js (and SVG)
Introduction to d3js (and SVG)zahid-mian
 
Google Visualization API
Google  Visualization  APIGoogle  Visualization  API
Google Visualization APIJason Young
 
Будь первым
Будь первымБудь первым
Будь первымFDConf
 
CSS3 Takes on the World
CSS3 Takes on the WorldCSS3 Takes on the World
CSS3 Takes on the WorldJonathan Snook
 
JSinSA - JS Visualisation APIs
JSinSA - JS Visualisation APIsJSinSA - JS Visualisation APIs
JSinSA - JS Visualisation APIsBrendon McLean
 
Canvas
CanvasCanvas
CanvasRajon
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! aleks-f
 

Similar a Drawing on canvas (20)

Exploring Canvas
Exploring CanvasExploring Canvas
Exploring Canvas
 
Bubble archery game(c program)
Bubble archery game(c program)Bubble archery game(c program)
Bubble archery game(c program)
 
Bubble archery game(c program)
Bubble archery game(c program)Bubble archery game(c program)
Bubble archery game(c program)
 
How to build a html5 websites.v1
How to build a html5 websites.v1How to build a html5 websites.v1
How to build a html5 websites.v1
 
Graphical representation of Stack
Graphical representation of StackGraphical representation of Stack
Graphical representation of Stack
 
Canvas - The Cure
Canvas - The CureCanvas - The Cure
Canvas - The Cure
 
Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...
Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...
Wynn Netherland: Accelerating Titanium Development with CoffeeScript, Compass...
 
JavaOne 2009 - 2d Vector Graphics in the browser with Canvas and SVG
JavaOne 2009 -  2d Vector Graphics in the browser with Canvas and SVGJavaOne 2009 -  2d Vector Graphics in the browser with Canvas and SVG
JavaOne 2009 - 2d Vector Graphics in the browser with Canvas and SVG
 
Css5 canvas
Css5 canvasCss5 canvas
Css5 canvas
 
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdfbfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
bfd23fd7-0d89-45c0-8b82-c991b30ed375.pdf
 
Introduction to d3js (and SVG)
Introduction to d3js (and SVG)Introduction to d3js (and SVG)
Introduction to d3js (and SVG)
 
Google Visualization API
Google  Visualization  APIGoogle  Visualization  API
Google Visualization API
 
Будь первым
Будь первымБудь первым
Будь первым
 
Borrador del blog
Borrador del blogBorrador del blog
Borrador del blog
 
CSS3 Takes on the World
CSS3 Takes on the WorldCSS3 Takes on the World
CSS3 Takes on the World
 
JSinSA - JS Visualisation APIs
JSinSA - JS Visualisation APIsJSinSA - JS Visualisation APIs
JSinSA - JS Visualisation APIs
 
Proga 0608
Proga 0608Proga 0608
Proga 0608
 
Canvas
CanvasCanvas
Canvas
 
Raphaël
RaphaëlRaphaël
Raphaël
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! Look Ma, “update DB to HTML5 using C++”, no hands! 
Look Ma, “update DB to HTML5 using C++”, no hands! 
 

Más de suitzero

The gravitational N -body pro
The gravitational N -body proThe gravitational N -body pro
The gravitational N -body prosuitzero
 
xUnitTestPattern/chapter16
xUnitTestPattern/chapter16xUnitTestPattern/chapter16
xUnitTestPattern/chapter16suitzero
 
HolubOnPatterns/chapter3_3
HolubOnPatterns/chapter3_3HolubOnPatterns/chapter3_3
HolubOnPatterns/chapter3_3suitzero
 
3장 자동적으로 움직이는 게임 에이전트 생성법_2
3장 자동적으로 움직이는 게임 에이전트 생성법_23장 자동적으로 움직이는 게임 에이전트 생성법_2
3장 자동적으로 움직이는 게임 에이전트 생성법_2suitzero
 
3장 자동적으로 움직이는 게임 에이전트 생성법
3장 자동적으로 움직이는 게임 에이전트 생성법3장 자동적으로 움직이는 게임 에이전트 생성법
3장 자동적으로 움직이는 게임 에이전트 생성법suitzero
 
부울 대수와 컴퓨터 논리
부울 대수와 컴퓨터 논리부울 대수와 컴퓨터 논리
부울 대수와 컴퓨터 논리suitzero
 
프리젠테이션-제목없음
프리젠테이션-제목없음프리젠테이션-제목없음
프리젠테이션-제목없음suitzero
 

Más de suitzero (7)

The gravitational N -body pro
The gravitational N -body proThe gravitational N -body pro
The gravitational N -body pro
 
xUnitTestPattern/chapter16
xUnitTestPattern/chapter16xUnitTestPattern/chapter16
xUnitTestPattern/chapter16
 
HolubOnPatterns/chapter3_3
HolubOnPatterns/chapter3_3HolubOnPatterns/chapter3_3
HolubOnPatterns/chapter3_3
 
3장 자동적으로 움직이는 게임 에이전트 생성법_2
3장 자동적으로 움직이는 게임 에이전트 생성법_23장 자동적으로 움직이는 게임 에이전트 생성법_2
3장 자동적으로 움직이는 게임 에이전트 생성법_2
 
3장 자동적으로 움직이는 게임 에이전트 생성법
3장 자동적으로 움직이는 게임 에이전트 생성법3장 자동적으로 움직이는 게임 에이전트 생성법
3장 자동적으로 움직이는 게임 에이전트 생성법
 
부울 대수와 컴퓨터 논리
부울 대수와 컴퓨터 논리부울 대수와 컴퓨터 논리
부울 대수와 컴퓨터 논리
 
프리젠테이션-제목없음
프리젠테이션-제목없음프리젠테이션-제목없음
프리젠테이션-제목없음
 

Último

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
 
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...Miguel Araújo
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
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
 
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
 
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
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
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 Scriptwesley chun
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
[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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
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
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 

Último (20)

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...
 
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...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
[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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
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
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 

Drawing on canvas

  • 1. DRAWING ON CANVAS chapter 6
  • 2. Javascript + canvas     - Drawing a Logo     - Graphing Statistics with RGraph
  • 3.   The Canvas Element <canvas id="my_canvas" width="150" height="150">   Fallback content here  </canvas> can't use css
  • 4.   var canvas = document.getElementById('my_canvas'); if (canvas.getContext){     var context = canvas.getContext('2d'); }else{ }
  • 5. canvas_simple_drawing.html var canvas = document.getElementById('my_canvas');   if (canvas.getContext){   var context = canvas.getContext('2d');    context.fillStyle = "rgb(200,0,0)";    context.fillRect (10, 10, 100, 100);    context.fillStyle = "rgb(0,0,200)";    context.fillRect (30, 30, 100, 100);    context.strokeStyle = "rgb(200,0,0)";    context.lineWidth = 2; context.beginPath(); context.moveTo (0, 0); context.lineTo(100, 100); context.stroke(); context. closePath();}........
  • 6. Drawing the logo string of text, an angeld path, a squeare, a triangle Adding Text     c o n t e x t . f o n t = ' i t a l i c 4 0 p x s a n s - s e r i f ' ;    c o n t e x t . t e x t B a s e l i n e = ' t o p ' ;    c o n t e x t . f i l l T e x t ( ' A w e s o m e C o ' , 6 0 , 0 ) ;
  • 7. Drawing the Logo Drawing lines     c o n t e x t . lineWidth=2;     c o n t e x t . beginPath();     c o n t e x t . moveTo(0,40);     c o n t e x t . lineTo(30,0);     c o n t e x t . lineTo(60,40);     c o n t e x t . lineTo(285,40);     c o n t e x t . stroke();     c o n t e x t . c l o s e P a t h ( ) 
  • 8. Drawing the Logo Moving the Origin     c o n t e x t . s a v e ( ) ;     c o n t e x t . t r a n s l a t e ( 2 0 , 2 0 ) ;     c o n t e x t . f i l l R e c t ( 0 , 0 , 2 0 , 2 0 ) ;
  • 9. Drawing the Logo Drawing a triangle     c o n t e x t . f i l l S t y l e = ' # f f f '     c o n t e x t . s t r o k e S t y l e = ' # f f f ' ;     c o n t e x t . l i n e W i d t h = 2 ;     c o n t e x t . b e g i n P a t h ( ) ;     c o n t e x t . m o v e T o ( 0 , 2 0 ) ;     c o n t e x t . l i n e T o ( 1 0 , 0 ) ;     c o n t e x t . l i n e T o ( 2 0 , 2 0 ) ;     c o n t e x t . l i n e T o ( 0 , 2 0 ) ;     c o n t e x t . f i l l ( ) ;     c o n t e x t . c l o s e P a t h ( ) ;     c o n t e x t . r e s t o r e ( ) ;
  • 10. Drawing the Logo AddingColors // context.fillStyle = "#f00"; // context.strokeStyle = "#f00"; var gradient = context.createLinearGradient(0, 0, 0, 40); gradient.addColorStop(0, '#a00'); // red gradient.addColorStop(1, '#f00'); // red context.fillStyle = gradient; context.strokeStyle = gradient;
  • 11. Falling back Google released a library called ExplorerCanvas <scriptsrc="javascripts/excanvas.js"> </script>
  • 12. Graphing Statistics with RGraph <canvas width="500" height="250" id="test">[no canvas support]</canvas>  <script type="text/javascript" charset="utf-8">   var bar = new RGraph.Bar('test', [50,25,15,10]); bar.Set ('chart.gutter', 50); bar.Set('chart.colors', ['red']); bar.Set ('chart.title', "A bar graph of my favorite pies"); bar.Set('chart. labels', ["Banana Creme", "Pumpkin", "Apple", "Cherry"]);   bar.Draw();   </script>
  • 13.   Describing Data with HTML <div id="graph_data"> <h1>Browser share for this site</h1> <ul> <li> <p data-name="Safari 4" data-percent="15"> Safari 4 - 15% </p> </li> <li> <p data-name="Internet Explorer" data- percent="55"> Internet Explorer - 55% </p> </li> <li> <p data- name="Firefox" data-percent="14"> Firefox - 14% </p> </li> <li> <p data-name="Google Chrome" data-percent="16"> Google Chrome - 16% </p> </li> </ul> </div>
  • 14.   load libraries <script type="text/javascript" charset="utf-8" src="http: //ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" > </script>  <script src="javascripts/RGraph.common.js" ></script> <script src="javascripts/RGraph.bar.js" ></script>
  • 15.   Turning Our HTML into a Bar Graph function canvasGraph(){ var title = $('#graph_data h1').text(); var labels = $("#graph_data>ul>li>p[data-name]").map (function(){ return $(this).attr("data-name"); }); var percents = $("#graph_data>ul>li>p[data-percent]").map (function(){ return parseInt($(this).attr("data-percent")); }); var bar = new RGraph.Bar('browsers', percents); bar.Set ('chart.gutter', 50); bar.Set('chart.colors', ['red']); bar.Set ('chart.title', title); bar.Set('chart.labels', labels); bar.Draw(); }
  • 16. Displaying Alternative Content var canvas = document.getElementById('browsers');   if (canvas.getContext) {   $('#graph_data').hide();   canvasGraph();   }
  • 17. Falling back function divGraph(barColor, textColor, width, spacer, lblHeight) {   $('#graph_data ul').hide();   var container = $("#graph_data");   container.css( { "display" : "block", "position" : "relative", "height": "300px"} ); $("#graph_data>ul>li>p").each(function(i){      var bar = $("<div>" + $(this).attr("data-percent") + "% </div>");     var label = $("<div>" + $(this).attr("data-name") + "</div>");               var commonCSS = { "width": width + "px", "position" : "absolute", "left" : i * (width + spacer) + "px"}; var barCSS = { "background-color" : barColor, "color" : textColor,