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

              
                .fade
.title the Pool Table
	small trigonometry is hard
.controls
	button.reset Reset
.table
	.table__pockets
		each val in [1, 2, 3, 4, 5, 6]
			span
	.table__inner
		.ball.primary
		each val in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
			.ball.racked(class='num-' + val)
	.table__sunk
a.branding(href="https://jboxcreative.com", target="_blank")
	img(
		src="https://jboxcreative.com/wp-content/uploads/2018/12/jbox-logo-abstract-white-200x200.png"
	)

              
            
!

CSS

              
                * {
	box-sizing: border-box;
}

body {
	min-height: 100vh;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	background-image: url("https://assets.codepen.io/450456/seamless-wood-floor-texture-free_1.jpeg");
	background-size: 350px;
	overflow: hidden;
}

.title {
	user-select: none;
	position: absolute;
	z-index: 20;
	font-family: "Major Mono Display", monospace;
	color: white;
	top: 16px;
	right: 16px;
	font-size: 28px;
	text-align: right;
	-webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale;
	word-spacing: -5px;
	letter-spacing: -1px;
	padding-bottom: 15px;
	text-shadow: 4px 4px 2px rgba(black, 0.5);

	small {
		font-size: 14px;
		position: absolute;
		top: 120%;
		right: 0;
	}

	&::before {
		content: "";
		position: absolute;
		bottom: 0;
		opacity: 0.25;
		right: 0px;
		height: 3px;
		width: 87px;
		background: white;
	}
}

.controls {
	position: relative;
	margin-bottom: 10px;

	button {
		border: 1px solid white;
		background: none;
		appearance: none;
		color: white;
		padding: 5px 10px;
		border-radius: 4px;
		text-transform: uppercase;
		font-size: 11px;
		cursor: pointer;

		&:hover {
			background: white;
			color: #333;
		}
	}
}

.fade {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-image: radial-gradient(rgba(black, 0), black);
}

