Copy link to clipboard
Copied
Image Comparison Slider
Hi Guys! Can anyone share to me how did they make this?
I need to know this very much. Thanks in advance!
Link -----> Rich Media Gallery
There are a few ways to do that. Assuming you want it to work in HTML5 Canvas, a simple way is to set up a timeline where there is a mask layer, that is a shape, and on the first frame the shape is a thin line on the left, and on the last frame it has stretched to fill the width of the photo. You put one of the photos in a lower layer, and then the other photo just under the mask layer, so that it's masked.
After that you would have code that looks at how far the mouse is across the screen, and y
...Copy link to clipboard
Copied
There are a few ways to do that. Assuming you want it to work in HTML5 Canvas, a simple way is to set up a timeline where there is a mask layer, that is a shape, and on the first frame the shape is a thin line on the left, and on the last frame it has stretched to fill the width of the photo. You put one of the photos in a lower layer, and then the other photo just under the mask layer, so that it's masked.
After that you would have code that looks at how far the mouse is across the screen, and you gotoAndStop() on the corresponding frame. There's no particular reason to use a few number of frames, so you could use as many frames as there are pixels across the stage. Then you are effectively just doing this.gotoAndStop(Math.round(stage.mouseX));
Copy link to clipboard
Copied
Thanks Colin, I will try that
Copy link to clipboard
Copied
Hi Colin!, is it possible to stop the function of | this.gotoAndStop(Math.round(stage.mouseX)); |???
I have a problem now when I add another image.. I will provide the file below thanks a lot!
Copy link to clipboard
Copied
I couldn't figure out when the ticks were not moving things, but there were several other things that were wrong. So I fixed the other wrong things and changed the way that the code is working. Here's the FLA:
http://colin.scienceninja.com/beforeafter3.zip
I deleted the three copies of this.stop() in other layers, and just added it to the one script, which I'll show below.
The tweens in the timeline don't want to have any easing in them. The user is moving the mouse, and will do their own easing.
I trimmed some frames at the end, which I think were there so that the tween reached the end, which it wasn't, because it had easing in it.
The main thing I changed was to use mouse down, press move, and press. For those to work on touch screens you have to enable Touch. Doing that does have a drawback in that you can't really test local files, other than from Animate. It should work fine on a server.
Here is the code in the first frame of the 'action' layer. Several parts are hard coded numbers, there's probably a better way to too that:
this.stop();
if (!(window.init==true)) {
createjs.Touch.enable(stage, false, true);
this.cover.addEventListener("mousedown",startpan.bind(this));
window.min = 0;
window.max = 190
window.movescale = 336 / (window.max-window.min);
window.init = true;
}
function startpan(){
this.cover.addEventListener("pressmove",movepan.bind(this));
this.cover.addEventListener("pressup",stoppan.bind(this));
}
function movepan(e) {
var mp = Math.round((e.stageX / stage.scaleX)/window.movescale);
var f = Math.max(window.min,Math.min(window.max,mp));
this.gotoAndStop(f);
}
function stoppan(e) {
this.cover.removeEventListener("pressmove",movepan);
this.cover.removeEventListener("pressup",stoppan);
}
It may be bad practices, but I'm finding that using window variables is very handy. To avoid having multiple mousedown listeners I use 'window.init' to check if I have set things up already.
'cover' is your border, but changed into a movieclip, just for me to add the listeners to it. In the end it wasn't needed, but I left it there anyway.
'window.min' and 'window.max' are the frame numbers for the first and last frames of the reveal pan.
You want the movement of the mouse across the stage to end up controlling the min to max frame range. I store that scaling in the 'window.movescale' variable. '336' is the width of your stage.
'startpan' and 'stoppan' just set up and remove the listeners that are needed. Everything clever happens in 'move pan'!
The 'e' in movepan includes the current stageX position of the mouse, or touch. But, the real position it is gets affected by the stage scaleX, so I divide the claimed mouse position by the stage scaleX, and that gives me where the mouse really is. You should find that you can do any changes in the publish settings to make the stage fill the browser window, and still it should work.
The /window.movescale part will take the true mouseX and will make it go to the right frame in the range.
The Math.max, Math.min part is to make sure you don't go to a frame outside of the range. If the rest of the arithmetic is correct, that shouldn't be needed, but I found that it was possible to a frame past the end. Changing 'var mp = Math.round(' to 'var mp = Math.floor(' does take care of that, but I would rather use the max/min line for peace of mind.
Let me know if any of that made sense!
Copy link to clipboard
Copied
https://forums.adobe.com/people/Colin+Holgate wrote
It may be bad practices, but I'm finding that using window variables is very handy. To avoid having multiple mousedown listeners I use 'window.init' to check if I have set things up already.
Copy link to clipboard
Copied
Yes, I know about that, but I'm also setting up global variables at the same time. Checking a global variable to know if you've initialized works well even if you're adding no listeners.
In this case though I could have used the presence of a listener instead of a global Boolean, to know whether to set everything up again.
Copy link to clipboard
Copied
Thanks colin! we appreciate your help. we will try this one..
btw.. we want to achieve something like this..
Find more inspiration, events, and resources on the new Adobe Community
Explore Now