main5.js 16 KB

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