Googles Animating Logo


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $(function() {
  2. var canvas = $("#c");
  3. var canvasHeight;
  4. var canvasWidth;
  5. var ctx;
  6. var dt = 0.1;
  7.  
  8. var pointCollection;
  9.  
  10. function init() {
  11. updateCanvasDimensions();
  12.  
  13. var g = [new Point(202, 78, 0.0, 9, "#ed9d33"), new Point(348, 83, 0.0, 9, "#d44d61"), new Point(256, 69, 0.0, 9, "#4f7af2"), new Point(214, 59, 0.0, 9, "#ef9a1e"), new Point(265, 36, 0.0, 9, "#4976f3"), new Point(300, 78, 0.0, 9, "#269230"), new Point(294, 59, 0.0, 9, "#1f9e2c"), new Point(45, 88, 0.0, 9, "#1c48dd"), new Point(268, 52, 0.0, 9, "#2a56ea"), new Point(73, 83, 0.0, 9, "#3355d8"), new Point(294, 6, 0.0, 9, "#36b641"), new Point(235, 62, 0.0, 9, "#2e5def"), new Point(353, 42, 0.0, 8, "#d53747"), new Point(336, 52, 0.0, 8, "#eb676f"), new Point(208, 41, 0.0, 8, "#f9b125"), new Point(321, 70, 0.0, 8, "#de3646"), new Point(8, 60, 0.0, 8, "#2a59f0"), new Point(180, 81, 0.0, 8, "#eb9c31"), new Point(146, 65, 0.0, 8, "#c41731"), new Point(145, 49, 0.0, 8, "#d82038"), new Point(246, 34, 0.0, 8, "#5f8af8"), new Point(169, 69, 0.0, 8, "#efa11e"), new Point(273, 99, 0.0, 8, "#2e55e2"), new Point(248, 120, 0.0, 8, "#4167e4"), new Point(294, 41, 0.0, 8, "#0b991a"), new Point(267, 114, 0.0, 8, "#4869e3"), new Point(78, 67, 0.0, 8, "#3059e3"), new Point(294, 23, 0.0, 8, "#10a11d"), new Point(117, 83, 0.0, 8, "#cf4055"), new Point(137, 80, 0.0, 8, "#cd4359"), new Point(14, 71, 0.0, 8, "#2855ea"), new Point(331, 80, 0.0, 8, "#ca273c"), new Point(25, 82, 0.0, 8, "#2650e1"), new Point(233, 46, 0.0, 8, "#4a7bf9"), new Point(73, 13, 0.0, 8, "#3d65e7"), new Point(327, 35, 0.0, 6, "#f47875"), new Point(319, 46, 0.0, 6, "#f36764"), new Point(256, 81, 0.0, 6, "#1d4eeb"), new Point(244, 88, 0.0, 6, "#698bf1"), new Point(194, 32, 0.0, 6, "#fac652"), new Point(97, 56, 0.0, 6, "#ee5257"), new Point(105, 75, 0.0, 6, "#cf2a3f"), new Point(42, 4, 0.0, 6, "#5681f5"), new Point(10, 27, 0.0, 6, "#4577f6"), new Point(166, 55, 0.0, 6, "#f7b326"), new Point(266, 88, 0.0, 6, "#2b58e8"), new Point(178, 34, 0.0, 6, "#facb5e"), new Point(100, 65, 0.0, 6, "#e02e3d"), new Point(343, 32, 0.0, 6, "#f16d6f"), new Point(59, 5, 0.0, 6, "#507bf2"), new Point(27, 9, 0.0, 6, "#5683f7"), new Point(233, 116, 0.0, 6, "#3158e2"), new Point(123, 32, 0.0, 6, "#f0696c"), new Point(6, 38, 0.0, 6, "#3769f6"), new Point(63, 62, 0.0, 6, "#6084ef"), new Point(6, 49, 0.0, 6, "#2a5cf4"), new Point(108, 36, 0.0, 6, "#f4716e"), new Point(169, 43, 0.0, 6, "#f8c247"), new Point(137, 37, 0.0, 6, "#e74653"), new Point(318, 58, 0.0, 6, "#ec4147"), new Point(226, 100, 0.0, 5, "#4876f1"), new Point(101, 46, 0.0, 5, "#ef5c5c"), new Point(226, 108, 0.0, 5, "#2552ea"), new Point(17, 17, 0.0, 5, "#4779f7"), new Point(232, 93, 0.0, 5, "#4b78f1")];
  14.  
  15. gLength = g.length;
  16. for (var i = 0; i < gLength; i++) {
  17. g[i].curPos.x = (canvasWidth/2 - 180) + g[i].curPos.x;
  18. g[i].curPos.y = (canvasHeight/2 - 65) + g[i].curPos.y;
  19.  
  20. g[i].originalPos.x = (canvasWidth/2 - 180) + g[i].originalPos.x;
  21. g[i].originalPos.y = (canvasHeight/2 - 65) + g[i].originalPos.y;
  22. };
  23.  
  24. pointCollection = new PointCollection();
  25. pointCollection.points = g;
  26.  
  27. initEventListeners();
  28. timeout();
  29. };
  30.  
  31. function initEventListeners() {
  32. $(window).bind('resize', updateCanvasDimensions).bind('mousemove', onMove);
  33.  
  34. canvas.get(0).ontouchmove = function(e) {
  35. e.preventDefault();
  36. onTouchMove(e);
  37. };
  38.  
  39. canvas.get(0).ontouchstart = function(e) {
  40. e.preventDefault();
  41. };
  42. };
  43.  
  44. function updateCanvasDimensions() {
  45. canvas.attr({height: $(window).height(), width: $(window).width()});
  46. canvasWidth = canvas.width();
  47. canvasHeight = canvas.height();
  48.  
  49. draw();
  50. };
  51.  
  52. function onMove(e) {
  53. if (pointCollection)
  54. pointCollection.mousePos.set(e.pageX, e.pageY);
  55. };
  56.  
  57. function onTouchMove(e) {
  58. if (pointCollection)
  59. pointCollection.mousePos.set(e.targetTouches[0].pageX, e.targetTouches[0].pageY);
  60. };
  61.  
  62. function timeout() {
  63. draw();
  64. update();
  65.  
  66. setTimeout(function() { timeout() }, 30);
  67. };
  68.  
  69. function draw() {
  70. var tmpCanvas = canvas.get(0);
  71.  
  72. if (tmpCanvas.getContext == null) {
  73. return;
  74. };
  75.  
  76. ctx = tmpCanvas.getContext('2d');
  77. ctx.clearRect(0, 0, canvasWidth, canvasHeight);
  78.  
  79. if (pointCollection)
  80. pointCollection.draw();
  81. };
  82.  
  83. function update() {
  84. if (pointCollection)
  85. pointCollection.update();
  86. };
  87.  
  88. function Vector(x, y, z) {
  89. this.x = x;
  90. this.y = y;
  91. this.z = z;
  92.  
  93. this.addX = function(x) {
  94. this.x += x;
  95. };
  96.  
  97. this.addY = function(y) {
  98. this.y += y;
  99. };
  100.  
  101. this.addZ = function(z) {
  102. this.z += z;
  103. };
  104.  
  105. this.set = function(x, y, z) {
  106. this.x = x;
  107. this.y = y;
  108. this.z = z;
  109. };
  110. };
  111.  
  112. function PointCollection() {
  113. this.mousePos = new Vector(0, 0);
  114. this.points = new Array();
  115.  
  116. this.newPoint = function(x, y, z) {
  117. var point = new Point(x, y, z);
  118. this.points.push(point);
  119. return point;
  120. };
  121.  
  122. this.update = function() {
  123. var pointsLength = this.points.length;
  124.  
  125. for (var i = 0; i < pointsLength; i++) {
  126. var point = this.points[i];
  127.  
  128. if (point == null)
  129. continue;
  130.  
  131. var dx = this.mousePos.x - point.curPos.x;
  132. var dy = this.mousePos.y - point.curPos.y;
  133. var dd = (dx * dx) + (dy * dy);
  134. var d = Math.sqrt(dd);
  135.  
  136. if (d < 150) {
  137. point.targetPos.x = (this.mousePos.x < point.curPos.x) ? point.curPos.x - dx : point.curPos.x - dx;
  138. point.targetPos.y = (this.mousePos.y < point.curPos.y) ? point.curPos.y - dy : point.curPos.y - dy;
  139. } else {
  140. point.targetPos.x = point.originalPos.x;
  141. point.targetPos.y = point.originalPos.y;
  142. };
  143.  
  144. point.update();
  145. };
  146. };
  147.  
  148. this.draw = function() {
  149. var pointsLength = this.points.length;
  150. for (var i = 0; i < pointsLength; i++) {
  151. var point = this.points[i];
  152.  
  153. if (point == null)
  154. continue;
  155.  
  156. point.draw();
  157. };
  158. };
  159. };
  160.  
  161. function Point(x, y, z, size, colour) {
  162. this.colour = colour;
  163. this.curPos = new Vector(x, y, z);
  164. this.friction = 0.8;
  165. this.originalPos = new Vector(x, y, z);
  166. this.radius = size;
  167. this.size = size;
  168. this.springStrength = 0.1;
  169. this.targetPos = new Vector(x, y, z);
  170. this.velocity = new Vector(0.0, 0.0, 0.0);
  171.  
  172. this.update = function() {
  173. var dx = this.targetPos.x - this.curPos.x;
  174. var ax = dx * this.springStrength;
  175. this.velocity.x += ax;
  176. this.velocity.x *= this.friction;
  177. this.curPos.x += this.velocity.x;
  178.  
  179. var dy = this.targetPos.y - this.curPos.y;
  180. var ay = dy * this.springStrength;
  181. this.velocity.y += ay;
  182. this.velocity.y *= this.friction;
  183. this.curPos.y += this.velocity.y;
  184.  
  185. var dox = this.originalPos.x - this.curPos.x;
  186. var doy = this.originalPos.y - this.curPos.y;
  187. var dd = (dox * dox) + (doy * doy);
  188. var d = Math.sqrt(dd);
  189.  
  190. this.targetPos.z = d/100 + 1;
  191. var dz = this.targetPos.z - this.curPos.z;
  192. var az = dz * this.springStrength;
  193. this.velocity.z += az;
  194. this.velocity.z *= this.friction;
  195. this.curPos.z += this.velocity.z;
  196.  
  197. this.radius = this.size*this.curPos.z;
  198. if (this.radius < 1) this.radius = 1;
  199. };
  200.  
  201. this.draw = function() {
  202. ctx.fillStyle = this.colour;
  203. ctx.beginPath();
  204. ctx.arc(this.curPos.x, this.curPos.y, this.radius, 0, Math.PI*2, true);
  205. ctx.fill();
  206. };
  207. };
  208.  
  209. init();
  210. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.