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.

Example:

   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
 dc.style.position="absolute";
 dc.innerHTML = "&nbsp;";
 dc.style.left = cx - r; 
 dc.style.top =  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");
    
  div.style.position="absolute";
  div.style.background = clr;
  div.style.left = cx - x;
  div.style.top = cy+y;
  div.style.width= x*2;
  div.style.height=gran;
  div.style.fontSize=1;
  div.innerHTML = "&nbsp;";
  setObjectOpacity(div, op);
  dc.appendChild(div);
  
  div = document.createElement("DIV");
  div.style.position="absolute";
  div.style.background = clr;
  div.style.left = cx - x;
  div.style.top = cy-y-gran;
  div.style.width= x*2;
  div.style.height=gran;
  div.style.fontSize=1;
  div.innerHTML = "&nbsp;";
  setObjectOpacity(div, op);
  dc.appendChild(div);
  
 }
 
 return dc;
}//drawCircle

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


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!


33 comments:

Anonymous said...

Cool, HTML5 will not come for some time yet

Anonymous said...

you forgot

document.body.appendChild(drawCircle(...))

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?

Website Development said...

I'm impressed. You're truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. I'm saving this for future use.

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.

Coach Factory said...

louboutin outlet, michael kors handbags clearance, air jordan, oakley vault, burberry outlet online, kate spade handbags, louis vuitton handbags, michael kors outlet online, ray ban sunglasses, cheap oakley sunglasses, michael kors outlet online, toms shoes, prada outlet, true religion outlet, jordan shoes, coach purses, longchamp handbags, michael kors outlet, polo ralph lauren outlet online, coach outlet store online, nike free, tiffany and co, polo ralph lauren outlet, prada handbags, oakley sunglasses, nike air max, longchamp handbags, true religion, nike free, kate spade outlet, nike shoes, tory burch outlet, nike air max, louis vuitton outlet online, coach outlet, coach factory outlet, true religion jeans, ray ban sunglasses outlet, air max, longchamp outlet, air max, gucci outlet, louboutin, cheap michael kors, chanel handbags

Coach Factory said...

lululemon outlet, ray ban, nike roshe, mont blanc, converse shoes, karen millen, michael kors, roshe run, nfl jerseys, nike huarache, birkin bag, beats by dre, mac cosmetics, hogan, louboutin, converse pas cher, ray ban pas cher, montre pas cher, michael kors, vans, north face jackets, lululemon, oakley pas cher, north face outlet, reebok outlet, michael kors, nike free, ralph lauren, nike trainers, louis vuitton, louis vuitton, hollister, hermes, timberland, abercrombie and fitch, supra shoes, new balance shoes, juicy couture outlet, mulberry, air max, lancel, hollister, north face, rolex watches, juicy couture outlet, burberry, wedding dresses, new balance pas cher, abercrombie and fitch, nike air max

Coach Factory said...

nike air max, air max pas cher, p90x3, converse, ugg boots, ferragamo shoes, giuseppe zanotti, louboutin, moncler, mcm handbags, air max pas cher, ralph lauren, ugg outlet, soccer jerseys, valentino shoes, herve leger, babyliss pro, moncler, moncler, jimmy choo, vans, asics gel, air max, moncler outlet, chi flat iron, canada goose outlet, uggs on sale, hollister, canada goose, instyler ionic styler, moncler, canada goose, insanity, marc jacobs, tn pas cher, ray ban, canada goose outlet, air max, soccer shoes, canada goose, bottega veneta, ghd, ugg, celine handbags, ugg, gucci, canada goose, ugg boots, oakley

mao yun said...

