BrU32

JS Triangle Acid Trip

Jan 2nd, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1.  
  2. window.onload = function() {
  3. var canvas = document.getElementById("canvas"),
  4. context = canvas.getContext("2d"),
  5. width = canvas.width = window.innerWidth,
  6. height = canvas.height = window.innerHeight;
  7.  
  8. var p0 = {
  9. x: 0,
  10. y: -321
  11. },
  12. p1 = {
  13. x: 278,
  14. y: 160
  15. },
  16. p2 = {
  17. x: -278,
  18. y: 160
  19. };
  20. var a = 0,
  21. b = 0,
  22. r = 0,
  23. tx, ty;
  24.  
  25. draw();
  26. function draw() {
  27. context.clearRect(0, 0, width, height);
  28. context.save();
  29. context.translate(width / 2, height / 2);
  30. context.rotate(r += 0.01);
  31. tx = .5 + Math.sin(a += .045) * .25;
  32. ty = .5 + Math.sin(b += .045) * .25;
  33. sierpinski(p0, p1, p2, 7);
  34. context.restore();
  35. requestAnimationFrame(draw);
  36. }
  37.  
  38. function sierpinski(p0, p1, p2, limit) {
  39. if(limit > 0) {
  40. var pA = {
  41. x: p0.x + (p1.x - p0.x) * tx,
  42. y: p0.y + (p1.y - p0.y) * ty
  43. },
  44. pB = {
  45. x: p1.x + (p2.x - p1.x) * tx,
  46. y: p1.y + (p2.y - p1.y) * ty
  47. },
  48. pC = {
  49. x: p2.x + (p0.x - p2.x) * tx,
  50. y: p2.y + (p0.y - p2.y) * ty
  51. };
  52. sierpinski(p0, pA, pC, limit - 1);
  53. sierpinski(pA, p1, pB, limit - 1);
  54. sierpinski(pC, pB, p2, limit - 1);
  55. }
  56. else {
  57. drawTriangle(p0, p1, p2);
  58. }
  59. }
  60.  
  61. function drawTriangle(p0, p1, p2) {
  62. context.beginPath();
  63. context.moveTo(p0.x, p0.y);
  64. context.lineTo(p1.x, p1.y);
  65. context.lineTo(p2.x++, p2.y);
  66. context.stroke();
  67. }
  68.  
  69. };
Advertisement
Add Comment
Please, Sign In to add comment