Jump to content

What did I wrong in scoping- not working?


 Share

Go to solution Solved by eaglehopes,

Recommended Posts

My simple html page and script is like this :

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="utf-8" />
	<title>Online canvas program</title>
</head>

<body>
<style type="text/css">
.GraphicArea {
	background-color: #ceccec;
	border-top-left-radius:10px;
	border-top-right-radius:10px;
	border-bottom-left-radius:10px;
	border-bottom-right-radius:10px;
}
</style>

<script type="text/javascript">
var gui = document;
var canvasElement = gui.getElementById("GraphicArea");
var icerik = canvasElement.getContext("2d");
var canvasWidth,canvasHeight;
  
if (canvasElement.getContext) {
  // canvas supported
  gui.getElementById("Canvas Result").innerHTML = "supported";
} else {
  // canvas-unsupported code here
  gui.getElementById("Canvas Result").innerHTML = "not supported";
}

function setCanvasWidth(w) {
canvasWidth=w;
canvasElement.style.width=canvasWidth+"px";
}

function setCanvasHeight(h) {
canvasHeight=h;
canvasElement.style.height=canvasHeight+"px";
}

</script>


<table>

<tr>
<td>
<p id="Canvas Result"></p>
</td>
</tr>

<tr>
<td>
<form>
	<label for="canvasWidth">Canvas width :</label>
	<input type="number" id="width" name="quantity" min="300" max="1000" value="350" onchange="setCanvasWidth(this.value)"></input><br>
	
	<label for="canvasWidth">Canvas heigth :</label>
	<input type="number" id="width" name="quantity" min="300" max="1000" value="350" onchange="setCanvasHeight(this.value)"></input><br>
	<input type="submit" value="Apply" ><br>
</form>
</td>
  
<td>
<canvas class="GraphicArea" id="GraphicArea"  alt="Graphics Area for drawing cross section">
</canvas>
</td>
  
</tr>

</table>

</body>
</html>

I tried to use gui,canvasElement,canvasWidth and canvasHeigth etc. variables as global scope. However, code does not work. Only work when I define all variables inside the functions again. Why? Where am I doing wrong?

Link to comment
Share on other sites

Posted (edited)

@maxxdI tried what you said as :

<script type="text/javascript">
window.onload = function () {
	draw();
}

var gui = document;
var canvasElement = gui.getElementById("GraphicArea");
var icerik = canvasElement.getContext("2d");
var canvasWidth=300;
var canvasHeight=300;
  ...
  
</script>

It worked like a charm. Thanks.

Edited by eaglehopes
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.