Click here to Skip to main content
15,879,348 members
Articles / Web Development / HTML

SilverLight Introduction

Rate me:
Please Sign up or sign in to vote.
4.62/5 (35 votes)
26 May 20074 min read 159.2K   1.1K   78  
Tinkering with SilverLight to get a 3D scene rendered in the Browser.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<script type="text/javascript" src="js/Silverlight.js"></script>
		<script type="text/javascript" src="js/ball3d.1.1.js"></script>
	</head>
	<body>
		<div id="wpfe">
			<div id="wpfeHost" class="host"> </div>
			    <script type="text/javascript">
			
	        	Sys.Silverlight.createObjectEx(
				{
					source: 'assets/ball_n.xaml', 
					parentElement: document.getElementById("wpfeHost"), 
					id: 'wpfeBlock', 
					properties:
						{
							width:'500', 
							height:'500', 
							background:'white', 
							isWindowless:'false', 
							framerate:'200', 
							version:'0.95.0'
						}, 
					events:
						{
							onError:null, 
							onLoad:null
						}, 
					context:null
				}
				);
  
   				</script>
		</div>
		<script language="JavaScript">
			var startOnLoad = true;
			var isLoaded = false;
			var numBalls = 4;
			var wpfeRoot;
			var mouseData = null;
			
			function changeNumberOfBalls(n) {
				startOnLoad = true;
				numBalls = n;
				_wpfeTest.stop();
			}
			/**
			 * Silverlight (WPF/e) Balls test
			 * 
			 * @param {Object} N
			 */
			function WPFETest(N) {
				WPFETest.baseConstructor.call(this, N);
				this._wpfeBlock = document.getElementById("wpfeBlock");

			}
			extend(WPFETest, BallsTest);
			
			WPFETest.prototype.start = function(N) {
				if (!WPFETest.base.start.call(this, N)) return;
				this._savedXaml = this._wpfeBlock.source;
				
				this._ballsO[0] = new WPFEBall(this._wpfeBlock, "wpfe_ball_0");
				
				for (var i=1; i<this._N; i++) {
					this._ballsO[i] = this._ballsO[0].clone("wpfe_ball_" + i);
				}
				
				this_ = this;
				model.walls.makeWalls = function() {
					model.walls.shapes = [];
					var wall = new WPFEFace(this_._wpfeBlock, wpfeRoot,
											[
											[this.minx, 0, 0],
											[this.maxx, 0, 0],
											[0, this.maxy*Math.tan(60*Math.PI/180), 0]
											]
											);
					wall._elem.fill = "#990000FF";						
					model.walls.shapes.push(wall);

					wall = new WPFEFace(this_._wpfeBlock, wpfeRoot,
											[
											[this.minx, 0, 0],
											[this.maxx, 0, 0],
											[(this.minx+this.maxx)/3, this.maxy*Math.tan(60*Math.PI/180)/3, this.maxx*Math.tan(60*Math.PI/180)]
											]
											);
					wall._elem.fill = "#99FF0000";
					model.walls.shapes.push(wall);
					
					wall = new WPFEFace(this_._wpfeBlock, wpfeRoot,
											[
											[this.maxx, 0, 0],
											[0, this.maxy*Math.tan(60*Math.PI/180), 0],
											[(this.minx+this.maxx)/3, this.maxy*Math.tan(60*Math.PI/180)/3, this.maxx*Math.tan(60*Math.PI/180)]
											]
											);
					wall._elem.fill = "#9900FF00";
					model.walls.shapes.push(wall);
					
					wall = new WPFEFace(this_._wpfeBlock, wpfeRoot,
											[
											[this.minx, 0, 0],
											[this.maxx, 0, 0],
											[(this.minx+this.maxx)/3, this.maxy*Math.tan(60*Math.PI/180)/3, this.maxx*Math.tan(60*Math.PI/180)/2]
											]
											);
					wall._elem.fill = "#99FFFF00";
					model.walls.shapes.push(wall);
					
					wall = new WPFEFace(this_._wpfeBlock, wpfeRoot,
											[
											[this.maxx, 0, 0],
											[0, this.maxy*Math.tan(60*Math.PI/180), 0],
											[(this.minx+this.maxx)/3, this.maxy*Math.tan(60*Math.PI/180)/3, this.maxx*Math.tan(60*Math.PI/180)/2]
											]
											);
					wall._elem.fill = "#9900FFFF";
					model.walls.shapes.push(wall);

					wall = new WPFEFace(this_._wpfeBlock, wpfeRoot,
											[
											[0, this.maxy*Math.tan(60*Math.PI/180), 0],
											[(this.minx+this.maxx)/3, this.maxy*Math.tan(60*Math.PI/180)/3, this.maxx*Math.tan(60*Math.PI/180)],
											[(this.minx+this.maxx)/3, this.maxy*Math.tan(60*Math.PI/180)/3, this.maxx*Math.tan(60*Math.PI/180)/2]
											]
											);
					wall._elem.fill = "#9900FFFF";
					model.walls.shapes.push(wall);
				}
				
				model.walls.makeWalls();
				
			}
			WPFETest.prototype.stop = function(){
				if (!WPFETest.base.stop.call(this)) return;
				this._wpfeBlock.source = this._savedXaml;
				
				isLoaded = false;
			}

			WPFETest.prototype._moveBalls = function(fps) {
				/*
				*/
				this._ballsO = this._ballsO.sort(
					function(a, b) {
						return b.p.scale - a.p.scale; 
					}
				);
				// rotate it unless user moves the mouse
				if (!mouseData)
					this._angleY += 0.005;
				model.setPOV(this._angleZ, this._angleY, this._distance);
			}
			WPFETest.prototype.setPOV = function(z, y) {
				this._angleZ = z; 
				this._angleY = y;
				model.setPOV(this._angleZ, this._angleY, this._distance);
			}
			WPFETest.prototype._gravity = false;
			WPFETest.prototype.gravitySwitch = function() {
				if (! this._gravity) {
					model.gravity = -0.2; model.elastity = 0.8;
				} else {
					model.gravity = 0; model.elastity = 0.0;
				}
				this._gravity = !this._gravity;
			}
			
			var _wpfeTest = new WPFETest(numBalls);
			// called from <Canvas ... Loaded="javascript:wpfeLoaded">
			function wpfeLoaded(sender) {
				//alert("hello");
				wpfeRoot = sender;
				if (startOnLoad) {
					_wpfeTest.start(numBalls);
					startOnLoad = false;
				}
				isLoaded = true;
			}
			function onMouseMove(sender, eventArgs) {
				  if (!mouseData) return;
				  var pt = eventArgs.getPosition(null);

				  var dy = (pt.y - mouseData.e.y)/200;
				  var dx = (pt.x - mouseData.e.x)/200;
				  _wpfeTest.setPOV(mouseData.start.z + dy, mouseData.start.y + dx);
			}
				
			function onLeftButtonDown(sender, eventArgs) {
			  sender.captureMouse();
			  mouseData = new Object();
			  mouseData.start = {"z":_wpfeTest._angleZ,"y":_wpfeTest._angleY};
			  mouseData.e = eventArgs.getPosition(null);
			}
			
			function onLeftButtonUp(sender) {
			  sender.releaseMouseCapture();
			  mouseData = null;
			}
			

		</script>
	</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect GE India Innovation Center
India India
Suchit is an Architect at GE India Innovation Center, Hyderabad.

He architected and developed portions of Proficy RX, a Process Analytical Technology (PAT) Solution of GE Fanuc Intelligent Platforms.

He also is the Architect of OPC Server for hardware devices of GE Sensing. These devices sense temperature, humidity, combustibles, fluid flow, pressure and various engineering parameters - primarily used in Industrial Automation & Process Control applications.

Interests: Computer Graphics, Mathematical Modeling, Scientific Applications Development.

He lives in Hyderabad India with. Loves reading books.

Comments and Discussions