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 class="btn" onclick="circlesColorChange(0)">
ランダム色に変更
</div>
<div class="btn set-color1" onclick="circlesColorChange(1)">
この色に変更→
<span class="color-circle colorset1-1">●</span>
<span class="color-circle colorset1-2">●</span>
<span class="color-circle colorset1-3">●</span>
<span class="color-circle colorset1-4">●</span>
</div>
<div class="btn set-color2" onclick="circlesColorChange(2)">
この色に変更→
<span class="color-circle colorset2-1">●</span>
<span class="color-circle colorset2-2">●</span>
<span class="color-circle colorset2-3">●</span>
<span class="color-circle colorset2-4">●</span>
</div>
<div class="btn set-color3" onclick="circlesColorChange(3)">
この色に変更→
<span class="color-circle colorset3-1">●</span>
<span class="color-circle colorset3-2">●</span>
<span class="color-circle colorset3-3">●</span>
<span class="color-circle colorset3-4">●</span>
</div>
<div class="btn increase-circle" onclick="increaseCircle()">
粒を10個増やす<br><span id="now-circle-num"></span>
</div>
<canvas id="cv"></canvas>
body{
margin: 0;
font-family: sans-serif;
}
.btn{
position: absolute;
top: 1em;
right: 0;
padding: 0.5em 1em;
font-weight: bold;
color: #55aabb;/*文字色*/
background: rgba(14,35,69,.6);
border-left: solid 6px #55aabb;/*左線*/
cursor: pointer;
transition: .2s;
&:hover {
transform: translateY(2px);
filter: brightness(1.5);
}
.colorset1-1{
color: #0f3057;
}
.colorset1-2{
color: #00587a;
}
.colorset1-3{
color: #008891;
}
.colorset1-4{
color: #e7e7de;
}
.colorset2-1{
color: #ffc7c7;
}
.colorset2-2{
color: #ffe2e2;
}
.colorset2-3{
color: #f6f6f6;
}
.colorset2-4{
color: #8785a2;
}
.colorset3-1{
color: #005874;
}
.colorset3-2{
color: #1c819e;
}
.colorset3-3{
color: #e6e6d4;
}
.colorset3-4{
color: #ffbe00;
}
}
.set-color1{
top: 4em;
}
.set-color2{
top: 8em;
}
.set-color3{
top: 12em;
}
.increase-circle{
top: 16em;
#now-circle-num{
font-size: .7em;
}
}
//----------------------------
// min以上〜max以下までの範囲でランダムな変数を生成
// 参考記事: https://qiita.com/uto-usui/items/7193db237175ba15aaa3
//----------------------------
function randRange(min,max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
//----------------------------
// ランダムなカラーコード生成
// 参考記事: https://qiita.com/_shimizu/items/e3826359b328974c9911
//----------------------------
function colorGen(){
return '#'+Math.floor(Math.random()*16777215).toString(16);
}
var colorSetArray = [
["#0f3057", "#00587a", "#008891", "#e7e7de"],
["#ffc7c7", "#ffe2e2", "#f6f6f6", "#8785a2"],
["#005874", "#1c819e", "#e6e6d4", "#ffbe00"]
];
//----------------------------
// 指定範囲の色を生成
//----------------------------
function colorGenRange(colorSetNum){
if(colorSetNum == 0){ // 値が0だったらランダム生成
return colorGen();
}else{ // 値が1だったら指定されたカラーコードのパレットよりランダムに生成
return colorSetArray[colorSetNum-1][randRange(1,4)];
}
}
//----------------------------
// Canvasを画面いっぱいに表示
// 参考記事: http://weathercook.hatenadiary.jp/entry/20111118/1321600756
//----------------------------
var cv = document.getElementById('cv');
var ctx = cv.getContext("2d");
expandCanvas();
function expandCanvas(){
var b = document.body;
var d = document.documentElement;
cv.width = Math.max(b.clientWidth , b.scrollWidth, d.scrollWidth, d.clientWidth);
cv.height = Math.max(b.clientHeight , b.scrollHeight, d.scrollHeight, d.clientHeight);
}
//----------------------------
// Circleオブジェクト
//----------------------------
var Circle = function(){
// オブジェクト生成と同時に値をセット
return this.set();
}
// オブジェクトの初期化
Circle.prototype.set = function(){
this.x = randRange(0, cv.width);
this.y = randRange(0, cv.height);
this.size = Math.random() * 4 + 1;
this.speed = Math.random() * 1 + 2;
this.color = colorGenRange(0);
var radian = Math.random() * (Math.PI * 360);
this.to_x = Math.cos( radian );
this.to_y = Math.sin( radian );
}
// オブジェクトを動かす操作
Circle.prototype.move = function(){
this.x += this.to_x * this.speed;
this.y += this.to_y * this.speed;
this.out_square_in();
}
// オブジェクトがウインドウ外に出た際の処理
Circle.prototype.out_square_in = function(){
if( this.x + this.size < 0 ) this.x = cv.width;
if( cv.width < this.x ) this.x = 0 - this.size;
if( this.y + this.size < 0 ) this.y = cv.height;
if( cv.height < this.y ) this.y = 0 - this.size;
}
// インスタンス化
var circleNum = 30;
var circles = [];
for( var i = 1; i <= circleNum; i++ ){
circles.push( new Circle() );
}
document.getElementById("now-circle-num").innerHTML = "※現在の粒の数: "+ circleNum +"個"
function myCanvasRun(){
ctx.clearRect(0, 0, cv.width, cv.height);
// ドローイング関数で実際にCanvas上に描画
function draw(){
// 背景の描画
ctx.fillStyle = "#000"; // setting background color
ctx.fillRect( 0, 0, cv.width, cv.height ); // drawing background
// オブジェクトの描画
var p;
for ( var i = 0; i < circles.length; i++) {
p = circles[i];
ctx.fillStyle = p.color; // setting the color of an instance
ctx.beginPath(); // パス書きの指定
ctx.arc(p.x, p.y, p.size, 10 * Math.PI / 180, 80 * Math.PI / 180, true);
ctx.fill();
p.move();
}
}
setInterval(draw, 20);
}
window.onload = myCanvasRun(0);
function circlesColorChange(colorSetNum){
var p;
for ( var i = 0; i < circles.length; i++) {
p = circles[i];
p.color = colorGenRange(colorSetNum);
}
}
function increaseCircle(){
circleNum += 10;
document.getElementById("now-circle-num").innerHTML = "※現在の粒の数: "+ circleNum +"個"
for( var i = 1; i <= 10; i++ ){
var p = new Circle();
circles.push( new Circle() );
}
}
Also see: Tab Triggers