1
instyler ionic styler
cheap soccer jerseys
tiffany and co jewelry
ghd hair straighteners
louboutin shoes
valentino sale
asics running shoes
adidas shoes
hollister
michael kors outlet online
jordan 4
insanity
christian louboutin
air jordan concord
giuseppe zanotti sale
cheap nba jerseys
insanity max 30
lacoste outlet
abercrombie and fitch
soccer jerseys
salomon running shoes
christian louboutin outlet
coach outlet store online
toms outlet
hollister clothing store
toms outlet
kate spade handbags
polo lacoste pas cher
cheap snapbacks
adidas outlet
babyliss
fred perry sale
christian louboutin outlet
pandora charms
discount oakley sunglasses
mlb jerseys wholesale
salomon shoes
ralph lauren outlet
converse outlet
ray ban aviator sunglasses
michael kors uk
20150326maoqiuyun

Weiming Zhu said...

ugg boots
air jordan 13
air jordan shoes
beats headphones
bottega veneta outlet
burberry outlet
canada goose outlet
chanel outlet
cheap wedding dresses
coach outlet
dansko outlet
evening dresses
hermes belt
hermes birkin
hermes outlet
juicy couture outlet
kate spade outlet
mbt shoes
michael kors outlet
mizuno running
moncler coats
mulberry outlet
soccer shoes
nike outlet store
the north face outlet
oakley sunglasses
ray ban sunglasses
stuart weitzman boots
surpa sneakers
north face outlet
the north face clearance
the north face jackets
the north face outlet store
north face jackets
the north face uk
tommy hilfiger outlet
tory burch outlet
true religion jeans
vans shoes
ysl outlet

Weiming Zhu said...

ugg boots
air jordan 13
air jordan shoes
beats headphones
bottega veneta outlet
burberry outlet
canada goose outlet
chanel outlet
cheap wedding dresses
coach outlet
dansko outlet
evening dresses
hermes belt
hermes birkin
hermes outlet
juicy couture outlet
kate spade outlet
mbt shoes
michael kors outlet
mizuno running
moncler coats
mulberry outlet
soccer shoes
nike outlet store
the north face outlet
oakley sunglasses
ray ban sunglasses
stuart weitzman boots
surpa sneakers
north face outlet
the north face clearance
the north face jackets
the north face outlet store
north face jackets
the north face uk
tommy hilfiger outlet
tory burch outlet
true religion jeans
vans shoes
ysl outlet

Meiqing Xu said...

20150623meiqing
louis vuitton
louis vuitton
fitflop footwear
oakley store
kate spade outlet
ralph lauren outlet
ray ban sunglasses
jordan 8 playoffs
oakley vault
toms outlet
www.louisvuitton.com
hollister
jordan concord 11s
michael kors outlet online
chanel bags
christian louboutin outlet
coach outlet
fitflops clearance
louboutin
louis vuitton outlet
coach outlet
coach outlet store online
marc jacobs handbags
toms promo code
oakley sunglasses wholesale
lebron james basketball shoes
prada
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

Wenhao Guo said...

ddddd0925fred perry polo, http://www.fredperrypoloshirts.com/
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, http://www.uggboot.com.co/
links of london uk, http://www.linksoflondons.co.uk/
louis vuitton outlet online, http://www.louisvuittonoutlet.in.net/
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, http://www.dansko-shoes.us/
kobe bryants shoes 2015, http://www.kobeshoes.us/
michael kors outlet store, http://www.michaelkorsoutletonlinstore.us.com/
monster beats, http://www.beats-headphones.in.net/
san antonio spurs, http://www.sanantoniospursjersey.com/
kobe bryant store, http://www.kobebryantshoes.in.net/
nfl jerseys wholesale, http://www.nfljerseys-wholesale.us.com/
new york knicks, http://www.newyorkknicksjerseys.com/
chanel handbags, http://www.chanelhandbags-outlet.us.com/

mao qiuyun said...

