Skip to content

Instantly share code, notes, and snippets.

@khornberg
Created July 30, 2018 15:51
Show Gist options
  • Save khornberg/a59a544e0b9215b46bf306a4ad320fcd to your computer and use it in GitHub Desktop.
Save khornberg/a59a544e0b9215b46bf306a4ad320fcd to your computer and use it in GitHub Desktop.
Circle with centered text via html canvas
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="84" height="84"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
var radius = (canvas.width - 2) / 2;
context.beginPath();
context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
context.fillStyle = '#eee';
context.fill();
context.strokeStyle = '#333';
context.stroke();
context.font = '20pt sans-serif';
context.fillStyle = '#333';
context.textBaseline = 'middle';
context.textAlign = 'center';
context.fillText('HW', centerX, centerY);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment