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.

            
              <canvas id="myCanvas" width=300 height=350></canvas>
            
          
!
            
              #myCanvas {
  background-color: rgba(158, 167, 184, 0.2);
}
            
          
!
            
              // See my tech blog at http://tech.scargill.net 
// Very simple example of getting mouse information from an HTML5 canvas for gauges etc.
// values returned include degrees, x and y and finally distance from centre of the canvas

// Get a "handle" on the canvas to work with
theCanvas = document.getElementById("myCanvas");
// define the centre point of the canvas
cX = Math.floor(theCanvas.width / 2);
cY = Math.floor(theCanvas.height / 2);
// Get a "context"
ctx= theCanvas.getContext("2d");

// set up the font
ctx.font = "12px Helvetica";
ctx.textAlign = 'center';
ctx.fillStyle = '#8A8A8A';

// the rest are functions - the "event listener" traps the mouse and displays results

// Get the position of the mouse relative to the canvas
function getMousePos(canvasDom, mouseEvent) {
  var rect = canvasDom.getBoundingClientRect();
  return {
    x: mouseEvent.clientX - rect.left,
    y: mouseEvent.clientY - rect.top
  };
}

// Here we get mouse coordinates and translate to a value
// Good old basic algebra from school
// Apparently the convention is  to start at 0 degrees on the right and go backwards
// - someone should have told that to clock-makers
theCanvas.addEventListener("mousemove", function(e) {
  var m = getMousePos(theCanvas, e);
  if (m.x < cX) {
    if (m.y < cY) pos = 180 + Math.floor(Math.atan((m.y - cY) / (cX - m.x)) * (180 / Math.PI));
    else pos = 180 + -Math.floor(Math.atan((m.y - cY) / (m.x - cX)) * (180 / Math.PI));
  } else {
    if (m.y < cY) pos = - Math.floor(Math.atan((cY - m.y) / (cX - m.x)) * (180 / Math.PI)) ;
    else pos = 360 + Math.floor(Math.atan((cY - m.y) / (m.x - cX)) * (180 / Math.PI));
  }

  // and for good measure get the distance from the centre
  // You may, for example, only be interested in an arc area around the edge of the canvas
  tX = Math.abs(cX - m.x);
  tY = Math.abs(cY - m.y);
  tD = Math.floor(Math.sqrt(tX * tX + tY * tY));


  // print the result
  ctx.clearRect(0, 0, myCanvas.width, myCanvas.height);
  ctx.moveTo(cX, cY);
  ctx.fillStyle = "#ff0044";
  ctx.arc(cX, cY, 5, 0, 360, false);
 // ctx.closePath();
  ctx.fill();
  
    // some colour just for the sake of it
  var gradient = ctx.createLinearGradient(0, 0, 150, 100);
  gradient.addColorStop(0, "rgb(255, 0, 128)");
  gradient.addColorStop(1, "rgb(105, 103, 255)");
  ctx.fillStyle = gradient;

  ctx.fillText("Mouse= " + pos + " degrees. (" + m.x + "x," + m.y + "y)" + " Dist; " + tD + "px", cX, 30);
}, false);

            
          
!
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