Skip to content

Instantly share code, notes, and snippets.

@tildebyte
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tildebyte/7a7ea45266d2fb89708f to your computer and use it in GitHub Desktop.
Save tildebyte/7a7ea45266d2fb89708f to your computer and use it in GitHub Desktop.
PyPy.js/p5.js experiment
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible">
<title>PyPy.js experiment</title>
<script src="http://code.jquery.com/jquery-2.1.4.min.js" type="text/javascript"></script>
<!-- shim for ES6 `Promise` builtin -->
<script src="./lib/Promise.min.js" type="text/javascript"></script>
<script src="./lib/pypy.js" type="text/javascript"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.4/p5.min.js"></script>
</head>
<body>
<script type="text/javascript">
var vm = new PyPyJS();
var p5js = new p5(function (p) {
}, "sketch01");
vm.ready.then(function() {
vm.eval(
// "import js\n"
// + "p = js.globals['p5js']\n"
// + "p.textSize(32)\n"
// + "p.text('word', 10, 30)\n"
// + "p.fill(0, 102, 153)"
"import js\n"
+ "p = js.globals['p5js']\n"
+ "xspacing = 16.0 # How far apart should each horizontal location be spaced\n"
+ "theta = 0.0 # Start angle at 0\n"
+ "amplitude = 75.0 # Height of wave\n"
+ "period = 500.0 # How many pixels before the wave repeats\n"
+ "# Value for incrementing X, a function of period and xspacing\n"
+ "dx = (p.TWO_PI / period) * xspacing\n"
+ "yvalues = []\n"
+ "for d in range(641):\n"
+ " yvalues[d] = 0.0\n"
+ "def calcWave():\n"
+ " global theta\n"
+ " # Increment theta (try different values for 'angular velocity' here\n"
+ " theta += 0.02\n"
+ " # For every x value, calculate a y value with sine function\n"
+ " x = theta\n"
+ " for i in range(len(yvalues)):\n"
+ " yvalues[i] = p.sin(x) * amplitude\n"
+ " x += dx\n"
+ "def renderWave():\n"
+ " p.noStroke()\n"
+ " p.fill(255)\n"
+ " # A simple way to draw the wave with an ellipse at each location\n"
+ " for j in range(len(yvalues)):\n"
+ " p.ellipse(j * xspacing, 360.0 / 2.0 + yvalues[j], 16, 16)\n"
+ "while True:\n"
+ " p.background(0)\n"
+ " calcWave()\n"
+ " renderWave()\n"
)});
</script>
<!-- <script type="text/javascript">
var vm = new PyPyJS();
function setup() {
PyPyApp();
}
function PyPyApp() {
vm.eval(
"import js\n"
+ "p = js.globals['p5']\n"
+ "p.textSize(32)\n"
+ "p.text('word', 10, 30)\n"
+ "p.fill(0, 102, 153)")}
vm.ready.then(setup());
</script> -->
<div id="sketch01">
</div>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment