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.
<html>
<head>
<style>canvas {border: 1px solid gray;}</style>
</head>
<body>
<canvas id="screen"></canvas>
<p id="info"></p>
</body>
</html>
// - 変数 - //
var Canvas;
var info;
var ctx;
var Run = true;
var mouse = new Point();
var fire = false;
var Chara_Shot_Count = 10;
var Enemy_Count = 10;
var counter = 0;
var position = new Point();
var i;
var j;
// - クラス - //
// Character
function Chara(){
this.position = new Point();
this.size = 0;
}
Chara.prototype = {
init:function(size){
//サイズ設定
this.size = size;
}
}
// Point
function Point(){
this.x = 0;
this.y = 0;
}
function Chara_Shot(){
this.position = new Point();
this.size = 0;
this.speed = 0;
this.alive = false;
}
Chara_Shot.prototype = {
set:function(position,size,speed){
//座標
this.position.x = position.x
this.position.y = position.y
//サイズ
this.size = size;
//スピード
this.speed = speed;
// 生存フラグを立てる
this.alive = true;
},
move:function(){
// 座標を真上にspeed分だけ移動
this.position.y -= this.speed;
// 一定以上の座標に到達⇒false
if(this.position.y < -this.size){
this.alive = false;
}
}
};
// Enemy
function Enemy(){
this.position = new Point();
this.size = 0;
this.type = 0;
this.alive = false;
}
Enemy.prototype = {
//setメソッド
set:function(postion,size,type){
// 座標
this.position.x = postion.x;
this.position.y = postion.y;
// サイズ
this.size = size;
// 生存フラグを立てる
this.alive = true;
// タイプ
this.type = type;
},
//moveメソッド
move:function(){
// タイプに応じて分岐
switch(this.type){
case 0:
// X方向 に進む
this.position.x += 2;
// 画面右端~左端に到達⇒生存フラグを降ろす
if(this.position.x > this.size + Canvas.width){
this.alive = false;
}
break;
case 1:
// マイナスX方向 に進む
this.position.x -= 2;
// 画面左端~右端に到達⇒生存フラグを降ろす
if(this.position.x < -this.size){
this.alive = false;
}
break;
}
}
};
// - メイン - //
window.onload = function(){
// スクリーンの初期化
Canvas = document.getElementById('screen');
Canvas.width = 256;
Canvas.height = 256;
// 2dコンテキスト
ctx = Canvas.getContext('2d');
// イベントの登録
Canvas.addEventListener('mousemove', mouseMove);
window.addEventListener('keydown', keyDown);
Canvas.addEventListener('mousedown', mouseDown);
// その他のエレメント関連
info = document.getElementById('info');
// 自機 初期化
var chara = new Chara();
chara.init(10);
//自機ショット 初期化
var charaShot = new Array(Chara_Shot);
for(i = 0; i < Chara_Shot_Count; i++){
charaShot[i] = new Chara_Shot();
}
// 敵機 初期化
var enemy = new Array(Enemy_Count);
for(i = 0; i < Enemy_Count; i++){
enemy[i] = new Enemy();
}
// 画面を繰り返し呼び出す
(function(){
// カウンタをインクリメント
counter++;
// HTMLを更新
info.innerHTML = mouse.x + ' : ' + mouse.y;
// screenクリア
ctx.clearRect(0, 0, Canvas.width, Canvas.height);
// パスの設定を開始
ctx.beginPath();
// 自機の位置を設定
chara.position.x = mouse.x;
chara.position.y = mouse.y;
// 自機を描くパスを設定
ctx.rect(chara.position.x, chara.position.y,30,30);
// 自機の色を設定する
ctx.fillStyle = "black";
// 自機を描く
ctx.fill();
// fireフラグ trueなら処理が進む
if(fire===true){
// すべての自機ショットを調査する
for(i = 0; i < Chara_Shot_Count; i++){
// 自機ショットが既に発射されているかチェック
if(charaShot[i].alive === false){
// 自機ショットを新しくセット
charaShot[i].set(chara.position, 5, 10);
// ループ抜ける
break;
}
}
// フラグをfalseにする
fire = false;
}
// パスの設定を開始
ctx.beginPath();
// すべての自機ショット(=10)を調べる
for(i = 0; i < Chara_Shot_Count; i++){
// 自機ショットが既に発射されているかチェック
if(charaShot[i].alive===true){
// 自機ショットを動かす
charaShot[i].move();
// 自機ショットを描くパスを設定
ctx.arc(
charaShot[i].position.x,
charaShot[i].position.y,
charaShot[i].size,
0, Math.PI * 2, false
);
// パスをセーブ
ctx.closePath();
}
}
// 自機ショットの色を設定する
ctx.fillStyle = "red";
// 自機ショットを描く
ctx.fill();
// 100フレームに1度敵機を出現
if(counter % 100 === 0){
// すべてのエネミー(=10)を調べる
for(i = 0; i < Enemy_Count; i++){
// エネミーが既に10体出現したかチェック
if(!enemy[i].alive){
// タイプを決定する
j = (counter % 200) / 100;
// タイプから初期位置を決める
var enemySize = 15;
position.x = -enemySize + (Canvas.width + enemySize * 2) * j;
position.y = Canvas.height / 2;
// エネミーを新規作成
enemy[i].set(position, enemySize, j);
// 1体出現したのでループ抜ける
break;
}
}
}
// パスの設定を開始
ctx.beginPath();
// すべての敵機(=10)を調べる
for(i = 0; i < Enemy_Count; i++){
// 敵機が既に10機出現したかチェック
if(enemy[i].alive){
// 敵機を移動
enemy[i].move();
// 敵機(=○)を描くパスを設定
ctx.arc(
enemy[i].position.x,
enemy[i].position.y,
enemy[i].size,
0, Math.PI * 2, false
);
// パスを閉じる
ctx.closePath();
}
}
// 敵機の色を設定する
ctx.fillStyle = "green";
// 敵機を描く
ctx.fill();
// フラグにより再帰呼び出し
if(Run){setTimeout(arguments.callee, 1000/30);}
})();
};
// 関数
function mouseMove(event){
// マウスカーソル座標の更新
mouse.x = event.clientX;
mouse.y = event.clientY;
}
function keyDown(event){
// キーコード取得
var ck = event.key;
// spaceKeyで画面が止まる
if(ck === " "){Run = false;}
}
function mouseDown(event){
// フラグを立てる
fire = true;
}
Also see: Tab Triggers