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!

code only code that draw a circle ? 8

Status
Not open for further replies.

NEVERSLEEP

Programmer
Apr 14, 2002
667
CA
can someone show me how to create a circle
NOT using some image tricks...
thanks ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
Hi mate,

This may or may not do what you want..
Code:
<SCRIPT LANGUAGE=&quot;JavaScript1.2&quot;>
function Circle (cx, cy, radius, color, dotSize, da) {
  this.id = Circle.cnt++;
  this.cx = cx || 100;
  this.cy = cy || 100;
  this.radius = radius || 100;
  this.color = color || 'black';
  this.dotSize = dotSize || 1;
  this.da = da || Math.PI / 180;
  this.left = this.cx - radius;
  this.top = this.cy - radius;
  this.width = 2 * this.radius + this.dotSize;
  this.height = 2 * this.radius + this.dotSize;
  this.relX = this.radius;
  this.relY = this.radius;
  if (document.layers) {
    var l = this.layer = new Layer (this.width);
    l.left = this.left;
    l.top = this.top; 
    l.clip.width = this.width;
    l.clip.height = this.height;
    l.visibility = 'show';
  }
  else if (document.all || document.getElementById) {
    var html = '';
    html += '<DIV ID=&quot;Circle' + this.id + '&quot;';
    html += ' STYLE=&quot;position: absolute;';
    html += ' left: ' + this.left + 'px; top: ' + this.top + 'px;';
    html += ' width: ' + this.width + 'px; height: ' + this.height 
+ 'px;';
    html += '&quot;';
    html += '>';
    html += '<\/DIV>';
    if (document.all) {
      document.body.insertAdjacentHTML('beforeEnd', html);
      this.layer = document.all['Circle' + this.id];
    }
    else if (document.getElementById) {
      var range = document.createRange();
      range.setStartAfter(document.body.lastChild);
      var docFrag = range.createContextualFragment(html);
      document.body.appendChild(docFrag);
      this.layer = document.getElementById('Circle' + this.id);
    }
  }
  for (var a = 0; a < Math.PI * 2; a += this.da) {
    var x = this.relY + this.radius * Math.sin(a);
    var y = this.relY + this.radius * Math.cos(a);
    if (document.layers) {
      l = new Layer (this.dotSize, this.layer);
      l.clip.width = this.dotSize;
      l.clip.height = this.dotSize;
      l.bgColor = this.color;
      l.left = x;
      l.top = y;
      l.visibility = 'inherit';
    }
    else if (document.all || document.getElementById) {
      var html = '';
      html += '<SPAN';
      html += ' STYLE=&quot;position: absolute; visibility: inherit;';
      html += ' left: ' + x + 'px; top: ' + y + 'px;';
      html += ' width: ' + this.dotSize + 'px; height: ' + this.dotSize 
+ 'px;';
      html += ' background-color: ' + this.color + ';';
      html += document.all ? 
        'clip: rect (0 ' + this.dotSize + ' ' + this.dotSize + ' 0);' :
        '';
      html += '&quot;';
      html += '>';
      html += '<\/SPAN>';
      if (document.all) {
        this.layer.insertAdjacentHTML('beforeEnd', html);
      }
      else if (document.getElementById) {
        var range = document.createRange();
        range.setStartAfter(this.layer);
        var docFrag = range.createContextualFragment(html);
        this.layer.appendChild(docFrag);
      }
    }
  }
}
Circle.cnt = 0;  
</SCRIPT>
<SCRIPT>
var circle1, circle2;
function init () {
  var cx = window.innerWidth ?
    parseInt(window.innerWidth / 2) :
    parseInt(document.body.clientWidth / 2);
  var cy = window.innerHeight ?
    parseInt(window.innerHeight / 2) :
    parseInt(document.body.clientHeight / 2);
  circle1 = new Circle (cx, cy, 100, 'lime');
  circle2 = new Circle (cx, cy, 150, 'orange', 2);
}
</SCRIPT>
<BODY ONLOAD=&quot;init();&quot;>

Hope this helps

Wullie

 
[thumbsup2]

many thanks

[atom] ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
how about a full solid circle ?

is there a official list
of accepted jsfunction by browser ?

many thanks

