init.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. var _shouldRun = true;
  2. var _speed = 1;
  3. function initCanvas() {
  4. var w = $(window);
  5. var canvas = $('.scribble-canvas');
  6. var ctx = canvas[0].getContext("2d");
  7. window.requestAnimFrame = (function(){
  8. return window.requestAnimationFrame ||
  9. window.webkitRequestAnimationFrame ||
  10. window.mozRequestAnimationFrame ||
  11. window.oRequestAnimationFrame ||
  12. window.msRequestAnimationFrame ||
  13. function(callback){
  14. window.setTimeout(callback, 1000 / 30);
  15. };
  16. })();
  17. $(document).keyup(function(e) {
  18. });
  19. var _inputdata = {};
  20. function checkinputs() {
  21. var curinput = {
  22. down: _inputdata.down,
  23. pressed: _inputdata.pressed,
  24. };
  25. _inputdata.pressed = {};
  26. return curinput;
  27. }
  28. var d = new Date();
  29. var lastframe = d.getTime() / 1000.0;
  30. function frame() {
  31. var d = new Date();
  32. var now = d.getTime() / 1000.0; // getTime is in milliseconds
  33. // time elapsed (since last frame)
  34. var te = now - lastframe;
  35. lastframe = now;
  36. // these can change when the window is resized
  37. var cw = ctx.canvas.width;
  38. var ch = ctx.canvas.height;
  39. if(_shouldRun) {
  40. ctx.clearRect(0, 0, cw, ch); // clear the entire canvas
  41. var input = checkinputs();
  42. drawLoop(ctx, te * _speed, input);
  43. }
  44. window.requestAnimFrame(frame);
  45. }
  46. // custom initialization
  47. init();
  48. // start drawing
  49. frame();
  50. }
  51. function toggleRun() {
  52. _shouldRun = !_shouldRun;
  53. return _shouldRun;
  54. }