.table {
	width: 540px;
	height: 310px;
	border-radius: 10px;
	background: #5b230d;
	position: relative;
	display: flex;
	padding: 20px;
	box-shadow: 0px 0px 15px 2px rgba(black, 0.5),
		inset 0px 0px 5px rgba(black, 0.5);

	&__pockets {
		position: absolute;
		z-index: 1;
		pointer-events: none;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		span {
			width: 25px;
			height: 25px;
			border-radius: 100%;
			background: black;
			position: absolute;
			background: linear-gradient(0deg, black, #111);
			box-shadow: inset 0 3px black, 0px 0px 5px rgba(black, 0.5);
			border: 1px solid rgba(black, 0.5);

			&:nth-of-type(1) {
				top: 7px;
				left: 7px;
			}

			&:nth-of-type(2) {
				top: 7px;
				right: 7px;
			}

			&:nth-of-type(3) {
				top: 7px;
				left: calc(50% - 12.5px);
			}

			&:nth-of-type(4) {
				bottom: 7px;
				left: 7px;
			}

			&:nth-of-type(5) {
				bottom: 7px;
				left: calc(50% - 12.5px);
			}

			&:nth-of-type(6) {
				bottom: 7px;
				right: 7px;
			}
		}
	}

	&__inner {
		background-image: url(https://st3.depositphotos.com/4072575/15241/i/600/depositphotos_152418750-stock-photo-green-felt-texture-for-poker.jpg);
		background-size: 100px;
		border-radius: 10px;
		width: 100%;
		height: 100%;
		box-shadow: inset 0px 0px 10px rgba(black, 0.5), 0px 0px 5px rgba(black, 0.5);
		position: relative;
	}

	&__sunk {
		position: absolute;
		top: calc(100% + 20px);
		left: 50%;
		transform: translateX(-50%);
		display: flex;
		padding: 5px;
		border-radius: 4px;

		.ball {
			margin: 0 2px;
			position: relative;
			top: 0 !important;
			left: 0 !important;
			box-shadow: 0px 0px 3px 1px rgba(black, 0.5);
		}

		&:not(:empty) {
			background: #bbb;
		}
	}
}

.ball {
	width: 20px;
	height: 20px;
	border-radius: 100%;
	position: absolute;
	background: white;
	overflow: hidden;
	// box-shadow: inset 0px 0px 2px black;

	&::before {
		content: "";
		position: absolute;
		z-index: 1;
		background: radial-gradient(rgba(black, 0), rgba(black, 0.35));
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		border-radius: 100%;
	}

	&::after {
		content: "";
		position: absolute;
		width: 30px;
		height: 10px;
		top: 5px;
		left: -5px;
	}

	&__aimer {
		position: absolute;
		top: 7.5px;
		left: -50px;
		width: 40px;
		height: 5px;
		border: 1px solid white;
		transform-origin: 60px 2.5px;
		overflow: hidden;

		span {
			background: white;
			position: absolute;
			top: 0;
			right: 0;
			height: 100%;
			width: 0;
		}
	}

	&.primary {
		top: calc(50% - 10px);
		left: 100px;
		overflow: visible;

		&.active {
			box-shadow: 0px 0px 5px black;
		}
	}

	&.num-1 {
		background: #e8952d;

		&.racked {
			left: 300px;
			top: calc(50% - 10px);
		}
	}

	&.num-2 {
		background: #07486e;

		&.racked {
			left: 320px;
			top: calc(50% - 21px);
		}
	}

	&.num-3 {
		background: #bd263a;

		&.racked {
			left: 320px;
			top: calc(50% + 1px);
		}
	}

	&.num-4 {
		background: #3a2e51;

		&.racked {
			left: 340px;
			top: calc(50% - 32px);
		}
	}

	&.num-5 {
		background: #de5136;

		&.racked {
			left: 340px;
			top: calc(50% + 12px);
		}
	}

	&.num-6 {
		background: #134a30;

		&.racked {
			left: 360px;
			top: calc(50% - 42px);
		}
	}

	&.num-7 {
		background: #602f2c;

		&.racked {
			left: 360px;
			top: calc(50% - 20px);
		}
	}

	&.num-8 {
		background: #111;

		&.racked {
			left: 340px;
			top: calc(50% - 10px);
		}
	}

	&.num-9 {
		&::after {
			display: block;
			background: #eb992f;
		}

		&.racked {
			left: 360px;
			top: calc(50% + 2px);
		}
	}

	&.num-10 {
		&::after {
			display: block;
			background: #07486e;
		}

		&.racked {
			left: 360px;
			top: calc(50% + 24px);
		}
	}

	&.num-11 {
		&::after {
			display: block;
			background: #bd263a;
		}

		&.racked {
			left: 380px;
			top: calc(50% - 52px);
		}
	}

	&.num-12 {
		&::after {
			display: block;
			background: #3a2e51;
		}

		&.racked {
			left: 380px;
			top: calc(50% - 30px);
		}
	}

	&.num-13 {
		&::after {
			display: block;
			background: #de5136;
		}

		&.racked {
			left: 380px;
			top: calc(50% - 8px);
		}
	}

	&.num-14 {
		&::after {
			display: block;
			background: #134a30;
		}

		&.racked {
			left: 380px;
			top: calc(50% + 14px);
		}
	}

	&.num-15 {
		&::after {
			display: block;
			background: #602f2c;
		}

		&.racked {
			left: 380px;
			top: calc(50% + 36px);
		}
	}
}

.branding {
	position: fixed;
	bottom: 10px;
	right: 10px;

	img {
		width: 50px;
	}
}

              
            
!

JS

              
                const balls = $(".ball");
const pockets = $(".table__pockets span");

$(".reset").on("click", function () {
	$(".table__sunk .ball").appendTo(".table__inner");
	$(".ball").removeClass("sunk").attr("style", "").data("velocity", 0);
});

$(".primary").on("mousedown", function (e) {
	$(this).addClass("active");
	$(this).append('<div class="ball__aimer"><span></span></div>');
});

$(document).on("mousemove", function (e) {
	if ($(".ball__aimer").length) {
		const ballX = $(".primary").offset().left + 10;
		const ballY = $(".primary").offset().top + 10;
		const angle = calcAngle(e.pageX, e.pageY, ballX, ballY);
		$(".ball__aimer").css("transform", "rotate(" + angle + "deg)");
		const pull = Math.min(calcDistance(e.pageX, e.pageY, ballX, ballY) - 20, 60);
		$(".ball__aimer span").css("width", pull + "px");
	}
});

$(document).on("mouseup", function (e) {
	if ($(".ball__aimer").length) {
		const ballX = $(".primary").offset().left + 10;
		const ballY = $(".primary").offset().top + 10;
		const angle = calcAngle(e.pageX, e.pageY, ballX, ballY);
		const pull = Math.min(calcDistance(e.pageX, e.pageY, ballX, ballY) - 20, 60);
		$(".primary").removeClass("active");
		$(".ball__aimer").remove();

		$(".primary").data("velocity", pull).data("angle", angle);
		console.log("releasing ball!", pull, angle);
	}
});

function calcDistance(x1, y1, x2, y2) {
	const y = x2 - x1;
	const x = y2 - y1;

	return Math.sqrt(x * x + y * y);
}

function calcAngle(x1, y1, x2, y2) {
	return (Math.atan2(y1 - y2, x1 - x2) * 180) / Math.PI + 180;
}

function angleReflect(incidenceAngle, surfaceAngle) {
	var a = surfaceAngle * 2 - incidenceAngle;
	return a >= 360 ? a - 360 : a < 0 ? a + 360 : a;
}

function init() {
	balls.each(function () {
		$(this).data("velocity", 0).data("angle", 0);
	});
}

function moveTick() {
	balls.each(function (index) {
		if ($(this).hasClass("sunk")) return;

		const currVelocity = $(this).data("velocity");
		if (currVelocity != 0) {
			const ref = $(this);
			console.log("ball" + index + " speed", currVelocity);
			const currAngle = ref.data("angle");
			const currLeft = parseFloat(ref.css("left"));
			const currTop = parseFloat(ref.css("top"));
			const xVelocity = Math.cos((currAngle * Math.PI) / 180) * currVelocity;
			const yVelocity = Math.sin((currAngle * Math.PI) / 180) * currVelocity;
			let newLeft = currLeft + xVelocity;
			let newTop = currTop + yVelocity;
			let newAngle = currAngle;
			let sunk = false;
			let newVelocity = Math.max(0, currVelocity - 0.1);
			const bounds = $(".table__inner")[0].getBoundingClientRect();
			if (newTop < 0 || newTop + 20 > bounds.height) {
				// bounce against top/bottom
				newAngle = angleReflect(currAngle, 0);
				if (newTop < 0) {
					newTop = 0;
				}
				if (newTop + 20 > bounds.height) {
					newTop = bounds.height - 20;
				}
			}
			if (newLeft < 0 || newLeft + 20 > bounds.width) {
				// bounce against top/bottom
				newAngle = angleReflect(currAngle, 90);
				if (newLeft < 0) {
					newLeft = 0;
				}
				if (newLeft + 20 > bounds.width) {
					newLeft = bounds.width - 20;
				}
			}

			balls.each(function (index2) {
				if (index != index2) {
					// never compare against current ball
					const ballLeft = parseFloat($(this).css("left")) + 10;
					const ballTop = parseFloat($(this).css("top")) + 10;

					if (calcDistance(ballLeft, ballTop, newLeft + 10, newTop + 10) < 20) {
						const angleBetween = calcAngle(
							newLeft + 10,
							newTop + 10,
							ballLeft,
							ballTop
						);
						console.log(
							"angle between ball" + index + " and ball" + index2,
							angleBetween
						);

						newVelocity = currVelocity * 0.25;
						// newVelocity = 0;
						newLeft = currLeft;
						newTop = currTop;
						newAngle = angleReflect(angleBetween, 90);

						$(this).data("velocity", currVelocity * 0.75);
						$(this).data("angle", angleReflect(currAngle, angleBetween));
					}

					console.log(
						"setting new velocity for ball" + index + " to " + newVelocity
					);
				}
			});

			pockets.each(function (index2) {
				const pocketLeft = parseFloat($(this).css("left")) + 12.5;
				const pocketTop = parseFloat($(this).css("top")) + 12.5;

				if (calcDistance(pocketLeft, pocketTop, newLeft + 10, newTop + 10) < 20) {
					newVelocity = 0;
					newAngle = 0;
					sunk = true;
				}
			});

			if (sunk) {
				ref.addClass("sunk").appendTo(".table__sunk");
			} else {
				ref.data("velocity", newVelocity);
				ref.data("angle", newAngle);
				ref.css("left", newLeft + "px");
				ref.css("top", newTop + "px");
			}
		}
	});
	window.requestAnimationFrame(moveTick);
}

init();
window.requestAnimationFrame(moveTick);

              
            
!
999px

Console