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.
<div id="overlay"><strong>⌛ 読み込み中 ⌛</strong></div>
<div id="media"></div>
<p id="message">🎵 右下のYouTubeプレイヤーをクリックすると再生が始まります</p>
<div id="control">
<button id="rewind">はじめから 🔄</button>
</div>
<div id="dummy"></div>
<script src="./index.js"></script>
main {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
align-items: center;
background: rgba(0, 0, 0, 0.2);
z-index: 100;
}
canvas {
margin: auto;
}
#media {
position: absolute;
right: 5px;
bottom: 5px;
z-index: 200;
}
#overlay {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0, 0, 0, 0.7);
color: rgba(255, 255, 255);
display: flex;
align-items: center;
z-index: 300;
}
#overlay > * {
margin: auto;
}
#message {
display: none;
}
#control {
display: none;
z-index: 201;
}
#dummy {
opacity: 0;
}
.inactive {
display: none !important;
}
.active {
display: block !important;
}
const P5 = p5;
const { Player, Ease } = TextAliveApp;
// プレイヤーの初期化 / Initialize TextAlive Player
const player = new Player({
app: {
token: "ZkFzIGUn8CuGxtOC"
},
mediaElement: "#media"
});
let init = false;
// リスナの登録 / Register listeners
player.addListener({
onAppReady: (app) => {
if (!app.managed) {
player.createFromSongUrl("http://www.youtube.com/watch?v=XSLhsjepelI");
}
},
onTextLoad: (text) => {
// Webフォントを確実に読み込むためDOM要素に歌詞を貼り付ける
document.querySelector("#dummy").textContent = text;
},
onVideoReady: () => {
if (!player.app.managed) {
document.querySelector("#message").className = "active";
}
document.querySelector("#overlay").className = "inactive";
},
onPlay: () => {
document.querySelector("#message").className = "inactive";
if (!player.app.managed) {
document.querySelector("#control").className = "";
}
console.log("player.onPlay");
},
onPause: () => {
console.log("player.onPause");
},
onSeek: () => {
console.log("player.onSeek");
},
onStop: () => {
if (!player.app.managed) {
document.querySelector("#control").className = "active";
}
console.log("player.onStop");
}
});
// 再生終了後に表示する巻き戻しボタン
document.querySelector("#rewind").addEventListener("click", () => {
player.requestPlay();
});
// p5.js を初期化
new P5((p5) => {
// キャンバスの大きさなどを計算
const width = Math.min(640, window.innerWidth);
const height = Math.min(270, window.innerHeight);
const margin = 30;
const numChars = 10;
const textAreaWidth = width - margin * 2;
// キャンバスを作成
p5.setup = () => {
p5.createCanvas(width, height);
p5.colorMode(p5.HSB, 100);
p5.frameRate(30);
p5.background(40);
p5.noStroke();
p5.textFont("Noto Sans JP");
p5.textAlign(p5.CENTER, p5.CENTER);
};
// ビートにあわせて背景を、発声にあわせて歌詞を表示
p5.draw = () => {
// プレイヤーが準備できていなかったら何もしない
if (!player || !player.video) {
return;
}
const position = player.timer.position;
// 背景
p5.background(40);
const beat = player.findBeat(position);
if (beat) {
const progress = beat.progress(position);
const rectHeight = Ease.quintIn(progress) * height;
p5.fill(0, 0, 0, Ease.quintOut(progress) * 60);
p5.rect(0, rectHeight, width, height - rectHeight);
}
// 歌詞
// - 再生位置より 100 [ms] 前の時点での発声文字を取得
// - { loose: true } にすることで発声中でなければ一つ後ろの文字を取得
let char = player.video.findChar(position - 100, { loose: true });
if (char) {
// 位置決めのため、文字が歌詞全体で何番目かも取得しておく
let index = player.video.findIndex(char);
while (char) {
if (char.endTime + 160 < position) {
// これ以降の文字は表示する必要がない
break;
}
if (char.startTime < position + 100) {
const x = ((index % numChars) + 0.5) * (textAreaWidth / numChars);
let transparency,
y = 0,
size = 39;
// 100 [ms] かけてフェードインしてくる
if (position < char.startTime) {
const progress = 1 - (char.startTime - position) / 100;
const eased = Ease.circIn(progress);
transparency = progress;
size = 39 * eased + Math.min(width, height) * (1 - eased);
}
// 160 [ms] かけてフェードアウトする
else if (char.endTime < position) {
const progress = (position - char.endTime) / 160;
const eased = Ease.quintIn(progress);
transparency = 1 - eased;
y = -eased * (height / 2);
}
// 発声区間中は完全に不透明
else {
transparency = 1;
}
p5.fill(0, 0, 100, transparency * 100);
p5.textSize(size);
p5.text(char.text, margin + x, height / 2 + y);
}
char = char.next;
index++;
}
}
};
});
Also see: Tab Triggers