Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing scriptaculous effect duration mid-effect

Status
Not open for further replies.
Dec 8, 2003
17,047
GB
Has anyone had any luck changing the duration that a scriptaculous effect takes - while the effect is in progress?

I tried the following, but it appears not to work, jumping to the end of the effect pretty much immediately.

For example, if I use an initial duration of 2.0, and then call this with 5.0 before halfway through the effect, the effect ends instead of carrying on for longer.

My guess is that the effect is picking up on some of the changes before all have had a chance to be set.

This is the code I'm using:

Code:
function changeEffectSpeed: function(newSpeed) {
	var queue = Effect.Queues.get('queueName');
	queue.each(function(theEffect) {
		var percentageThroughEffect = theEffect.currentFrame / theEffect.totalFrames; 

		// Copied from Effect.Base
		theEffect.options.duration = newSpeed;
		theEffect.startOn      = theEffect.options.delay * 1000;
		theEffect.finishOn     = theEffect.startOn + (theEffect.options.duration * 1000);
		theEffect.fromToDelta  = theEffect.options.to - theEffect.options.from;
		theEffect.totalTime    = theEffect.finishOn - theEffect.startOn;
		theEffect.totalFrames  = theEffect.options.fps * theEffect.options.duration;

		// Now set the current frame based upon the new duration
		theEffect.currentFrame = theEffect.totalFrames * percentageThroughEffect;
	});
}

Thanks,

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Aaah - I found it.

After digging a bit more, I realised I needed to add the timestamp to the start and end times:

Code:
function changeEffectSpeed: function(newSpeed) {
	var queue = Effect.Queues.get('queueName');
	queue.each(function(theEffect) {
		var percentageThroughEffect = theEffect.currentFrame / theEffect.totalFrames;

		[!]// Calculate the timestamp when the effect was started
		var startEndDelta = theEffect.finishOn - theEffect.startOn;
		var timeStamp = theEffect.finishOn - startEndDelta;[/!]

		// Copied from Effect.Base
		theEffect.options.duration = newSpeed;
		theEffect.startOn      = theEffect.options.delay * 1000;
		theEffect.finishOn     = theEffect.startOn + (theEffect.options.duration * 1000);
		theEffect.fromToDelta  = theEffect.options.to - theEffect.options.from;
		theEffect.totalTime    = theEffect.finishOn - theEffect.startOn;
		theEffect.totalFrames  = theEffect.options.fps * theEffect.options.duration;

		[!]theEffect.startOn += timeStamp;
		theEffect.finishOn += timeStamp;[/!]

		// Now set the current frame based upon the new duration
		theEffect.currentFrame = theEffect.totalFrames * percentageThroughEffect;
	});
}

It works a treat!

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top