css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

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.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <button>create</button>
<button>reset</button>

<div class="canvas-wrapper">
  <canvas class="myCanvas">
    <p>Add suitable fallback here.</p>
  </canvas>
</div>

            
          
!
            
              canvas {
  margin: 0 auto;
  overflow: hidden;
}
.canvas-wrapper {
  margin: 0 auto;
  width: 500px;
  height: 500px;
  overflow: hidden;
}
            
          
!
            
              var canvas = document.querySelector('.myCanvas');
var wrapper = document.querySelector('.canvas-wrapper');
var createBtn = document.querySelector('button');
var resetBtn = document.querySelector('button').nextElementSibling;


var width = canvas.width = window.innerWidth;
var height = canvas.height = window.innerHeight;

var ctx = canvas.getContext('2d');
// //-----sample st----//

// //塗りつぶしの色を決める
// ctx.fillStyle = 'blue';
// //どこでどの程度の図形を描画するか
// //usage:fillRect(x座標,y座標,横幅,縦幅);
// ctx.fillRect(300, 200, 50, 50);
// // ctx.lineWidth = 5;

// //-----sample en----//


const createRect = (color,x,y,w,h)=> {
  ctx.fillStyle = color;
  ctx.fillRect(x,y,w,h);
}

//-----四角形を100個ランダムに生成 st----//
const randomRectHandler = ()=>{
  
  const random = (max) => {
    return Math.floor(Math.random(100) * Math.floor(max)); 
  }
  const randomColor = ()=> {
   var code1 = Math.floor(Math.random(255) * Math.floor(255));
   var code2 = Math.floor(Math.random(255) * Math.floor(255));
   var code3 = Math.floor(Math.random(255) * Math.floor(255));
    return `rgb(${code1},${code2},${code3})`;
  }
  console.log(randomColor());

  const  randomRect = ()=> {
    for (let i = 0; i < 100; i++){
    // st 乱数で描画する図形の位置・サイズ・色を生成
   var color = randomColor();
   var xr = random(window.innerWidth); 
   var yr = random(window.innerWidth);
   var wr = random(100);
   var hr = random(100); 
    //en
      console.log(color);
      console.log(xr);
      createRect(color,xr,yr,wr,hr);
    }
  }
  randomRect();
}
const canvasReset = ()=> {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
}

createBtn.addEventListener('click',randomRectHandler);
resetBtn.addEventListener('click',canvasReset);

//-----en -----

//度数をラジアン変換
function degToRad(degrees) {
  return degrees * Math.PI / 180;
};

//-----三角形描画 st----//
const createTriangle = ()=> {
ctx.fillStyle = 'rgb(255, 0, 0)'; 

//beginPathはデフォルト(0,0)の状態から描画を開始する
ctx.beginPath();

//movetoでデフォルトの状態(0,0)から指定した値分座標を移動
ctx.moveTo(50, 50);

//(150,50)に渡って線を書く(x軸に100px)ことを宣言
ctx.lineTo(150, 50);
var triHeight = 50 * Math.tan(degToRad(60));

ctx.lineTo(100, 50+triHeight);
ctx.lineTo(50, 50);
ctx.lineWidth = 2;
//トレースしたパスに沿ってアウトラインを引く
ctx.stroke();
//トレースしたパスを塗りつぶし
ctx.fill();
}
// createTriangle();
//-----en----//


//-----円を描画 st----//
const createCircle = ()=> {
  ctx.fillStyle = 'rgb(0, 0, 255)';
  ctx.beginPath();
  //.arc(x,y(円弧の中心),円の半径,開始角度,終了角度,円を時計or反時計周り)
  ctx.arc(150, 106, 50, degToRad(0), degToRad(360), false);
  ctx.fill();
}
//-----en----//


//-----文字描画 st----//
const createLetter = ()=> {
ctx.strokeStyle = 'black';
ctx.lineWidth = 1;
ctx.font = '36px arial';
ctx.strokeText('Canvas text sample!', 50, 50);

ctx.fillStyle = 'red';
ctx.font = '48px georgia';
ctx.fillText('Canvas text sample!', 50, 150);
}
// createLetter();

//-----en----//


//-----animation----//


//-----en----//

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console