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 esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM 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.
<!--
Made with LUME
http://github.com/lume/lume
(Working towards an initial release!)
-->
<script src="//assets.codepen.io/191583/LUME.unversioned.8.js"></script>
<!--
ABOUT:
This demo shows how to make a camera rig for rotating a camera around an object.
-->
<!-- Polyfill for Pointer Events (boo Safari) -->
<script src="https://code.jquery.com/pep/0.4.3/pep.js"></script>
<lume-scene id="scene" perspective="800" webgl enable-css="false" touch-action="none">
<camera-rig rotation="0 30 0" align="0.5 0.5 0.5" min-polar-angle="-11"></camera-rig>
<flickering-orbs id="lights" align="0.5 0.5 0.5" rotation="0 30 0"></flickering-orbs>
<lume-box id="box" color="black" size="200 200 200" align="0.5 0.5 0.5" mount-point="0.5 0.5 0.5"></lume-box>
<!-- the floor -->
<lume-plane color="black" size="4000 4000" rotation="90 0 0" align="0.5 0.5 0.5" mount-point="0.5 0.5 0.5" position="0 300 0"></lume-plane>
</lume-scene>
<id id="YzGbeKG"></id>
html,
body {
width: 100%;
height: 100%;
margin: 0;
background: black;
}
lume-scene {
touch-action: none;
}
// This serves no purpose other than to trigger codepen to use <script type="module">
// import {} from 'http://blah.com/blahblah.js';
const {
Node,
element,
html,
reactify,
Motor,
autorun,
numberAttribute,
stringAttribute
} = LUME;
// Make the default LUME 3D elements available with their default names.
LUME.useDefaultNames();
const { variable } = LUME;
export class ScrollFling {
x = variable(0);
y = variable(0);
deltaX = 0;
deltaY = 0;
constructor({
target = document,
initialX = 0,
initialY = 0,
minX = -Infinity,
maxX = Infinity,
minY = -Infinity,
maxY = Infinity
}) {
this.__target = target;
this.x(initialX);
this.y(initialY);
}
__onWheel = (event) => {
event.preventDefault();
this.deltaX = event.deltaX;
this.deltaY = event.deltaY;
this.x(clamp(this.x() + this.deltaX, minX, maxX));
this.y(clamp(this.y() + this.deltaY, minY, maxY));
console.log(minY, this.y());
if (this.deltaX === 0 && this.deltaY === 0) return;
if (this.__task) Motor.removeRenderTask(this.__task);
let dx = this.deltaX;
let dy = this.deltaY;
// slow the rotation down based on former drag speed
this.__task = Motor.addRenderTask(() => {
dx = dx * 0.95;
dy = dy * 0.95;
this.x(clamp(this.x() + dx, minX, maxX));
this.y(clamp(this.y() + dy, minY, maxY));
// stop rotation once the deltas are small enough that we
// no longer notice a change.
if (Math.abs(dx) < 0.01 && Math.abs(dy) < 0.01) return false;
});
};
// TODO switch to Pointer Events
start() {
this.__target.addEventListener("wheel", this.__onWheel);
return this;
}
stop() {
this.__target.removeEventListener("wheel", this.__onWheel);
return this;
}
}
@element("camera-rig")
export class CameraRig extends Node {
@numberAttribute(0) initialPolarAngle = 0;
@numberAttribute(-90) minPolarAngle = -90;
@numberAttribute(90) maxPolarAngle = 90;
@numberAttribute(1000) initialDistance = 1000;
@numberAttribute(200) minDistance = 200;
@numberAttribute(2000) maxDistance = 2000;
template = () => html`
<lume-node
size="1 1 1"
rotation=${() => LUME.untrack(() => [this.initialPolarAngle, 0, 0])}
size-mode="proportional proportional proportional"
>
<lume-perspective-camera
ref=${(cam) => (this.cam = cam)}
active
position=${() => LUME.untrack(() => [0, 0, this.initialDistance])}
align="0.5 0.5 0.5"
far="10000"
></lume-perspective-camera>
</lume-node>
`;
connectedCallback() {
super.connectedCallback();
// Uses initial attribute values only, changes not tracked at the moment.
flingRotation({
interactionInitiator: this.scene,
rotationYTarget: this,
minFlingRotationX: this.minPolarAngle,
maxFlingRotationX: this.maxPolarAngle
});
this.scrollFling = new ScrollFling({
target: this.scene,
initialY: this.cam.position.z,
initialY: this.initialDistance,
minY: this.minDistance,
maxY: this.maxDistance
}).start();
autorun(() => {
this.scrollFling.y();
LUME.untrack(() => {
this.cam.position.z = this.scrollFling.y();
});
});
}
disconnectedCallback() {
super.disconnectedCallback();
this.scrollFling.stop();
}
}
@element("flickering-orbs")
export class FlickeringOrbs extends Node {
@numberAttribute(0) shadowBias = 0;
template = () => html`
<>
<flickering-orb color="yellow" position="500 0 0" attr:shadow-bias=${() =>
this.shadowBias}></flickering-orb>
<flickering-orb color="deeppink" position="-500 0 0" attr:shadow-bias=${() =>
this.shadowBias}></flickering-orb>
<flickering-orb color="cyan" position="0 0 500" attr:shadow-bias=${() =>
this.shadowBias}></flickering-orb>
<flickering-orb color="limegreen" position="0 0 -500" attr:shadow-bias=${() =>
this.shadowBias}></flickering-orb>
<flickering-orb color="white" position="0 -500 0" attr:shadow-bias=${() =>
this.shadowBias}></flickering-orb>
<flickering-orb color="white" position="0 250 0" attr:shadow-bias=${() =>
this.shadowBias}></flickering-orb>
</>
`;
}
// customElements.define("flickering-orbs", FlickeringOrbs);
@element("flickering-orb")
export class FlickeringOrb extends Node {
@stringAttribute("royalblue") color = "royalblue";
@numberAttribute(1.3) intensity = 1.3;
@numberAttribute(0) shadowBias = 0;
template = () => html`
<lume-point-light
ref=${(l) => (this.light = l)}
attr:color=${() => this.color}
attr:intensity=${() => this.intensity}
attr:shadow-bias=${() => this.shadowBias}
distance="10000"
>
<lume-sphere
ref=${(s) => (this.sphere = s)}
has="basic-material"
attr:color=${() => this.color}
opacity="0.5"
mount-point="0.5 0.5 0.5"
size="10 10 10"
cast-shadow="false"
receive-shadow="false"
></lume-sphere>
</lume-point-light>
`;
connectedCallback() {
super.connectedCallback();
const initialIntensity = this.intensity;
const initialOpacity = this.opacity;
// Prior art: https://www.instructables.com/Realistic-Fire-Effect-with-Arduino-and-LEDs/
const flickerFunction = () => {
const flicker = (Math.random() - 1) * 0.4;
this.light.intensity = initialIntensity + flicker;
this.sphere.opacity = initialOpacity + flicker;
setTimeout(() => Motor.once(flickerFunction), Math.random() * 100);
};
Motor.once(flickerFunction);
}
}
// customElements.define("flickering-orb", FlickeringOrb);
export function flingRotation({
// The object that will be rotated on Y.
rotationYTarget,
// The object that will be rotated on X. Defaults to the element inside the rotationYTarget (it's like a gimball).
rotationXTarget = rotationYTarget.children[0],
// The element on which the pointer should be placed down on in
// order to initiate drag tracking. This defaults to rotationXTarget.
interactionInitiator = rotationXTarget,
// The X rotation can not go below this value. Defaults to -90
// which means facing straight up.
minFlingRotationX = -90,
// The X rotation can not go above this value. Defaults to 90 which
// means facing straight down.
maxFlingRotationX = 90,
// The area in which drag tacking will happen. Defaults to document
// because usually you want to track in the whole viewport, otherwise
// if the pointer comes up outside of this area it will leave things
// in a bad state.
interactionContainer = document
}) {
interactionInitiator.addEventListener("pointercancel", () => console.log('POINTER CANCEL'))
interactionInitiator.addEventListener("pointerdown", () => {
// Stop rotation if any.
rotationYTarget.rotation = () => false;
let deltaX = 0;
let deltaY = 0;
const onMove = (event) => {
deltaX = event.movementY * 0.2;
rotationXTarget.rotation.x = clamp(
rotationXTarget.rotation.x + deltaX,
minFlingRotationX,
maxFlingRotationX
);
deltaY = -event.movementX * 0.2;
rotationYTarget.rotation.y += deltaY;
};
interactionContainer.addEventListener("pointermove", onMove);
interactionContainer.addEventListener(
"pointerup",
() => {
// stop dragging
interactionContainer.removeEventListener("pointermove", onMove);
if (deltaX === 0 && deltaY === 0) return;
// slow the rotation down based on former drag speed
rotationXTarget.rotation = (x, y, z) => {
deltaX = deltaX * 0.95;
// stop rotation once the delta is small enough that we
// no longer notice the rotation.
if (Math.abs(deltaX) < 0.01) return false;
return [clamp(x + deltaX, minFlingRotationX, maxFlingRotationX), y, z];
};
rotationYTarget.rotation = (x, y, z) => {
deltaY = deltaY * 0.95;
// stop rotation once the delta is small enough that we
// no longer notice the rotation.
if (Math.abs(deltaY) < 0.01) return false;
return [x, y + deltaY, z];
};
},
{ once: true }
);
});
}
export function clamp(n, min, max) {
return Math.max(Math.min(n, max), min);
}
// Run code only if this script is running in this pen. Otherwise other pens may import the above.
if (document.querySelector("#YzGbeKG")) {
lights.rotation = (x, y, z) => [x, y + 0.2, z];
}
Also see: Tab Triggers