Skip to content

Instantly share code, notes, and snippets.

@garciadelcastillo
Last active August 29, 2015 14:11
Show Gist options
  • Save garciadelcastillo/728ddfa3cadbcd7cde08 to your computer and use it in GitHub Desktop.
Save garciadelcastillo/728ddfa3cadbcd7cde08 to your computer and use it in GitHub Desktop.
Sketchpad.js - Labels and Tags
<!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');
// Render a fixed text Tag on XY position
var title = new pad.Tag('This is a fixed Tag', 320, 40);
// Create a text Label linked to Node P
// Note how Label.compose takes as arguments all the elements
// the Label is child to, plus a callback function
// returning the Label text string
var P = new pad.Node(320, 200);
var posLabel = pad.Label.compose(P, function() {
return '[' + Math.round(P.x) + ', ' + Math.round(P.y) + ']';
});
// Now render a text Tag on Node P with text Label
var positionTag = pad.Tag.on(P, posLabel);
// Similarly, display Node distance to [0, 0]
var distLabel = pad.Label.compose(P, function() {
var d = Math.sqrt(P.x * P.x + P.y * P.y);
return 'This is a dynamic Tag. D = ' + Math.round(d) + ' px';
});
var anchor = new pad.Point(320, 375),
distTag = pad.Tag.on(anchor, distLabel);
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