HTML5 Canvas (Continue)

Understanding Coordinates

The fillRect method above had the parameters (0,0,150,75).

This means: Draw a 150×75 rectangle on the canvas, starting at the top left corner (0,0).

The canvas’ X and Y coordinates are used to position drawings on the canvas.

 

More Canvas Examples

Below are more examples of drawing on the canvas element:

 - Line

<!DOCTYPE html>
<html>
<body>

<canvas id=”myCanvas” width=”200″ height=”100″ style=”border:1px solid #c3c3c3;”>
Your browser does not support the canvas element.
</canvas>

<script type=”text/javascript”>

var c=document.getElementById(“myCanvas”;);
var cxt=c.getContext(“2d”;);
cxt.moveTo(10,10);
cxt.lineTo(150,50);
cxt.lineTo(10,50);
cxt.stroke();

</script>

</body>
</html>

 

- Circle

<!DOCTYPE html>
<html>
<body>

<canvas id=”myCanvas” width=”200″ height=”100″ style=”border:1px solid #c3c3c3;”>
Your browser does not support the canvas element.
</canvas>

<script type=”text/javascript”>

var c=document.getElementById(“myCanvas”;);
var cxt=c.getContext(“2d”;);
cxt.fillStyle=”#FF0000″;
cxt.beginPath();
cxt.arc(70,18,15,0,Math.PI*2,true);
cxt.closePath();
cxt.fill();

</script>

</body>
</html>

 

- Gradient

<!DOCTYPE html>
<html>
<body>

<canvas id=”myCanvas” width=”200″ height=”100″ style=”border:1px solid #c3c3c3;”>
Your browser does not support the canvas element.
</canvas>

<script type=”text/javascript”>

var c=document.getElementById(“myCanvas”;);
var cxt=c.getContext(“2d”;);
var grd=cxt.createLinearGradient(0,0,175,50);
grd.addColorStop(0,”#FF0000″;);
grd.addColorStop(1,”#00FF00″;);
cxt.fillStyle=grd;
cxt.fillRect(0,0,175,50);

</script>

</body>
</html>

- Image

<!DOCTYPE html>
<html>
<body>

<canvas id=”myCanvas” width=”200″ height=”100″ style=”border:1px solid #c3c3c3;”>
Your browser does not support the canvas element.
</canvas>

<script type=”text/javascript”>

var c=document.getElementById(“myCanvas”;);
var cxt=c.getContext(“2d”;);
var img=new Image();
img.src=”img_flwr.png”;
cxt.drawImage(img,0,0);

</script>

</body>
</html>

HTML5 <canvas> Tag

Tag Description
<canvas> Defines graphics

 

 

Tagged with:
 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

[+] kaskus emoticons