Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript program for drawing curvs

Status
Not open for further replies.

vladibo

Programmer
Sep 14, 2003
161
CA
This is JavaScript program for drawing curvs:
Code:
 <html>
 
<head>
 
  <title>Test</title>
	<style>
	.PE{
		position:absolute;
		width:1px;
		height:1px;
		background-color:#EDEDED;
	}
	.PEMark{
		position:absolute;
		width:1px;
		height:1px;	
		background-color:#FF0000;
	}
	.PEAxis{
		position:absolute;
		width:1px;
		height:1px;	
		background-color:#990000;
	}
	</style>
 
  <script>
	function Test(){
		var dr = new JsDrawer("Container",160,160,-80,80);
		dr.DrawCanvas();
		dr.DrawLine();
	}
	
	
	JsDrawer.prototype.DrawCanvas = JsDrawer_DrawCanvas;
	JsDrawer.prototype.DrawLine = JsDrawer_DrawLine;
	JsDrawer.prototype.Equation = JsDrawer_Equation;
	function JsDrawer(sId,iNumRows,iNumCols,iStartX,iStartY){
		this.Id = sId;
		this.InitX = 50;
		this.InitY = 50;
		this.NumRows = iNumRows + this.InitX;
		this.NumCols = iNumCols + this.InitY;
		this.StartX  = iStartX;
		this.StartY  = iStartY;
		this.Container = document.getElementById(this.Id);
		this.StartTime = null;
		this.EndTime = null
	}

//----------------------- EQUATATION IS DEFINED HERE:
	function JsDrawer_Equation(x){
		
		var y = x * x;
		
		return y;
	}
//---------------------------------------------------
	function JsDrawer_DrawLine(){
		var x = this.StartX;
		for(var i=this.InitX; i<this.NumCols; i += 1){
			x += 1;

			var y = this.Equation(x);

			y = (y > 0)?Math.floor(y):Math.ceil(y);
			
			var point = document.getElementById("E_" + x + "_" + y);
			if(point != null) point.className = "PEMark";
		}
		this.EndTime = new Date().getTime();
window.status = "End, time taken: " + (this.EndTime - this.StartTime);
	}
	function JsDrawer_DrawCanvas(){
		var str = "";
		this.StartTime = new Date().getTime();
window.status = "Please wait ...";
		var x = this.StartX;
		for(var i=this.InitX; i<this.NumCols; i += 1){
			var y = this.StartY;
			for(var j=this.InitY; j<this.NumRows; j += 1){
				var currSpan = document.createElement('span');
				currSpan.id = "E_" + x + "_" + y;
				currSpan.style.left = i+"px";
				currSpan.style.top = j+"px";
				currSpan.className = (x==0 || y==0)?"PEAxis":"PE";
				this.Container.appendChild(currSpan);
				y -= 1;
			}
			x += 1;
		}
		var endTimeFirstStage = new Date().getTime();
//prompt(',this.Container.innerHTML);		
window.status = "Elements created, it took this time: " + (endTimeFirstStage - this.StartTime);
	}
	
	
	
	
  </script>
 
</head>
 
<body>

<div id="Container" style="left:0;top:0"></div>

<br><br><br><br><br><br><br><br><br><br><br><br>

<hr>

<form>
<input type="button" value="Try" onclick="Test()">
</form>
 
</body>
 
</html>
 
When it comes to drawing with JavaScript, I recommend using JavaScript to manipulate an SVG image.

Adam
 
What is SVG image?
 
Just a friendly note to anyone wanting to run this script (which I can't believe the original poster did not mention out of courtesy):

When run, this script will appear to lock your browser up for a good 3.5 minutes (just under what it took on my 3GhZ P4 using Firefox) while it creates the canvas. Oh... and (again, this was running under Firefox) it will increase your memory consumption by a good 40Mb.

Then, it will plot about 8 points on that canvas.

vladibo,

Why oh why oh why are you creating every pixel of the canvas as a new DIV? It is pointless, it is a waste of browser (and user time), and a waste of system resources. Why not just have 1 div for the canvas, 1 for the horizontal axis, and 1 for the vertical axis. That would take all of 1 second to draw, instead of 3.5 minutes. Then, you would simply add absolutely positioned divs for your individial pixels.

Hope this helps,
Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Mine with IE takes 10 seconds. I didn't try with Mozzila....

Yes you are right I should not be creating every pixel, I'll fix that when next time I feel bored enough at work :)
 
My idea was to create something that is drawing images then I simplified it for curvs, that's why I draw every pixel.
 
Waw I just tried that with Mozzila.

DO NOT USE THIS PROGRAM WITH MOZZILA

It takes minutes to run. With IE it takes 6 secs. You can think about this as a performance test and the Mozzila has failed
 
> What is SVG image?

Click on the link I posted to find out.

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top