Matt Kruse said:
Andy Baxter wrote: and it was going OK until I wanted to call one object
method from another - I couldn't find a syntax for this that worked.
Depends on exactly what kind of syntax you were using.
For example:
var MyLib2 = {};
MyLib2.funcC = function() { MyLib.funcA(); }
In the code below, the line I've marked doesn't work - it never reaches
the pano.moveImages() function. If I change it to just 'moveImages;'
(which is what I tried first), it gives an error: 'moveImages is not
defined'. I'm confused about this, because referring to object properties
like mouseDown and pan from inside the functions works fine without adding
'pano.' in front, but referring to one of the object methods doesn't. If
you have any general comments on the way I've written this, I'd appreciate
that too, as this is the first program I've written in javascript.
// Panorama script
// By Andy Baxter, May 2006
function showMessage(str) {
var rep=document.getElementById("message1");
rep.innerHTML=str;
}
function showMessage2(str) {
var rep=document.getElementById("message2");
rep.innerHTML=str;
}
var pano = {
// initialisation for the panorama.
// pixels to move when mouse is at full left / right.
scale: 0,
// mouse status.
mouseDown: 0,
mouseX: 0,
mouseY: 0,
// ratio that panorama is panned to left.
pan: 0,
// size of pano image [width,height]
imgSize: 0,
scaledImgSize: 0,
// references to DOM objects.
panoDiv: 0,
panoPic1: 0,
init: function() {
// initialise variables;
scale=30;
mouseDown=0;
mouseX=0;
mouseY=0;
pan=0;
panoDiv = document.getElementById("panoDiv");
panoPic1 = document.getElementById("panoImage1");
panoPic2 = document.getElementById("panoImage2");
imgSize=new Array(2);
imgSize[0]=3996;
imgSize[1]=500;
var imgScale=parseInt(panoDiv.style.height)/imgSize[1];
scaledImgSize=new Array(2);
scaledImgSize[0]=imgScale*imgSize[0];
scaledImgSize[1]=imgScale*imgSize[1];
panoPic1.style.width=scaledImgSize[0]+"px";
panoPic1.style.height=scaledImgSize[1]+"px";
panoPic2.style.width=scaledImgSize[0]+"px";
panoPic2.style.height=scaledImgSize[1]+"px";
// connect events.
dojo.event.connect(panoDiv, "onmousedown", pano, "onMouseDown");
dojo.event.connect(panoDiv, "onmouseup", pano, "onMouseUp");
dojo.event.connect(panoDiv, "onmousemove", pano, "onMouseMove");
dojo.event.connect(panoPic1, "onmousemove", pano, "stopdefault");
dojo.event.connect(panoPic1, "onmousedown", pano, "stopdefault");
dojo.event.connect(panoPic1, "onmouseup", pano, "stopdefault");
dojo.event.connect(panoPic1, "onmouseclick", pano, "stopdefault");
setInterval( pano.mover ,100);
},
mover: function() {
var d=new Date();
showMessage("mouseX: "+mouseX+" mouseY: "+mouseY+" mousedown:"+mouseDown+" secs:"+d.getSeconds());
// called every so often to move the panorama.
if (! mouseDown) return;
var width=parseInt(panoDiv.style.width);
// work out how much to move based on position in the viewport.
var offset=-Math.round(scale*2*((mouseX-width/2)/width));
pan -= offset/scaledImgSize[0];
if (pan <0) {
pan +=1;
}
else if (pan >1) {
pan -=1;
}
**** next line doesn't work *****
pano.moveImages;
//panoPic1.style.left="-"+(pan * scaledImgSize[0])+"px";
//panoPic2.style.left=((-pan*scaledImgSize[0])+scaledImgSize[0])+"px";
},
clipImage: function(img) {
// clip an image to make it fit in the viewport.
var pos=parseInt(img.style.left);
var width=parseInt(img.style.width);
var divWidth=parseInt(panoDiv.style.width);
var leftClip, rightClip;
// clip off the left side if the image crosses the left edge of the viewport.
leftClip=(pos<0)?-pos:0;
// and the same for the right side.
rightClip=(pos+width>divWidth)?pos+divWidth:width;
img.style.clip="rect(0px,"+rightClip+"px,"+panoDiv .style.height+","+leftClip+"px)";
},
moveImages: function() {
// move images to a position depending on the pan value.
alert(here);
showMessage2("offset: "+offset+" pic x:"+panoPic1.style.left+" pan:"+pan);
panoPic1.style.left="-"+(pan * scaledImgSize[0])+"px";
panoPic2.style.left=((-pan*scaledImgSize[0])+scaledImgSize[0])+"px";
// clip the images according to where they are in the viewport.
//this.clipImage(panoPic1);
//this.clipImage(panoPic2);
},
stopDefault: function(evt) {
evt.preventDefault;
},
onMouseDown: function(evt) {
mouseDown=-1;
evt.stopPropagation;
evt.preventDefault;
},
onMouseUp: function(evt) {
mouseDown=0;
evt.stopPropagation;
evt.preventDefault;
},
onMouseMove: function(evt) {
// update the mouse coords.
mouseX=evt.clientX-dojo.style.getTotalOffset(panoDiv,"left",0);
mouseY=evt.clientY-dojo.style.getTotalOffset(panoDiv,"top",0);
evt.stopPropagation;
evt.preventDefault;
}
}
dojo.addOnLoad(pano.init);
**** HTML file follows ****
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"> <html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>Panorama-test</title>
<script type="text/javascript" src="js/dojo/dojo.js"></script> <script
type="text/javascript" src="js/pano.js"></script>
</head>
<body>
<h1 id="heading">Panorama Test
</h1>
<p id="message1"> </p>
<p id="message2"> </p>
<div style="position: relative; top: 0px; left: 0px;"> <div id="panoDiv"
style="position: absolute; top: 0px; left: 30px; width: 700px; height:
400px; clip: rect(0px, 700px, 400px, 0px);"><img id="panoImage1"
style="position: absolute; width: auto; height: 400px; left: 0px;
z-index: 10;" alt="" src="pano2.jpg"><img id="panoImage2"
style="position: absolute; width: auto; height: 400px; left: 0px;
z-index: 9;" alt="" src="pano2.jpg"></div>
</div>
<p> </p>
</body>
</html>
--
http://www.niftybits.ukfsn.org/
remove 'n-u-l-l' to email me. html mail or attachments will go in the spam
bin unless notified with [html] or [attachment] in the subject line.