cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              body {
  margin: 0;
  padding: 0;
  overflow: hidden;
}
#gco {
  position: fixed;
  top: 0;
  left: 0;
}
            
          
!
            
              var canvas = document.getElementById('canvas'),
    ctx = canvas.getContext('2d'),
    NUM = 1, // 背景のパーティクル数
    cw = document.documentElement.clientWidth,
    ch = document.documentElement.clientHeight,
    particles = [],
    gco = document.getElementById('gco');
canvas.width = cw, canvas.height = ch;

// 中央のパーティクルを描画
for(var i = 0; i < NUM; i++) {
  positionX = cw / 2;
  positionY = ch / 2;
  particle = new Particle(ctx, positionX, positionY);
  particles.push(particle);
}

// マウスに追従するパーティクルを描画
mousetrace = new Particle(ctx, 0, 0, 320);
// マウスに合わせて再描画
canvas.addEventListener('mousemove', mousetrace_e);
canvas.addEventListener('touchmove', mousetrace_e);
function mousetrace_e(e) {
  e.preventDefault();
  var touch = e.targetTouches === undefined ? e : e.targetTouches[0];
  render();
  mousetrace.x = touch.clientX, mousetrace.y = touch.clientY;
  mousetrace.render();
}

function Particle(ctx, x, y, r) {
  // 初期化
  this.ctx = ctx;
  this.radius = r || 400;
  this.x = x || 0;
  this.y = y || 0;
  this.mx = 0;
  this.my = 0;
  // 色をランダムにする
  this.color = {
    r: Math.floor(Math.random() * 255),
    g: Math.floor(Math.random() * 255),
    b: Math.floor(Math.random() * 255),
    a: 1
  };
}
Particle.prototype.render = function() {
  this.draw();
  // マウス位置に描く
}
Particle.prototype.draw = function() {
  ctx = this.ctx;
  ctx.beginPath();
  ctx.fillStyle = this.gradation();
  ctx.arc(
    this.x,
    this.y,
    this.radius,
    Math.PI * 2,
    false
  );
  ctx.fill();
  ctx.closePath();
}

Particle.prototype.gradation = function() {
  // グラデーションをつくる
  var col = this.color.r + ', ' + this.color.g + ', ' + this.color.b;
  var gr = this.ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.radius);
  gr.addColorStop(0, 'rgba(' + col + ', 1)');
  gr.addColorStop(0.3, 'rgba(' + col + ', 0.8)');
  gr.addColorStop(1, 'rgba(' + col + ', 0)');
  return gr;
}

render();
function render() {
  // 初期化
  cw = document.documentElement.clientWidth, ch = document.documentElement.clientHeight;
  // キャンバス再描画
  ctx.clearRect(0, 0, cw, ch);
  particles.forEach(
    function(e){
      e.render();
    }
  );
}

// セレクトボックスが変更されたら再描画
gco.addEventListener('change', function(){
  render();
});

// 窓リサイズの処理
var resizeTimer;
var interval = Math.floor(1000 / 60 * 10);
window.addEventListener('resize', function (event) {
  if (resizeTimer !== false) {
    clearTimeout(resizeTimer);
  }
  resizeTimer = setTimeout(function () {
   render();
  }, interval);
});

            
          
!
999px
Loading ..................

Console