main4.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. (function() {
  2. window.CanvasSlideshow = function( options ) {
  3. // SCOPE
  4. /// ---------------------------
  5. var that = this;
  6. // OPTIONS
  7. /// ---------------------------
  8. options = options || {};
  9. options.stageWidth = options.hasOwnProperty('stageWidth') ? options.stageWidth : 1920;
  10. options.stageHeight = options.hasOwnProperty('stageHeight') ? options.stageHeight : 1080;
  11. options.pixiSprites = options.hasOwnProperty('sprites') ? options.sprites : [];
  12. options.texts = options.hasOwnProperty('texts') ? options.texts : [];
  13. options.autoPlay = options.hasOwnProperty('autoPlay') ? options.autoPlay : true;
  14. options.autoPlaySpeed = options.hasOwnProperty('autoPlaySpeed') ? options.autoPlaySpeed : [10, 3];
  15. options.fullScreen = options.hasOwnProperty('fullScreen') ? options.fullScreen : true;
  16. options.displaceScale = options.hasOwnProperty('displaceScale') ? options.displaceScale : [200, 70];
  17. options.displacementImage = options.hasOwnProperty('displacementImage') ? options.displacementImage : '';
  18. options.navElement = options.hasOwnProperty('navElement') ? options.navElement : document.querySelectorAll( '.scene-nav' );
  19. options.displaceAutoFit = options.hasOwnProperty('displaceAutoFit') ? options.displaceAutoFit : false;
  20. options.wacky = options.hasOwnProperty('wacky') ? options.wacky : false;
  21. options.interactive = options.hasOwnProperty('interactive') ? options.interactive : false;
  22. options.displaceScaleTo = ( options.autoPlay === false ) ? [ 0, 0 ] : [ 20, 20 ];
  23. options.textColor = options.hasOwnProperty('textColor') ? options.textColor : '#fff';
  24. options.displacementCenter = options.hasOwnProperty('displacementCenter') ? options.displacementCenter : false;
  25. options.dispatchPointerOver = options.hasOwnProperty('dispatchPointerOver') ? options.dispatchPointerOver : false;
  26. // PIXI VARIABLES
  27. /// ---------------------------
  28. var renderer = new PIXI.autoDetectRenderer(options.stageWidth, options.stageHeight, { transparent: true });
  29. var stage = new PIXI.Container();
  30. var slidesContainer = new PIXI.Container();
  31. var displacementSprite = new PIXI.Sprite.fromImage( options.displacementImage );
  32. var displacementFilter = new PIXI.filters.DisplacementFilter( displacementSprite );
  33. // TEXTS
  34. /// ---------------------------
  35. var style = new PIXI.TextStyle({
  36. fill: options.textColor,
  37. wordWrap: true,
  38. wordWrapWidth: 400,
  39. letterSpacing: 20,
  40. fontSize: 14
  41. });
  42. // SLIDES ARRAY INDEX
  43. /// ---------------------------
  44. this.currentIndex = 0;
  45. /// ---------------------------
  46. // INITIALISE PIXI
  47. /// ---------------------------
  48. this.initPixi = function() {
  49. // Add canvas to the HTML
  50. document.body.appendChild( renderer.view );
  51. // Add child container to the main container
  52. stage.addChild( slidesContainer );
  53. // Enable Interactions
  54. stage.interactive = true;
  55. // Fit renderer to the screen
  56. if ( options.fullScreen === true ) {
  57. renderer.view.style.objectFit = 'cover';
  58. renderer.view.style.width = '100%';
  59. renderer.view.style.height = '100%';
  60. renderer.view.style.top = '50%';
  61. renderer.view.style.left = '50%';
  62. renderer.view.style.webkitTransform = 'translate( -50%, -50% ) scale(1.2)';
  63. renderer.view.style.transform = 'translate( -50%, -50% ) scale(1.2)';
  64. } else {
  65. renderer.view.style.maxWidth = '100%';
  66. renderer.view.style.top = '50%';
  67. renderer.view.style.left = '50%';
  68. renderer.view.style.webkitTransform = 'translate( -50%, -50% )';
  69. renderer.view.style.transform = 'translate( -50%, -50% )';
  70. }
  71. displacementSprite.texture.baseTexture.wrapMode = PIXI.WRAP_MODES.REPEAT;
  72. // Set the filter to stage and set some default values for the animation
  73. stage.filters = [displacementFilter];
  74. if ( options.autoPlay === false ) {
  75. displacementFilter.scale.x = 0;
  76. displacementFilter.scale.y = 0;
  77. }
  78. if ( options.wacky === true ) {
  79. displacementSprite.anchor.set(0.5);
  80. displacementSprite.x = renderer.width / 2;
  81. displacementSprite.y = renderer.height / 2;
  82. }
  83. displacementSprite.scale.x = 2;
  84. displacementSprite.scale.y = 2;
  85. // PIXI tries to fit the filter bounding box to the renderer so we optionally bypass
  86. displacementFilter.autoFit = options.displaceAutoFit;
  87. stage.addChild( displacementSprite );
  88. };
  89. /// ---------------------------
  90. // LOAD SLIDES TO CANVAS
  91. /// ---------------------------
  92. this.loadPixiSprites = function( sprites ) {
  93. var rSprites = options.sprites;
  94. var rTexts = options.texts;
  95. for ( var i = 0; i < rSprites.length; i++ ) {
  96. var texture = new PIXI.Texture.fromImage( sprites[i] );
  97. var image = new PIXI.Sprite( texture );
  98. if ( rTexts ) {
  99. var richText = new PIXI.Text( rTexts[i], style);
  100. image.addChild(richText);
  101. richText.anchor.set(0.5);
  102. richText.x = image.width / 2;
  103. richText.y = image.height / 2;
  104. }
  105. if ( options.centerSprites === true ) {
  106. image.anchor.set(0.5);
  107. image.x = renderer.width / 2;
  108. image.y = renderer.height / 2;
  109. }
  110. //image.transform.scale.x = 1.3;
  111. //image.transform.scale.y = 1.3;
  112. if ( i !== 0 ) {
  113. TweenMax.set( image, { alpha: 0 } );
  114. }
  115. slidesContainer.addChild( image );
  116. }
  117. };
  118. /// ---------------------------
  119. // DEFAULT RENDER/ANIMATION
  120. /// ---------------------------
  121. if ( options.autoPlay === true ) {
  122. var ticker = new PIXI.ticker.Ticker();
  123. ticker.autoStart = options.autoPlay;
  124. ticker.add(function( delta ) {
  125. displacementSprite.x += options.autoPlaySpeed[0] * delta;
  126. displacementSprite.y += options.autoPlaySpeed[1];
  127. displacementSprite.x += 2.14 * delta;
  128. displacementSprite.y += 22.24 * delta;
  129. displacementSprite.rotation.x += 20.3;
  130. renderer.render( stage );
  131. });
  132. } else {
  133. var render = new PIXI.ticker.Ticker();
  134. render.autoStart = true;
  135. render.add(function( delta ) {
  136. renderer.render( stage );
  137. });
  138. }
  139. /// ---------------------------
  140. // TRANSITION BETWEEN SLIDES
  141. /// ---------------------------
  142. var transitionAnimation = new PIXI.ticker.Ticker();
  143. transitionAnimation.autoStart = false;
  144. transitionAnimation.add(function( delta ) {
  145. displacementSprite.x += 2.14 * delta;
  146. displacementSprite.y += 22.24 * delta;
  147. displacementSprite.rotation.x += 20.3;
  148. });
  149. var isPlaying = false;
  150. var slideImages = slidesContainer.children;
  151. this.moveSlider = function( newIndex ) {
  152. isPlaying = true;
  153. transitionAnimation.start();
  154. var baseTimeline = new TimelineMax( { onComplete: function () {
  155. that.currentIndex = newIndex;
  156. isPlaying = false;
  157. transitionAnimation.stop();
  158. if ( options.wacky === true ) {
  159. displacementSprite.scale.set( 1 );
  160. }
  161. },onUpdate: function() {
  162. if ( options.wacky === true ) {
  163. displacementSprite.rotation += baseTimeline.progress() * 0.02;
  164. displacementSprite.scale.set( baseTimeline.progress() * 3 );
  165. }
  166. } });
  167. baseTimeline.clear();
  168. if ( baseTimeline.isActive() ) {
  169. return;
  170. }
  171. baseTimeline
  172. .to(displacementFilter.scale, 1, { y: "+="+1280+"", ease: Power3.easeOut })
  173. .to(slideImages[that.currentIndex], 0.5, { alpha: 0, ease: Power3.easeOut }, 0.4)
  174. .to(slideImages[newIndex], 0.5, { alpha: 1, ease: Power3.easeInOut }, 0.7)
  175. .to(displacementFilter.scale, 1, { y: 20, ease: Power3.easeOut }, 1 );
  176. };
  177. /// ---------------------------
  178. // CLICK HANDLERS
  179. /// ---------------------------
  180. var nav = options.navElement;
  181. for ( var i = 0; i < nav.length; i++ ) {
  182. var navItem = nav[i];
  183. navItem.onclick = function( event ) {
  184. // Make sure the previous transition has ended
  185. if ( isPlaying ) {
  186. return false;
  187. }
  188. if ( this.getAttribute('data-nav') === 'next' ) {
  189. if ( that.currentIndex >= 0 && that.currentIndex < slideImages.length - 1 ) {
  190. that.moveSlider( that.currentIndex + 1 );
  191. } else {
  192. that.moveSlider( 0 );
  193. }
  194. } else {
  195. if ( that.currentIndex > 0 && that.currentIndex < slideImages.length ) {
  196. that.moveSlider( that.currentIndex - 1 );
  197. } else {
  198. that.moveSlider( spriteImages.length - 1 );
  199. }
  200. }
  201. return false;
  202. }
  203. }
  204. /// ---------------------------
  205. // INIT FUNCTIONS
  206. /// ---------------------------
  207. this.init = function() {
  208. that.initPixi();
  209. that.loadPixiSprites( options.pixiSprites );
  210. /*
  211. if ( options.fullScreen === true ) {
  212. window.addEventListener("resize", function( event ){
  213. scaleToWindow( renderer.view );
  214. });
  215. scaleToWindow( renderer.view );
  216. }
  217. */
  218. };
  219. /// ---------------------------
  220. // INTERACTIONS
  221. /// ---------------------------
  222. function rotateSpite() {
  223. displacementSprite.rotation += 0.001;
  224. rafID = requestAnimationFrame( rotateSpite );
  225. }
  226. if ( options.interactive === true ) {
  227. var rafID, mouseX, mouseY;
  228. // Enable interactions on our slider
  229. slidesContainer.interactive = true;
  230. slidesContainer.buttonMode = true;
  231. // HOVER
  232. if ( options.interactionEvent === 'hover' || options.interactionEvent === 'both' ) {
  233. slidesContainer.pointerover = function( mouseData ){
  234. mouseX = mouseData.data.global.x;
  235. mouseY = mouseData.data.global.y;
  236. TweenMax.to( displacementFilter.scale, 1, { x: "+=" + Math.sin( mouseX ) * 100 + "", y: "+=" + Math.cos( mouseY ) * 100 + "" });
  237. rotateSpite();
  238. };
  239. slidesContainer.pointerout = function( mouseData ){
  240. TweenMax.to( displacementFilter.scale, 1, { x: 0, y: 0 });
  241. cancelAnimationFrame( rafID );
  242. };
  243. }
  244. // CLICK
  245. if ( options.interactionEvent === 'click' || options.interactionEvent === 'both' ) {
  246. slidesContainer.pointerup = function( mouseData ){
  247. if ( options.dispatchPointerOver === true ) {
  248. TweenMax.to( displacementFilter.scale, 1, { x: 0, y: 0, onComplete: function() {
  249. TweenMax.to( displacementFilter.scale, 1, { x: 20, y: 20 });
  250. } });
  251. } else {
  252. TweenMax.to( displacementFilter.scale, 1, { x: 0, y: 0 });
  253. cancelAnimationFrame( rafID );
  254. }
  255. };
  256. slidesContainer.pointerdown = function( mouseData ){
  257. mouseX = mouseData.data.global.x;
  258. mouseY = mouseData.data.global.y;
  259. TweenMax.to( displacementFilter.scale, 1, { x: "+=" + Math.sin( mouseX ) * 1200 + "", y: "+=" + Math.cos( mouseY ) * 200 + "" });
  260. };
  261. slidesContainer.pointerout = function( mouseData ){
  262. if ( options.dispatchPointerOver === true ) {
  263. TweenMax.to( displacementFilter.scale, 1, { x: 0, y: 0, onComplete: function() {
  264. TweenMax.to( displacementFilter.scale, 1, { x: 20, y: 20 });
  265. } });
  266. } else {
  267. TweenMax.to( displacementFilter.scale, 1, { x: 0, y: 0 });
  268. cancelAnimationFrame( rafID );
  269. }
  270. };
  271. }
  272. }
  273. /// ---------------------------
  274. // CENTER DISPLACEMENT
  275. /// ---------------------------
  276. if ( options.displacementCenter === true ) {
  277. displacementSprite.anchor.set(0.5);
  278. displacementSprite.x = renderer.view.width / 2;
  279. displacementSprite.y = renderer.view.height / 2;
  280. }
  281. /// ---------------------------
  282. // START
  283. /// ---------------------------
  284. this.init();
  285. /// ---------------------------
  286. // HELPER FUNCTIONS
  287. /// ---------------------------
  288. function scaleToWindow( canvas, backgroundColor ) {
  289. var scaleX, scaleY, scale, center;
  290. //1. Scale the canvas to the correct size
  291. //Figure out the scale amount on each axis
  292. scaleX = window.innerWidth / canvas.offsetWidth;
  293. scaleY = window.innerHeight / canvas.offsetHeight;
  294. //Scale the canvas based on whichever value is less: `scaleX` or `scaleY`
  295. scale = Math.min(scaleX, scaleY);
  296. canvas.style.transformOrigin = "0 0";
  297. canvas.style.transform = "scale(" + scale + ")";
  298. //2. Center the canvas.
  299. //Decide whether to center the canvas vertically or horizontally.
  300. //Wide canvases should be centered vertically, and
  301. //square or tall canvases should be centered horizontally
  302. if (canvas.offsetWidth > canvas.offsetHeight) {
  303. if (canvas.offsetWidth * scale < window.innerWidth) {
  304. center = "horizontally";
  305. } else {
  306. center = "vertically";
  307. }
  308. } else {
  309. if (canvas.offsetHeight * scale < window.innerHeight) {
  310. center = "vertically";
  311. } else {
  312. center = "horizontally";
  313. }
  314. }
  315. //Center horizontally (for square or tall canvases)
  316. var margin;
  317. if (center === "horizontally") {
  318. margin = (window.innerWidth - canvas.offsetWidth * scale) / 2;
  319. canvas.style.marginTop = 0 + "px";
  320. canvas.style.marginBottom = 0 + "px";
  321. canvas.style.marginLeft = margin + "px";
  322. canvas.style.marginRight = margin + "px";
  323. }
  324. //Center vertically (for wide canvases)
  325. if (center === "vertically") {
  326. margin = (window.innerHeight - canvas.offsetHeight * scale) / 2;
  327. canvas.style.marginTop = margin + "px";
  328. canvas.style.marginBottom = margin + "px";
  329. canvas.style.marginLeft = 0 + "px";
  330. canvas.style.marginRight = 0 + "px";
  331. }
  332. //3. Remove any padding from the canvas and body and set the canvas
  333. //display style to "block"
  334. canvas.style.paddingLeft = 0 + "px";
  335. canvas.style.paddingRight = 0 + "px";
  336. canvas.style.paddingTop = 0 + "px";
  337. canvas.style.paddingBottom = 0 + "px";
  338. canvas.style.display = "block";
  339. //4. Set the color of the HTML body background
  340. document.body.style.backgroundColor = backgroundColor;
  341. //Fix some quirkiness in scaling for Safari
  342. var ua = navigator.userAgent.toLowerCase();
  343. if (ua.indexOf("safari") != -1) {
  344. if (ua.indexOf("chrome") > -1) {
  345. // Chrome
  346. } else {
  347. // Safari
  348. //canvas.style.maxHeight = "100%";
  349. //canvas.style.minHeight = "100%";
  350. }
  351. }
  352. //5. Return the `scale` value. This is important, because you'll nee this value
  353. //for correct hit testing between the pointer and sprites
  354. return scale;
  355. } // http://bit.ly/2y1Yk2k
  356. };
  357. })();