Skip to main content
denimkjb
Participant
August 6, 2018
Answered

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

  • August 6, 2018
  • 5 replies
  • 9204 views

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 has been closed for replies.
Correct answer JoãoCésar17023019

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

5 replies

olliet44775419
Participant
September 7, 2020

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

 

Ollie

JoãoCésar17023019
Community Expert
Community Expert
September 7, 2020

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

olliet44775419
Participant
September 7, 2020

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

Participant
July 31, 2020

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

Participant
July 31, 2020

How to invert mouse wheel?

Participant
July 31, 2020

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

Participant
October 24, 2019

Thank You!!!!

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
August 6, 2018

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

Participant
April 20, 2022

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

JoãoCésar17023019
Community Expert
Community Expert
April 20, 2022

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