I'm try to draw objects using canvas and JS on HTML5. for that I've 2 file index.html with the follwing :
<html>
<body>
<canvas id="myCanvas"></canvas>
<script type="text/javascript" src="script.js">
</script>
</body>
</html>
As far as I understood I'm callign here the script.js with the following :
var myCanvas = document.getElementById("myCanvas");
myCanvas.width = 500;
myCanvas.height = 500;
var ctx = myCanvas.getContext("2d");
function drawLine(ctx, startX, startY, endX, endY){
ctx.beginPath();
ctx.moveTo(startX,startY);
ctx.lineTo(endX,endY);
ctx.stroke();
}
function drawArc(ctx, centerX, centerY, radius, startAngle, endAngle){
ctx.beginPath();
ctx.arc(centerX, centerY, radius, startAngle, endAngle);
ctx.stroke();
}
function drawPieSlice(ctx,centerX, centerY, radius, startAngle, endAngle, color ){
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo(centerX,centerY);
ctx.arc(centerX, centerY, radius, startAngle, endAngle);
ctx.closePath();
ctx.fill();
}
this.drawLine(_ctx,100,100,200,200);
this.drawArc(_ctx, 150,150,150, 0, Math.PI/3);
this.drawPieSlice(_ctx, 150,150,150, Math.PI/2, Math.PI/2 + Math.PI/4, '#ff0000');
Opening the index.html I can't see a line or an arc, so my question is what I'm missing here ?
thanks in advance !
_ctxin your function calls but you defined it asctx. Could that be it?this.at the start isn't necessary here; in your code,thisrefers to thewindowobject, which is the default context anyway.