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 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.
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 class="debug">
<p>HSL: <span class="hsl-swatch"></span><span class="hsl"></span></p>
<p>Brightness: <span class="brightness"></span></p>
</div>
<div class="holder">
<div class="inner">
<div class="dial"></div>
<div class="controls">
<div class="color">
<div class="handle"></div>
</div>
<div class="brightness">
<div class="handle">
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
<i></i>
</div>
</div>
</div>
</div>
</div>
$size: 320px;
$dark: #000002;
body { background: $dark; font-family: Helvetica, Arial, sans-serif; }
*, *:before, *:after {
box-sizing: border-box;
}
.debug {
color: #fff;
span.hex-swatch, span.hsl-swatch {
display: inline-block;
width: 15px;
height: 15px;
margin: 0 5px;
}
}
.holder {
width: $size;
margin: 30px auto;
.inner {
position: relative;
width: 100%;
height: 0;
padding-bottom: 100%;
.dial, .controls {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.dial {
z-index: 1;
background-position: center;
background-repeat: no-repeat;
background-size: contain;
}
.controls {
z-index: 2;
pointer-events: none;
.color {
width: 4px;
height: 25%;
position: absolute;
left: 50%;
top: 0;
z-index: 2;
transform: translateX(-50%);
background: $dark;
.handle {
width: 30px;
height: 30px;
background: #fff;
position: absolute;
left: 50%;
transform: translateX(-50%);
border-radius: 50%;
top: 10px;
pointer-events: auto;
border: 3px solid #fff;
box-shadow: 0 3px 10px 0 rgba(#000, 0.3);
}
}
.brightness {
//display: none;
position: absolute;
top: 25%;
left: 25%;
width: 50%;
height: 50%;
border-radius: 50%;
z-index: 1;
overflow: hidden;
background-image: linear-gradient(-180deg, #66717D 0%, #4D5660 50%, #333B44 100%);
border: 5px solid $dark;
pointer-events: auto;
&:after {
content: '';
display: block;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
border-radius: 50%;
background-image: linear-gradient(-180deg, #66717D 0%, rgba(#4D5660, 0) 50%, #333B44 100%);
pointer-events: none;
}
.handle {
position: absolute;
left: 0;
right: 0;
top: 0;
padding: 50% 0;
i {
display: block;
width: 20px;
height: 2px;
background: #CFCBC7;
margin: 40px auto 0;
&:first-child { margin-top: 0; }
}
}
}
}
}
}
.handle {
cursor: move;
cursor: -webkit-grab;
cursor: -moz-grab;
cursor: grab;
&:active {
cursor: -webkit-grabbing;
cursor: -moz-grabbing;
cursor: grabbing;
}
}
/**
*
* ----------------------------------
* Inspired by the Lifx app
* @see https://itunes.apple.com/us/app/lifx/id657758311
* @author Levi Cole at Skape Collective
* ----------------------------------
*
*/
(function ($) {
'use strict';
$.fn.LifxColorPicker = function(options) {
const self = this;
options = $.extend(true, {
hue: 0,
saturation: 0.5,
brightness: 1,
size: 320,
// Callback
changed: function(color) {
// color.hex
// color.hue
// color.saturation
// color.brigtness
}
}, options);
/**
* Public
* Color data
*/
self.data = {
hue: 0,
saturation: 0,
brightness: 0
};
/**
* Private
* DOM data
*/
const dom = {
rotation: 0,
brightness: 0,
saturation: 0
};
/**
* Global DOM Elements
* @type {jQuery}
*/
const $this = this;
const $dial = $this.find('.dial');
const $saturation_handle = $this.find('.color .handle');
const $brightness_handle = $this.find('.brightness .handle');
/*
* Canvas properties
*/
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = options.size;
canvas.height = options.size;
context.scale(2, 2);
/**
* Math variables
*/
const x = options.size / 4; // Canvas center X (divided by 4 because of retina)
const y = options.size / 4; // Canvas center Y (divided by 4 because of retina)
const r = x; // Canvas radius
const d2r = Math.PI / 180; // Degrees to radians
const r2d = 180 / Math.PI; // Radians to degrees
const abs_x = $dial.offset().left + (r * 2);
const abs_y = $dial.offset().top + (r * 2);
/**
* Public
* Simple function to retrieve the current [H]ue [S]aturation [L]ightness
*/
self.hsl = () => {
let round_sat = Math.round(self.data.saturation * 100);
let h = self.data.hue;
let s = 100;//round_sat;
let l = Math.round(100 - (round_sat / 2));
return 'hsl('+h+', '+s+'%, '+l+'%)';
};
/**
* Private
* Generate radial gradient image
*/
const generate_gradient = () => {
for (let angle = 0; angle <= 360; angle++) {
const arc_start = -((angle + 90) * d2r);
const arc_end = -((angle + 88) * d2r);
// Draw shape
context.beginPath();
context.moveTo(x, y);
context.arc(x, y, r, arc_start, arc_end, false);
context.closePath();
// Generate gradient for shape
let gradient = context.createRadialGradient(x, y, 0, x, y, r);
gradient.addColorStop(0.5, 'hsl('+angle+', 10%, 100%)');
gradient.addColorStop(0.75, 'hsl('+angle+', 100%, 70%)');
gradient.addColorStop(1, 'hsl('+angle+', 100%, 30%)');
context.fillStyle = gradient;
context.fill();
}
$dial.css('background-image', 'url('+canvas.toDataURL('image/png', 1)+')');
};
/**
* Calculate mouse rotation relative to $dial center
* @param int mouse_x
* @param int mouse_y
*/
const get_degrees = (mouse_x, mouse_y) => {
const radians = Math.atan2(mouse_x - abs_x, mouse_y - abs_y);
const degrees = Math.round((radians * r2d * -1) + 100);
return degrees < 0 ? 360 - Math.abs(degrees) : degrees;
};
/**
* Private
* Run when any values are changed
*/
const trigger_change = () => {
let c_color = self.hsl();
let c_top = ((1 - dom.saturation) * ($saturation_handle.parent().outerHeight() - $saturation_handle.outerHeight()) );
// Update dial rotation
$dial.css('transform', 'rotate('+dom.rotation+'deg)');
// Update saturation handle
$saturation_handle.css('background', c_color);
$saturation_handle.css('top', c_top+'px');
// Update brightness handle
$brightness_handle.css('top', -(dom.brightness * $brightness_handle.height())+'px')
// Send data to callback
if (typeof options.changed === 'function') {
let send_data = self.data;
send_data.hsl = self.hsl();
options.changed(send_data);
}
};
/**
* Public
* Set Hue
*/
self.set_hue = (rotation) => {
let hue = typeof rotation !== undefined ? rotation : self.data.hue;
if (hue > 360) {
hue = hue - 360;
}
if (hue < 0) {
hue = 360 - Math.abs(hue);
}
// Set data values
self.data.hue = hue;
dom.rotation = hue;
trigger_change();
};
/**
* Public
* Set saturation
*/
self.set_saturation = (saturation) => {
saturation = typeof saturation !== undefined ? saturation : self.data.saturation;
if (saturation > 1) { saturation = 1; }
if (saturation < 0) { saturation = 0; }
self.data.saturation = saturation.toFixed(2);
dom.saturation = saturation;
trigger_change();
};
/**
* Public
* Set brightness
*/
self.set_brightness = (brightness) => {
brightness = typeof brightness !== undefined ? brightness : self.data.brightness;
if (brightness > 1) { brightness = 1; }
if (brightness < 0) { brightness = 0; }
self.data.brightness = brightness.toFixed(2);
dom.brightness = brightness;
trigger_change();
};
/**
* Public
* Set color from Hue and Saturation
*/
self.set_color = (hue, saturation, brightness) => {
self.set_hue(hue);
self.set_saturation(saturation);
self.set_brightness(brightness);
};
/**
* Dial rotate click and drag binding
*/
$dial.on('mousedown', function(event) {
// Calculate the mouse position in degrees
const start_degrees = get_degrees(event.pageX, event.pageY);
const start_dom = dom.rotation;
$(document).bind('mousemove', function(event) {
// Calculate the mouse move position, removing starting point
const end_degrees = get_degrees(event.pageX, event.pageY);
let difference = 0;
if (end_degrees < start_degrees) {
difference = (end_degrees + 360) - start_degrees;
} else {
difference = end_degrees - start_degrees;
}
self.set_hue( start_dom + difference );
});
});
/**
* Vertical color range click and drag binding
*/
$saturation_handle.on('mousedown', function() {
$(document).bind('mousemove', function(event) {
const $parent = $saturation_handle.parent();
const h_size = $saturation_handle.outerWidth();
const h_half = h_size / 2;
const p_size = $parent.outerHeight() - h_size;
const bounds = $parent.offset();
var y = event.clientY - (bounds.top + h_half);
if (y > p_size) {
y = p_size;
}
if (y < 0) {
y = 0;
}
self.set_saturation( 1 - (y / p_size) );
});
});
/**
* Brightness click and drag binding
*/
$brightness_handle.parent().on('mousedown', function(event) {
var click_y = event.clientY;
const top = parseInt($brightness_handle.css('top'));
$(document).bind('mousemove', top, function(event) {
const h_size = $brightness_handle.height();
var y = top - (click_y - event.clientY);
if (y > 0) {
y = 0;
}
if (y < (-h_size)) {
y = -h_size;
}
self.set_brightness( Math.abs(y) / h_size );
});
});
/**
* Remove all click and drag bindings
*/
$(document).on('mouseup', function() {
$(document).unbind('mousemove');
});
$(window).bind('mousewheel DOMMouseScroll', function(event) {
if (event.target === $brightness_handle.parent()[0] || $brightness_handle.parent().has( event.target ).length) {
const current_value = self.data.brightness * 100;
let new_value = 0;
if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) {
new_value = current_value - 1;
} else {
new_value = current_value + 1;
}
new_value = new_value / 100;
self.set_brightness( new_value );
event.preventDefault();
return false;
}
});
/**
* Initiate
*/
generate_gradient();
self.set_color(Math.round(options.hue), parseFloat(options.saturation), parseFloat(options.brightness));
};
})(jQuery);
/**
*
* ----------------------------------
* Initiate plugin
* ----------------------------------
*
*/
$(document).ready(function() {
$('.holder').LifxColorPicker({
changed: (color) => {
$('span.hsl').text(color.hsl);
$('span.brightness').text(color.brightness);
$('span.hsl-swatch').css({
'background': color.hsl,
'opacity': color.brightness
});
}
});
});
Also see: Tab Triggers