Canvas is a new HTML element because its terminal figure suggests, it build a canvas on which a developer can paint anything.Canvas was initially introduced by Apple for use inside their own Mac OS X WebKit component in 2004.With the help of javascript users can draw in builds and deliver images on canvas.If javascript is disable in a browser even so, the canvas elemet will show its dawdle content(Content inside the tag instead of tag itself)
Since the canvas gives developers an open arena to act with, it can quite useful to draw custom interfacesuch as game.
The canvas tag has been supported since firefox 1.5, Chrome1.0, Opera 9.0, Safari 2. and IE 9.0
CANVAS TAG
This is a Bare tag to display content.This tag just define width and hight..Here a simple examble to define canvas in an HTML document.
Note:Default width 300 pixel height 150 pixel
Verfying canvas supprot
JavaScript:
Try it yourself »
Using strokeText():
JavaScript:
Try it yourself »
There are two different types of gradients:
The addColorStop() method specifies the color stops, and its position along the gradient. Gradient positions can be anywhere between 0 to 1.
To use the gradient, set the fillStyle or strokeStyle property to the gradient, and then draw the shape, like a rectangle, text, or a line.
Using createLinearGradient():
JavaScript:
Try it yourself »
Using createRadialGradient():
JavaScript:
Try it yourself »
JavaScript:
Try it yourself »
Since the canvas gives developers an open arena to act with, it can quite useful to draw custom interfacesuch as game.
The canvas tag has been supported since firefox 1.5, Chrome1.0, Opera 9.0, Safari 2. and IE 9.0
CANVAS TAG
This is a Bare tag to display content.This tag just define width and hight..Here a simple examble to define canvas in an HTML document.
<canvas id="example" width="200" height="200"> This text is displayed if your browser does not support HTML5 Canvas. </canvas>
Note:Default width 300 pixel height 150 pixel
Verfying canvas supprot
function isCanvasSupported(){
var elem = document.createElement('canvas');
return !!(elem.getContext && elem.getContext('2d'));
}
Draw Onto The Canvas With JavaScript
All drawing on the canvas must be done inside a JavaScript:Example
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);
</script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);
</script>
Example explained:
First, find the <canvas> element:
Then, call its getContext() method (you must pass the string "2d" to the
getContext() method):
The getContext("2d") object is a built-in HTML5 object, with many properties and methods for
drawing paths, boxes, circles, text, images, and more.
The next two lines draws a red rectangle:
The fillStyle property can be a CSS color, a gradient, or a pattern. The
default fillStyle is #000000 (black).
The fillRect(x,y,width,height) method draws a rectangle filled with the current fill style.
First, find the <canvas> element:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
The next two lines draws a red rectangle:
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);
ctx.fillRect(0,0,150,75);
The fillRect(x,y,width,height) method draws a rectangle filled with the current fill style.
Canvas - Text
To draw text on a canvas, the most important property and methods are:- font - defines the font properties for text
- fillText(text,x,y) - Draws "filled" text on the canvas
- strokeText(text,x,y) - Draws text on the canvas (no fill)
Example
Write a 30px high filled text on the canvas, using the font "Arial":JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="30px Arial";
ctx.fillText("Hello World",10,50);
var ctx=c.getContext("2d");
ctx.font="30px Arial";
ctx.fillText("Hello World",10,50);
Try it yourself »
Example
Write a 30px high text (no fill) on the canvas, using the font "Arial":JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="30px Arial";
ctx.strokeText("Hello World",10,50);
var ctx=c.getContext("2d");
ctx.font="30px Arial";
ctx.strokeText("Hello World",10,50);
Try it yourself »
Canvas - Gradients
Gradients can be used to fill rectangles, circles, lines, text, etc. Shapes on the canvas are not limited to solid colors.There are two different types of gradients:
- createLinearGradient(x,y,x1,y1) - Creates a linear gradient
- createRadialGradient(x,y,r,x1,y1,r1) - Creates a radial/circular gradient
The addColorStop() method specifies the color stops, and its position along the gradient. Gradient positions can be anywhere between 0 to 1.
To use the gradient, set the fillStyle or strokeStyle property to the gradient, and then draw the shape, like a rectangle, text, or a line.
Using createLinearGradient():
Example
Create a linear gradient. Fill rectangle with the gradient:JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
// Create gradient
var grd=ctx.createLinearGradient(0,0,200,0);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
// Fill with gradient
ctx.fillStyle=grd;
ctx.fillRect(10,10,150,80);
var ctx=c.getContext("2d");
// Create gradient
var grd=ctx.createLinearGradient(0,0,200,0);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
// Fill with gradient
ctx.fillStyle=grd;
ctx.fillRect(10,10,150,80);
Try it yourself »
Example
Create a radial/circular gradient. Fill rectangle with the gradient:JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
// Create gradient
var grd=ctx.createRadialGradient(75,50,5,90,60,100);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
// Fill with gradient
ctx.fillStyle=grd;
ctx.fillRect(10,10,150,80);
var ctx=c.getContext("2d");
// Create gradient
var grd=ctx.createRadialGradient(75,50,5,90,60,100);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
// Fill with gradient
ctx.fillStyle=grd;
ctx.fillRect(10,10,150,80);
Try it yourself »
Canvas - Images
To draw an image on a canvas, we will use the following method:- drawImage(image,x,y)
Image to use:
Example
Draw the image onto the canvas:JavaScript:
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var img=document.getElementById("scream");
ctx.drawImage(img,10,10);
var ctx=c.getContext("2d");
var img=document.getElementById("scream");
ctx.drawImage(img,10,10);
Try it yourself »
NOTE: This article is Copied from
http://www.w3schools.com/html/html5_canvas.asp
You can visit this page for further detailes
A detailed tutorial about html canvas 5 will post later
W3 School
0 comments
Posts a comment