Professional Documents
Culture Documents
To Create A Canvas
To Create A Canvas
To Print Hello
<body>
<canvas id="myCanvas" width="578" height="200"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
To draw line
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.beginPath(); // it is optional
context.moveTo(100, 150);
context.lineTo(450, 50);
context.stroke(); //t o make line visible
</script>
butt
Play it
round
Play it
square
context.moveTo(239, 150);
context.lineTo(289, 50);
context.lineTo(339, 150);
context.lineJoin = 'round';
context.stroke();
To draw rectangle
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
context.beginPath();
context.rect(100, 50, 200, 50);
context.fillStyle = 'yellow';
context.fill();
context.lineWidth = 7;
context.strokeStyle = 'green';
context.stroke();
</script>
To Draw Circle
context.arc(x,y,r,sAngle,eAngle,counterclockwise);
In circle sangle is always 0 while eAngle is always 2*MATH.PI
context.beginPath();
context.arc(100,50, 50, 0, 2 * Math.PI);
context.fillStyle = 'green';
context.fill();
context.lineWidth = 5;
context.strokeStyle = '#003300';
context.stroke();
To Draw semicircle
context.arc(288, 75, 70, 0, Math.PI, false);
Linear
2.
Radial
A linear gradient changes color with a linear pattern, either horizontally, vertically,
or diagonally.
A radial gradient change color with a circular pattern, changing color from the inside
and out.
Linear Gradients
As mentioned earlier, a linear gradient changes color using a linear pattern. A linear
gradient is created using the 2D Context function createLinearGradient(). Here is an
example:
varcanvas=document.getElementById("ex1");
varcontext=canvas.getContext("2d")
varx1=0;
vary1=0;
varx2=100;
vary2=0;
varlinearGradient1=context.createLinearGradient(x1,y1,x2,y2);
The createLinearGradient() function takes 4 parameters: x1, y1, x2, y2. These 4
parameters determine the direction and extension of the gradient pattern. The gradient
extends from the first point x1, y1 to the second point x2, y2.
A horizontal gradient is created by only varying the parameter values on the x-axis (for
x1 and x2), like this:
varx1=0;
vary1=0;
varx2=100;
vary2=0;
varlinearGradient1=context.createLinearGradient(x1,y1,x2,y2);
A vertical gradient is created by only varying the parameter values on the y-axis (for y1
and y2), like this:
varx1=0;
vary1=0;
varx2=0;
vary2=100;
varlinearGradient1=context.createLinearGradient(x1,y1,x2,y2);:
varx1=0;
vary1=0;
varx2=100;
vary2=100;
varlinearGradient1=context.createLinearGradient(x1,y1,x2,y2);
Color Stops
The examples above did not show what color the gradient was given. In order to set
the colors of a gradient you use the addColorStop() function on the gradient object.
Here is an example:
varlinearGradient1=context.createLinearGradient(0,0,100,0);
linearGradient1.addColorStop(0,'rgb(255,0,0)');
linearGradient1.addColorStop(1,'rgb(0,0,0)');
This examples adds the color blue which is located in the middle of the gradient.
The gradient will thus change smoothly from red, to blue, and then to black.
Example:
varcanvas=document.getElementById("ex2");
varcontext=canvas.getContext("2d");
varlinearGradient1=context.createLinearGradient(0,0,100,0);
linearGradient1.addColorStop(0,'rgb(255,0,0)');
linearGradient1.addColorStop(0.5,'rgb(0,0,255)');
linearGradient1.addColorStop(1,'rgb(0,0,0)');
context.fillStyle=linearGradient1;
context.fillRect(10,10,100,100);
varlinearGradient2=context.createLinearGradient(125,0,225,0);
linearGradient2.addColorStop(0,'rgb(255,0,0)');
linearGradient2.addColorStop(0.5,'rgb(0,0,255)');
linearGradient2.addColorStop(1,'rgb(0,0,0)');
context.strokeStyle=linearGradient2;
context.strokeRect(125,10,100,100);
A radial gradient is defined by 2 circles. Each circle has a center point and a radius.
Here is a code example:
varx1=100;//xof1.circlecenterpoint
vary1=100;//yof1.circlecenterpoint
varr1=30;//radiusof1.circle
varx2=100;//xof2.circlecenterpoint
vary2=100;//yof2.circlecenterpoint
varr2=100;//radiusof2.circle
varradialGradient1=
context.createRadialGradient(x1,y1,r1,x2,y2,r2);
radialGradient1.addColorStop(0,'rgb(0,0,255)');
radialGradient1.addColorStop(1,'rgb(0,255,0)');
context.fillStyle=radialGradient1;
context.fillRect(10,10,200,200);
Shadows
Negative values means that the shadow is drawn to the left of (x) and above (y) the shape.
varcanvas=document.getElementById("ex1");
varcontext=canvas.getContext("2d");
context.shadowOffsetX=10;
context.shadowOffsetY=10;
context.shadowBlur=4;
context.shadowColor="#666666";//orusergb(red,green,blue)
context.fillStyle="#000000";
context.fillRect(10,10,50,50);
context.fillStyle="#000066";
context.font="30pxArial";
context.fillText("HTML5CanvasShadow",10,120);