Skip to content

Instantly share code, notes, and snippets.

@garciadelcastillo
Last active August 29, 2015 14:11
Show Gist options
  • Save garciadelcastillo/72814128ae7786409e89 to your computer and use it in GitHub Desktop.
Save garciadelcastillo/72814128ae7786409e89 to your computer and use it in GitHub Desktop.
Sketchpad.js - Centering on Canvas
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div id="sketchpadDiv">
<canvas id="sketchpadCanvas"></canvas>
</div>
</body>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="http://www.garciadelcastillo.es/sketchpad/sketchpad.js"></script>
<script type="text/javascript" src="sketch.js"></script>
</html>
// Create new instance of Sketchpad in target Canvas
var pad = new Sketchpad('sketchpadCanvas');
// .width and .height properties are accessible as Measures to inherit from
var label = pad.Label.compose(pad.width, pad.height, function() {
return 'Canvas dimensions: ' + pad.width.value + 'x' + pad.height.value + ' px';
});
// Compose half Measures and attach a Circle on Canvas center
var halfWidth = pad.Measure.compose(pad.width, function () {
return pad.width.value / 2;
}),
halfHeight = pad.Measure.compose(pad.height, function() {
return pad.height.value / 2;
});
var C = pad.Point.fromMeasures(halfWidth, halfHeight),
circle = pad.Circle.centerRadius(C, 101);
// Display dimension on the center
var tag = pad.Tag.on(C, label);
body {
margin: 0;
padding: 0;
}
#sketchpadDiv {
position: absolute;
width: 100%;
height: 100%;
border: 1px solid #000;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
#sketchpadCanvas {
position: absolute;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment