Saturday, January 21, 2012

HTML5 canvas: Draw Line on Path

Example of HTML5 canvas, draw line on path:
HTML5 canvas: Draw Line on Path
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mobile-Web-App: HTML5 Canvas</title>
<script type="text/javascript">
function loadcanvas(){
var canvas;
var context;

//get element by ID of "mycanvas"
canvas = document.getElementById("mycanvas");

//create a "2d" context object
context = canvas.getContext("2d");

drawPath(context);
}

function drawPath(c){
c.strokeStyle = "#ff0000";
c.lineWidth = 10;
c.beginPath();
c.moveTo(0, 0);
c.lineTo(100, 0);
c.lineTo(100, 100);
c.lineTo(0, 100);
c.lineTo(0, 0);
c.stroke();
c.closePath();

c.strokeStyle = "#00ff00";
c.lineWidth = 1;
c.beginPath();
c.moveTo(50, 50);
c.lineTo(250, 50);
c.lineTo(250, 250);
c.lineTo(50, 250);
c.lineTo(50, 50);
c.stroke();
c.closePath();

c.strokeStyle = "#0000ff";
c.lineWidth = 20;
c.beginPath();
c.moveTo(200, 200);
c.lineTo(300, 200);
c.lineTo(300, 300);
c.lineTo(200, 300);
c.lineTo(200, 200);
c.stroke();
c.closePath();

}

</script>
</head>
<body onload="loadcanvas();">
<h1>Mobile-Web-App: HTML5 canvas</h1>

<canvas id="mycanvas" style="border: 5px solid;" width="400" height="300">
Sorry! Your browser doesn't support Canvas.
</canvas>
</body>
</html>


No comments:

Post a Comment