Ask your own question, for FREE!
Mathematics 9 Online
OpenStudy (wwb00):

This won't be answered in CS. I have been writing some HTML canvas code in order to make a landscape. The code will not execute properly. What is my syntax error? Thanks, Benjamin var canvas = document.getElementById("mycanvas"); var ctx = canvas.getContext("2d"); var llocation = 600; var length = 360; var linewidth = 2; var testInt = length/(linewidth*2); var wColour = "#994c00"; for(i=0;i

OpenStudy (compassionate):

for(i=0;i<testInt+1;i++){

OpenStudy (wwb00):

I'll try it.

OpenStudy (wwb00):

some code got cut off. wait a sec.

OpenStudy (wwb00):

ctx.beginPath(); ctx.strokeStyle = wColour; ctx.lineWidth = linewidth; ctx.rect(llocation,300,length,250); ctx.stroke(); ctx.closePath(); var llocation = llocation + linewidth; var length = length - (linewidth*2); }

OpenStudy (wwb00):

It's still not working.

OpenStudy (compassionate):

No need for the { attached to the end. Try spacing it

OpenStudy (wwb00):

Ok

OpenStudy (compassionate):

for(i=0;i<testInt+1;i++){ Might help to change it to for(i=0;i<testInt+1;i++) {

OpenStudy (wwb00):

Still did'nt fix.

OpenStudy (wwb00):

Ill post all of my files.

OpenStudy (compassionate):

Hmm... @dan815 @goformit100 @mathmale

OpenStudy (wwb00):

OpenStudy (wwb00):

It won't let me post the other because of "forbidden" script

OpenStudy (compassionate):

You should try asking this question on Yahoo Answers

OpenStudy (wwb00):

I am.

OpenStudy (wwb00):

And stack overflow.

OpenStudy (anonymous):

Have you got any HTML to go with this? Saves me the trouble of making some to fit your script.

OpenStudy (wwb00):

I do, but it won't let me post it.

OpenStudy (anonymous):

Also, this is unnecessary: if(isEven(i)==true) { var wColour = "#994c00"; } else if(isEven(i)==false){ var wColour = "#B87333"; } There's no point evaluating isEven(i) == false because if you've already found that it's true then this must be false. Just get rid of "if(isEven(i)==false)"

OpenStudy (wwb00):

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="styles.css"> <link rel='shortcut icon' href='favicon.ico' type='image/x-icon'/> <title>Canvas</title> <style>#mycanvas {border: 1px solid #000;} </style> </head> <body> <div class="menubar"> <p><a href="landing.html" class="formatting">Home</a></p> <p><a href="rectangle.html" class="formatting">Rectangle</a></p> <p><a href="circle.html" class="formatting">Circle</a></p> </div> <h1 class="custom">Canvas Practice</h1> <article class="textbox"> <h1>House</h1> <canvas id="mycanvas" width=1000 height=600> <p> Your browser does not support the canvas app</p> </canvas> </article> <script src="house.js"></script> </body> </html>

OpenStudy (anonymous):

Thanks. Is your isEven function defined elsewhere? No need to post it if it is, just checking it does actually exist.

OpenStudy (wwb00):

It works now!!!

OpenStudy (wwb00):

Thank you all!!!

OpenStudy (wwb00):

I can't give two best responses...

OpenStudy (wwb00):

sorry Compassionate

OpenStudy (anonymous):

I was going to say, this code is working fine for me in JSFiddle: https://jsfiddle.net/4rhuf2kd/ For future reference, browser developing tools help massively when debugging JS. In Chrome, press F12 and you'll get the dev panel at the bottom. View the console and see what errors are there when you view your site, for me it said "Uncaught ReferenceError: isEven is not defined".

OpenStudy (wwb00):

Thanks!

OpenStudy (wwb00):

I have another problem now.

OpenStudy (wwb00):

This code won't run in a function.

OpenStudy (wwb00):

function drawHouse(){ for(i=0;i<testInt+1;i++) { function isEven(value) { if (value%2 == 0) return true; else return false; } if(isEven(i)==true) { var wColour = "#994c00"; } else if(isEven(i)==false){ var wColour = "#B87333"; } ctx.beginPath(); ctx.strokeStyle = wColour; ctx.lineWidth = linewidth; ctx.rect(llocation,300,length,250); ctx.closePath(); ctx.stroke(); var llocation = llocation + linewidth; var length = length - (linewidth*2); } }

OpenStudy (wwb00):

var canvas = document.getElementById("mycanvas"); var ctx = canvas.getContext("2d"); var llocation = 600; var length = 360; var linewidth = 2; var testInt = length/(linewidth*2); var wColour = "#994c00"; function drawBackground(){ ctx.beginPath(); ctx.strokeStyle = "#3e9aff"; ctx.lineWidth = 10; ctx.rect(0,0,1000,600); ctx.stroke(); ctx.fillStyle = "#3e9aff"; ctx.fill(); ctx.closePath(); } function drawHouse(){ for(i=0;i<testInt+1;i++) { function isEven(value) { if (value%2 == 0) return true; else return false; } if(isEven(i)==true) { var wColour = "#994c00"; } else if(isEven(i)==false){ var wColour = "#B87333"; } ctx.beginPath(); ctx.strokeStyle = wColour; ctx.lineWidth = linewidth; ctx.rect(llocation,300,length,250); ctx.closePath(); ctx.stroke(); var llocation = llocation + linewidth; var length = length - (linewidth*2); } } function drawGround(){ ctx.beginPath(); ctx.strokeStyle = "green"; ctx.lineWidth = 10; ctx.rect(0,550,1000,50); ctx.stroke(); ctx.fillStyle = "green"; ctx.fill(); ctx.closePath(); } drawBackground(); drawHouse(); drawGround();

OpenStudy (wwb00):

OpenStudy (wwb00):

There is nothing wrong in the terminal.

OpenStudy (anonymous):

Just curious, why do you define each function then run them straight away? Might as well save yourself the declaration and just have one long block of code - unless you need to call them specifically elsewhere or you've been asked to do it this way specifically.

OpenStudy (wwb00):

I wanted to have a functions reference, then a code that runs in a simplified manner. If this were to work, I would make local variables for the function.

OpenStudy (anonymous):

Okay, I understand. What exactly are you expecting this code to do? It's showing a blue rectangle on top of a green one for me.

OpenStudy (wwb00):

That's the background. It's to make a landscape.

OpenStudy (wwb00):

The house is not displaying

OpenStudy (okdutchman7):

Could please post this in Computer Science?

OpenStudy (wwb00):

There are very few people on that section. Thank you tom982

OpenStudy (anonymous):

Give me a second, I've given up with JSFiddle, going local now.

OpenStudy (wwb00):

Ok

OpenStudy (wwb00):

The house works perfectly not as a function.

OpenStudy (wwb00):

I just got a tip from another section that it may be a scope issue with wColour

OpenStudy (wwb00):

I fixed the scope, but it still won't draw the obj.

OpenStudy (wwb00):

function drawHouse(){ var wColour = "#994c00"; function isEven(value) { if (value%2 == 0) return true; else return false; } alert(wColour); for(i=0;i<testInt+1;i++) { if(isEven(i)==true) { var wColour = "#994c00"; } else if(isEven(i)==false){ var wColour = "#B87333"; } alert(wColour); ctx.beginPath(); ctx.strokeStyle = wColour; ctx.lineWidth = linewidth; ctx.rect(llocation,300,length,250); ctx.closePath(); ctx.stroke(); var llocation = llocation + linewidth; var length = length - (linewidth*2); } }

OpenStudy (wwb00):

remove alert(wColour) it was for diagnostic purposes.

OpenStudy (wwb00):

I fixed it. All the variables had the wrong scope!

OpenStudy (wwb00):

Thanks so much!

OpenStudy (wwb00):

function drawHouse(){ var wColour = "#994c00"; var llocation = 600; var length = 360; var linewidth = 2; var testInt = length/(linewidth*2); function isEven(value) { if (value%2 == 0) return true; else return false; } alert(wColour); for(i=0;i<testInt+1;i++) { if(isEven(i)==true) { var wColour = "#994c00"; } else if(isEven(i)==false){ var wColour = "#B87333"; } ctx.beginPath(); ctx.strokeStyle = wColour; ctx.lineWidth = linewidth; ctx.rect(llocation,300,length,250); ctx.stroke(); ctx.closePath(); var llocation = llocation + linewidth; var length = length - (linewidth*2); }

OpenStudy (anonymous):

Great, this was driving me mad.

OpenStudy (wwb00):

I gave you the code you you can see it work. As we both have wanted.

OpenStudy (anonymous):

Ah I see the problem now, and sorry but your fix isn't very tidy. In Javascript, if you want to access a variable outside the scope of the function you have to make sure you don't use 'var'. Simply removing var from the variables you used in drawHouse() fixes this: https://jsfiddle.net/z0ranjdh/

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!