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.

            
              <center>
  <span>
    2018 NBA TITLE ODDS <span>
    <p> Top 5 Teams </p>
    <div></div>
  <canvas width='1000' height='500'>
    </canvas> 
    <p> Odds as of June 12 at Bovada</p>
</center>

            
          
!
            
              canvas {
  border: 2px solid black;
}
span {
  font-size: 25px;
}
p {
  font-size: 20px;
}
            
          
!
            
              var cx = document.querySelector('canvas').getContext('2d'); 
var canvas = document.querySelector('canvas'); 


//mouse Position
function getMousePos(canvas, event) {
        var rect = canvas.getBoundingClientRect();
        var x = event.clientX - rect.left;
        var y = event.clientY - rect.top; 
        return {
          x : event.clientX - rect.left,  
          y : event.clientY - rect.top
        };
}

canvas.addEventListener('mousemove', function(event) {
  var mousePos = getMousePos(canvas, event); 
  //console.log(mousePos); 
});

//get Colors
function rgbToHex(r, g, b) {
    if (r > 255 || g > 255 || b > 255)
        throw "Invalid color component";
    return ((r << 16) | (g << 8) | b).toString(16);
}
/*
canvas.addEventListener('mousemove', function(event) {
    var pos = getMousePos(canvas, event);
    var x = pos.x;
    var y = pos.y;
    //console.log([x,y])
    var coord = "x=" + x + ", y=" + y;
    var p = cx.getImageData(x, y, 1, 1).data; 
    var hex = "#" + ("000000" + rgbToHex(p[0], p[1], p[2])).slice(-6);
    //console.log(hex);
});
*/

//teams OBJECT 
var teams = [
  {
  team : 'Golden State Warriors', 
  count : 2929.67,
  odds : '-150',
  color : '#0017ff'
  }, 
  {
  team : 'Cleveland Cavaliers', 
  count : 1464.83,
  odds : '+300',
  color : '#871717'
  },
  {
  team : 'Boston Celtics', 
  count : 366.21,
  odds : '+1200',
  color : '#198717'
  },
  {
  team : 'San Antonio Spurs', 
  count : 366.21,
  odds : '+1200',
  color : '#838383'
  },
  {
  team : 'Houston Rockets', 
  count : 219.73,
  odds : '+2000',
  color : '#ff5757'
  }//,
  //{
  //team : 'Los Angeles Clippers', 
  //count : 133.17,
  //odds : '+3300',
  //color : '#ff0000'
  //},
  //{
  //team : 'Toronto Raptors', 
  //count : 87.89,
  //odds : '+5000',
 // color : '#c9bc1c'
 // }, 
 // {
  //team : 'Washington Wizards', 
  //count : 87.89,
  //odds : '+5000',
  //color : '#001575'
  //},
  //{
  //team : 'Oklahoma City Thunder', 
  //count : 66.58,
  //odds : '+6600',
  //color : '#339cff'
  //}, 
]



//PIE CHART
var total = teams.reduce((total, their) => total + their.count, 0) 
var currentAngle = -0.5 * Math.PI; 
var xCenter = 500, yCenter = 250; 
//
canvas.addEventListener('mousemove', function(event) {
    var pos = getMousePos(canvas, event);
    var x = pos.x;
    var y = pos.y;
    //console.log([x,y])
    var coord = "x=" + x + ", y=" + y;
    var p = cx.getImageData(x, y, 1, 1).data; 
    var hex = "#" + ("000000" + rgbToHex(p[0], p[1], p[2])).slice(-6);
    //console.log(hex);
//
teams.forEach(their => {
  var sliceAngle = (their.count / total) * 2 * Math.PI; 
  cx.beginPath(); 
  cx.arc(xCenter, yCenter, 150, currentAngle, currentAngle + sliceAngle);
    var middleAngle = currentAngle + 0.5 * sliceAngle;
    var xText = Math.cos(middleAngle) * 230 + xCenter;
    var yText = Math.sin(middleAngle) * 195 + yCenter;
    cx.textBaseLine = "middle";
    if (Math.cos(middleAngle) > -0.35) 
      cx.textAlign = "left";
    else
      cx.textAlign = "right";
    cx.font = "20px sans-serif";
    cx.fillStyle = "black";
  if (hex === their.color) {
    cx.strokeText(their.team, xText, yText);
    cx.strokeText(their.odds, xText, yText + 20)
    var measure = cx.measureText(their.team); 
    //console.log([text.width(), text.height()]);
  } else if (hex !== their.color) {   
      //need to find way to remove text? 
      
  }
  
  

  currentAngle += sliceAngle; 
  cx.lineTo(xCenter, yCenter); 
  cx.fillStyle = their.color; 
  cx.fill();
  });
});



            
          
!
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