Skip to main content
denimkjb
Participant
Aug 6, 2018 7:40 PM UTC
Answered

How do I animate the timeline with the mouse wheel in Animate CC?

Hi everyone! The title says it all.

My project is HTML5 Canvas

I have an animation on my main timeline and I will like to control the animation with the mouse wheel going forward and backwards, and if I don't touch the mouse wheel it won't start.

How do I do this on my action layer?

Thanks!!!

This topic is closed to new replies. Start a new post to keep the conversation going.
JoãoCésar17023019
JoãoCésar17023019Correct answer
Community Expert
Aug 6, 2018 8:31 PM UTC

UPDATED ANSWER (05/28/2021):

https://github.com/joao-cesar/adobe/tree/master/animate%20cc/html5_canvas/mouse_wheel_navigation

 

 

var that = this;
this.targetTimeline = this;
this.targetTimeline.loop = true;
this.targetTimeline.force = 40;
this.targetTimeline.friction = 0.9;
this.targetTimeline.minFrame = 0; // set the start range value here
this.targetTimeline.maxFrame = this.targetTimeline.totalFrames - 1; // set the end range value here

this.loopClamp = function(value, min, max)
{
	if (value < min)
		return max;
	
	if (value > max)
		return min;
		
	return value;
};

this.clamp = function(value, min, max)
{
	if (value < min)
		return min;
	
	if (value > max)
		return max;
		
	return value;
};

this.onMouseWheel = function (e)
{
	e.preventDefault();

	var evt = window.event || e;
	var delta = Math.max(-1, Math.min(1, evt.wheelDelta || -evt.detail));

	that.targetTimeline.speed = delta * that.force;
};

this.tickHandler = function (e)
{
	var clamp = that.targetTimeline.loop ? "loopClamp" : "clamp";
	
	that.targetTimeline.speed *= that.targetTimeline.friction;
	that.targetTimeline.gotoAndStop(that[clamp](that.targetTimeline.currentFrame + that.targetTimeline.speed, that.targetTimeline.minFrame, that.targetTimeline.maxFrame));
};

this.start = function ()
{
	canvas.addEventListener('mousewheel', that.onMouseWheel.bind(that));
	canvas.addEventListener('DOMMouseScroll', that.onMouseWheel.bind(that));
	createjs.Ticker.on("tick", that.tickHandler);
};

if (!this.hasStarted)
{
	this.gotoAndStop(this.targetTimeline.minFrame);
	this.start();
	this.hasStarted = true;
}

 

______________________________________________________________________________

 

Hi.

 

Here is my suggestion.

 

var that = this;

this.targetTimeline = this;

this.targetTimeline.loop = false;

this.force = 2;

 

this.onMouseWheel = function(e)

{

    e.preventDefault();

 

    var delta;

 

    if (e == window.event)

          delta = -10 / window.event.wheelDeltaY;

    else

          delta = e.detail / 30;

 

    that.scrubTimeline(that.targetTimeline, delta, that.force);

};

 

this.scrubTimeline = function(targetTimeline, delta, amount = 1)

{

    var direction = delta < 0 ? 1 : -1;

    targetTimeline.gotoAndStop(targetTimeline.currentFrame + (amount * direction));

};

 

this.start = function()

{

    document.getElementById('canvas').addEventListener('mousewheel', that.onMouseWheel.bind(that));

    document.getElementById('canvas').addEventListener('DOMMouseScroll', that.onMouseWheel.bind(that));

};

 

if (!this.hasStarted)

{

    this.stop();

    this.start();

    this.hasStarted = true;

}

 

I hope this helps.

 

Regards,

JC

35 replies

olliet44775419
Participant
Sep 7, 2020 12:17 PM UTC

Hi there, is there similar code to animate with page scroll rather than mouse wheel? Thanks in advance!

 

Ollie

Translate
JoãoCésar17023019
Community Expert
Community Expert
Sep 7, 2020 1:36 PM UTC

Hi.

 

This may be what you want.

https://github.com/joao-cesar/adobe/tree/master/animate%20cc/html5_canvas/animate_on_scroll

 

Please let us know.

 

Regards,

JC

Translate
olliet44775419
Participant
Sep 7, 2020 2:22 PM UTC

Hi Joao,

 

Thank you so much for getting back to me so quickly, unfortunately this does not work in the same way. In the code you originally supplied the animation plays and reverses with the mouse scroll but not the page scroll, it would be perfect if the animation was controlled in exactly the same with but with page scroll rather than mouse scroll. Do you know of a way to do this?

 

