Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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

              
                <label class="saw-switch">
	<span class="saw-switch__inner">
		<input class="saw-switch__input" type="checkbox" value="on">
		<span class="saw-switch__sr">Saw Switch</span>
		<span class="saw-switch__blade"></span>
		<span class="saw-switch__blade"></span>
		<span class="saw-switch__blade"></span>
		<span class="saw-switch__blade"></span>
		<span class="saw-switch__blade"></span>
		<span class="saw-switch__blade"></span>
		<span class="saw-switch__blade"></span>
		<span class="saw-switch__blade"></span>
		<span class="saw-switch__blade"></span>
		<span class="saw-switch__blade"></span>
		<span class="saw-switch__blade"></span>
		<span class="saw-switch__blade"></span>
		<span class="saw-switch__blade"></span>
		<span class="saw-switch__blade"></span>
	</span>
</label>
              
            
!

CSS

              
                *
	border: 0
	box-sizing: border-box
	margin: 0
	padding: 0

\:root
	font-size: calc(24px + (32 - 24) * (100vw - 320px) / (1280 - 320))

body, input
	font: 1em/1.5 sans-serif

body
	background: hsl(223,10%,70%)
	height: 100vh
	display: grid
	place-items: center

.saw-switch
	$durA: 99999s
	$durB: 1.2s
	border-radius: 2.5em
	width: 8em
	height: 5em

	&__inner,
	&__blade,
	&__particle
		pointer-events: none

	&__inner,
	&__input
		position: relative

	&__inner
		display: block
		margin: 1em
		width: calc(100% - 2em)
		height: calc(100% - 2em)
		-webkit-tap-highlight-color: transparent

	&__input
		background-color: hsl(3,90%,55%)
		border-radius: 1.5em
		box-shadow: 0 0 0 0.2em inset
		color: hsl(223,10%,90%)
		width: 100%
		height: 100%
		transition: background-color 0.25s ease-in-out
		-webkit-appearance: none
		appearance: none
		&:before
			border-radius: 50%
			content: ""
			display: block
			top: 0.5em
			left: 0.5em
			width: 2em
			height: 2em
			transition: transform 0.25s ease-in-out
		&:checked
			background-color: hsl(123,90%,45%)
			&:before
				transform: translateX(3em)

	&__input:before,
	&__blade
		background-color: hsl(223,10%,90%)
		position: absolute

	&__blade
		animation: revolve $durA linear infinite
		clip-path: polygon(50% 0,100% 100%,0 100%)
		bottom: 50%
		left: 1em
		width: 1em
		height: 1em
		transform: translate(0em,-1.4em) rotate(0deg)
		transform-origin: 50% 2.4em // triangle height + 1.4em

	&__input:checked ~ &__blade
		animation-duration: $durB

	@for $b from 3 through 16
		&__input ~ &__blade:nth-child(#{$b})
			animation-delay: ($durA * -1 / 14) * ($b - 2)
		&__input:checked ~ &__blade:nth-child(#{$b})
			animation-delay: ($durB * -1 / 14) * ($b - 2)
		
	&__particle
		background-color: hsl(3,90%,40%)
		border-radius: 50%
		display: block
		opacity: 0
		position: fixed
		top: -0.25em
		left: -0.25em
		width: 0.5em
		height: 0.5em
		z-index: 1

	&__sr
		clip: rect(1px,1px,1px,1px)
		overflow: hidden
		position: absolute
		width: 1px
		height: 1px

/* Animations */
@keyframes revolve
	from
		transform: translate(0,-1.4em) rotate(0)
	30%
		transform: translate(0,-1.4em) rotate(-0.5turn)
	50%
		transform: translate(3em,-1.4em) rotate(-0.5turn)
	80%
		transform: translate(3em,-1.4em) rotate(-1turn)
	to
		transform: translate(0,-1.4em) rotate(-1turn)
              
            
!

JS

              
                window.addEventListener("DOMContentLoaded",() => {
	const sawSwitch = new SawSwitch(".saw-switch");
});

class SawSwitch {
	constructor(el) {
		this.el = document.querySelector(el);
		this.input = this.el.querySelector(".saw-switch__input");
		this.touching = false;
		this.cursor = {
			x: 0,
			y: 0
		};
		this.isMobile = "ontouchstart" in document.documentElement;

		if (this.el) {
			this.el.addEventListener(this.isMobile ? "touchmove" : "mousemove",this.updateCursor.bind(this));
			this.el.addEventListener(this.isMobile ? "touchend" : "mouseout",this.stopBlood.bind(this));
		}
	}
	updateCursor(e) {
		const event = this.isMobile ? e.touches[0] : e;
		// coords
		this.cursor.x = event.pageX;
		this.cursor.y = event.pageY;
		const {x,y} = this.cursor;
		// bounds of the switch
		const bounds = this.el.getBoundingClientRect();
		const {top,left,width,height} = bounds;
		// collision
		const outsideX = x < left || x > left + width;
		const outsideY = y < top || y > top + height;
		const outsideSwitch = outsideX || outsideY;
		const withinX = x >= left + width * (1 / 8) && x <= left + width * (7 / 8);
		const withinY = y >= top + height * (1 / 5) && y <= top + height * (4 / 5);
		const withinBlades = withinX && withinY;

		if (this.input.checked && !outsideSwitch && !withinBlades) {
			if (!this.touching) {
				this.touching = true;
				this.drawBlood();
			}

		} else {
			this.touching = false;
		}
	}
	drawBlood() {
		if (this.touching && this.input.checked) {
			this.createParticle(this.cursor.x,this.cursor.y);
			requestAnimationFrame(this.drawBlood.bind(this));
		}
	}
	stopBlood() {
		this.touching = false;
	}
	randomInt(min,max) {
		return Math.round(Math.random() * (max - min)) + min;
	}
	createParticle(x,y) {
		const particle = document.createElement("span");
		particle.className = "saw-switch__particle";
		this.el.appendChild(particle);

		const size = this.randomInt(1,6) * 0.1;
		const maxD = 5;

		particle.style.width = `${size}em`;
		particle.style.height = `${size}em`;

		const dx = +((Math.random() - 0.5) * maxD).toFixed(2);
		const dy = +((Math.random() - 0.5) * maxD).toFixed(2);
		const anim = particle.animate(
			[
				{
					transform: `translate(${x}px,${y}px) scale(1)`,
					opacity: 1
				},
				{
					transform: `translate(calc(${x}px + ${dx}em),calc(${y}px + ${dy}em)) scale(0)`,
					opacity: 1
				}
			], {
				duration: this.randomInt(250,300),
				easing: "ease-out",
				delay: this.randomInt(0,50)
			}
		);

		anim.onfinish = () => {
			particle.remove();
		};
	}
}
              
            
!
999px

Console