Saturday, January 8, 2011

How To Draw a Circle in HTML <5 (without Canvas/SVG)

The following tiny script allows to draw circles in "regular" pre-HTML 5 document, it is so fast that it really allows for animations in real time.


   drawCircle(500, 500, 150, "red", 50, 1);

This will draw a  red circle, 50% transparent, with 150 px radius at the point (500,500). The circle will be drawn using 1px-fine pen (granularity).
The wider is the "pen" the faster is the routine. I usually increase granularity to 4px while dragging/sizing for speed and then re-render with 1px granularity upon drag grip release.

Here is the self-sufficient(does not rely on any libs) procedural source code:

//classic one
function isIE()
 return (navigator.appName.indexOf("Microsoft")!=-1);

function drawCircle(cx, cy, r, clr, op, gran)
 var x = 0;
 var div; 
 var dc = document.createElement("DIV");//aka "Device Context" wich is a DIV in doc tree"absolute";
 dc.innerHTML = "&nbsp;"; = cx - r; =  cy - r; 
 cx = r; cy = r;
 if (gran<1) gran = 1;
 if ((r/gran)> 280) gran = Math.round(r / 280);
 for(var y=0; y<=r; y+=gran)
  x = Math.sqrt((r*r) - (y*y));
  div = document.createElement("DIV");"absolute"; = clr; = cx - x; = cy+y; x*2;;;
  div.innerHTML = "&nbsp;";
  setObjectOpacity(div, op);
  div = document.createElement("DIV");"absolute"; = clr; = cx - x; = cy-y-gran; x*2;;;
  div.innerHTML = "&nbsp;";
  setObjectOpacity(div, op);
 return dc;

//semi-transparent through CSS/filter
function setObjectOpacity(obj, opacity) 
  if (isIE()) = "alpha(opacity:"+opacity+")";
    opacity = (opacity == 100)?99.999:opacity;//99.9 for Firefox flicker bug
    // Konqueror, Safari = opacity / 100;
    // Old Mozilla and Firefox = opacity / 100;
    // CSS3,  Safari , new Firefox (Gecko) = opacity / 100;

As you can see, this approach uses DIVs as a vehicle for graphics delivery. Also, one may ask why I did not use squares to reduce
 the number of DIVs - the answer is - flicker - squares would have overlapped and would have worked slower than interlaced DIVs.

Use as you please!


Anonymous said...

Cool, HTML5 will not come for some time yet

Anonymous said...

you forgot


Anonymous said...

This is obsolete, but great post though. One of my clients uses ie6 and plans to keep it till Xp supporting hardware is available bummmmmmr

web design Perth said...

Thank you for sharing, I've been dabbling with CSS and now I think I seem to live HTML5 better.

Anonymous said...

I am new to this, would you mind showing where the function drawCircle() is called from? Presume it is html?

Web Design Birmingham AL said...

Thank you for the hard work and effort putting on the website. Your blog is great with more informative and useful ideas.

Unknown said...

louis vuitton
louis vuitton
fitflop footwear
oakley store
kate spade outlet
ralph lauren outlet
ray ban sunglasses
jordan 8 playoffs
oakley vault
toms outlet
jordan concord 11s
michael kors outlet online
chanel bags
christian louboutin outlet
coach outlet
fitflops clearance
louis vuitton outlet
coach outlet
coach outlet store online
marc jacobs handbags
toms promo code
oakley sunglasses wholesale
lebron james basketball shoes
jeremy scott shoes
oakley sunglasses
abercrombie outlet
louis vuitton
discount oakley sunglasses
christian louboutin outlet
michael kors uk
ray ban wayfarer
concords 11
kd shoes
kate spade handbags
pandora charms
abercrombie & fitch

Unknown said...

ddddd0925fred perry polo,
coach outlet online,coach purses,coach handbags,coach bags,coach handbags sale,coach handbags outlet
hollister,hollister canada,abercrombie and fitch,abercrombie,abercrombie fitch,abercrombie kids,a&f
nike air foamposite one,foamposite,foamposites,foamposite release 2015,foamposite sneakers,foamposites for sale,foamposite gold
ugg boots,
links of london uk,
louis vuitton outlet online,
rolex watches,rolex,watches for men,watches for women,omega watches,replica watches,rolex watches for sale,rolex replica,rolex watch,cartier watches,rolex submariner,fake rolex,rolex replica watches,replica rolex
ralph lauren,ralph lauren uk,ralph lauren outlet,ralph lauren outlet online,polo ralph lauren outlet,ralph lauren polo
dansko clogs,
kobe bryants shoes 2015,
michael kors outlet store,
monster beats,
san antonio spurs,
kobe bryant store,
nfl jerseys wholesale,
new york knicks,
chanel handbags,

mmjiaxin said...

Cheap Oakley Sunglasses
nike huaraches
mlb jerseys authentic
fitflop shoes
oakley sunglasses,oakley vault,oakley sunglasses cheap,oakleys,,sunglasses outlet,cheap oakley
tiffany & co
michael kors outlet online
mlb jerseys authentic
cheap air jordans
tiffany and co
michael kors outlets
nfl jerseys
michael kors purses
nike huarache
ralph lauren online,cheap ralph lauren

chenlina said...

michael kors outlet
ugg outlet
ray bans
fitflop shoes
seattle seahawks jerseys
cheap ray ban sunglasses
kate spade outlet
los angeles clippers jerseys
adidas outlet
oakley sunglasses

Unknown said...

pittsburgh steelers jersey
skechers outlet
bears jerseys
michael kors outlet
dolce and gabbana outlet
michael kors handbags
coach outlet
colts jerseys
coach outlet
oakley sunglasses

John said...

cavaliers jerseys
nike roshe one
michael kors outlet store
pandora charms sale clearance
jerseys cheap
mlb jerseys
ysl bags
christian louboutin shoes
pandora bracelet

Pengobatan Sakit Pinggang said...

Obat Penghancur Benjolan di Leher
Pengobatan efektif Menghilangkan Benjolan di Payudara
Pengobatan Miom tanpa Operasi
Obat Luka Berair yang Tak Kunjung Sembuh
Obat Tradisional Radang Tenggorokan
Khasiat Daun Sirsak untuk Benjolan

Unknown said...

2018531 leilei3915
canada goose
ray ban sunglasses
pandora outlet
coach outlet store online
pandora charms
kate spade outlet online
michael kors handbags
christian louboutin shoes
louis vuitton outlet online
true religion

Obat Maag Alami said...

thank you for joining us. his article is very helpful

Obat Keputihan Alami