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="display">
<div class="bar"></div>
<div class="ball"></div>
<div class="explain-label">click to start</div>
<div class="left-button">
<div>←</div>
</div>
<div class="right-button">
<div>→</div>
</div>
</div>
.display {
position: relative;
width: 400px;
height: 500px;
border-radius: 10px;
background: #333;
border: 2px solid #cc7799;
}
.bar {
position: absolute;
width: 60px;
height: 20px;
background: #99ff99;
}
.block {
position: absolute;
box-sizing: border-box;
width: 40px;
height: 20px;
border-radius: 4px;
border: 2px solid #ffffff;
background: #339999;
}
.ball {
position: absolute;
width: 10px;
height: 10px;
border-radius: 30px;
background: #ff3333;
border: 2px solid #ffffff;
}
.explain-label {
position: absolute;
text-align: center;
width: 400px;
font-size: 20px;
color: #ffffff;
pointer-events: none;
}
.left-button,
.right-button {
position: absolute;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
font-size: 28px;
font-weight: bold;
color: #ffffff;
background: #99ff9980;
width: 100px;
height: 50px;
text-align: center;
border-radius: 4px;
border: 2px solid #ffffff;
}
window.onload = () => {
/**
* domの生成・位置取得
*/
const setDomPosition = (dom, pos) => {
dom.style.left = `${pos.x}px`;
dom.style.top = `${pos.y}px`;
}
const getDomPosition = (dom) => {
const pos = {
x: parseFloat(dom.style.left),
y: parseFloat(dom.style.top)
};
return pos;
}
const createDom = (className) => {
const dom = document.createElement('div');
dom.classList.add(className);
return dom;
}
const addChildDom = (parentDom, childDom) => {
parentDom.append(childDom);
}
const removeChildDom = (parentDom, childDom) => {
parentDom.removeChild(childDom);
}
const changeDomTextContent = (dom, textContent) => {
dom.textContent = textContent;
}
/**
* 入力イベント
*/
const leftButtonPress = () => {
barLeftSpeed = 5;
}
const leftButtonRelease = () => {
barLeftSpeed = 0;
}
const rightButtonPress = () => {
barRightSpeed = 5;
}
const rightButtonRelease = () => {
barRightSpeed = 0;
}
const displayRelease = () => {
if (gameMode === "startWait") {
changeDomTextContent(explainLabelDom, "break all blocks!");
gameMode = "gamePlaying";
}
}
/**
* メインループイベント
*/
const mainLoop = () => {
switch (gameMode) {
case "gamePlaying":
{
// バーの操作
const barPos = getDomPosition(barDom);
const barMovedPos = {
x: barPos.x + barRightSpeed - barLeftSpeed,
y: barPos.y
};
if (barMovedPos.x >= 0 && barMovedPos.x <= 340) {
setDomPosition(barDom, barMovedPos);
}
// ボール移動
const ballPos = getDomPosition(ballDom);
let ballMovedPos = {
x: ballPos.x + ballVec.x,
y: ballPos.y + ballVec.y
};
if (ballMovedPos.x > 390) {
ballMovedPos.x = 390;
ballVec.x = -ballVec.x;
}
if (ballMovedPos.x < 0) {
ballMovedPos.x = 0;
ballVec.x = -ballVec.x;
}
if (ballMovedPos.y < 0) {
ballMovedPos.y = 0;
ballVec.y = -ballVec.y;
}
if (ballMovedPos.y > 510) {
init(); // ボールが画面下にいってしまうとゲームオーバーなので初期化する
} else {
setDomPosition(ballDom, ballMovedPos);
}
// ブロックとの衝突処理
for (const blockDom of blockDoms) {
const blockPos = getDomPosition(blockDom);
if (checkCollision({ x: blockPos.x, y: blockPos.y, width: 40, height: 20 }, { x: ballMovedPos.x, y: ballMovedPos.y, width: 10, height: 10 })) {
removeChildDom(displayDom, blockDom);
blockDoms = blockDoms.filter((dom => {
return dom != blockDom;
}));
ballVec.y = -ballVec.y;
}
}
// バーと球の衝突処理
if (checkCollision({ x: barPos.x, y: barPos.y, width: 60, height: 20 }, { x: ballMovedPos.x, y: ballMovedPos.y, width: 10, height: 10 })) {
ballMovedPos.y = barDom.y;
ballVec.y = -ballVec.y;
}
// ブロックが全てなくなったか調べる
if (blockDoms.length === 0) {
gameMode = "clear";
changeDomTextContent(explainLabelDom, "CLEAR!");
}
break;
}
}
window.requestAnimationFrame(mainLoop);
};
window.requestAnimationFrame(mainLoop);
/**
* ロジック部分
*/
// 初期化関数
const init = () => {
// 状態の初期化
gameMode = "startWait";
changeDomTextContent(explainLabelDom, "click to start");
// 静的な定義DOMの座標定義
setDomPosition(barDom, { x: 175, y: 410 });
setDomPosition(leftButtonDom, { x: 50, y: 440 });
setDomPosition(rightButtonDom, { x: 250, y: 440 });
setDomPosition(ballDom, { x: 190, y: 300 });
setDomPosition(explainLabelDom, { x: 0, y: 350 });
// ボール向き定義
ballVec = { x: 3, y: -3 };
// 画面にブロックが残っていれば、全部削除してblockDomsを空にする
while (blockDoms.length > 0) {
removeChildDom(displayDom, blockDoms.pop());
}
//ブロック生成 (w:20px h:10px)
for (let xIndex = 0; xIndex < 10; xIndex++) {
for (let yIndex = 0; yIndex < 10; yIndex++) {
const blockDom = createDom("block");
setDomPosition(blockDom, { x: xIndex * 40, y: yIndex * 20 });
addChildDom(displayDom, blockDom);
blockDoms.push(blockDom);
}
}
}
const calcLength = (pos1, pos2) => {
return Math.sqrt((pos1.x - pos2.x) ** 2 + (pos1.y - pos2.y) ** 2);
}
const checkCollision = (rect1, rect2) => {
if ((rect1.x + rect1.width) >= rect2.x && rect1.x <= (rect2.x + rect2.width) && (rect1.y + rect1.height) >= rect2.y && rect1.y <= (rect2.y + rect2.height)) {
return true;
}
return false;
}
// 副作用を持つ状態変数の定義
let blockDoms = [];
let barLeftSpeed = 0;
let barRightSpeed = 0;
let ballVec = { x: 5, y: 5 };
let gameMode = "startWait"; // "startWait"|"gamePlaying"|"clear"
// 初期から存在するDOMの定義
const displayDom = document.getElementsByClassName("display")[0];
const barDom = document.getElementsByClassName("bar")[0];
const ballDom = document.getElementsByClassName("ball")[0];
const leftButtonDom = document.getElementsByClassName("left-button")[0];
const rightButtonDom = document.getElementsByClassName("right-button")[0];
const explainLabelDom = document.getElementsByClassName("explain-label")[0];
/**
* 入力系
*/
// MEMO: PCとスマホでタッチにベントが異なるので複数作成する
// 実はPC・スマホ両方使えるpointerdown/pointerupというイベントがあるのだが、古いsafariがこれで動作しないので不採用
// あと数年たったら死滅すると思われるのでpointer系のイベントが使えるようになるはず
// PC用
leftButtonDom.addEventListener("mousedown", leftButtonPress, false);
leftButtonDom.addEventListener("mouseup", leftButtonRelease, false);
leftButtonDom.addEventListener("mouseout", leftButtonRelease, false);
rightButtonDom.addEventListener("mousedown", rightButtonPress, false);
rightButtonDom.addEventListener("mouseup", rightButtonRelease, false);
rightButtonDom.addEventListener("mouseout", rightButtonRelease, false);
displayDom.addEventListener("mouseup", displayRelease, false);
// スマホ用
leftButtonDom.addEventListener("touchstart", leftButtonPress, false);
leftButtonDom.addEventListener("touchend", leftButtonRelease, false);
rightButtonDom.addEventListener("touchstart", rightButtonPress, false);
rightButtonDom.addEventListener("touchend", rightButtonRelease, false);
displayDom.addEventListener("touchend", displayRelease, false);
init();
}
Also see: Tab Triggers