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-container">
  <canvas id="source-canvas"></canvas>
  <canvas id="target-canvas"></canvas>
</div>
              
            
!

CSS

              
                body {
  margin: 0;
  background: #000000;
  overflow: hidden;
}

#source-canvas {
  display: none;
}
              
            
!

JS

              
                //https://codepen.io/lucasmotta/details/dpDqB
// https://codepen.io/soulwire/pen/Ffvlo?editors=0010

function map_range(value, low1, high1, low2, high2) {
    return low2 + (high2 - low2) * (value - low1) / (high1 - low1);
}

function dot(ctx, x, y, radius, color) {
  ctx.beginPath();
  ctx.arc(x, y, radius, 0, 2 * Math.PI, false);
  ctx.fillStyle = color;
  ctx.fill();
}

class Dot {
  constructor(options) {
    this.originalPos = new p5.Vector(options.x, options.y);
    this.pos = this.originalPos.copy();
    this.target = new p5.Vector(options.x, options.y);
    this.vel = new p5.Vector(0, 0);
    this.acc = new p5.Vector(0, 0);
    
    this.radius = options.radius;
    this.color = options.color;
    this.originalColor = this.color;
    this.hoverColor = options.hoverColor;
    this.ctx = options.context;
    
    this.maxforce = 0.1; // Arbitrary value
    this.maxspeed = 4; // Arbitrary value
    this.comfortZone = 100; // Arbitrary value
  }
  
  addPredator (predator) {
    this.predator = predator;
  }
  
  update () {
    if (this.predator) {
      const distanceFromPredator = this.pos.dist(this.predator.pos);
      if (distanceFromPredator < this.comfortZone) {
          this.color = this.hoverColor;
          this.target = this.predator.pos;
      } else {
        this.target = this.originalPos;
        this.color = this.originalColor;
      }
    }
    this.vel.add(this.acc);
    this.vel.limit(this.maxspeed);
    this.pos.add(this.vel);
    this.acc.mult(0);
  }
  
  applyForce (force) {
    this.acc.add(force);
  }
  
  seek (target) {
    const desired = p5.Vector.sub(target, this.pos);  // A vector pointing from the position to the target
    const d = desired.mag();
    // Scale with arbitrary damping within 100 pixels
    if (d < 100) {
      const m = map_range(d, 0, 100, 0, this.maxspeed);
      desired.setMag(m);
    } else {
      desired.setMag(this.maxspeed);
    }

    // Steering = Desired minus Velocity
    const steer = p5.Vector.sub(desired, this.vel);
    steer.limit(this.maxforce);  // Limit to maximum steering force
    this.applyForce(steer);
  }
  
  draw () {
    dot(this.ctx, this.pos.x, this.pos.y, this.radius, this.color)
  }
}

class Mouse {
  constructor(context) {
    this.pos = new p5.Vector(window.innerWidth / 2, window.innerHeight/ 2);
    this.context = context;
  }
  
  updatePos (e) {
    this.pos.x = e.clientX;
    this.pos.y = e.clientY;
  }
  
  draw () {
    dot(this.context, this.pos.x, this.pos.y, 10, 'red')
  }
}

var sourceCanvas = document.getElementById('source-canvas');
var targetCanvas = document.getElementById('target-canvas');
var sourceCtx = sourceCanvas.getContext('2d');
var targetCtx = targetCanvas.getContext('2d');
var imageObj = new Image();
const dots = [];

// PARAMS
const imageSrc = 'https://cdn.rawgit.com/TheoGil/codepen-assets/5880eb52/portrait.jpg?raw=true';
const detailLevel = 7;
const minDotRadius = 0.5;
const maxDotRadius = 4;
const dotColor = '#7CDFFF'; // #E9007F #D5FFFB

// Allow us to use context.getImageData()
imageObj.crossOrigin = "Anonymous";
imageObj.src = imageSrc; // CREDIT: https://pixabay.com/fr/noir-et-blanc-photographie-visage-1590771/
imageObj.onload = initializeDots;

function initializeDots () {
  sourceCanvas.width = window.innerWidth;
  sourceCanvas.height = window.innerHeight;
  
  targetCanvas.width = window.innerWidth;
  targetCanvas.height = window.innerHeight;
  
  sourceCtx.drawImage(
    imageObj, 
    targetCanvas.width / 2 - imageObj.width / 2,
    targetCanvas.height / 2 - imageObj.height / 2
  );
  
  var imageData = sourceCtx.getImageData(0, 0, sourceCanvas.width, sourceCanvas.height);
  
  // Iterate over pixels, and drawing on dot with a radius defined by the brightness of the pixel
  for(var y = 0; y < sourceCanvas.height; y += detailLevel) {
    const row = sourceCanvas.width * y;
    for(var x = 0; x < sourceCanvas.width; x += detailLevel) {
      const pixel = (row + x) * 4;
      rPos = pixel;
      gPos = pixel + 1;
      bPos = pixel + 2;
    
      // Compute the average brightness of a pixel
      var brightness = (imageData.data[rPos] + imageData.data[gPos] + imageData.data[bPos]) / 3;
      const radius = map_range(brightness, 0, 255, 0, maxDotRadius);
      
      // Make sure that we don't waste ressources on drawing imperceptibles dots
      if (radius > minDotRadius) {
        const dot = new Dot({
          x,
          y,
          radius,
          color: dotColor,
          hoverColor: '#0099cc',
          context: targetCtx,
        });
        dot.addPredator(mouse);
        dots.push(dot);
      }
    }
  }
  
  loop();
}

const mouse = new Mouse(targetCtx);
targetCanvas.addEventListener('mousemove', (e) => {
  mouse.updatePos(e);
});

function loop () {
  requestAnimationFrame(loop);
  targetCtx.clearRect(0, 0, targetCanvas.width, targetCanvas.height);
  dots.forEach((dot) => {
    dot.seek(dot.target);
    dot.update();
    dot.draw();
  });
  //mouse.draw();
}

              
            
!
999px

Console