Background Segment Effect

A background image segment effect as seen on Filippo Bello's Portfolio, employing the CSS clip-path property and powered by anime.js.

SegmentEffect_800x600

View demo Download source

Today we’d like to share a little decorative effect with you that we’ve encountered on Filippo Bello’s Portfolio, maybe you’ve seen it. It’s a really neat way to add some jazz to background images. The idea is to replicate boxes from a background with the same background image and make these boxes move in perspective towards the viewer. Adding a fitting shadow and some parallax makes all this look quite interesting. Furthermore, we’re employing anime.js, the easy-to-use JavaScript animation library by Julian Garnier.

Have a look at the effect seen on Filippo’s portfolio:

SegmentEffect_01

SegmentEffect_02

The technique that we use for this effect is based on the CSS clip-path property. Although the technique seen on Filippo’s portfolio uses a different approach (background-size: cover combined with background-attachment: fixed), we found that Firefox does not seem to like that combination.

Update: After first using just the CSS clip property, we’ve updated the script to use clip-path when supported. CSS clip is deprecated but still works as a great fallback for clip-path (rectangular shapes). Thanks to Larry for pointing that out in the comments!

Attention: We are using 3D Transforms which are not supported in older browsers.

The Segment Effect

The script that we’ve built takes care of the basic functioning of the segment effect which includes a couple of adjustable options.

For the markup we simply need a division and a background image:

<div class="segmenter" style="background-image: url(img/3.jpg)"></div>

The script turns this into the following structure:

<div class="segmenter">
    <div class="segmenter__background" style="background-image: url(img/3.jpg);"></div>
    <div class="segmenter__pieces">
        <div class="segmenter__piece-wrap">
            <div class="segmenter__shadow" style="top: 80%; left: 10%; width: 30%; height: 20%"></div>
            <div class="segmenter__piece" style="background-image: url(img/3.jpg); -webkit-clip-path: polygon(10% 80%, 40% 80%, 40% 100%, 10% 100%); clip-path: polygon(10% 80%, 40% 80%, 40% 100%, 10% 100%)"></div>
        </div>
        <div class="segmenter__piece-wrap">
            <div class="segmenter__shadow" style="top: 2%; left: 2%; width: 40%; height: 40%"></div>
            <div class="segmenter__piece" style="background-image: url(img/3.jpg); -webkit-clip-path: polygon(2% 2%, 42% 2%, 42% 42%, 2% 42%); clip-path: polygon(2% 2%, 42% 2%, 42% 42%, 2% 42%)"></div>
        </div>
        <div class="segmenter__piece-wrap">
            <div class="segmenter__shadow" style="top: 30%; left: 60%; width: 30%; height: 60%"></div>
            <div class="segmenter__piece" style="background-image: url(img/3.jpg); -webkit-clip-path: polygon(60% 30%, 90% 30%, 90% 90%, 60% 90%); clip-path: polygon(60% 30%, 90% 30%, 90% 90%, 60% 90%)"></div>
        </div>
        <div class="segmenter__piece-wrap">
            <div class="segmenter__shadow" style="top: 10%; left: 20%; width: 50%; height: 60%"></div>
            <div class="segmenter__piece" style="background-image: url(img/3.jpg); -webkit-clip-path: polygon(20% 10%, 70% 10%, 70% 70%, 20% 70%); clip-path: polygon(20% 10%, 70% 10%, 70% 70%, 20% 70%)"></div>
        </div>
    </div>
</div>

As you can see, the images becomes a division with a background image and all pieces are wrapped into the segmenter__pieces div. Each piece consists of an element for the shadow (if set) and an element for the image piece that has a clip-path (clip: rect(), if not supported) defined based on the options input we define.

SegmentEffect_03

The styles needed for this to work are the following (vendor prefixes omitted):

.segmenter {
	width: 100vw;
	height: 100vh;
	position: relative;
	overflow: hidden;
}

.segmenter__background,
.segmenter__pieces,
.segmenter__piece-wrap,
.segmenter__piece-parallax,
.segmenter__piece {
	position: absolute;
	width: 100%;
	height: 100%;
	top: 0;
	left: 0;
	transform-style: preserve-3d;
}

.segmenter__piece-parallax {
	transition: transform 0.2s ease-out;
}

.segmenter__pieces {
	perspective: 400px;
}

.segmenter__background,
.segmenter__piece,
.segmenter {
	background-size: cover;
	background-repeat: no-repeat;
	background-position: 50% 50%;
}

.segmenter__shadow {
	position: absolute;
	opacity: 0;
	box-shadow: 0px 2px 15px rgba(0,0,0,0.7);
}

