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.
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.min.js"></script>
<canvas id="firework"></canvas>
const Y_AXIS = 1;
const X_AXIS = 2;
let canvas;
let fireworks = [];
let star = [];
function windowResized() {
resizeCanvas(window.innerWidth, window.innerHeight);
this.preStar();
}
function setup() {
// キャンバスの設定
background(0);
let canvas = document.getElementById('firework');
let ctx = canvas.getContext('2d');
canvas = createCanvas(window.innerWidth, window.innerHeight);
canvas.position(0, 0);
canvas.style("z-index", "-1");
colorMode(RGB);
frameRate(60);
this.preStar();
}
function draw() {
// 背景色を設定
setGradient(0, 0, width, height, color(0, 0, 0), color(24, 32, 72), Y_AXIS);
noStroke();
// 星を描く
this.drawStar();
// 花火を打ち上げる間隔を調整
if (0 === frameCount % 100) {
// 打ち上がるスピード
let speed = random(10, 30);
fireworks.push(new FireWork(random(width), height, 0, speed, 0.98));
}
for (let fw of fireworks) {
// 打ち切った花火を処理対象から外す(配列から削除する)
if (2 === fw.getType || 30000 < fw.getFrame) {
fireworks = fireworks.filter((n) => n !== fw);
continue;
}
// 打ち上げアニメーションを呼び出す
fw.fire();
}
}
class FireWork {
// 初期設定
constructor(x, y, vx, vy, gv) {
// フレームカウンター
this.frame = 0;
this.type = 0;
this.next = 0;
// 花火の色
this.r = random(155) + 80;
this.g = random(155) + 80;
this.b = random(155) + 80;
this.a = 255;
// 初期位置
this.x = x;
this.y = y;
// 玉の大きさ
this.w = random(10, 5);
// 打ち上がる高さ
this.maxHeight = random(height / 6, height / 2);
this.fireHeight = height - this.maxHeight;
// 重力
this.vx = vx;
this.vy = vy;
this.gv = gv;
// 残像表示用配列
this.afterImages = [];
// 爆発用配列
this.explosions = [];
// 消えてから爆発までの遅延時間
this.exDelay = random(10, 40);
// 爆発の大きさ
this.large = random(5, 15);
// 爆発の玉の数
this.ball = random(20, 100);
// 爆発から消えるまでの長さ
this.exEnd = random(20, 40);
// 爆発のブレーキ
this.exStop = 0.96;
}
get getFrame() {
return this.frame;
}
get getType() {
return this.type;
}
// 処理コントロール
fire() {
// 0:打ち上げ(初期) 1:爆発
switch (this.type) {
case 0:
this.rising();
break;
case 1:
this.explosion();
break;
}
}
// 打ち上げアニメーション
rising() {
// 頂点まで達したら消す
if (this.y * 0.8 < this.maxHeight) {
this.a = this.a - 6;
}
// 指定の高さまで上昇する
this.x += this.vx;
this.y -= this.vy * ((this.fireHeight - (height - this.y)) / this.fireHeight);
// 残像を表示
this.afterImages.push(new Afterimage(this.r, this.g, this.b, this.x, this.y, this.w, this.a));
for (let ai of this.afterImages) {
if (ai.getAlpha <= 0) {
this.afterImages = this.afterImages.filter((n) => n !== ai);
continue;
}
ai.rsImage();
}
// 打ち上げ表示
this.update(this.x, this.y, this.w);
// 全ての表示が消えたら処理の種類を変更する
if (0 == this.afterImages.length) {
if (0 === this.next) {
// 消えてから爆発まで遅延させる
this.next = this.frame + Math.round(this.exDelay);
} else if (this.next === this.frame) {
// 花火の大きさ
for (let i = 0; i < this.ball; i++) {
// 爆発の角度
let r = random(0, 360);
// 花火の内側を作る(バラバラ)
let s = random(0.1, 0.9);
let vx = Math.cos((r * Math.PI) / 180) * s * this.large;
let vy = Math.sin((r * Math.PI) / 180) * s * this.large;
this.explosions.push(new FireWork(this.x, this.y, vx, vy, this.exStop));
// 花火の輪郭を作る(丸くなるようにする)
let cr = random(0, 360);
let cs = random(0.9, 1);
let cvx = Math.cos((cr * Math.PI) / 180) * cs * this.large;
let cvy = Math.sin((cr * Math.PI) / 180) * cs * this.large;
this.explosions.push(new FireWork(this.x, this.y, cvx, cvy, this.exStop));
}
this.a = 255;
this.type = 1;
}
}
}
// 爆発アニメーション
explosion() {
for (let ex of this.explosions) {
ex.frame++;
// 爆発し終わった花火を配列から除去する
if (2 === ex.getType) {
this.explosions = this.explosions.filter((n) => n !== ex);
continue;
}
// 残像を描画
if (0 === Math.round(random(0, 32))) {
ex.afterImages.push(new Afterimage(this.r, this.g, this.b, ex.x, ex.y, ex.w, ex.a));
}
for (let ai of ex.afterImages) {
if (ai.getAlpha < 0) {
ex.afterImages = ex.afterImages.filter((n) => n !== ai);
continue;
}
ai.exImage();
}
// 爆発を描画
this.update(ex.x, ex.y, ex.w, ex.a);
ex.x += ex.vx;
ex.y += ex.vy;
ex.vx = ex.vx * ex.gv;
ex.vy = ex.vy * ex.gv;
ex.vy = ex.vy + ex.gv / 30;
if (this.exEnd < ex.frame) {
ex.w -= 0.1;
ex.a = ex.a - 4;
if (ex.a < 0 && 0 === ex.afterImages.length) {
ex.type = 2;
}
}
}
}
// 花火を表示する
update(x, y, w, a) {
this.frame++;
if (0 < this.a) {
let c = color(this.r, this.g, this.b);
c.setAlpha(a);
fill(c);
ellipse(x, y, w, w);
}
}
}
// 残像処理用クラス
class Afterimage {
constructor(r, g, b, x, y, w, a) {
this.frame = 0;
this.r = r;
this.g = g;
this.b = b;
this.x = x;
this.y = y;
this.w = w;
this.a = a;
this.vx = random(-0.24, 0.24);
this.vy = random(0.2, 0.8);
this.vw = random(0.05, 0.2);
}
get getAlpha() {
return this.a;
}
// 打ち上げ用
rsImage() {
if (0 < this.a) {
this.update(this.r, this.g, this.b, this.x, this.y, this.w, this.a);
this.r += 4;
this.g += 4;
this.b += 4;
this.x = this.x + this.vx;
this.y = this.y + this.vy;
if (0 < this.w) {
this.w = this.w - this.vw;
}
this.a = this.a - 4;
}
}
// 爆発用
exImage() {
if (0 < this.a) {
this.update(this.r, this.g, this.b, this.x, this.y, this.w, this.a);
this.r += 2.5;
this.g += 2.5;
this.b += 2.5;
this.x = this.x + this.vx;
this.y = this.y + this.vy;
if (0 < this.w) {
this.w = this.w - this.vw;
}
this.a = this.a - 1.5;
}
}
update(r, g, b, x, y, w, a) {
this.frame++;
let c = color(r, g, b);
c.setAlpha(a);
fill(c);
ellipse(x, y, w, w);
}
}
// グラデーションを描画
function setGradient(x, y, w, h, c1, c2, axis) {
noFill();
if (axis === Y_AXIS) {
// Top to bottom gradient
for (let i = y; i <= y + h; i++) {
let inter = map(i, y, y + h, 0, 1);
let c = lerpColor(c1, c2, inter);
stroke(c);
line(x, i, x + w, i);
}
} else if (axis === X_AXIS) {
// Left to right gradient
for (let i = x; i <= x + w; i++) {
let inter = map(i, x, x + w, 0, 1);
let c = lerpColor(c1, c2, inter);
stroke(c);
line(i, y, i, y + h);
}
}
}
// 星を作成
function preStar() {
star = [];
for (let i = 0; i < 100; i++) {
star.push([random(width), random(height / 2), random(1, 4)]);
}
}
// 星を描画
function drawStar() {
// 星を描く
for (let s of star) {
let c = color(random(150, 255), random(150, 255), 255);
c.setAlpha(random(150, 200));
fill(c);
ellipse(s[0], s[1], s[2], s[2]);
}
}
Also see: Tab Triggers