Skip to main content
Known Participant
July 4, 2020
Answered

Media playbar for HTML5 animation

  • July 4, 2020
  • 2 replies
  • 710 views

Hi Team

I've developed an interactive animation using HTML5 canvas.

I intend to have a media playbar in the animation so that the user can move to his desired portion of the animation at any point in time.

How do I do it?

Regards

 

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    Hi.

     

    Here is a sample. I hope it helps.

     

    Preview:

    https://bit.ly/2VUeAwN

     

    JavaScript code (just put it in the first frame of the FLA and change the player and target instance names):

    	var root = this;
    	var player;
    
    	root.start = function()
    	{
    		document.body.style.backgroundColor = "black";
    		player = new root.MovieClipPlayer( { player: root.player, target: this } ); // target is the Movie Clip instance you want to control
    	};
    
    	if (!root.frame0Started)
    	{
    		root.MovieClipPlayer = function(props)
    		{
    			this.player = props.player;
    			this.target = props.target;
    			this.start();
    		};
    		
    		root.MovieClipPlayer.prototype.start = function()
    		{
    			if (this.isMobile())
    				createjs.Touch.enable(this.player.stage);
    			
    			this.player.stage.mouseMoveOutside = true;
    			this.mouseDown = this.player.on("mousedown", this.mouseDownHandler, this);
    			this.pressUp = this.player.stage.on("pressup", this.pressUpHandler, this);
    			this.tick = createjs.Ticker.on("tick", this.tickHandler, this);
    		}
    		
    		root.MovieClipPlayer.prototype.mouseDownHandler = function(e)
    		{
    			if (this.player.contains(e.target))
    			{
    				if (e.target === this.player.bar || e.target === this.player.playHead)
    				{
    					this.paused = this.target.paused;
    					this.dragging = true;
    				}				
    				else if (e.target === this.player.playPause || this.player.playPause.contains(e.target))
    				{
    					if (this.target.paused)
    						this.target.play();
    					else
    						this.target.stop();
    				}
    			}		
    		};
    		
    		root.MovieClipPlayer.prototype.tickHandler = function(e)
    		{
    			this.mousePos = this.player.globalToLocal(this.player.stage.mouseX, this.player.stage.mouseY);
    			this.ratioX = this.mousePos.x / this.player.bar.nominalBounds.width;
    			
    			if (this.dragging)
    				this.target.gotoAndStop(Math.floor(this.target.totalFrames * this.ratioX));
    			
    			this.player.playHead.x = (this.target.currentFrame / this.target.totalFrames) * this.player.bar.nominalBounds.width;
    			this.player.playPause.gotoAndStop(this.target.paused ? 0 : 1);
    		};
    		
    		root.MovieClipPlayer.prototype.pressUpHandler = function(e)
    		{
    			this.dragging = false;
    			
    			if (!this.paused)
    				this.target.play();
    		};
    		
    		root.MovieClipPlayer.prototype.isMobile = function()
    		{
    			return createjs.BrowserDetect.isWindowPhone || createjs.BrowserDetect.isIOS  || createjs.BrowserDetect.isAndroid  || createjs.BrowserDetect.isBlackberry;
    		};
    		
    		root.start();
    		root.frame0Started = true;
    	}

     

    Code / source / FLA:

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

     

    Regards,

    JC

     

    2 replies

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    July 6, 2020

    Hi.

     

    Here is a sample. I hope it helps.

     

    Preview:

    https://bit.ly/2VUeAwN

     

    JavaScript code (just put it in the first frame of the FLA and change the player and target instance names):

    	var root = this;
    	var player;
    
    	root.start = function()
    	{
    		document.body.style.backgroundColor = "black";
    		player = new root.MovieClipPlayer( { player: root.player, target: this } ); // target is the Movie Clip instance you want to control
    	};
    
    	if (!root.frame0Started)
    	{
    		root.MovieClipPlayer = function(props)
    		{
    			this.player = props.player;
    			this.target = props.target;
    			this.start();
    		};
    		
    		root.MovieClipPlayer.prototype.start = function()
    		{
    			if (this.isMobile())
    				createjs.Touch.enable(this.player.stage);
    			
    			this.player.stage.mouseMoveOutside = true;
    			this.mouseDown = this.player.on("mousedown", this.mouseDownHandler, this);
    			this.pressUp = this.player.stage.on("pressup", this.pressUpHandler, this);
    			this.tick = createjs.Ticker.on("tick", this.tickHandler, this);
    		}
    		
    		root.MovieClipPlayer.prototype.mouseDownHandler = function(e)
    		{
    			if (this.player.contains(e.target))
    			{
    				if (e.target === this.player.bar || e.target === this.player.playHead)
    				{
    					this.paused = this.target.paused;
    					this.dragging = true;
    				}				
    				else if (e.target === this.player.playPause || this.player.playPause.contains(e.target))
    				{
    					if (this.target.paused)
    						this.target.play();
    					else
    						this.target.stop();
    				}
    			}		
    		};
    		
    		root.MovieClipPlayer.prototype.tickHandler = function(e)
    		{
    			this.mousePos = this.player.globalToLocal(this.player.stage.mouseX, this.player.stage.mouseY);
    			this.ratioX = this.mousePos.x / this.player.bar.nominalBounds.width;
    			
    			if (this.dragging)
    				this.target.gotoAndStop(Math.floor(this.target.totalFrames * this.ratioX));
    			
    			this.player.playHead.x = (this.target.currentFrame / this.target.totalFrames) * this.player.bar.nominalBounds.width;
    			this.player.playPause.gotoAndStop(this.target.paused ? 0 : 1);
    		};
    		
    		root.MovieClipPlayer.prototype.pressUpHandler = function(e)
    		{
    			this.dragging = false;
    			
    			if (!this.paused)
    				this.target.play();
    		};
    		
    		root.MovieClipPlayer.prototype.isMobile = function()
    		{
    			return createjs.BrowserDetect.isWindowPhone || createjs.BrowserDetect.isIOS  || createjs.BrowserDetect.isAndroid  || createjs.BrowserDetect.isBlackberry;
    		};
    		
    		root.start();
    		root.frame0Started = true;
    	}

     

    Code / source / FLA:

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

     

    Regards,

    JC

     

    Known Participant
    July 7, 2020

    Thanks for the reply JC!

    Will check and revert asap

    Known Participant
    July 8, 2020

    JC

    The playbar works like a charm!! Thanks a ton mate!

    Regards

    Known Participant
    July 6, 2020

    Hi

    Anybody who can pitch in your views? I'm kinda running short of time and your advice would be really helpful.

    Regards