Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="kaleidoscope" class="kaleidoscope"></div>
              
            
!

CSS

              
                // Use segmentCount to add styles for the required number of segments.
// Only 6 and 8 work for now because I can't be bothered to fix the image overlap
$segmentCount: 8;
$step: 360 / $segmentCount;
$bgColour: rgb(51, 82, 102);
$imageUrl: 'https://assets.codepen.io/1366040/kaleidoscope-flower.svg';
// Uncomment this for a different photo
// $imageUrl: 'https://picsum.photos/1000';
// $imageUrl: 'https://assets.codepen.io/1366040/5328478.jpg';
// $imageUrl: 'https://assets.codepen.io/1366040/waves.png';

html, body {
	height: 100vh;
	overflow: hidden;
}

// Make the animation take the whole available space
.kaleidoscope {
	width: 100vw;
	height: 100vh;
	overflow: hidden;
	background: $bgColour;

	// Place all segments in the bottom left quadrant of the screen
	&__segment {
		position: absolute;
		left: 0;
		top: 50%;
		height: 100vmax;
		width: 50%;
		z-index: 0;
		overflow: hidden;
		// Use the top right corner as the attachment point for when we're doing our rotations
		transform-origin: 100% 0;
		
		// Rotate each segment by an appropriate number of degrees so that they cover the whole space. 
		// Every one of them will have their initial top right corner in the middle of the screen
		@for $i from 1 through $segmentCount {
			&:nth-child(#{$i}) {
				transform: rotate(#{$step * $i}deg);
			}
		}
	}

	// Image is actually a div / wrapper inside the kaleidoscope__section element
	// We apply a background image to it and rotate it so that only a portion of it is visible
	&__image {
		position: relative;
		top: 0;
		left: 100%;
		height: 100%;
		width: 100%;
		background-image: url($imageUrl);
		background-position: center;
		transform-origin: 0 0;
		// scaleX(-1) will flip the image horizontally so we get the mirror-like reflection in each pair of sections
		transform: scaleX(-1) rotate(0deg);
		transition: background-position 1s;

		.kaleidoscope__segment:nth-child(2n) & {
			// Reverse the flip on images in every other section
			transform: rotate(#{360 / $segmentCount}deg);
		}
	}
	
}
              
            
!

JS

              
                (function () {
	// Either 6 or 8 for now, please!
	const segmentCount = 8;
	const wrapper = document.querySelector('#kaleidoscope');
	let images = createSegments(segmentCount, wrapper);

	// Generate the segments and image wrappers with js
	// Could be done on the BE if that's your thing
	function createSegments(count, wrapper) {
		let images = [];

		for(let i = 0; i < count; i++) {
			const element = document.createElement('div');
			element.classList.add('kaleidoscope__segment');

			const image = document.createElement('div');
			image.classList.add('kaleidoscope__image');

			element.appendChild(image);
			images.push(image);
			wrapper.appendChild(element);
		}

		return images;
	}

	// Update background position on mouse move
	function handleMouseMove(e) {
		const nx = e.pageX;
		const ny = -e.pageY;

		images.forEach(image => {
			image.style.backgroundPosition = [nx + "px", ny + "px"].join(' ');
		});
	}

	// Do not do the animation for people who do not want it
	if(window.matchMedia("(prefers-reduced-motion: no-preference)")) {
		wrapper.addEventListener("mousemove", handleMouseMove);
	}
})();
              
            
!
999px

Console