binaryfloater.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. function BinaryFloater() {
  2. var x = 10, y = 10, radius = 5;
  3. var velocity = {x: 1, y: 1};
  4. var binaryText = "1";
  5. this.init = function(canvas) {
  6. x = Math.floor(canvas.width * Math.random());
  7. y = Math.floor(canvas.height * Math.random());
  8. velocity.x = Math.floor(10 * Math.random() - 5);
  9. velocity.y = Math.floor(10 * Math.random() - 5);
  10. binaryText = (Math.floor(Math.random() * 2) == 0) ? "1" : "0";
  11. };
  12. this.update = function(canvas) {
  13. x += velocity.x;
  14. y += velocity.y;
  15. if(x + radius > canvas.width) {
  16. x = canvas.width - radius;
  17. velocity.x *= -1.1;
  18. }
  19. if(x < radius) {
  20. x = radius;
  21. velocity.x *= -1.1;
  22. }
  23. if(y + radius > canvas.height) {
  24. y = canvas.height - radius;
  25. velocity.y *= -1.1;
  26. }
  27. if(y < radius) {
  28. y = radius;
  29. velocity.y *= -1.1;
  30. }
  31. velocity.x = Math.max(-5, Math.min(5, velocity.x));
  32. velocity.y = Math.max(-5, Math.min(5, velocity.y));
  33. },
  34. this.draw = function(context) {
  35. context.fillStyle = "#00FF00";
  36. context.font = "16px Roboto";
  37. context.fillText(binaryText, x - radius, y + radius);
  38. }
  39. }