HTML5 Canvas
This is Sample Code for HTML Animation.
getContext():-this is used to draw 2d graphics
<!DOCTYPE html>
<html>
<title>HTML Drawing using Canvas</title>
<head>
<!-- After title -- Style Comes -->
<style>
#canv{
border:solid 2px red;
}
</style>
</head>
<body>
<canvas id="canv" width="500" height="300"></canvas>
<script>
let canvas=document.getElementById("canv");
let graphic=canvas.getContext("2d");
console.log(graphic);
graphic.font="40px courier";
graphic.fillText("Pranay Canvas",50,50);
graphic.strokeStyle="orange";
</script>
</body>
</html>
_________________________________________________________________________________
Output of Above code
_________________________________________________________________________________
Now if you want to color the Text then use method " graphic.strokeText"
<!DOCTYPE html>
<html>
<title>HTML Drawing using Canvas</title>
<head>
<!-- After title -- Style Comes -->
<style>
#canv{
border:solid 2px red;
}
</style>
</head>
<body>
<canvas id="canv" width="500" height="300"></canvas>
<script>
let canvas=document.getElementById("canv");
let graphic=canvas.getContext("2d");
console.log(graphic);
graphic.font="40px courier";
graphic.strokeStyle="orange";
graphic.strokeText("Pranay canvas",50,50);
console.log("message");
</script>
</body>
</html>
___________________________________________________________________________
Drawing Rectangle in canvas
To draw shape in canvas , use below function
fillRect(x,y,width,height) x-axis , y axis will form the shape
fillstyle will fill the box with mentioned color
Sample Code
<!DOCTYPE html>
<html>
<title>HTML Drawing using Canvas</title>
<head>
<!-- After title -- Style Comes -->
<style>
#canv{
border:solid 2px red;
}
</style>
</head>
<body>
<canvas id="canv" width="500" height="300"></canvas>
<script>
let canvas=document.getElementById("canv");
let graphic=canvas.getContext("2d");
console.log(graphic);
graphic.fillStyle="orange";
graphic.fillRect(50,50,200,200);
</script>
</body>
</html>
_________________________________________________________________________________Output of above code
HTML Drawing using Canvas
_________________________________________________________________________________
strokeRect(x,y,width,height) x-axis , y axis will form the shape
strokeStyle will fill the border of the box with mentioned color
<!DOCTYPE html>
<html>
<title>HTML Drawing using Canvas</title>
<head>
<!-- After title -- Style Comes -->
<style>
#canv{
border:solid 2px red;
}
</style>
</head>
<body>
<canvas id="canv" width="500" height="300"></canvas>
<script>
let canvas=document.getElementById("canv");
let graphic=canvas.getContext("2d");
console.log(graphic);
graphic.strokeStyle="orange";
graphic.strokeRect(50,50,200,200);
</script>
</body>
</html>
______________________________________________________________________________
HTML5 ARC Tutorial
__________________________________________________________________________
<!DOCTYPE html>
<html>
<title>Arc tutorial by Pranay Tiwari</title>
<head>
<style>
#canva{
border:solid 2px red;
}
</style>
</head>
<body>
<canvas id="canva" width="300" height="300"></canvas>
<script>
let drawele=document.getElementById("canva");
let graphic=drawele.getContext("2d");
graphic.strokeStyle="red";
graphic.arc(150,150,150,0,2*Math.PI);
graphic.stroke();
</script>
</body>
</html>
_____________________________________________________________________________
________________________________________________________________________
getContext():-this is used to draw 2d graphics
<!DOCTYPE html>
<html>
<title>HTML Drawing using Canvas</title>
<head>
<!-- After title -- Style Comes -->
<style>
#canv{
border:solid 2px red;
}
</style>
</head>
<body>
<canvas id="canv" width="500" height="300"></canvas>
<script>
let canvas=document.getElementById("canv");
let graphic=canvas.getContext("2d");
console.log(graphic);
graphic.font="40px courier";
graphic.fillText("Pranay Canvas",50,50);
graphic.strokeStyle="orange";
</script>
</body>
</html>
_________________________________________________________________________________
Output of Above code
_________________________________________________________________________________
Now if you want to color the Text then use method " graphic.strokeText"
<!DOCTYPE html>
<html>
<title>HTML Drawing using Canvas</title>
<head>
<!-- After title -- Style Comes -->
<style>
#canv{
border:solid 2px red;
}
</style>
</head>
<body>
<canvas id="canv" width="500" height="300"></canvas>
<script>
let canvas=document.getElementById("canv");
let graphic=canvas.getContext("2d");
console.log(graphic);
graphic.font="40px courier";
graphic.strokeStyle="orange";
graphic.strokeText("Pranay canvas",50,50);
console.log("message");
</script>
</body>
</html>
___________________________________________________________________________
Drawing Rectangle in canvas
To draw shape in canvas , use below function
fillRect(x,y,width,height) x-axis , y axis will form the shape
fillstyle will fill the box with mentioned color
Sample Code
<!DOCTYPE html>
<html>
<title>HTML Drawing using Canvas</title>
<head>
<!-- After title -- Style Comes -->
<style>
#canv{
border:solid 2px red;
}
</style>
</head>
<body>
<canvas id="canv" width="500" height="300"></canvas>
<script>
let canvas=document.getElementById("canv");
let graphic=canvas.getContext("2d");
console.log(graphic);
graphic.fillStyle="orange";
graphic.fillRect(50,50,200,200);
</script>
</body>
</html>
_________________________________________________________________________________Output of above code
strokeRect(x,y,width,height) x-axis , y axis will form the shape
strokeStyle will fill the border of the box with mentioned color
<!DOCTYPE html>
<html>
<title>HTML Drawing using Canvas</title>
<head>
<!-- After title -- Style Comes -->
<style>
#canv{
border:solid 2px red;
}
</style>
</head>
<body>
<canvas id="canv" width="500" height="300"></canvas>
<script>
let canvas=document.getElementById("canv");
let graphic=canvas.getContext("2d");
console.log(graphic);
graphic.strokeStyle="orange";
graphic.strokeRect(50,50,200,200);
</script>
</body>
</html>
______________________________________________________________________________
HTML5 ARC Tutorial
__________________________________________________________________________
<!DOCTYPE html>
<html>
<title>Arc tutorial by Pranay Tiwari</title>
<head>
<style>
#canva{
border:solid 2px red;
}
</style>
</head>
<body>
<canvas id="canva" width="300" height="300"></canvas>
<script>
let drawele=document.getElementById("canva");
let graphic=drawele.getContext("2d");
graphic.strokeStyle="red";
graphic.arc(150,150,150,0,2*Math.PI);
graphic.stroke();
</script>
</body>
</html>
_____________________________________________________________________________
________________________________________________________________________
changine the value in above code with below one , arc will change
graphic.arc(150,150,150,0,1.5*Math.PI);
The arc() method creates an arc/curve (used to create circles, or parts of circles).
Tip: To create a circle with arc(): Set start angle to 0 and end angle to 2*Math.PI.
Center
arc(100,75,50,0*Math.PI,1.5*Math.PI)
Start angle
arc(100,75,50,0,1.5*Math.PI)
End angle
arc(100,75,50,0*Math.PI,1.5*Math.PI)JavaScript syntax: | context.arc(x,y,r,sAngle,eAngle,counterclockwise); |
---|
Sample code
<!DOCTYPE html>
<html>
<title>Arc tutorial by Pranay Tiwari</title>
<head>
<style>
#canva{
border:solid 2px red;
}
</style>
</head>
<body>
<canvas id="canva" width="300" height="300"></canvas>
<script>
let drawele=document.getElementById("canva");
let graphic=drawele.getContext("2d");
graphic.strokeStyle="red";
graphic.beginPath();
graphic.arc(150,150,150,0,2*Math.PI);
graphic.stroke();
graphic.beginPath();
graphic.strokeStyle="orange";
graphic.arc(150,150,130,0,2*Math.PI);
graphic.stroke();
graphic.strokeStyle="blue";
graphic.beginPath();
graphic.arc(150,150,100,0,2*Math.PI);
graphic.stroke();
graphic.strokeStyle="pink";
graphic.beginPath();
graphic.arc(150,150,50,0,2*Math.PI);
graphic.stroke();
</script>
</body>
</html>
Sample Code 2
<!DOCTYPE html>
<html>
<title>Arc tutorial by Pranay Tiwari</title>
<head>
<style>
#canva{
border:solid 2px red;
}
</style>
</head>
<body>
<canvas id="canva" width="300" height="300"></canvas>
<script>
let drawele=document.getElementById("canva");
let graphic=drawele.getContext("2d");
graphic.fillStyle="red";
graphic.beginPath();
graphic.arc(150,150,150,0,2*Math.PI);
graphic.fill();
graphic.beginPath();
graphic.fillStyle="orange";
graphic.arc(150,150,130,0,2*Math.PI);
graphic.fill();
graphic.fillStyle="blue";
graphic.beginPath();
graphic.arc(150,150,100,0,2*Math.PI);
graphic.fill();
graphic.fillStyle="pink";
graphic.beginPath();
graphic.arc(150,150,50,0,2*Math.PI);
graphic.fill();
</script>
</body>
</html>
Another sample HTML Animation code
<!DOCTYPE html>
<html>
<head>
<title>Animation by Pranay Tiwari</title>
<style>
#canva{
border:solid 2px red;
}
</style>
<body>
</head>
<canvas id="canva" width="300" height="300"></canvas>
<script>
let drawEle=document.getElementById("canva");
let graphic=drawEle.getContext("2d");
let x=50;
let y=50;
function animation()
{
console.log(graphic);
graphic.beginPath();
graphic.fillStyle="rgba(0,0,0,0.05)";
graphic.fillRect(0,0,drawEle.width,drawEle.height);
graphic.beginPath();
graphic.fillStyle="white";
graphic.arc(x,y,50,0,2*Math.PI);
x++;
if (x>=250)
{
x=250;
y++;
}
graphic.fill();
}
setInterval("animation()",50);
</script>
</body>
</html>
Comments
Post a Comment