chanel handbags, http://www.chanelhandbags-outlet.co.uk/
the north face jackets, http://www.thenorthfacejacket.us.com/
canada goose outlet, http://www.canadagoose.us.org/
the north face outlet store, http://www.thenorthfaceoutletstores.org/
ugg boots, http://www.uggboot.com.co/
oakley sunglasses, http://www.oakleysunglasses-outlet.us.com/
hermes bags, http://www.hermesbags.co.uk/
oakley sunglasses, http://www.oakleysunglasseswholesale.in.net/
vans shoes, http://www.vans-shoes.cc/
coach outlet store, http://www.coach-outlet-store.us.com/
coach outlet canada, http://www.coachoutletcanada.com.co/
coach outlet, http://www.coachoutletstoreonline.in.net/
the north face uk, http://www.thenorthfaces.org.uk/
nike air max uk, http://www.nikeairmaxshoes.org.uk/
ray ban sunglasses, http://www.raybansunglass.us.com/
kobe shoes, http://www.kobeshoes.us/
nike free, http://www.nikefreerunning.org/
tory burch outlet, http://www.toryburchoutletonline.in.net/
ugg boots, http://www.uggbootscheap.eu.com/
designer handbags, http://www.designerhandbags.us.com/
cheap wedding dresses, http://www.cheap-weddingdresses.net/
michael kors outlet, http://www.michaelkorsoutletonlinestores.us.com/
air jordan 4, http://www.airjordan4.org/
fitflop, http://www.fitflop.in.net/
the north face outlet, http://www.northface-outlet.cc/
soccer jerseys, http://www.soccerjerseys.us.com/
air max 90, http://www.airmax90.us.com/
swarovski jewelry, http://www.swarovski.in.net/
ralph lauren uk, http://www.ralphlauren-outletonline.co.uk/
1006maoqiuyun

oakleyses said...

kate spade outlet online, nike air max, tory burch outlet online, ugg boots clearance, prada handbags, nike air max, ray ban sunglasses, michael kors handbags, chanel handbags, jordan shoes, nike outlet, longchamp outlet online, gucci handbags, christian louboutin, christian louboutin shoes, burberry outlet online, ugg outlet, oakley sunglasses, louis vuitton outlet, ray ban outlet, nike free, cheap oakley sunglasses, cheap ugg boots outlet, louis vuitton, longchamp outlet, michael kors outlet online, polo ralph lauren outlet, burberry outlet online, christian louboutin outlet, michael kors outlet online, oakley sunglasses, ray ban sunglasses, oakley vault, ugg australia, cheap ugg boots, red bottom shoes, replica watches, michael kors outlet, tiffany and co, tiffany jewelry, louis vuitton outlet online, replica watches, polo ralph lauren, prada outlet, louis vuitton handbags

oakleyses said...

guess pas cher, nike air max, nike roshe run, mulberry uk, coach outlet, true religion outlet, true religion outlet, new balance pas cher, abercrombie and fitch, longchamp, nike air max, scarpe hogan, air max pas cher, sac vanessa bruno, ray ban pas cher, nike roshe, north face, coach purses, burberry pas cher, nike free pas cher, lacoste pas cher, timberland pas cher, coach outlet, michael kors uk, michael kors, nike free, longchamp pas cher, tn pas cher, hollister uk, vans pas cher, true religion jeans, ray ban uk, true religion, michael kors outlet online, north face pas cher, hermes pas cher, air jordan, lululemon outlet online, hollister, michael kors canada, louboutin, nike blazer pas cher, kate spade outlet, coach outlet store online, oakley pas cher, converse pas cher, nike air force, ralph lauren uk, air max, ralph lauren pas cher

oakleyses said...

nike air max, longchamp, new balance outlet, oakley, mcm handbags, nike roshe, ferragamo shoes, nfl jerseys, asics shoes, ralph lauren, celine handbags, mont blanc pens, hollister, s6 cases, baseball bats, soccer jerseys, nike air huarache, p90x workout, hollister clothing store, bottega veneta, giuseppe zanotti, insanity workout, abercrombie and fitch, ghd, iphone cases, valentino shoes, mac cosmetics, herve leger, iphone 6 cases, iphone 5s cases, lululemon outlet, instyler ionic styler, north face jackets, vans outlet, wedding dresses, soccer shoes, north face jackets, jimmy choo shoes, iphone 6s plus cases, birkin bag, beats headphones, timberland boots, nike trainers, iphone 6 plus cases, louboutin, reebok shoes, babyliss, chi flat iron

