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.
[[[https://codepen.io/inlet/pen/2b7da2053276c634928a7eca450648c8]]]
body {
height: 100vh;
margin: 0;
display: grid;
place-items: center;
}
#root {
display: flex;
flex-direction: column;
align-items: center;
gap: 1rem;
}
canvas {
background: #efefef;
box-shadow: 3px 3px 0 #ccc;
}
.buttons-group > button {
margin: 0 0.25rem;
font-family: system-ui;
border: none;
padding: 0.5rem 0.8rem;
background: #efefef;
box-shadow: 3px 3px 0 #ccc;
}
console.clear();
const { useState, useEffect, useMemo, useCallback, useRef, forwardRef } = React;
const { Stage, Container, Sprite, PixiComponent, useApp, useTick } = ReactPixi;
const { Texture } = PIXI;
PIXI.settings.SCALE_MODE = PIXI.SCALE_MODES.NEAREST;
const width = 500;
const height = 500;
const stageOptions = {
antialias: true,
autoDensity: true,
backgroundAlpha: 0
};
const areas = {
'world': [1000, 1000, 2000, 2000],
'center': [1000, 1000, 400, 400],
'tl': [100, 100, 200, 200],
'tr': [1900, 100, 200, 200],
'bl': [100, 1900, 200, 200],
'br': [1900, 1900, 200, 200]
};
useIteration = (incr = 0.1) => {
const [i, setI] = React.useState(0);
useTick((delta) => {
setI(i => i + incr * delta);
});
return i;
};
// create and instantiate the viewport component
// we share the ticker and interaction from app
const PixiViewportComponent = PixiComponent("Viewport", {
create(props) {
const { app, ...viewportProps } = props;
const viewport = new Viewport.Viewport({
ticker: props.app.ticker,
interaction: props.app.renderer.plugins.interaction,
...viewportProps
});
// activate plugins
(props.plugins || []).forEach((plugin) => {
viewport[plugin]();
});
return viewport;
},
applyProps(viewport, _oldProps, _newProps) {
const { plugins: oldPlugins, children: oldChildren, ...oldProps } = _oldProps;
const { plugins: newPlugins, children: newChildren, ...newProps } = _newProps;
Object.keys(newProps).forEach((p) => {
if (oldProps[p] !== newProps[p]) {
viewport[p] = newProps[p];
}
});
},
didMount() {
console.log("viewport mounted");
}
});
// create a component that can be consumed
// that automatically pass down the app
const PixiViewport = forwardRef((props, ref) => (
<PixiViewportComponent ref={ref} app={useApp()} {...props} />
));
PixiViewport.displayName = 'PixiViewport';
// Wiggling bunny
const Bunny = forwardRef((props, ref) => {
// abstracted away, see settings>js
const i = useIteration(0.1);
return (
<Sprite
ref={ref}
image="https://s3-us-west-2.amazonaws.com/s.cdpn.io/693612/IaUrttj.png"
anchor={0.5}
scale={2}
rotation={Math.cos(i) * 0.98}
{...props}
/>
);
});
Bunny.displayName = 'Bunny';
// 4 squared bunnies
// positioned by its name
const BunniesContainer = ({ name, ...props }) => {
const [x, y] = areas[name];
return (
<Container x={x} y={y} {...props}>
<Bunny x={-50} y={-50} />
<Bunny x={50} y={-50} />
<Bunny x={-50} y={50} />
<Bunny x={50} y={50} />
</Container>
);
}
const BunnyFollowingCircle = forwardRef(({x, y, rad}, ref) => {
const i = useIteration(0.02);
return <Bunny ref={ref} x={x + Math.cos(i) * rad} y={y + Math.sin(i) * rad} scale={6} />
});
// the main app
const App = () => {
// get the actual viewport instance
const viewportRef = useRef();
// get ref of the bunny to follow
const followBunny = useRef();
// interact with viewport directly
// move and zoom to specified area
const focus = useCallback((p) => {
const viewport = viewportRef.current;
const [x, y, width, height] = areas[p];
// pause following
viewport.plugins.pause('follow');
// and snap to selected
viewport.snapZoom({ width, height, removeOnComplete: true });
viewport.snap(x, y, { removeOnComplete: true });
}, []);
const follow = useCallback(() => {
const viewport = viewportRef.current;
viewport.snapZoom({ width: 1000, height: 1000 });
viewport.follow(followBunny.current, { speed: 20 });
}, []);
return (
<>
<div class="buttons-group">
<button onClick={() => focus('world')}>Fit</button>
<button onClick={() => focus('center')}>Center</button>
<button onClick={() => focus('tl')}>TL</button>
<button onClick={() => focus('tr')}>TR</button>
<button onClick={() => focus('bl')}>BL</button>
<button onClick={() => focus('br')}>BR</button>
<button onClick={() => follow()}>Follow</button>
</div>
<Stage width={width} height={height} options={stageOptions}>
<PixiViewport
ref={viewportRef}
plugins={["drag", "pinch", "wheel", "decelerate"]}
screenWidth={width}
screenHeight={height}
worldWidth={2000}
worldHeight={2000}
>
<BunniesContainer name="tl" />
<BunniesContainer name="tr" />
<BunniesContainer name="bl" />
<BunniesContainer name="br" />
<BunniesContainer name="center" scale={2} />
<BunnyFollowingCircle x={1000} y={1000} rad={500} ref={followBunny} />
</PixiViewport>
</Stage>
</>
);
};
ReactDOM.render(<App />, document.getElementById("root"));
Also see: Tab Triggers