SlideShare ist ein Scribd-Unternehmen logo
1 von 15
<canvas>
/*** CANVAS* */(function() {var my = {};my.name = 'Stijn Van Minnebruggen';my.work = 'Multimedia developer at These Days Antwerp';my.hobbies = 'Passionate about photography, graphic design, typography and music.';my.tweets = 'twitter.com/donotfold';my.website = 'donotfold.be';
varabout = {};about.canvas= 'The Canvas element allows dynamic rendering of 2D shapes and bitmap 			images in the browser';	about.html5 = 'Canvas is part of HTML5 and introduced in 2004';about.how= 'The shapes and images that are drawn on the canvas are scriptable with Javascript';
	<canvas width="300" height="250">		This text is displayed if your browser does not support canvas.	</canvas>
	<canvas id="myFirstCanvas" width="300" height="250">	This text is displayed if your browser does not support canvas.	</canvas>	<p><a href="#" onclick="drawIt();">Click me to draw rectangle</a></p>	<script>		function drawIt() {varmyFirstCanvas = document.getElementById("myFirstCanvas");var context = myFirstCanvas.getContext("2d");context.fillStyle = 'rgb(255,0,0)';context.fillRect(50, 25, 100, 150);	}	</script>
// basic context methodscontext.fillStyle = "property"; // css color, pattern or gradient (default: black)context.fillRect(x, y, width, height); // draws a rectangle with the current fillStylecontext.strokeStyle = "property"; // allows same properties as fillStylecontext.strokeRect(x, y, width, height); // draws a rectangle with the current strokeStyle(no fill)context.clearRect(x, y, width, height); // clears the pixels in the specified rectangle
	// strokes	// 1. define start point	// 2. define end point	// 3. set optional styles	// 4. draw linecontext.moveTo(x, y); // move the pencil to the starting pointcontext.lineTo(x, y); // imagine a line from the starting point to this ending pointcontext.strokeStyle = "property"; // set same style properties as before if you want context.stroke(); // draw the actual line
context.beginPath(); context.moveTo(0,0);context.lineTo(0,100);context.lineTo(100,100);context.lineTo(100,0);context.lineTo(0,0);context.stroke();
	// gradients	// fill or stroke styles allow gradientsvarmyLinearGradient = context.createLinearGradient(x0, y0, x1, y1);varmyRadialGradient = context.createRadialGradient(x0, y0, r0, x1, y1, r1);	// make as many color stops as you like, between 0 and 1myLinearGradient.addColorStop(0, "black"); myLinearGradient.addColorStop(1, "white");	// add the gradient to the style propertycontext.fillStyle= myLinearGradient;
	// imagesvarmyImage = new Image();myImage.src = "img/html5.png";myImage.onload = function() {context.drawImage(myImage, 0, 0);	};
// event listenerscanvas.addEventListener("click", drawIt, false);	function drawIt(e) {varcursorPosition = getCursorPosition(e);var x = cursorPosition.x;var y = cursorPosition.y;var w = x+10;var h = y+10;context.fillRect(x, y, w, h);	}	function getCursorPosition(e) {var x, y;		if(e.pageX != undefined && e.pageY != undefined) {			x = e.pageX;			y = e.pageY;		} else {			x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;			y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;		}	x -= canvas.offsetLeft;		y -= canvas.offsetTop;	return { "x": x, "y": y };}
// animationvarmyIterval = setInterval(function() {drawIt();	}, 500);
	// safer animationvarmyTimeout = setTimeout(loop, 500);var loop = function() {drawIt();clearTimeout(myTimeout);myTimeout = setTimeout(loop, 500);});
	// best animationwindow.requestAnimFrame= (function() {		return window.requestAnimationFrame       || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame    || window.oRequestAnimationFrame      || window.msRequestAnimationFrame     || 			function(/* function */ callback, /* DOMElement */ element){window.setTimeout(callback, 1000 / 60);			};	})();
	// TUTORIALS	http://diveintohtml5.org/canvas.html		// base for this presentation	http://billmill.org/static/canvastutorial/index.html	// pong tutorial	http://html5doctor.com/an-introduction-to-the-canvas-2d-api/	// TOOLS	http://blog.nihilogic.dk/2009/02/html5-canvas-cheat-sheet.html 	// cheat sheet	http://visitmix.com/labs/ai2canvas/				// AI to Canvas	// DEMOS	http://www.20thingsilearned.com		// good integration	http://www.frameratefest.com	// canvas contest	http://demo.thesedays.com			// background effect	http://easeljs.com// game demos	http://soulwire.co.uk/hello			// experiments	http://9elements.com/io/projects/html5/canvas/})();

Weitere ähnliche Inhalte

Was ist angesagt?

Teddy Bear Blue
Teddy Bear BlueTeddy Bear Blue
Teddy Bear Blueangeliclv
 
CSS3 Transforms Transitions and Animations
CSS3 Transforms Transitions and AnimationsCSS3 Transforms Transitions and Animations
CSS3 Transforms Transitions and AnimationsInayaili León
 
Dynamic CSS: Transforms, Transitions, and Animation Basics
Dynamic CSS: Transforms, Transitions, and Animation BasicsDynamic CSS: Transforms, Transitions, and Animation Basics
Dynamic CSS: Transforms, Transitions, and Animation BasicsBeth Soderberg
 
CSS3 : Animation ,Transitions, Gradients
CSS3 : Animation ,Transitions, GradientsCSS3 : Animation ,Transitions, Gradients
CSS3 : Animation ,Transitions, GradientsJatin_23
 
DRY up your views
DRY up your viewsDRY up your views
DRY up your viewslachie
 
keyboard accessibility
keyboard accessibilitykeyboard accessibility
keyboard accessibilityakira9515
 
Web Development for Mobile: GTUG Talk at Google
Web Development for Mobile: GTUG Talk at GoogleWeb Development for Mobile: GTUG Talk at Google
Web Development for Mobile: GTUG Talk at GoogleEstelle Weyl
 

Was ist angesagt? (10)

Teddy Bear Blue
Teddy Bear BlueTeddy Bear Blue
Teddy Bear Blue
 
CSS3 Transforms Transitions and Animations
CSS3 Transforms Transitions and AnimationsCSS3 Transforms Transitions and Animations
CSS3 Transforms Transitions and Animations
 
Canvas - HTML 5
Canvas - HTML 5Canvas - HTML 5
Canvas - HTML 5
 
Dynamic CSS: Transforms, Transitions, and Animation Basics
Dynamic CSS: Transforms, Transitions, and Animation BasicsDynamic CSS: Transforms, Transitions, and Animation Basics
Dynamic CSS: Transforms, Transitions, and Animation Basics
 
CSS3 : Animation ,Transitions, Gradients
CSS3 : Animation ,Transitions, GradientsCSS3 : Animation ,Transitions, Gradients
CSS3 : Animation ,Transitions, Gradients
 
DRY up your views
DRY up your viewsDRY up your views
DRY up your views
 
keyboard accessibility
keyboard accessibilitykeyboard accessibility
keyboard accessibility
 
HTML 5_Canvas
HTML 5_CanvasHTML 5_Canvas
HTML 5_Canvas
 
HTML CANVAS
HTML CANVASHTML CANVAS
HTML CANVAS
 
Web Development for Mobile: GTUG Talk at Google
Web Development for Mobile: GTUG Talk at GoogleWeb Development for Mobile: GTUG Talk at Google
Web Development for Mobile: GTUG Talk at Google
 

Andere mochten auch

Choosing Lenses For Power Led Based Luminaire
Choosing Lenses For Power Led Based LuminaireChoosing Lenses For Power Led Based Luminaire
Choosing Lenses For Power Led Based Luminairevfurlan
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika AldabaLightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldabaux singapore
 

Andere mochten auch (6)

jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
jQuery basics
jQuery basicsjQuery basics
jQuery basics
 
So
SoSo
So
 
Choosing Lenses For Power Led Based Luminaire
Choosing Lenses For Power Led Based LuminaireChoosing Lenses For Power Led Based Luminaire
Choosing Lenses For Power Led Based Luminaire
 
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job? Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
Succession “Losers”: What Happens to Executives Passed Over for the CEO Job?
 
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika AldabaLightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
Lightning Talk #9: How UX and Data Storytelling Can Shape Policy by Mika Aldaba
 

Ähnlich wie Canvas

JSinSA - JS Visualisation APIs
JSinSA - JS Visualisation APIsJSinSA - JS Visualisation APIs
JSinSA - JS Visualisation APIsBrendon McLean
 
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
 
CSS: A Slippery Slope to the Backend
CSS: A Slippery Slope to the BackendCSS: A Slippery Slope to the Backend
CSS: A Slippery Slope to the BackendFITC
 
Introduction to HTML5 Canvas
Introduction to HTML5 CanvasIntroduction to HTML5 Canvas
Introduction to HTML5 CanvasMindy McAdams
 
How to make a video game
How to make a video gameHow to make a video game
How to make a video gamedandylion13
 
Standards.Next: Canvas
Standards.Next: CanvasStandards.Next: Canvas
Standards.Next: CanvasMartin Kliehm
 
CSS Less framework overview, Pros and Cons
CSS Less framework overview, Pros and ConsCSS Less framework overview, Pros and Cons
CSS Less framework overview, Pros and ConsSanjoy Kr. Paul
 
Svg Overview And Js Libraries
Svg Overview And Js LibrariesSvg Overview And Js Libraries
Svg Overview And Js Librariesseamusjr
 
Drawing with the HTML5 Canvas
Drawing with the HTML5 CanvasDrawing with the HTML5 Canvas
Drawing with the HTML5 CanvasHenry Osborne
 
Drawing on canvas
Drawing on canvasDrawing on canvas
Drawing on canvassuitzero
 
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
 
Fasten RWD Development with Sass
Fasten RWD Development with SassFasten RWD Development with Sass
Fasten RWD Development with SassSven Wolfermann
 
Sass & Compass (Barcamp Stuttgart 2012)
Sass & Compass (Barcamp Stuttgart 2012)Sass & Compass (Barcamp Stuttgart 2012)
Sass & Compass (Barcamp Stuttgart 2012)emrox
 

Ähnlich wie Canvas (20)

JSinSA - JS Visualisation APIs
JSinSA - JS Visualisation APIsJSinSA - JS Visualisation APIs
JSinSA - JS Visualisation APIs
 
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
 
Accelerated Stylesheets
Accelerated StylesheetsAccelerated Stylesheets
Accelerated Stylesheets
 
Theming and Sass
Theming and SassTheming and Sass
Theming and Sass
 
CSS: A Slippery Slope to the Backend
CSS: A Slippery Slope to the BackendCSS: A Slippery Slope to the Backend
CSS: A Slippery Slope to the Backend
 
Introduction to HTML5 Canvas
Introduction to HTML5 CanvasIntroduction to HTML5 Canvas
Introduction to HTML5 Canvas
 
Plunge into HTML5 Canvas – Let’s begin
Plunge into HTML5 Canvas – Let’s beginPlunge into HTML5 Canvas – Let’s begin
Plunge into HTML5 Canvas – Let’s begin
 
How to make a video game
How to make a video gameHow to make a video game
How to make a video game
 
Standards.Next: Canvas
Standards.Next: CanvasStandards.Next: Canvas
Standards.Next: Canvas
 
CSS Less framework overview, Pros and Cons
CSS Less framework overview, Pros and ConsCSS Less framework overview, Pros and Cons
CSS Less framework overview, Pros and Cons
 
Svg Overview And Js Libraries
Svg Overview And Js LibrariesSvg Overview And Js Libraries
Svg Overview And Js Libraries
 
Workshop 6: Designer tools
Workshop 6: Designer toolsWorkshop 6: Designer tools
Workshop 6: Designer tools
 
Sencha Touch
Sencha TouchSencha Touch
Sencha Touch
 
Drawing with the HTML5 Canvas
Drawing with the HTML5 CanvasDrawing with the HTML5 Canvas
Drawing with the HTML5 Canvas
 
Drawing on canvas
Drawing on canvasDrawing on canvas
Drawing on canvas
 
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
 
Working With Canvas
Working With CanvasWorking With Canvas
Working With Canvas
 
Fasten RWD Development with Sass
Fasten RWD Development with SassFasten RWD Development with Sass
Fasten RWD Development with Sass
 
Web Vector Graphics
Web Vector GraphicsWeb Vector Graphics
Web Vector Graphics
 
Sass & Compass (Barcamp Stuttgart 2012)
Sass & Compass (Barcamp Stuttgart 2012)Sass & Compass (Barcamp Stuttgart 2012)
Sass & Compass (Barcamp Stuttgart 2012)
 

Kürzlich hochgeladen

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 

Kürzlich hochgeladen (20)

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

Canvas

  • 2. /*** CANVAS* */(function() {var my = {};my.name = 'Stijn Van Minnebruggen';my.work = 'Multimedia developer at These Days Antwerp';my.hobbies = 'Passionate about photography, graphic design, typography and music.';my.tweets = 'twitter.com/donotfold';my.website = 'donotfold.be';
  • 3. varabout = {};about.canvas= 'The Canvas element allows dynamic rendering of 2D shapes and bitmap images in the browser'; about.html5 = 'Canvas is part of HTML5 and introduced in 2004';about.how= 'The shapes and images that are drawn on the canvas are scriptable with Javascript';
  • 4. <canvas width="300" height="250"> This text is displayed if your browser does not support canvas. </canvas>
  • 5. <canvas id="myFirstCanvas" width="300" height="250"> This text is displayed if your browser does not support canvas. </canvas> <p><a href="#" onclick="drawIt();">Click me to draw rectangle</a></p> <script> function drawIt() {varmyFirstCanvas = document.getElementById("myFirstCanvas");var context = myFirstCanvas.getContext("2d");context.fillStyle = 'rgb(255,0,0)';context.fillRect(50, 25, 100, 150); } </script>
  • 6. // basic context methodscontext.fillStyle = "property"; // css color, pattern or gradient (default: black)context.fillRect(x, y, width, height); // draws a rectangle with the current fillStylecontext.strokeStyle = "property"; // allows same properties as fillStylecontext.strokeRect(x, y, width, height); // draws a rectangle with the current strokeStyle(no fill)context.clearRect(x, y, width, height); // clears the pixels in the specified rectangle
  • 7. // strokes // 1. define start point // 2. define end point // 3. set optional styles // 4. draw linecontext.moveTo(x, y); // move the pencil to the starting pointcontext.lineTo(x, y); // imagine a line from the starting point to this ending pointcontext.strokeStyle = "property"; // set same style properties as before if you want context.stroke(); // draw the actual line
  • 9. // gradients // fill or stroke styles allow gradientsvarmyLinearGradient = context.createLinearGradient(x0, y0, x1, y1);varmyRadialGradient = context.createRadialGradient(x0, y0, r0, x1, y1, r1); // make as many color stops as you like, between 0 and 1myLinearGradient.addColorStop(0, "black"); myLinearGradient.addColorStop(1, "white"); // add the gradient to the style propertycontext.fillStyle= myLinearGradient;
  • 10. // imagesvarmyImage = new Image();myImage.src = "img/html5.png";myImage.onload = function() {context.drawImage(myImage, 0, 0); };
  • 11. // event listenerscanvas.addEventListener("click", drawIt, false); function drawIt(e) {varcursorPosition = getCursorPosition(e);var x = cursorPosition.x;var y = cursorPosition.y;var w = x+10;var h = y+10;context.fillRect(x, y, w, h); } function getCursorPosition(e) {var x, y; if(e.pageX != undefined && e.pageY != undefined) { x = e.pageX; y = e.pageY; } else { x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop; } x -= canvas.offsetLeft; y -= canvas.offsetTop; return { "x": x, "y": y };}
  • 12. // animationvarmyIterval = setInterval(function() {drawIt(); }, 500);
  • 13. // safer animationvarmyTimeout = setTimeout(loop, 500);var loop = function() {drawIt();clearTimeout(myTimeout);myTimeout = setTimeout(loop, 500);});
  • 14. // best animationwindow.requestAnimFrame= (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(/* function */ callback, /* DOMElement */ element){window.setTimeout(callback, 1000 / 60); }; })();
  • 15. // TUTORIALS http://diveintohtml5.org/canvas.html // base for this presentation http://billmill.org/static/canvastutorial/index.html // pong tutorial http://html5doctor.com/an-introduction-to-the-canvas-2d-api/ // TOOLS http://blog.nihilogic.dk/2009/02/html5-canvas-cheat-sheet.html // cheat sheet http://visitmix.com/labs/ai2canvas/ // AI to Canvas // DEMOS http://www.20thingsilearned.com // good integration http://www.frameratefest.com // canvas contest http://demo.thesedays.com // background effect http://easeljs.com// game demos http://soulwire.co.uk/hello // experiments http://9elements.com/io/projects/html5/canvas/})();