oakleyses said...

pandora jewelry, swarovski uk, canada goose, moncler outlet, links of london uk, coach outlet, ugg boots, hollister canada, sac lancel, marc jacobs handbags, air max, pandora jewelry, converse, replica watches, vans, wedding dress, uggs outlet, canada goose pas cher, canada goose, uggs canada, louis vuitton uk, moncler, barbour, gucci, supra shoes, moncler outlet, canada goose, canada goose outlet, doke gabbana outlet, moncler, juicy couture outlet, sac louis vuitton, canada goose, canada goose outlet, montre femme, thomas sabo uk, ugg soldes, louis vuitton, louis vuitton pas cher, moncler, swarovski jewelry, michael kors outlet online, louis vuitton canada, barbour jackets, converse shoes, ugg, canada goose outlet, ugg, ray ban, karen millen, moncler

mmjiaxin said...

herve leger dresses
louis vuitton handbags outlet
coach outlet store
ralph lauren outlet
ray-ban sunglasses
kobe bryants shoes
tiffany and co
karen millen uk
coach outlet online
phone cases
rolex uk
nike free 5
swarovski crystal
louis vuitton handbags outlet
ray ban sunglasses
cheap oakley sunglasses
michael kors canada
nba jerseys
mlb jerseys
michael kors handbags
mm1202

mmjiaxin said...

herve leger dresses
louis vuitton handbags outlet
coach outlet store
ralph lauren outlet
ray-ban sunglasses
kobe bryants shoes
tiffany and co
karen millen uk
coach outlet online
phone cases
rolex uk
nike free 5
swarovski crystal
louis vuitton handbags outlet
ray ban sunglasses
cheap oakley sunglasses
michael kors canada
nba jerseys
mlb jerseys
michael kors handbags
mm1202

方松腾 said...

ugg boots
cheap oakley sunglasses
true religion sale
air jordan 11 free shipping
toms outlet
louis vuitton outlet
michael kors outlet,michael kors outlet online,michael kors,kors outlet,michael kors handbags,michael kors outlet online sale,michael kors handbags clearance,michael kors purses,michaelkors.com,michael kors bags,michael kors shoes,michaelkors,cheap michael kors
north face jackets
ferragamo shoes
soccer jerseys,soccer jerseys wholesale,soccer jerseys cheap,soccer jerseys for sale,cheap soccer jersey,usa soccer jersey,football jerseys
ugg boots on sale
vans sneakers
oakley sunglasses
fitflops outlet
coach outlet online,coach purses,coach handbags,coach bags,coach handbags sale,coach handbags outlet

方松腾 said...

ugg boots
cheap oakley sunglasses
true religion sale
air jordan 11 free shipping
toms outlet
louis vuitton outlet
michael kors outlet,michael kors outlet online,michael kors,kors outlet,michael kors handbags,michael kors outlet online sale,michael kors handbags clearance,michael kors purses,michaelkors.com,michael kors bags,michael kors shoes,michaelkors,cheap michael kors
north face jackets
ferragamo shoes
soccer jerseys,soccer jerseys wholesale,soccer jerseys cheap,soccer jerseys for sale,cheap soccer jersey,usa soccer jersey,football jerseys
ugg boots on sale
vans sneakers
oakley sunglasses
fitflops outlet
coach outlet online,coach purses,coach handbags,coach bags,coach handbags sale,coach handbags outlet

dong dong23 said...

