Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. 'use strict';
  2.  
  3. /**
  4. * Parallax component
  5. *
  6. * @type {jQuery}
  7. * @param {int} data-speed - aspect of the motion
  8. * @param {string} data-start - from where start to render motion. Values: [page|top|center]
  9. * @example
  10. * <canvas data-parallax data-speed="4" data-start="page">
  11. * <picture>
  12. * <source media="(max-width: 768px)" srcset="images/homepage/hp-carousel-slide-1-sm.jpg?$staticlink$">
  13. * <img alt="receipt banner" src="images/homepage/hp-carousel-slide-1-lg.jpg?$staticlink$" />
  14. * </picture>
  15. * </canvas>
  16. */
  17.  
  18. var $ = require('jquery'),
  19. scrollListener = require('_core_ext/scroll-listener');
  20.  
  21. var $cache = {},
  22. instances = [];
  23.  
  24. /**
  25. * Creates new Parallax object
  26. * @constructor
  27. *
  28. * @param {object} canvas - DOM node. `canvas` is expected
  29. * @param {string} src - image src URI string
  30. * @param {int} top - position from the top of the instance
  31. * @param {object} options - options object
  32. */
  33. var Parallax = function (canvas, src, top, options) {
  34. this.canvas = canvas[0];
  35. this.context = this.canvas.getContext('2d');
  36. this.options = options;
  37. this.src = src;
  38. this.top = top;
  39. this.preloadImg();
  40. };
  41.  
  42. /**
  43. * @function
  44. * @description preload image from picture source
  45. */
  46. Parallax.prototype.preloadImg = function () {
  47. var self = this;
  48. this.image = new Image();
  49. this.image.onload = function () {
  50. self.initSize();
  51. self.loaded = true;
  52. self.prerender();
  53. };
  54. this.image.src = this.src;
  55. };
  56.  
  57. /**
  58. * @function
  59. * @description set sizes of loaded image to canvas element
  60. */
  61. Parallax.prototype.initSize = function () {
  62. this.canvas.width = this.image.width;
  63. this.canvas.height = this.image.height;
  64. };
  65.  
  66. /**
  67. * @function
  68. * @description prerended image on canvas
  69. */
  70. Parallax.prototype.prerender = function () {
  71. if (!this.loaded) {
  72. return false;
  73. }
  74.  
  75. this.context.drawImage(this.image, 0, 0);
  76. };
  77.  
  78. /**
  79. * @function
  80. * @description render image on canvas according to scroll position
  81. * @param {int} scrollTop - scrolled position
  82. */
  83. Parallax.prototype.render = function (scrollTop) {
  84. var start;
  85.  
  86. if (!this.loaded || scrollTop > this.top + this.image.height) {
  87. return false;
  88. }
  89.  
  90. switch (this.options.start) {
  91. case 'page':
  92. start = 0;
  93. break;
  94. case 'middle':
  95. start = this.canvas.getBoundingClientRect().top;
  96. break;
  97. default:
  98. start = this.top;
  99. }
  100.  
  101. if (scrollTop >= start) {
  102. var localScroll = scrollTop - start;
  103. this.context.clearRect(0, 0, this.image.width, this.image.height);
  104. this.context.drawImage(this.image, 0, localScroll / this.options.speed);
  105. }
  106. };
  107.  
  108. $.fn.parallax = function () {
  109. $(this).each(function () {
  110. var $this = $(this);
  111. var src = $this.find('img')[0].currentSrc || $this.find('img')[0].src;
  112. var top = $this.offset().top;
  113. var options = {
  114. speed: $this.attr('data-speed') || 2,
  115. start: $this.attr('data-start') || 'top'
  116. };
  117.  
  118. instances.push(new Parallax($this, src, top, options));
  119. });
  120.  
  121. return this;
  122. };
  123.  
  124. /*
  125. * Initialize Cache
  126. */
  127. function initializeCache() {
  128. $cache.document = $(document);
  129. $cache.canvas = $('canvas[data-parallax]');
  130. }
  131.  
  132. /*
  133. * Initialize DOM
  134. */
  135. function initializeDOM() {
  136. $cache.canvas.parallax();
  137. }
  138.  
  139. /*
  140. * Initialize Events
  141. */
  142. function initializeEvents() {
  143. window.onload = function () {
  144. instances.forEach(function (inst) {
  145. inst.prerender();
  146. });
  147. };
  148.  
  149. instances.forEach(function (inst) {
  150. scrollListener.registerHandler(function () {
  151. inst.render(scrollY);
  152. });
  153. });
  154.  
  155. $cache.document.on('window.modechanged', function() {
  156. initializeDOM();
  157. });
  158. }
  159.  
  160. module.exports = {
  161. init: function () {
  162. initializeCache();
  163. initializeDOM();
  164. initializeEvents();
  165. }
  166. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement