Guest User

Untitled

a guest
Oct 19th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. $(function(){
  2. var scene = document.getElementById('metropallax');
  3. var parallaxInstance = new Parallax(scene, {
  4. relativeInput: true,
  5. calibrateX: false,
  6. calibrateY: true,
  7. invertX: false,
  8. invertY: true,
  9. limitX: false,
  10. //limitY: 10,
  11. limitY: 0,
  12. scalarX: 2,
  13. scalarY: 8,
  14. frictionX: 0.2,
  15. frictionY: 0.8,
  16. originX: 0.0,
  17. originY: 1.0
  18. });
  19. });
  20.  
  21. $(function(){
  22.  
  23. // Create an array to store our particles
  24. var particles = [];
  25.  
  26. // The amount of particles to render
  27. var particleCount = 8;
  28. // Orig 30
  29.  
  30. // The maximum velocity in each direction
  31. var maxVelocity = 4;
  32.  
  33. // The target frames per second (how often do we want to update / redraw the scene)
  34. var targetFPS = 24;
  35. // Orig 33
  36.  
  37. // Set the dimensions of the canvas as variables so they can be used.
  38. var canvasWidth = 400;
  39. var canvasHeight = 400;
  40.  
  41. // Create an image object (only need one instance)
  42. var imageObj = new Image();
  43.  
  44. // Once the image has been downloaded then set the image on all of the particles
  45. imageObj.onload = function() {
  46. particles.forEach(function(particle) {
  47. particle.setImage(imageObj);
  48. });
  49. };
  50.  
  51. // Once the callback is arranged then set the source of the image
  52. imageObj.src = "http://www.blog.jonnycornwell.com/wp-content/uploads/2012/07/Smoke10.png";
  53.  
  54. // A function to create a particle object.
  55. function Particle(context) {
  56.  
  57. // Set the initial x and y positions
  58. this.x = 0;
  59. this.y = 0;
  60.  
  61. // Set the initial velocity
  62. this.xVelocity = 0;
  63. this.yVelocity = 0;
  64.  
  65. // Set the radius
  66. this.radius = 5;
  67.  
  68. // Store the context which will be used to draw the particle
  69. this.context = context;
  70.  
  71. // The function to draw the particle on the canvas.
  72. this.draw = function() {
  73.  
  74. // If an image is set draw it
  75. if(this.image){
  76. this.context.drawImage(this.image, this.x-128, this.y-128);
  77. // If the image is being rendered do not draw the circle so break out of the draw function
  78. return;
  79. }
  80. // Draw the circle as before, with the addition of using the position and the radius from this object.
  81. this.context.beginPath();
  82. this.context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI, false);
  83. this.context.fillStyle = "rgba(0, 255, 255, 1)";
  84. this.context.fill();
  85. this.context.closePath();
  86. };
  87.  
  88. // Update the particle.
  89. this.update = function() {
  90. // Update the position of the particle with the addition of the velocity.
  91. this.x += this.xVelocity;
  92. this.y += this.yVelocity;
  93.  
  94. // Check if has crossed the right edge
  95. if (this.x >= canvasWidth) {
  96. this.xVelocity = -this.xVelocity;
  97. this.x = canvasWidth;
  98. }
  99. // Check if has crossed the left edge
  100. else if (this.x <= 0) {
  101. this.xVelocity = -this.xVelocity;
  102. this.x = 0;
  103. }
  104.  
  105. // Check if has crossed the bottom edge
  106. if (this.y >= canvasHeight) {
  107. this.yVelocity = -this.yVelocity;
  108. this.y = canvasHeight;
  109. }
  110.  
  111. // Check if has crossed the top edge
  112. else if (this.y <= 0) {
  113. this.yVelocity = -this.yVelocity;
  114. this.y = 0;
  115. }
  116. };
  117.  
  118. // A function to set the position of the particle.
  119. this.setPosition = function(x, y) {
  120. this.x = x;
  121. this.y = y;
  122. };
  123.  
  124. // Function to set the velocity.
  125. this.setVelocity = function(x, y) {
  126. this.xVelocity = x;
  127. this.yVelocity = y;
  128. };
  129.  
  130. this.setImage = function(image){
  131. this.image = image;
  132. }
  133. }
  134.  
  135. // A function to generate a random number between 2 values
  136. function generateRandom(min, max){
  137. return Math.random() * (max - min) + min;
  138. }
  139.  
  140. // The canvas context if it is defined.
  141. var context;
  142.  
  143. // Initialise the scene and set the context if possible
  144. function init() {
  145. var canvas = document.getElementById('smoke');
  146. if (canvas.getContext) {
  147.  
  148. // Set the context variable so it can be re-used
  149. context = canvas.getContext('2d');
  150.  
  151. // Create the particles and set their initial positions and velocities
  152. for(var i=0; i < particleCount; ++i){
  153. var particle = new Particle(context);
  154.  
  155. // Set the position to be inside the canvas bounds
  156. particle.setPosition(generateRandom(0, canvasWidth), generateRandom(0, canvasHeight));
  157.  
  158. // Set the initial velocity to be either random and either negative or positive
  159. particle.setVelocity(generateRandom(-maxVelocity, maxVelocity), generateRandom(-maxVelocity, maxVelocity));
  160. particles.push(particle);
  161. }
  162. }
  163. else {
  164. alert("Please use a modern browser");
  165. }
  166. }
  167.  
  168. // The function to draw the scene
  169. function draw() {
  170. // Clear the drawing surface and fill it with a black background
  171. context.fillStyle = "rgba(0, 0, 0, 0.5)";
  172. context.fillRect(0, 0, 400, 400);
  173.  
  174. // Go through all of the particles and draw them.
  175. particles.forEach(function(particle) {
  176. particle.draw();
  177. });
  178. }
  179.  
  180. // Update the scene
  181. function update() {
  182. particles.forEach(function(particle) {
  183. particle.update();
  184. });
  185. }
  186.  
  187. // Initialize the scene
  188. init();
  189.  
  190. // If the context is set then we can draw the scene (if not then the browser does not support canvas)
  191. if (context) {
  192. setInterval(function() {
  193. // Update the scene befoe drawing
  194. update();
  195.  
  196. // Draw the scene
  197. draw();
  198. }, 1000 / targetFPS);
  199. }
  200. });
Add Comment
Please, Sign In to add comment