and another..star ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
wow, that is A LOT of code to draw a circle! i can't wait for SVG to become a standard.
 
alright ! [2thumbsup] ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
this thread got my curiosity going, so i opened up my math book and after some tweaking i managed to write a circle script that is a lot simpler than the one above IMHO:

Code:
<style>
I {position:absolute; width:1; height:1; clip:rect(0 1 1 0); background-color:red}
#circle {position:absolute; clip:rect(0 630 400 0)}
</style>
<script>
D = document;
IE = D.all;
function plotCircle(radius,layer) {
	plot = '';
	pi = Math.PI;
	x = -pi;
	while (x<pi+.000078125) {
		y = Math.sqrt((radius*radius-x*x));
		if (!isNaN(y)) {
			if (IE) plot += '<i style=left:' + Math.round(x*50+314) + ';top:' + Math.round(y*50+200) + '></i>';
			else D[layer].document.write('<layer left=' + Math.round(x*50+314) + ' top=' + Math.round(y*50+194) + '><font size=1 color=red>·</layer>');
		}
		if (x>-pi+.78 && x<pi-.78) x += .02;
		else if (x>-pi+.26 && x<pi-.26) x += .01;
		else if (x>-pi+.06 && x<pi-.08) x += .005;
		else if (x>-pi+.003 && x<pi-.004) x += .00125;
		else x += .000078125;
	}
	x = pi;
	while (x>-pi-.000078125) {
		y = -Math.sqrt((radius*radius-x*x));
		if (!isNaN(y)) {
			if (IE) plot += '<i style=left:' + Math.round(x*50+314) + ';top:' + Math.round(y*50+200) + '></i>';
			else D[layer].document.write('<layer left=' + Math.round(x*50+314) + ' top=' + Math.round(y*50+194) + '><font size=1 color=red>·</layer>');
		}
		if (x<pi-.78 && x>-pi+.78) x -= .02;
		else if (x<pi-.26 && x>-pi+.26) x -= .01;
		else if (x<pi-.06 && x>-pi+.08) x -= .005;
		else if (x<pi-.003 && x>-pi+.004) x -= .00125;
		else x -= .000078125;
	}
	if (IE) IE[layer].innerHTML = plot;
}
</script>
<div id=circle></div>
<TABLE width=630 height=400 bgcolor=black><tr><td><br></td></tr></TABLE>
<FORM>
<input type=button value=Draw! onClick=plotCircle(Math.PI,'circle')><br><br>
</FORM>

since a property of a circle is that x and y are always going to be the same distance from the center (0,0 in this case), you basically take the radius equation (sqrt of r^2-x^2) and run a loop through x to evaluate its y function. voila, by the end of the loop you've got yourself a circle :).

the code above is just a concept...ideally there should be a multiplier to automatically iterate x depending on a given radius or else you'll have a broken circle if you plug in a really big radius. the reason i didn't iterate x by .000078125 all the way through is because that'll bring most systems to a crawl.

after playing with javascript's built-in math functions, i reckon that theoretically any symmetric pattern can be drawn with javascript. yes, your browser is also a fractal generator! 8)
 
very nice !
can u make it a full red circle ? ---------------------------------------
wmail.jpg


someone knowledge ends where
someone else knowledge starts
 
yes. you already have the radius, so loop y from 0 to pi, then for each y position, write a red 1px high x layer with length = x*2. that should do the trick, hope that helps.
 
Hello,

What about this one:

<span style=&quot;position: absolute; color: red; font-size: 622pt; top: 200px; left: 200px;&quot;>& #9679</span>

That's short and cross-browser! :)
You can even have triangles! :)

Have a good day all.
 
the code proves a point but its not very versatile. Cant specify dimensions with the above method or cannot use it for scale drawings etc, even though I like the simplicity of it.
 

You can make drawings too!

<span style=&quot;position: absolute; z-index: 4; color: white; font-size: 800pt; top: 0px; left: 95px; &quot;>X</span>
<span style=&quot;position: absolute; z-index: 2; color: blue; font-size: 622pt; top: 100px; left: 0px; &quot;>& #9679;</span>
<span style=&quot;position: absolute; z-index: 3; color: red; font-size: 622pt; top: 100px; left: 0px; &quot;>& #9678;</span>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top