Skip to content

Instantly share code, notes, and snippets.

@garciadelcastillo
Last active August 29, 2015 14:13
Show Gist options
  • Save garciadelcastillo/ec1beaaf0216b4a57df7 to your computer and use it in GitHub Desktop.
Save garciadelcastillo/ec1beaaf0216b4a57df7 to your computer and use it in GitHub Desktop.
Animation

Animation

Scripted changes can be added to the sketch by overriding its update function. This process would be analogous to Processing's draw() function:

<!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');
// Construct a circle with a rotating line inside
var center = new pad.Node(pad.width.value / 2, pad.height.value / 2),
circle = pad.Circle.centerRadius(center, 100);
tip = pad.Point.along(circle, 0.0),
hand = pad.Line.between(center, tip);
// Override sketch's update function with a custom updater
pad.update = function () {
// Make sure to use object's methods to modify values
// or changes won't replicate to children elements
tip.setParameter(pad.frameCount / 500);
// tip.parameter = pad.frameCount / 500; // this won't work
};
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