Advertisement
PastersGonnaPaste

hexapaths

Jan 23rd, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.50 KB | None | 0 0
  1. <html>
  2. <head>
  3. </head>
  4. <body style="background: #dddddd;">
  5. <div>
  6. Step size: <input id="steps" value="10"></input>
  7. Step count: <input id="stepcount" value="100"></input>
  8. <button onclick="draw(document.getElementById('steps').value,document.getElementById('stepcount').value);">Draw!</button>
  9. <br />
  10. Drawn: <input id="drawn" value="" size="100"></input>
  11. </div>
  12. <canvas id="canvas" width="800" height="800"></canvas>
  13. <script>
  14. var P = function(_x, _y, _w) {
  15.     return {
  16.         x: _x,
  17.         y: _y,
  18.         w: _w,
  19.         setPos: function(_x, _y) {
  20.             this.x = _x;
  21.             this.y = _y;
  22.         }
  23.     }
  24. };
  25.  
  26. var canvas = document.getElementById("canvas"),
  27.     ctx = canvas.getContext("2d");
  28.  
  29.  
  30. function circle(x,y,radius,color){
  31.     ctx.strokeStyle =color;
  32.     ctx.beginPath();
  33.     ctx.arc(x,y,radius,0,Math.PI*2);
  34.     ctx.closePath();
  35.     ctx.stroke();
  36. }  
  37.  
  38. function draw(steps, stepcount){
  39.     ctx.fillStyle="rgba(255, 255, 255, 1)";
  40.     ctx.fillRect(0, 0, canvas.width, canvas.height);
  41.     var p = new P(400, 400, Math.PI),
  42.         sixty = Math.PI/3;
  43.     document.getElementById('drawn').value='';
  44.     circle(p.x,p.y,1,'#0f0');
  45.    
  46.     for (var i = 0; i<stepcount; i++) {
  47.         ctx.strokeStyle = "rgba(0,0,0,.2)";
  48.         ctx.beginPath();
  49.         ctx.moveTo(p.x,p.y);
  50.         var res = Math.floor((Math.random()*2));
  51.         document.getElementById('drawn').value += res;
  52.         p.w+=sixty - res*sixty*2;
  53.         p.setPos(p.x + Math.sin(p.w)* steps, p.y + Math.cos(p.w)* steps);
  54.         ctx.lineTo(p.x, p.y);
  55.         ctx.closePath();
  56.         ctx.stroke();
  57.     }
  58.     circle(p.x,p.y,1,'#f00');
  59.  
  60. }
  61. </script>
  62. </body>
  63. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement