123

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.

            
              html
  body
    canvas(width="500", height="500", style="background-color: #95a5a6;")
      
            
          
!
            
              @import url('//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css');
            
          
!
            
              
let requestAnimFrame = (cb) => {
  return window.requestAnimationFrame(cb);
}

function getMousePos(canvas, evt) {
    var rect = canvas.getBoundingClientRect();
    return new Point(
      evt.clientX - rect.left,
      evt.clientY - rect.top
    );
}

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
  
  add(p) {
    return new Point(
      this.x + p.x,
      this.y + p.y
    );
  }
  
  subtract(p) {
    return new Point(
      this.x - p.x,
      this.y - p.y
    );
  }

  copy() {
    return this.add(new Point(0, 0));
  }
  
  distanceTo(p) {
    let dx = p.x - this.x;
    let dy = p.y - this.y;
    return Math.sqrt(
      dx * dx + dy * dy
    );
  }
  
  projectOntoCircle(circleCenterPoint, circleRadius) {
    
    let angle = this.getAngleBetween(circleCenterPoint, circleRadius);
    
    let newX = circleCenterPoint.x + Math.cos(angle) * circleRadius;
    
    let newY = circleCenterPoint.y + Math.sin(angle) * circleRadius;
    // console.log(distanceBetween, angle, newX, newY);
    return new Point(newX, newY);
  }
  
  getAngleBetween(circleCenterPoint, circleRadius) {
    let delta = this.subtract(circleCenterPoint);
    let angle = Math.atan2(delta.y, delta.x);
    return angle;
  }
}

class VolumeBar {
  constructor(canvas) {
    
    this.canvas = canvas;
    
    this.VOLUME_ICON_MAPPING = [
      [0, '\uF026'], // mute
      [30, "\uF027"], // low
      [70, "\uF028"], // up
    ];
    
    // The total radius of the volume dial
    this.TOTAL_RADIUS = 100;
    
    // The radius of the scrubber control
    this.SCRUBBER_RADIUS = 10;
    
    // The degrees to offset the circular calculations
    this.ANGLE_OFFSET = -90;
    
    this.volume = 0;
    this.maxVolume = 100;
    this.angleInDegrees = .5;

    this.volumeCenter = new Point(250, 250);
    this.circleCenter = this.volumeCenter.add(new Point(0, -this.TOTAL_RADIUS));
    this.scrubberCenter = this.volumeCenter.copy();
    
    this.scrubberDragging = false;
    
    this._bindEvents();
  }
  
  _bindEvents() {
    this.canvas.addEventListener('mousedown', this._onMouseDown.bind(this));
    this.canvas.addEventListener('mouseup', this._onMouseUp.bind(this));
    this.canvas.addEventListener('mouseout', this._onMouseOut.bind(this));
    this.canvas.addEventListener('mousemove', this._onMouseMove.bind(this));
  }
  
  _unbindEvents() {
    this.canvas.removeEventListener('mousedown', this._onMouseDown);
    this.canvas.removeEventListener('mouseup', this._onMouseUp);
    this.canvas.removeEventListener('mouseout', this._onMouseOut);
    this.canvas.removeEventListener('mousemove', this._onMouseMove);
  }
  
  _onMouseDown(e) {
    // console.log('Mousedown happening!');
    
    let cursorP = getMousePos(this.canvas, e);
    
    let distanceToScrubber = this.scrubberCenter.distanceTo(cursorP);
    
    if (distanceToScrubber < this.SCRUBBER_RADIUS) {
      // console.log('You fucking clicked it!');
      this.scrubberDragging = true;
    }
  }
  
  _onMouseOut() {
    // console.log('Mouseout happening!');
    this.scrubberDragging = false;
  }
  
  _onMouseUp() {
    // console.log('Mouseup happening!');
    this.scrubberDragging = false;
  }
  
  _onMouseMove(e) {
    if (this.scrubberDragging) {
      // console.log('Dragging scrubber!');
      
      let cursorP = getMousePos(this.canvas, e);
      
      // Find the scrubber's nearest position on the circle
      this.scrubberCenter = cursorP.projectOntoCircle(this.circleCenter, this.TOTAL_RADIUS);
      
      this.angle = cursorP.getAngleBetween(this.circleCenter, this.TOTAL_RADIUS);
      this.angleInDegrees = 360 - ((this.angle * 180 / Math.PI + 360 + this.ANGLE_OFFSET) % 360);
      this.volume = this.angleInDegrees / 360;
    }
  }
  
  update(ctx) {
    
    // this.volume += 1/60 * 50;
    // this.volume = this.volume % 100;
    
    this.render(ctx);
  }
  