Thanks,

Ollie

Translate
Participant
Jul 31, 2020 10:26 AM UTC

I really need a solution to make it works also with mobile swipe. Can you help me please?

Translate
Participant
Jul 31, 2020 9:17 AM UTC

How to invert mouse wheel?

Translate
Participant
Jul 31, 2020 10:11 AM UTC

OK, FOUND: that.targetTimeline.speed = -delta * that.force;

Translate
Participant
Oct 24, 2019 9:54 PM UTC

Thank You!!!!

Translate
JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
Aug 6, 2018 8:31 PM UTC

UPDATED ANSWER (05/28/2021):

https://github.com/joao-cesar/adobe/tree/master/animate%20cc/html5_canvas/mouse_wheel_navigation

 

 

var that = this;
this.targetTimeline = this;
this.targetTimeline.loop = true;
this.targetTimeline.force = 40;
this.targetTimeline.friction = 0.9;
this.targetTimeline.minFrame = 0; // set the start range value here
this.targetTimeline.maxFrame = this.targetTimeline.totalFrames - 1; // set the end range value here

this.loopClamp = function(value, min, max)
{
	if (value < min)
		return max;
	
	if (value > max)
		return min;
		
	return value;
};

this.clamp = function(value, min, max)
{
	if (value < min)
		return min;
	
	if (value > max)
		return max;
		
	return value;
};

this.onMouseWheel = function (e)
{
	e.preventDefault();

	var evt = window.event || e;
	var delta = Math.max(-1, Math.min(1, evt.wheelDelta || -evt.detail));

	that.targetTimeline.speed = delta * that.force;
};

this.tickHandler = function (e)
{
	var clamp = that.targetTimeline.loop ? "loopClamp" : "clamp";
	
	that.targetTimeline.speed *= that.targetTimeline.friction;
	that.targetTimeline.gotoAndStop(that[clamp](that.targetTimeline.currentFrame + that.targetTimeline.speed, that.targetTimeline.minFrame, that.targetTimeline.maxFrame));
};

this.start = function ()
{
	canvas.addEventListener('mousewheel', that.onMouseWheel.bind(that));
	canvas.addEventListener('DOMMouseScroll', that.onMouseWheel.bind(that));
	createjs.Ticker.on("tick", that.tickHandler);
};

if (!this.hasStarted)
{
	this.gotoAndStop(this.targetTimeline.minFrame);
	this.start();
	this.hasStarted = true;
}

 

______________________________________________________________________________

 

Hi.

 

Here is my suggestion.

 

var that = this;

this.targetTimeline = this;

this.targetTimeline.loop = false;

this.force = 2;

 

this.onMouseWheel = function(e)

{

    e.preventDefault();

 

    var delta;

 

    if (e == window.event)

          delta = -10 / window.event.wheelDeltaY;

    else

          delta = e.detail / 30;

 

    that.scrubTimeline(that.targetTimeline, delta, that.force);

};

 

this.scrubTimeline = function(targetTimeline, delta, amount = 1)

{

    var direction = delta < 0 ? 1 : -1;

    targetTimeline.gotoAndStop(targetTimeline.currentFrame + (amount * direction));

};

 

this.start = function()

{

    document.getElementById('canvas').addEventListener('mousewheel', that.onMouseWheel.bind(that));

    document.getElementById('canvas').addEventListener('DOMMouseScroll', that.onMouseWheel.bind(that));

};

 

if (!this.hasStarted)

{

    this.stop();

    this.start();

    this.hasStarted = true;

}

 

I hope this helps.

 

Regards,

JC

Translate
Participant
Apr 20, 2022 11:30 AM UTC

Hi João,

 

This is great, thank you! 

I'm struggling to understand the logic behind the code so I have a couple more questions if you have the time to answer?

1. I'd like to change the direction of travel to up/down Y axis, if possibe?

2. If I wanted to add a second object to the scene (but travelling in the opposite direction) is there a way of achiving this also?

 

Many thanks!

Nick

Translate
JoãoCésar17023019
Community Expert
Community Expert
Apr 20, 2022 1:03 PM UTC

Hi, Nick!

 

If I understand correctly what you want to do, you just have to change the animation in the main timeline.

 

Because the purpose of the code is to control the main timeline with the mouse wheel or with drag. The visual effect depends more on creativity.

 

So you would animate de box going from bottom to top and another object going from top to botttom, for example.

 

Please let us know if this is what you're looking for.

 

Regards,

JC

Translate