Note how the pieces all have the size of the full wrapper; it’s the clip-path that cuts them to the correct size and position. This element is hence a bit restricted as to what styles we can apply to it, i.e. we can’t just add a box shadow because the element is clipped.

SegmentEffect_04

Let’s have a look at the default options:

Segmenter.prototype.options = {
	// Number of pieces.
	pieces: 4, 
	// Show pieces already styled.
	renderOnLoad: false,
	// Add an element for the shadow.
	shadows: true,
	// Animations for the shadow (valid properties: opacity, translateX, translateY).
	shadowsAnimation: {
		opacity: 1,
		// translateX: 20,
		// translateY: 20
	},
	// Parallax effect for the pieces.
	parallax: false,
	// Movements for the parallax effect.
	parallaxMovement: {min: 10, max: 40},
	// Animation for the pieces (valid properties: duration, easing, delay, opacity, translate[XYZ]).
	animation: {
		duration: 1500,
		easing: 'easeOutQuad',
		delay: 0, // Delay increment per piece.
		// opacity: 0.5,
		translateZ: {min: 10, max: 65}, // We can also use an integer for a specific value.
		// translateX: {min: -100, max: 100}, // We can also use an integer for a specific value.
		// translateY: {min: -100, max: 100} // We can also use an integer for a specific value.
	},
	// Callbacks
	onAnimationComplete: function() { return false; },
	onAnimationStart: function() { return false; },
	// The positions of the pieces in percentage values. 
	// We can also use random values by setting options.positions to "random".
	positions: [
	{
		top: 80, left: 10, width: 30, height: 20
	},
	{
		top: 2, left: 2, width: 40, height: 40
	},
	{
		top: 30, left: 60, width: 30, height: 60
	},
	{
		top: 10, left: 20, width: 50, height: 60
	}]
};

SegmentEffect_05

The options allow for a variety of looks, including the extreme abuse of the parallax effect to a psychedelic absurdum (in combination with several same-sized pieces):

SegmentEffect_06

We hope you enjoy this little effect and find it useful!

References and Credits

Note, that we’ve updated the script after publication to use clip-path (and clip as fallback only). Don’t forget to download the new version.

Browser Support:
  • ChromeSupported
  • FirefoxSupported
  • Internet ExplorerSupported from version 11
  • SafariSupported
  • OperaSupported

View demo Download source

Previous:
Next:

Tagged with:

ML is a freelance web designer and developer with a passion for interaction design. She studied Cognitive Science and Computational Logic and has a weakness for the smell of freshly ground peppercorns.

View all contributions by

Website: http://www.codrops.com

Related Articles

CSS Reference

Learn about all important CSS properties from the basics with our extensive and easy-to-read CSS Reference.

It doesn't matter if you are a beginner or intermediate, start leanirng CSS now.

Feedback 39

  1. 3

    I totally agree about the clip tecniche approach! For Filippo’s website I’ve used the fixed/covered tecnique, but if I had to do it back I’ll probably go for your approach!

    Nice job ????

    • 4

      Hi Alessandro :) Your fixed/covered technique is way nicer, really a pity that FF does not play along! Thanks a lot for your feedback and really awesome work! Cheers, ML

    • 11

      Hi Larry,
      good catch, we were so focused on making this effect work that we totally missed that! Of course, clip-path should be used instead when supported. We’ve updated the script with that new feature. Thanks a bunch again and cheers, ML

  2. 12

    I don’t know what to say. You are developing ideas faster than I can find words to describe how amazing your demos are. So a simple, understated thank you will have to do.

    • 18

      i’m also having problems to trigger the animation onload. can anyone help?

      whats wrong with this code?
      onReady: function() { segmenter.animate(); }

    • 19

      Thanks for pointing that out guys. There was a small bug which is now fixed. Could you please try again? Cheers.

  3. 20

    Manoela: Sweetheart, this is a fantastic effect. I have very limited knowledge of coding, but I was able to manage make them work. However, after I made them work I thought the way they are presented is useless. Again, with my limited knowledge I did not know how to get out. I fire an effect, it plays but then what would I do for the next step? Bellos site has this as an autoplay slider so it has some use.

    But, I do love you though!

  4. 22

    A lot of thanks ´Cause with you incredible work and knowledge you are changing the visual assets in websites around all internet. Thanks again!

  5. 23

    Is this effect possible with a shape other then a square or circle ? Something like a hexagon or custom .svg shape ? I was inspired by this demo to experiment with an – but ran into some issues with the shadow effect and image positioning.

  6. 25

    hello ! could you please tell me how to launch this effect right after the page finished to load ?
    thanks you very much =D

    • 26

      hello ! could you please tell me how to launch this effect right after the page finished to load ?

      thank you………!

Follow this discussion

Leave a Comment