Use JS to implement the 3D spherical label cloud sample code

Source: Internet
Author: User

Matter:

1. Failed to hover (to be resolved) When an instance is found to be insufficient)
2. The system cannot be automatically colored at random (to be resolved)

First, use the Tag Cloud's high-profile website, the photo sharing website, and then use the QR code. The designer of Tag Cloud is interaction designer Stewart Butterfield. Then, the tag cloud is adopted by websites such as del and Technorati.
The first appearance Tag Cloud (or at least a weighted list) can be attributed mainly to "subconscious archives" in Douglas Coupland Microserfs (1995 ). In Lester Leaps Out, the Wales poet Lang Dao uses the same logic-weighted text to create a graphic text map to describe jazz music. The poem appears in his book Magic Fire Chevrolet (1980 ).

Html code segment
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Headers>
<Meta charset = UTF-8/>
<Title> 3D spherical label </title>
<Link rel = "stylesheet" type = "text/css" href = "3Dtag_style.css"/>
<Script type = "text/javascript" src = "3Dtag. js"> </script>
</Headers>
<Body>
<Div id = "div1">
<A href = "#"> fruit type </a>
<A href = "#" class = "red"> Apple </a>
<A href = "#"> bananas </a>
<A href = "#"> watermelon </a>
<A href = "#" class = "blue"> 3 Dtag </a>
<A href = "#"> SEO </a>
<A href = "#"> dragon fruit </a>
<A href = "#" class = "yellow"> pear </a>
<A href = "#"> spherical </a>
<A href = "#" class = "red"> CSS </a>
<A href = "#"> PHP </a>
<A href = "#" class = "blue"> Java </a>
<A href = "#"> inheritance </a>
<A href = "#"> C # </a>
<A href = "#" class = "blue"> OOP </a>
<A href = "#"> language type </a>
<A href = "#" class = "blue"> tomato </a>
<A href = "#"> C ++ </a>
<A href = "#"> C </a>
<A href = "#"> tomato </a>
<A href = "#"> demo </a>
</Div>
<P> 3D spherical tag </p>
</Body>
Javascript code segment
Copy codeThe Code is as follows:
Var radius = 120; // rotation radius;
Var dtr = Math. PI/180;
Var d = 300;

Var mcList = [];
Var active = false;
Var lasta = 1;
Var lastb = 1;
Var distr = true;
Var tspeed = 10;
Var size = 250;

Var mouseX = 0;
Var mouseY = 0;

Var howElliptical = 1;

Var aA = null;
Var oDiv = null;

Window. onload = function ()
{
Var I = 0;
Var oTag = null;

ODiv = document. getElementById ('div1 ');

AA = oDiv. getElementsByTagName ('A ');

For (I = 0; I <aA. length; I ++)
{
OTag = {};

OTag. offsetWidth = aA [I]. offsetWidth;
OTag. offsetHeight = aA [I]. offsetHeight;

McList. push (oTag );
}

SineCosine (0, 0 );

PositionAll ();

ODiv. onmouseover = function ()
{
Active = true;
};

ODiv. onmouseout = function ()
{
Active = false;
};

ODiv. onmousemove = function (ev)
{
Var oEvent = window. event | ev;

MouseX = oEvent. clientX-(oDiv. offsetLeft + oDiv. offsetWidth/2 );
MouseY = oEvent. clientY-(oDiv. offsetTop + oDiv. offsetHeight/2 );

MouseX/= 5;
MouseY/= 5;
};

SetInterval (update, 30 );
};

Function update ()
{
Var;
Var B;

If (active)
{
A = (-Math. min (Math. max (-mouseY,-size), size)/radius) * tspeed;
B = (Math. min (Math. max (-mouseX,-size), size)/radius) * tspeed;
}
Else
{
A = lasta * 0.98;
B = lastb * 0.98;
}

Lasta =;
Lastb = B;

If (Math. abs (a) <= 0.01 & Math. abs (B) <= 0.01)
{
Return;
}

Var c = 0;
SineCosine (a, B, c );
For (var j = 0; j <mcList. length; j ++)
{
Var rx1 = mcList [j]. cx;
Var ry1 = mcList [j]. cy * ca + mcList [j]. cz * (-sa );
Var rz1 = mcList [j]. cy * sa + mcList [j]. cz * ca;

Var rx2 = rx1 * cb + rz1 * sb;
Var ry2 = ry1;
Var rz2 = rx1 * (-sb) + rz1 * cb;

Var rx3 = rx2 * cc + ry2 * (-SC );
Var ry3 = rx2 * SC + ry2 * cc;
Var rz3 = rz2;

McList [j]. cx = rx3;
McList [j]. cy = ry3;
McList [j]. cz = rz3;

Per = d/(d + rz3 );

McList [j]. x = (howElliptical * rx3 * per)-(howElliptical * 2 );
McList [j]. y = ry3 * per;
McList [j]. scale = per;
McList [j]. alpha = per;

McList [j]. alpha = (mcList [j]. alpha-0.6) * (10/6 );
}

DoPosition ();
DepthSort ();
}

