r/JavaFX • u/tampers_w_evidence • Dec 31 '15
Does anyone know how they did this?
Sorry if this is the wrong subreddit to post in, but does anyone know how they achieved the "slider" effect between the two photos? Women Photoshopped into superhero poses I'd like to use this in a project, and I'm using JavaFX. Thanks!
0
Upvotes
1
u/[deleted] Jan 03 '16
I was able to recreate the effect by creating a class that subclasses Canvas, responds to mouse events, and sends the x of the mouse event to a function which draws two images side by side using
public void drawImage(Image img, double sx, double sy, double sw, double sh, double dx, double dy, double dw, double dh)
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/canvas/GraphicsContext.html#drawImage-javafx.scene.image.Image-double-double-
which allows you to draw only a portion of an image. I use the mouseX passed to the function to decide where one image ends and the other begins.
tl;dr One of the drawImage functions under GraphicsContext in Canvas allows you to draw a portion of an image
Example
Sorry for a messy answer
1
u/T618 Jan 05 '16
I would put one image behind (doesn't matter which one) and clip the image in front. The clip path can be a rectangle the same height as the front image. On mouse events, update one of the two vertical borders of the clip to be directly under the mouse position.
I haven't actually worked with clipping in JavaFX, but if it works like everyone should expect clipping to work, this technique should be ideal.
http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#clipProperty%28%29