Mohammed Hammed Mohammed Hammed - 6 months ago 47
Javascript Question

Little issue with canvas

I'm trying to run this code, where the constructor is the ball function, and the

is the prototype which should include the code in ball.

Can't seem to display the drawing of the ball.

var canvas = document.getElementById('ok');
var d = canvas.getContext('2d');
function Ball() {
ok = 100;
okk = 100;

var circle = function(x, y, radius, fillCircle){
d.arc(x, y, radius, 0, Math.PI * 2, false);
} else{
d.stroke(); } };

Ball.prototype.draw = function() {
d.lineWidth = 2;
d.strokeStyle = "black";
d.fillStyle = "red";
circle(ok, okk, 30, true); };



When using the prototypes in JS, you still would need to instantiate a new instance of the object you're modeling.

So your code is fine, except the last line; instead you want to create a new ball and then call the member function draw on it, like so:

var ball = new Ball();