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.
@import url('https://fonts.googleapis.com/css?family=Anton');
body{
background:
radial-gradient(black 15%, transparent 16%) 0 0,
radial-gradient(black 15%, transparent 16%) 8px 8px,
radial-gradient(rgba(255,255,255,.1) 15%, transparent 20%) 0 1px,
radial-gradient(rgba(255,255,255,.1) 15%, transparent 20%) 8px 9px;
background-color:#282828;
background-size:16px 16px;
font-family: 'Anton', sans-serif;
font-size: 12px;
color: white;
user-select: none;
}
.field{
border: rgba(255, 255, 255, 0.103) 16px solid;
}
.message{
border: 16px solid rgba(0, 0, 0, 0);
text-align: center;
}
// ブロックの状態
const NEUTRAL = 0; // 静止状態
const EXTINCT = 1; // 発火中
const VANISH = 2; // 消滅中
const FALLING = 3; // 落下中
const MOVEDOWN = 4; // 落下完了
const SWAPPING_R = 5; // 入れ替え中
const SWAPPING_L = 6; // 入れ替え中
const SWAPPED = 7; // 入れ替え完了
const REVITATE = 8; // 浮遊中
const CHECK_FLOOR = 9; // 浮遊終了
// 寸法
const WIDTH = 6;
const HEIGHT = 12;
const BLOCK_SIZE = 30;
const NUM = WIDTH * HEIGHT;
// イージング
const easeOutCubic = x => 1 - (1 - x) * (1 - x) * (1 - x);
// 遷移フレーム数
const SAWP_FRAME = 4;
const FALL_FRAME = 8;
const VANISH_DELAY_FRAME = 6;
const VANISH_FRAME = 20;
// 見た目情報
const RELIEF = ['♥','●','▼','◆','★','▲'];
const LIGHT_COLORS = ['rgb(255,0,0)','rgb(0,255,0)','rgb(0,0,255)','rgb(128,0,128)','rgb(255,255,0)','rgb(0,255,255)'];
const COLORS = ['rgb(192,0,0)','rgb(0,192,0)','rgb(0,0,192)','rgb(96,0,96)','rgb(192,192,0)','rgb(0,192,192)'];
const DARK_COLORS = ['rgb(128,0,0)','rgb(0,128,0)','rgb(0,0,128)','rgb(64,0,64)','rgb(128,128,0)','rgb(0,128,128)'];
// ブロッククラス
class Block{
constructor(type){
this.count = 0;
this.type = type;
this.state = NEUTRAL;
this.vanishIndex = -1;
this.vanishNum = 0;
}
setState(st){
this.state = st;
this.count = 0;
}
// 空きブロックと見なせるか
isVacancy(){
return (this.type < 0 && this.state == NEUTRAL) || this.state == FALLING;
}
// 可視状態
isVisible(){
if(this.type < 0)return false;
return this.state == EXTINCT ? this.count & 2 : this.state != VANISH || this.count <= VANISH_FRAME + this.vanishIndex * VANISH_DELAY_FRAME;
}
// 入れ替え可能ブロックか
isSwappable(){
return this.state == NEUTRAL;
}
// 連結可能ブロックか
isConnectable(){
return this.type >= 0 && (this.state == NEUTRAL || (this.state == EXTINCT && this.count == 0));
}
// 表示上のXオフセット量
offsetX(){
return this.state == SWAPPING_L || this.state == SWAPPING_R ? (this.state == SWAPPING_L ? 1 : -1) * easeOutCubic(this.count / SAWP_FRAME) * BLOCK_SIZE : 0;
}
// 表示上のYオフセット量
offsetY(){
return this.state == FALLING ? this.count * BLOCK_SIZE / FALL_FRAME : 0;
}
// 表示上のスケール
scale(){
return this.state == VANISH ? 1.1 : 1.0;
}
// 状態更新
update(){
switch(this.state){
case NEUTRAL:
return;
case EXTINCT:
if(this.count == 50){
this.setState(VANISH);
}
break;
case VANISH:
if(this.count == VANISH_FRAME + this.vanishNum * VANISH_DELAY_FRAME){
this.setState(NEUTRAL);
this.type = -1;
}
break;
case FALLING:
if(this.count == FALL_FRAME)this.setState(MOVEDOWN);
break;
case SWAPPING_L:
if(this.count == SAWP_FRAME)this.setState(SWAPPED);
break;
case REVITATE:
if(this.count == 8)this.setState(CHECK_FLOOR);
break;
}
this.count++;
}
}
// メインキャンバス
const canvas = document.createElement('canvas');
Object.assign(canvas.style, {
position:'absolute',
left:'0px',
top:'28px',
transition:'filter 0.666s'
});
canvas.classList.add('field');
Object.assign(canvas, { width:BLOCK_SIZE * WIDTH, height:BLOCK_SIZE * HEIGHT });
const g = canvas.getContext('2d');
document.body.append(canvas);
// GAME OVERテキスト
const game_over = document.createElement('div');
Object.assign(game_over.style, {
position:'absolute',
width:`${BLOCK_SIZE * WIDTH}px`,
height:`${BLOCK_SIZE * HEIGHT}px`,
left:'0px',
top:'28px',
lineHeight:`${BLOCK_SIZE * HEIGHT}px`,
fontSize:`${BLOCK_SIZE}px`
});
game_over.classList.add('message');
game_over.innerText = 'GAME OVER';
document.body.append(game_over);
// 中央レイアウト処理
const doLayout = () => {
const w = document.body.clientWidth;
const h = document.body.clientHeight;
canvas.style.left = `${(w - BLOCK_SIZE * WIDTH) * 0.5}px`;
game_over.style.left = `${(w - BLOCK_SIZE * WIDTH) * 0.5}px`;
};
document.body.onresize = doLayout;
doLayout();
// ブロック配列を1次元で作成
let blocks = Array(NUM).fill().map(() => new Block(-1));
// 予告ブロック配列作成
let next = Array(WIDTH);
// 予告ブロックを生成
const genNext = () => {
for(let i = 0;i < WIDTH;++i){
let exclude = blocks[i + (HEIGHT - 1) * WIDTH].type;
if(exclude != blocks[i + (HEIGHT - 2) * WIDTH].type){
exclude = -1;
}
// ポップしたときに勝手に消えてしまわないように色を抽選
do {
next[i] = Math.floor(Math.random() * (exclude < 0 ? 6 : 5));
next[i] += next[i] == exclude;
} while(i && next[i - 1] == next[i]);
}
};
// キーイベント
let continueFunc;
let upKey = false;
let leftKey = false;
let rightKey = false;
let downKey = false;
let swapKey = false;
let raiseKey = false;
window.addEventListener('keydown', function(ev) {
switch(ev.keyCode){
case 37:// left
leftKey=true;
ev.preventDefault();
break;
case 38:// up
upKey=true;
ev.preventDefault();
break;
case 39:// right
rightKey=true;
ev.preventDefault();
break;
case 40:// down
downKey=true;
ev.preventDefault();
break;
case 65:// A
swapKey = !ev.repeat;
break;
case 83:// S
if(!ev.repeat)raiseKey = true;
break;
}
if(continueFunc && (swapKey || raiseKey)){
continueFunc();
continueFunc = null;
}
});
window.addEventListener('keyup', function(ev) {
switch(ev.keyCode){
case 83:// S
raiseKey = false;
break;
}
});
let x; // カーソル位置
let y;
let counter;
let raiseCount;
let intervalKey;
let raiseTime;
// ゲーム初期化
const resetGame = () => {
for(let x = 0;x < WIDTH;++x){
for(let y = 0;y < HEIGHT;++y){
if(y >= HEIGHT >> 1){
// 開始直後にブロックが勝手に消えないよう隣接を確認しながら初期化
let color;
do{
color = Math.floor(Math.random() * 6);
}while((x >= 2 && color == blocks[x - 1 + y * WIDTH].type && color == blocks[x - 2 + y * WIDTH].type) ||
(y >= 2 && color == blocks[x + (y - 1) * WIDTH].type && color == blocks[x + (y - 2) * WIDTH].type));
blocks[x + y * WIDTH].type = color;
}else{
blocks[x + y * WIDTH].type = -1;
}
blocks[x + y * WIDTH].setState(NEUTRAL);
}
}
genNext();
x = WIDTH >> 1;
y = HEIGHT >> 1;
counter = 0;
raiseCount = 0;
raiseTime = 600;
game_over.style.visibility = 'hidden';
game_over.style.filter = `blur(${BLOCK_SIZE / 4}px)`;
canvas.style.filter = 'blur(0px)';
g.filter = 'none';
intervalKey = setInterval(updateFunc, 16);
};
// 連結判定
const judge = (x, y) => {
const b = blocks[x+y*WIDTH];
if(!b.isConnectable())return 0;
const scan = (dx, dy) => {
let num = 0;
let sx = x + dx;
let sy = y + dy;
while(sx < WIDTH && sy < HEIGHT){
const d = blocks[sx + sy * WIDTH];
if(b.type != d.type || !d.isConnectable()){
break;
}
num++;
sx += dx;
sy += dy;
}
return num;
};
let num = 0;
const right = scan(1, 0);
if(right + 1 >= 3){
num += right + 1;
for(let i = x;i <= x + right;++i){
blocks[i+y*WIDTH].setState(EXTINCT);
}
}
const down = scan(0, 1);
if(down + 1 >= 3){
num += down + 1;
for(let i = y;i <= y + down;++i){
blocks[x+i*WIDTH].setState(EXTINCT);
}
}
return num;
};
const updateFunc = () => {
let stopRaise = false;
// 更新
for(let x = 0;x < WIDTH;++x){
for(let y = HEIGHT;y--;){
let b = blocks[x+y*WIDTH];
b.update();
let fallCheck = HEIGHT;
if(b.state == MOVEDOWN){
b.setState(NEUTRAL);
let d = blocks[x+(y+1)*WIDTH];
Object.assign(d, b);
b.type = -1;
fallCheck = y + 2;
}else
if(b.state == SWAPPED){
let o = blocks[x+1+y*WIDTH];
b.setState(REVITATE);
o.setState(REVITATE);
blocks[x+1+y*WIDTH] = b;
blocks[x+y*WIDTH] = b = o;
}else
if(b.state == CHECK_FLOOR){
b.setState(NEUTRAL);
fallCheck = y + 1;
}
if(b.state == REVITATE){
let d = blocks[x+(y+1)*WIDTH];
if(!d || !d.isVacancy())b.setState(NEUTRAL);
}
if(fallCheck < HEIGHT){
let d = blocks[x+(fallCheck-1)*WIDTH];
let u = blocks[x+fallCheck*WIDTH];
if(u.isVacancy())d.setState(FALLING);
}else
if(y > 0 && b.isVacancy()){
let d = blocks[x+(y-1)*WIDTH];
if(d.type >= 0 && d.state == NEUTRAL)d.setState(FALLING);
}
stopRaise |= b.state == EXTINCT;
}
}
// 消滅判定
let extinctCount = 0;
for(let x = 0;x < WIDTH;++x){
for(let y = 0;y < HEIGHT;++y){
extinctCount += judge(x, y);
}
}
if(extinctCount){
let vanishIndex = 0;
for(let b of blocks){
if(b.state == EXTINCT && b.count == 0){
// 左上から順に消えていくようディレイ値を設定
b.vanishIndex = vanishIndex++;
b.vanishNum = extinctCount;
}
}
raiseTime -= extinctCount;
stopRaise = true;
}
if(raiseTime < 40)raiseTime = 40;
// 移動
if(upKey) {
if(y > 1)y--;
}else
if(downKey) {
if(y < HEIGHT - 1)y++;
}else
if(rightKey) {
if(x < WIDTH - 2)x++;
}else
if(leftKey) {
if(x > 0)x--;
}
// 入れ替え
if(swapKey){
const l = blocks[x + y * WIDTH];
const r = blocks[x + 1 + y * WIDTH];
let swappable = l.isSwappable() && r.isSwappable();
if(swappable){
const ul = blocks[x + (y - 1) * WIDTH];
const ur = blocks[x + 1 + (y - 1) * WIDTH];
swappable = ul.state != FALLING && ur.state != FALLING;
}
if(swappable){
l.setState(SWAPPING_L);
r.setState(SWAPPING_R);
}
}
// せり上がり
let pump = 1;
if(raiseKey){
pump = 20;
}
if(!stopRaise){
raiseCount += pump;
}
if(raiseCount >= raiseTime){
raiseCount = 0;
let gameOver = false;
for(let x = 0;x < WIDTH;++x){
// 最上段まで埋まってしまった
gameOver = gameOver || blocks[x + WIDTH].type >= 0;
let b = blocks[x];
b.type = next[x];
b.setState(NEUTRAL);
for(let y = HEIGHT;y--;){
let n = blocks[x + y * WIDTH];
blocks[x + y * WIDTH] = b;
b = n;
}
}
genNext();
if(y > 1)y--;
if(gameOver){
raiseKey = false;
game_over.style.visibility = 'visible';
game_over.style.transition = 'filter 0.666s';
game_over.style.filter = 'blur(0px)';
canvas.style.filter = `blur(${BLOCK_SIZE / 4}px)`;
clearInterval(intervalKey);
continueFunc = resetGame;
}
}
counter++;
upKey = false;
leftKey = false;
rightKey = false;
downKey = false;
swapKey = false;
// 指定座標を中心に文字列を描画
const drawText = (str, x, y) => {
const tm = g.measureText(str);
g.fillText(str, x - 0.5 * tm.width, y + 0.5 * tm.actualBoundingBoxAscent);
};
g.clearRect(0, 0, canvas.width, canvas.height);
g.font = `${BLOCK_SIZE*0.8}px メイリオ`;
let raiseY = Math.floor(BLOCK_SIZE * raiseCount / raiseTime);
// ブロック
for(let x = 0;x < WIDTH;++x){
for(let y = 0;y < HEIGHT;++y){
const b = blocks[x+y*WIDTH];
if(b.isVisible()) {
const scale = b.scale();
g.translate((x + 1.0 - scale) * BLOCK_SIZE + b.offsetX(), (y + 1.0 - scale) * BLOCK_SIZE + b.offsetY() - raiseY);
g.scale(scale, scale);
g.fillStyle = COLORS[b.type];
g.fillRect(0, 0, BLOCK_SIZE, BLOCK_SIZE);
g.fillStyle = LIGHT_COLORS[b.type];
drawText(RELIEF[b.type], 0.5 * BLOCK_SIZE, 0.5 * BLOCK_SIZE);
g.fillRect(0, 0, BLOCK_SIZE, BLOCK_SIZE / 16);
g.fillRect(0, 0, BLOCK_SIZE / 16, BLOCK_SIZE);
g.fillStyle = DARK_COLORS[b.type];
g.fillRect(0, BLOCK_SIZE - BLOCK_SIZE / 16, BLOCK_SIZE, BLOCK_SIZE / 16);
g.fillRect(BLOCK_SIZE - BLOCK_SIZE / 16, 0, BLOCK_SIZE / 16, BLOCK_SIZE);
g.setTransform(1, 0, 0, 1, 0, 0);
}
}
g.fillStyle = DARK_COLORS[next[x]];
g.fillRect(x*BLOCK_SIZE,HEIGHT*BLOCK_SIZE-raiseY,BLOCK_SIZE,BLOCK_SIZE);
}
// カーソル
g.translate((x+0.5)*BLOCK_SIZE, (y+0.5)*BLOCK_SIZE-raiseY);
g.fillStyle = 'white';
g.beginPath();
const offset = -((Math.floor(counter / 30) & 1) + BLOCK_SIZE * 0.5);
for(let i = 0;i<2;++i){
for(let j = 0;j<4;++j){
g.rotate(0.5 * Math.PI);
g.rect(offset, offset, BLOCK_SIZE / 3, BLOCK_SIZE / 8);
g.rect(offset, offset, BLOCK_SIZE / 8, BLOCK_SIZE / 3);
}
g.translate(BLOCK_SIZE, 0);
}
g.fill();
g.setTransform(1, 0, 0, 1, 0, 0);
};
resetGame();
Also see: Tab Triggers