main2.js 16 KB

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