FAQ
It is currently Fri Apr 28, 2017 1:54 pm


Author Message
jmans
Post  Post subject: JavaScript help  |  Posted: Mon Mar 13, 2017 5:53 am

Joined: Mon Mar 13, 2017 5:48 am
Posts: 1

Offline
I seem to have a bug in my script. Can anyone tell me what I am missing to make my function fire. here is the javascript


/ ** initializations

// reference the canvas element
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');

// initialize variables
var blobArray = [];



// ** functions

function Init(canvas); {

for (x = 0; x<100; x++) {

var blob = {
x: canvas.width/2,
y: canvas.height/2,
size: Math.random() * 50,
xSpeed: Math.random() * 10 - Math.random() * 10,
ySpeed: Math.random() * 10 - Math.random() * 10,
color: '#' + Math.floor(Math.random()*16777215).toString(16)
}

blobArray.push(blob);
}

}


function MainLoop() {

// update the canvas
DrawCanvas();

// repeat this function every 10 microseconds
window.setTimeout(MainLoop, 10);

}


function DrawCanvas() {

context.fillStyle = "black";
context.fillRect(0, 0, canvas.width, canvas.height);

for (var i = 0; i < blobArray.length; i++) {

blob = blobArray[i];
context.fillStyle = blob.color;
context.fillRect(blob.x, blob.y, blob.size, blob.size);

blob.x = blob.x + blob.xSpeed;
blob.y = blob.y + blob.ySpeed;

if (blob.x > canvas.width || blob.x < 0 || blob.y > canvas.height || blob.y < 0) {
blob.x = canvas.width/2;
blob.y = canvas.height/2;
blob.xSpeed = Math.random() * 10 - Math.random() * 10;
blob.ySpeed = Math.random() * 10 - Math.random() * 10;
}

}

}



// ** start

Init();


Top
scire
Post  Post subject: Re: JavaScript help  |  Posted: Wed Mar 15, 2017 3:55 am

Joined: Sat May 14, 2016 8:09 pm
Posts: 28

Offline
I'll preface by saying that JavaScript is not my language.

However, a few things I saw:

1) / ** initializations

There is no ending comment to this. Shouldn't it be: /** initializers **/?

2) This is likely the source of all your issues:

function Init(canvas); {

There is a semicolon before the beginning brace. Should be: function Init(canvas) {

3) Init takes a parameter and is called without one


Top
Display posts from previous:  Sort by  
Print view

Who is online
Users browsing this forum: No registered users and 0 guests
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
Jump to:   
cron

Delete all board cookies | The team | All times are UTC


This free forum is proudly hosted by ProphpBB | phpBB software | Report Abuse | Privacy