HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div id="react-volume-knob">
</div>
#scroll-wheel-wrapper {
position: relative;
width: 100px;
height: 100px;
padding: 0px;
border-radius: 100px;
overflow: hidden;
background: transparent;
#scroll-wheel {
height: 100%;
position: relative;
pointer-events: none;
background-color: rgba(0, 191, 255, 0.875);
border-radius: 100%;
transform: rotate(180deg);
#thumb {
touch-action: none;
-ms-touch-action: none;
position: absolute;
cursor: grabbing;
top: 5px;
left: 50%;
width: 20px;
height: 20px;
z-index: 200;
background: blue;
border: 1px solid black;
border-radius: 50%;
transform: translate3d(-50%,0,0);
}
}
#interaction-area {
position: absolute;
background: transparent;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 900;
cursor: -webkit-grab;
cursor: grab;
}
}
const VolumeKnobComponent = () => {
const [volumeLevel, setVolumeLevel] = React.useState(70);
const interactionAreaRef = React.useRef();
const scrollWheelRef = React.useRef();
const scrollWheelWrapperRef = React.useRef();
let width = 100;
let height = 100;
let touchPosX = 0;
let touchPosY = height / 2;
let currentAngle = 0;
const setDefaultVolumeLevel = () => {
scrollWheelRef.current.style.transform = 'rotate('+ 45 +'deg)';
}
const setEventListeners = () => {
interactionAreaRef.current.addEventListener("touchmove", calculateAngle(event));
interactionAreaRef.current.addEventListener("touchstart", setTouchPos(event));
interactionAreaRef.current.addEventListener("mousedown", setTouchPos(event));
interactionAreaRef.current.addEventListener("mousemove", calculateAngle(event));
}
const setTouchPos = (event) => {
touchPosX = event.layerX - (width / 2);
touchPosY = (height / 2) - event.layerY;
}
const calculateAngle = (event) => {
let adjustedLayerX = event.layerX - (width / 2);
let adjustedLayerY = (height / 2) - event.layerY;
let startAngle = Math.atan2(touchPosY, touchPosX) * 180 / Math.PI - 90;
let deltaAngle = Math.atan2(adjustedLayerY, adjustedLayerX) * 180 / Math.PI - 90;
currentAngle += (startAngle - deltaAngle);
switch(true) {
case currentAngle > 150 && currentAngle <= 210:
setVolumeLevel(0);
break;
case currentAngle > 210 && currentAngle <= 240:
setVolumeLevel(10);
break;
case (currentAngle > 240 && currentAngle <= 269) || currentAngle === -90:
setVolumeLevel(20);
break;
case currentAngle > -90 && currentAngle <= -60:
setVolumeLevel(30);
break;
case currentAngle > -60 && currentAngle <= -30:
setVolumeLevel(40);
break;
case currentAngle > -30 && currentAngle < 0:
setVolumeLevel(50);
break;
case currentAngle > 0 && currentAngle <= 30:
setVolumeLevel(60);
break;
case currentAngle > 30 && currentAngle <= 60:
setVolumeLevel(70);
break;
case currentAngle > 60 && currentAngle <= 90:
setVolumeLevel(80);
break;
case currentAngle > 90 && currentAngle <= 120:
setVolumeLevel(90);
break;
case currentAngle > 120 && currentAngle <= 150:
setVolumeLevel(100);
break;
}
scrollWheelRef.current.style.transform = 'rotate('+ currentAngle +'deg)';
if (!volumeLevel) {
scrollWheelRef.current.style.background = "rgba(0, 0, 0, 0.2)";
}
else if (volumeLevel === 90) {
scrollWheelRef.current.style.background = "rgba(255,255,0, 1.0)";
}
else if (volumeLevel === 100) {
scrollWheelRef.current.style.background = "rgba(360, 0, 0, 1.0)";
}
else {
scrollWheelRef.current.style.background = "rgba(0, 191, 255, " + volumeLevel * 0.0125 + ")";
}
touchPosX = adjustedLayerX;
touchPosY = adjustedLayerY;
}
React.useEffect(() => {
setDefaultVolumeLevel();
//This prevents the iOS Safari bounce.
scrollWheelWrapperRef.current.addEventListener('touchmove', (event) => event.preventDefault());
});
return(
<div>
<div ref={scrollWheelWrapperRef} id="scroll-wheel-wrapper">
<div ref={scrollWheelRef} id="scroll-wheel">
<div id="thumb">
</div>
</div>
<div ref={interactionAreaRef} id="interaction-area" onMouseMove={() => setEventListeners()} onTouchMove={() => setEventListeners()}>
</div>
</div>
<div id="volume-level">
{volumeLevel}
</div>
<div>
<p>
I turned a project called <a href="https://codepen.io/marcwiethe/pen/RwNwmyL">VanillaJS: Volume Knob / Circular Slider</a> by
<a href="https://codepen.io/marcwiethe">Marc Wiethe</a> into a React component. Thanks a lot for your project <a href="https://codepen.io/marcwiethe">Marc Wiethe</a>, it helped me an enormous amount with mine.
</p>
<p>
If you want to use this as a volume knob on iOS and iPadOS devices, you will have to use the Web Audio API so that you can control the gain on the volume. I did a <a href="https://codepen.io/TazExprez/pen/NWGMdRz?editors=1010">Web Audio API React Component</a> that you can use for this purpose.
</p>
<p>
There is currently an iOS 13 and iPadOS 13 bug that causes issues and you can read about it at the <a href="https://bugs.webkit.org/show_bug.cgi?id=203435">WebKit Bugzilla</a> website. The createMediaElementSource() method of the AudioContext Interface is currently not working properly in the iOS 13 and the iPadOS 13 versions of Safari. Once the issue is resolved, the <a href="https://codepen.io/TazExprez/pen/NWGMdRz?editors=1010">Web Audio API React Component</a> should be working properly in iOS 13 and iPad OS 13. I tested an iOS 12 device and found no issues.
</p>
</div>
</div>
);
}
ReactDOM.render(<VolumeKnobComponent/>, document.getElementById("react-volume-knob"));
Also see: Tab Triggers