  render(ctx) {
    
    let center = this.volumeCenter;
    
    let radius = this.TOTAL_RADIUS;
    
    ctx.fillStyle = "#000";
    ctx.strokeStyle = "#000";
    ctx.lineWidth = 1;
    
    // Draw center dot for reference
    ctx.beginPath();
    ctx.arc(center.x, center.y, 2, 0, Math.PI * 2);
    ctx.stroke();
    ctx.fill();
    ctx.closePath();
    
    // Draw current sound icon
    ctx.save();
    ctx.font = "26px FontAwesome";
    ctx.textAlign = "center";
    ctx.textBaseline = "middle";
    ctx.fillStyle = "#ecf0f1";
    ctx.fillText(this._getVolumeIcon(), center.x, center.y + 35);
    ctx.restore();
    
    // Draw circle center
    ctx.color = "#000";
    ctx.beginPath();
    ctx.arc(this.circleCenter.x, this.circleCenter.y, 2, 0, Math.PI * 2);
    ctx.stroke();
    ctx.fill();
    ctx.closePath();
    
    // Draw outer boundary
    // ctx.beginPath();
    // ctx.arc(this.circleCenter.x, this.circleCenter.y, radius, 0, Math.PI * 2);
    // ctx.stroke();
    // ctx.closePath();
    
    // Angle thing
    ctx.save();
    ctx.lineWidth = 15;
    ctx.beginPath();
    ctx.strokeStyle = "#7f8c8d";
    let rads = (Math.PI * 2) - (this.angleInDegrees * Math.PI / 180);
    ctx.arc(this.circleCenter.x, this.circleCenter.y, 50, Math.PI / 2, rads + Math.PI / 2, true);
    ctx.stroke();
    ctx.restore();
    
    // Scrubber Baseline
    ctx.save();
    ctx.strokeStyle = "#444";
    ctx.beginPath();
    ctx.moveTo(this.circleCenter.x, this.circleCenter.y);
    ctx.lineTo(this.volumeCenter.x, this.volumeCenter.y);
    ctx.lineWidth = 20;
    ctx.lineCap = 'round';
    ctx.stroke();
    ctx.closePath();
    ctx.restore();
    
    // Scrubber Path
    ctx.save();
    ctx.strokeStyle = "#ccc";
    ctx.beginPath();
    ctx.moveTo(this.circleCenter.x, this.circleCenter.y);
    ctx.lineTo(this.scrubberCenter.x, this.scrubberCenter.y);
    ctx.lineWidth = 36;
    ctx.shadowColor = 'rgba(0, 0, 0, .5)';
    ctx.shadowBlur = 5;
    ctx.shadowOffsetX = -2;
    ctx.shadowOffsetY = 2;
    ctx.lineCap = 'round';
    ctx.stroke();
    ctx.closePath();
    ctx.restore();
    
    // Scrubber Path Interior
    ctx.save();
    ctx.strokeStyle = "#777";
    ctx.beginPath();
    ctx.moveTo(this.circleCenter.x, this.circleCenter.y);
    ctx.lineTo(this.scrubberCenter.x, this.scrubberCenter.y);
    ctx.lineWidth = 26;
    ctx.lineCap = 'round';
    ctx.stroke();
    ctx.closePath();
    ctx.restore();
    
    // Draw scrubber
    ctx.save();
    // ctx.fillStyle = "#3498db";
    // ctx.fillStyle = "#34495e";
    ctx.fillStyle = "#1abc9c";
    ctx.beginPath();
    ctx.arc(this.scrubberCenter.x, this.scrubberCenter.y, this.SCRUBBER_RADIUS, 0, Math.PI * 2);
    ctx.shadowColor = 'rgba(0, 0, 0, .8)';
    ctx.shadowBlur = 4;
    ctx.fill();
    ctx.closePath();
    ctx.restore();
    
    // Angle
    ctx.save();
    ctx.fillStyle = "#fff";
    ctx.font = "10px Arial";
    ctx.textAlign = "center";
    ctx.textBaseline = "middle";
    ctx.fillText(~~this.angleInDegrees, this.scrubberCenter.x, this.scrubberCenter.y);
    ctx.restore();
  }
  
  _getVolumeIcon() {
   
    let res = [];
    let fullSpeakers = ~~(this.angleInDegrees / 100);

    for (let i = 0; i < fullSpeakers; i++) {
      res.push( this.VOLUME_ICON_MAPPING[ this.VOLUME_ICON_MAPPING.length - 1 ][1] );
    }
    
    let fractionalSpeakers = ((this.angleInDegrees / 100.0) - fullSpeakers) * 100;
    
    
    let partial;
    this.VOLUME_ICON_MAPPING.forEach((icon, index) => {
      console.log(fractionalSpeakers, icon[0]);
      if (fractionalSpeakers >= icon[0]) {
        partial = icon[1];
      }
    });
    
    return res.concat(partial).join(" ");
  }
}

let canvas = document.getElementsByTagName("canvas")[0];
let ctx = canvas.getContext("2d");
let bar = new VolumeBar(canvas);

let loop = () => {
  ctx.clearRect(0, 0, 500, 500);
  bar.update(ctx);
  requestAnimFrame(loop);
}

requestAnimFrame(loop);
            
          
!
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.

Console