adidas trainers
supra shoes
cheap jordan shoes
michael kors handbags
gucci shoes
mont blanc pens
michael kors handbags
air max 90
true religion shorts
coach outlet
louis vuitton
michael kors purses
louis vuitton
gucci outlet
fitflop sandals
beats wireless headphones
ray ban sunglasses
louis vuitton outlet
asics shoes
oakley sunglasses
hollister clothing
cheap jordan shoes
louis vuitton handbags
tiffany jewelry
nike uk
kate spade handbags
ugg boots
vans shoes
fitflops sale clearance
jordans
nike huarache
air jordans
coach factory outlet
louis vuitton handbags
tory burch outlet
ray ban outlet
michael kors handbags
cheap jerseys
louis vuitton
nike blazers
20164.15wengdongdong

mmjiaxin said...

toms outlet store
cheap air jordans
http://www.airjordanretro.uk
kobe sneakers
cheap air jordan
coach outlet online
fitflop shoes
michael kors outlet store
cheap uggs
oakley sunglasses,oakley outlet sunglasses
ralph lauren uk
chrome hearts online
oakley sunglasses
tiffany and co jewelry
air jordan
Michael Kors Outlet Store
kobe shoes,kobe zoom,nike zoom kobe
mlb jerseys authentic
huaraches sale
tiffany jewelry
gg

mmjiaxin said...

Cheap Oakley Sunglasses
nike huaraches
http://www.cheapauthenticjordans.us.com
mlb jerseys authentic
fitflop shoes
oakley sunglasses,oakley vault,oakley sunglasses cheap,oakleys,oakley.com,sunglasses outlet,cheap oakley
http://www.christian-louboutin-outlet.uk
tiffany & co
michael kors outlet online
mlb jerseys authentic
cheap air jordans
tiffany and co
michael kors outlets
nfl jerseys
michael kors purses
http://www.jordansforcheap.us.com
http://www.michaeljordanshoes.us.com
nike huarache
ralph lauren online,cheap ralph lauren
http://www.kobesneakers.com
gg

chenlili said...

true religion sale
kevin durant 8 shoes
coach factory outlet online
ugg outlet
louis vuitton handbags
cheap air jordans
golden state warriors jerseys
uggs on sale
michael kors handbags
christian louboutin wedding shoes
moncler coats
ugg boots
louis vuitton
nike tn pas cher
rolex watches
rolex watches
toms sale
ugg boots
curry 3
christian louboutin
coach outlet store online
canada goose pas cher
cheap toms
louis vuitton outlet
rolex watches
ugg boots on sale
christian louboutin outlet
coach outlet
adidas running shoes
jordan retro 4
uggs sale
cincinnati bengals jerseys
uggs pas cher
true religion uk
ray ban sunglasses
uggs on sale
ugg boots
canada goose
chenlili20160829

chenlili said...

true religion sale
kevin durant 8 shoes
coach factory outlet online
ugg outlet
louis vuitton handbags
cheap air jordans
golden state warriors jerseys
uggs on sale
michael kors handbags
christian louboutin wedding shoes
moncler coats
ugg boots
louis vuitton
nike tn pas cher
rolex watches
rolex watches
toms sale
ugg boots
curry 3
christian louboutin
coach outlet store online
canada goose pas cher
cheap toms
louis vuitton outlet
rolex watches
ugg boots on sale
christian louboutin outlet
coach outlet
adidas running shoes
jordan retro 4
uggs sale
cincinnati bengals jerseys
uggs pas cher
true religion uk
ray ban sunglasses
uggs on sale
ugg boots
canada goose
chenlili20160829

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
chenlina20161027

Unknown said...

miami heat jersey
hugo boss
carolina panthers jersey
oakley sunglasses
michael kors handbags
lions jerseys
san antonio spurs jerseys
ecco shoes outlet
ray ban sunglasses
ecco shoes

Unknown said...

michael kors handbags wholesale
salomon shoes
the north face
chargers jerseys
raiders jerseys
toms shoes
hermes belts
skechers shoes
san antonio spurs jerseys
replica rolex

alice asd said...

air jordan 4
cheap oakley sunglasses
christian louboutin shoes
moncler outlet
miami heat jersey
toms shoes
coach factory outlet
nike outlet
washington redskins jerseys
polo ralph lauren
20170420alice0589