SVG表現3D圖形
阿新 • • 發佈:2019-01-13
<svg width="100%" height="100%" viewBox="0 0 350 300" xml:space="preserve" onload="Initialize(evt)" onmousedown="MouseDown(evt)" onmousemove="MouseMove(evt)" onmouseup="MouseUp(evt)">
<title>3D.svg</title>
<desc>An interactive 3d model.</desc>
<script language="Javascript"><! [CDATA[
var FontScalar =8//10
//var ChargeScale = 0.8
var Dimensions =200
var CameraDistance = Dimensions *2
var HitherPlane = CameraDistance /20
var MouseScalar = .005
var MinMouseSpeed = .01
var MouseMoveAverage = .3
var MaxRotationalSpeed = .05
var RotationsPerCycle =4
var ApproachRange =3* RotationsPerCycle
var BondSpread =3
var MinTurnIncrement = .0001
var ShadowDistance = Dimensions /4
var radio =3;
var SVGDocument =null
var Rotating =true
var Scale =1
var XOffset =0
var YOffset =0
var ZOffset =0
var MinX =0
var MaxX =0
var MinY =0
var MaxY =0
var MinZ =0
var MaxZ =0
var Theta =0;
var Phi =0;
var ThrownTheta =0;
var ThrownPhi =0;
var Atoms =new Array();
var Bonds =new Array();
var debugFlag =true; //My debug flag;
function Initialize(LoadEvent)
...{
SVGDocument = LoadEvent.getTarget().getOwnerDocument();
GenerateStructure();
/**//*
if(debugFlag == true)
{
s=printNode(SVGDocument.getElementById("atoms"));
alert(s);
debugFlag = false;
}
*/
}
function Atom(x, y, z, fillStyle)
...{
FixRange(x, y, z)
if (x !=null) this.x = x;
if (y !=null) this.y = y;
if (z !=null) this.z = z;
this.SVG = SVGDocument.createElement("circle");
this.SVG.setAttributeNS(null,"fill", fillStyle);
//this.SVG.setAttributeNS(null,"filter","url(#dropShadow)");
SVGDocument.getElementById("atoms").appendChild(this.SVG);
Atoms[Atoms.length] =this; //Save to array
this.refresh();
returnthis;
}
Atom.prototype.x =0;
Atom.prototype.y =0;
Atom.prototype.z =0;
Atom.prototype.bonds =new Array();
Atom.prototype.element ="";
Atom.prototype.displayPhi =0;
Atom.prototype.displayTheta =0;
Atom.prototype.distanceScalar =1;
Atom.prototype.refresh = Atom_Refresh;
Atom.prototype.fixCoords = Atom_FixCoordinates;
Atom.prototype.calculateRotation = Atom_CalculateRotation
function Atom_FixCoordinates()
...{
this.x += XOffset;
this.y += YOffset;
this.z += ZOffset;
this.x =this.x * Scale;
this.y =this.y * Scale;
this.z =this.z * Scale;
this.refresh()
}
function Atom_Refresh()
...{
oldx =this.x
oldy =this.y
this.calculateRotation();
if (this.SVG !=null)
...{
this.SVG.setAttribute("transform", "translate("+this.displayX +", "+this.displayY +")");
this.SVG.setAttributeNS(null,"r", this.displayZ);
}
//Sort by displayZ
var elem = SVGDocument.getElementById("atoms");
var nodes = elem.getChildNodes;
var insertFlag =false;
//for(i = 1;i < ((nodes.length)-1);i += 2)
for(i =0;i < ((nodes.length-1));i ++)
...{
//Compare by r attribute in SVG node
var nodeMap = nodes.item(i).attributes;
var r = nodeMap.getNamedItemNS(null, "r");
if(this.displayZ < r.nodeValue)
...{
elem.insertBefore(this.SVG, nodes.item(i));
insertFlag =true;
break;
}
}
//The nearest one
if(insertFlag ==false)
elem.appendChild(this.SVG);
return ((oldx !=this.x) || (oldy !=this.y))
}
function Atom_CalculateRotation()
...{
var clipTheta =true;
var clipPhi =true;
for (var I =1; I < RotationsPerCycle; I++)
...{
dTheta = Theta -this.displayTheta;
dPhi = Phi -this.displayPhi;
<title>3D.svg</title>
<desc>An interactive 3d model.</desc>
<script language="Javascript"><!
var FontScalar =8//10
//var ChargeScale = 0.8
var Dimensions =200
var CameraDistance = Dimensions *2
var HitherPlane = CameraDistance /20
var MouseScalar = .005
var MinMouseSpeed = .01
var MaxRotationalSpeed = .05
var RotationsPerCycle =4
var ApproachRange =3* RotationsPerCycle
var BondSpread =3
var MinTurnIncrement = .0001
var ShadowDistance = Dimensions /4
var SVGDocument =null
var Rotating =true
var Scale =1
var XOffset =0
var YOffset =0
var ZOffset =0
var MinX =0
var MaxX =0
var MinY =0
var MaxY =0
var MinZ =0
var MaxZ =0
var Theta =0;
var Phi =0;
var ThrownTheta =0;
var ThrownPhi =0;
var Atoms =new Array();
var Bonds =new Array();
var debugFlag =true; //My debug flag;
function Initialize(LoadEvent)
...{
SVGDocument = LoadEvent.getTarget().getOwnerDocument();
GenerateStructure();
/**//*
if(debugFlag == true)
{
s=printNode(SVGDocument.getElementById("atoms"));
alert(s);
debugFlag = false;
}
*/
}
function Atom(x, y, z, fillStyle)
...{
FixRange(x, y, z)
if (x !=null) this.x = x;
if (y !=null) this.y = y;
if (z !=null) this.z = z;
this.SVG = SVGDocument.createElement("circle");
this.SVG.setAttributeNS(null,"fill", fillStyle);
//this.SVG.setAttributeNS(null,"filter","url(#dropShadow)");
SVGDocument.getElementById("atoms").appendChild(this.SVG);
Atoms[Atoms.length] =this; //Save to array
this.refresh();
returnthis;
}
Atom.prototype.x =0;
Atom.prototype.y =0;
Atom.prototype.z =0;
Atom.prototype.bonds =new Array();
Atom.prototype.element ="";
Atom.prototype.displayPhi =0;
Atom.prototype.displayTheta =0;
Atom.prototype.distanceScalar =1;
Atom.prototype.refresh = Atom_Refresh;
Atom.prototype.fixCoords = Atom_FixCoordinates;
Atom.prototype.calculateRotation = Atom_CalculateRotation
function Atom_FixCoordinates()
...{
this.x += XOffset;
this.y += YOffset;
this.z += ZOffset;
this.x =this.x * Scale;
this.y =this.y * Scale;
this.z =this.z * Scale;
this.refresh()
}
function Atom_Refresh()
...{
oldx =this.x
oldy =this.y
this.calculateRotation();
if (this.SVG !=null)
...{
this.SVG.setAttribute("transform", "translate("+this.displayX +", "+this.displayY +")");
this.SVG.setAttributeNS(null,"r", this.displayZ);
}
//Sort by displayZ
var elem = SVGDocument.getElementById("atoms");
var nodes = elem.getChildNodes;
var insertFlag =false;
//for(i = 1;i < ((nodes.length)-1);i += 2)
for(i =0;i < ((nodes.length-1));i ++)
...{
//Compare by r attribute in SVG node
var nodeMap = nodes.item(i).attributes;
var r = nodeMap.getNamedItemNS(null, "r");
if(this.displayZ < r.nodeValue)
...{
elem.insertBefore(this.SVG, nodes.item(i));
insertFlag =true;
break;
}
}
//The nearest one
if(insertFlag ==false)
elem.appendChild(this.SVG);
return ((oldx !=this.x) || (oldy !=this.y))
}
function Atom_CalculateRotation()
...{
var clipTheta =true;
var clipPhi =true;
for (var I =1; I < RotationsPerCycle; I++)
...{
dTheta = Theta -this.displayTheta;
dPhi = Phi -this.displayPhi;