Function depthSort ()
{
Var I = 0;
Var aTmp = [];

For (I = 0; I <aA. length; I ++)
{
ATmp. push (aA [I]);
}

ATmp. sort
(
Function (vItem1, vItem2)
{
If (vItem1.cz> vItem2.cz)
{
Return-1;
}
Else if (vItem1.cz <vItem2.cz)
{
Return 1;
}
Else
{
Return 0;
}
}
);

For (I = 0; I <aTmp. length; I ++)
{
ATmp [I]. style. zIndex = I;
}
}

Function positionAll ()
{
Var phi = 0;
Var theta = 0;
Var max = mcList. length;
Var I = 0;

Var aTmp = [];
Var oFragment = document. createDocumentFragment ();

// Random sorting
For (I = 0; I <aA. length; I ++)
{
ATmp. push (aA [I]);
}

ATmp. sort
(
Function ()
{
Return Math. random () <0.5? 1:-1;
}
);

For (I = 0; I <aTmp. length; I ++)
{
OFragment. appendChild (aTmp [I]);
}

ODiv. appendChild (oFragment );

For (var I = 1; I <max + 1; I ++ ){
If (distr)
{
Phi = Math. acos (-1 + (2 * i-1)/max );
Theta = Math. sqrt (max * Math. PI) * phi;
}
Else
{
Phi = Math. random () * (Math. PI );
Theta = Math. random () * (2 * Math. PI );
}
// Coordinate Transformation
McList [I-1]. cx = radius * Math. cos (theta) * Math. sin (phi );
McList [I-1]. cy = radius * Math. sin (theta) * Math. sin (phi );
McList [I-1]. cz = radius * Math. cos (phi );

AA [I-1]. style. left = mcList [I-1]. cx + oDiv. offsetWidth/2-mcList [I-1]. offsetWidth/2 + 'px ';
AA [I-1]. style. top = mcList [I-1]. cy + oDiv. offsetHeight/2-mcList [I-1]. offsetHeight/2 + 'px ';
}
}

Function doPosition ()
{
Var l = oDiv. offsetWidth/2;
Var t = oDiv. offsetHeight/2;
For (var I = 0; I <mcList. length; I ++)
{
AA [I]. style. left = mcList [I]. cx + l-mcList [I]. offsetWidth/2 + 'px ';
AA [I]. style. top = mcList [I]. cy + t-mcList [I]. offsetHeight/2 + 'px ';

AA [I]. style. fontSize = Math. ceil (12 * mcList [I]. scale/2) + 8 + 'px ';

AA [I]. style. filter = "alpha (opacity =" + 100 * mcList [I]. alpha + ")";
AA [I]. style. opacity = mcList [I]. alpha;
}
}

Function sineCosine (a, B, c)
{
Sa = Math. sin (a * dtr );
Ca = Math. cos (a * dtr );
Sb = Math. sin (B * dtr );
Cb = Math. cos (B * dtr );
SC = Math. sin (c * dtr );
Cc = Math. cos (c * dtr );
}

CSS code segment
Copy codeThe Code is as follows:
Body {background: #000 url(index.png) no-repeat center 230px ;}
# Div1 {position: relative; width: pixel PX; height: pixel PX; margin: 20px auto 0 ;}
# Div1 a {position: absolute; top: 0px; left: 0px; font-family: Microsoft YaHei; color: # fff; font-weight: bold; text-decoration: none; padding: 3px 6px ;}
# Div1 a: hover {border: 1px solid # eee; background: #000 ;}
# Div1. blue {color: blue ;}
# Div1. red {color: red ;}
# Div1. yellow {color: yellow ;}

P {font: 16px Microsoft YaHei; text-align: center; color: # ba0c0c ;}
P a {font-size: 14px; color: # ba0c0c ;}
P a: hover {color: red ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.