Pen Settings

HTML

CSS

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

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

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.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="canvas-box">
  <p>CANVAS 入門</p>
  <canvas id="canvas01"></canvas>
</div>
              
            
!

CSS

              
                body {
  display:flex;
  justify-content: center;
  padding: 10px;
  
  .canvas-box {
    
    p {
      color : #fff;
      text-align: center;
      font: bold;
      padding: 0 0 10px;
    }
    
    padding: 10px;
    background: #48dbfb;
    border-radius: 10px;
  }
}
              
            
!

JS

              
                $(function(){
  
  // キャンバスサイズ   
  const WIDTH = 500;
  const HEIGHT = 200;
  
  // 丸の数   
  const circleNum = 20;
  
  // 丸の最大最小サイズ
  const circleMinSize = 2;
  const circleMaxSize = 5 - circleMinSize;

  // canvas要素取得
  const canvas01 = $('#canvas01').get(0);
  canvas01.width = WIDTH;
  canvas01.height = HEIGHT;

  // コンテキスト取得
  const ctx = canvas01.getContext('2d');

  // 円の情報
  const circles = [];

  // 円の追加
  for(var i=0; i <= circleNum; i++){
    let xpath = Math.floor( Math.random() * WIDTH+1 ) ;
    let ypath = Math.floor( Math.random() * HEIGHT+1 ) ;
    const size = Math.floor( Math.random() * circleMaxSize+1 ) + circleMinSize ;
    const xspeed = Math.round( (Math.random()*20)-10, 10)/10;
    const yspeed = Math.round( (Math.random()*20)-10, 10)/10;
    circles.push({x:xpath, y:ypath, r: size, xs:xspeed, ys:yspeed}); 
  }

  // 各円を描画
  for(const c of circles) {
    ctx.beginPath();
    ctx.fillStyle = 'rgb(0, 0, 0)'; // 黒色
    ctx.arc(c.x, c.y, c.r, 0, 2 * Math.PI);
    ctx.fill();
  }
  
  function loop(ts){
    // 画面をクリア     
    ctx.clearRect(0, 0, WIDTH, HEIGHT);
    
    // 塗りつぶし     
    ctx.fillStyle = '#48dbfb';
    ctx.fillRect(0, 0, WIDTH, HEIGHT);
    
    let num = 0;
    
    // 各円を描画する。
    for(const c of circles) {
      
      let afters = circleNum - num;
      
      if(c.y <= 0 || c.y >= HEIGHT){
        c.ys = c.ys * -1;
      }
      if(c.x <= 0 || c.x >= WIDTH){
        c.xs = c.xs * -1;
      }
      
      // 線引く       
      for(let n=0; n <=afters; n++){
        if( Math.sqrt(((c.x - circles[circleNum-(afters-n)].x) ** 2) + ((c.y - circles[circleNum-(afters-n)].y) ** 2)) <= 70 ){
          ctx.beginPath();
          ctx.strokeStyle = '#fff';
          ctx.moveTo(c.x, c.y);
          ctx.lineTo(circles[circleNum-(afters-n)].x, circles[circleNum-(afters-n)].y);
          ctx.ineWidth = 0.1;
          ctx.closePath();
          ctx.stroke(); 
        }
      }
      
      c.x = c.x + c.xs;
      c.y = c.y + c.ys;
      
      ctx.beginPath();
      ctx.fillStyle = '#fff';
      ctx.arc(c.x, c.y, c.r, 0, 2 * Math.PI);
      ctx.fill();
    }
    
    num++;
    window.requestAnimationFrame((ts) => loop(ts));
  }
  

  window.requestAnimationFrame((ts) => loop(ts));
})
              
            
!
999px

Console