main3.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. fontSize: 36
  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. if ( options.centerSprites === true ) {
  107. image.anchor.set(0.5);
  108. image.x = renderer.width / 2;
  109. image.y = renderer.height / 2;
  110. }
  111. // image.transform.scale.x = 1.3;
  112. // image.transform.scale.y = 1.3;
  113. if ( i !== 0 ) {
  114. TweenMax.set( image, { alpha: 0 } );
  115. }
  116. slidesContainer.addChild( image );
  117. }
  118. };
  119. /// ---------------------------
  120. // DEFAULT RENDER/ANIMATION
  121. /// ---------------------------
  122. if ( options.autoPlay === true ) {
  123. var ticker = new PIXI.ticker.Ticker();
  124. ticker.autoStart = options.autoPlay;
  125. ticker.add(function( delta ) {
  126. displacementSprite.x += options.autoPlaySpeed[0] * delta;
  127. displacementSprite.y += options.autoPlaySpeed[1];
  128. renderer.render( stage );
  129. });
  130. } else {
  131. var render = new PIXI.ticker.Ticker();
  132. render.autoStart = true;
  133. render.add(function( delta ) {
  134. renderer.render( stage );
  135. });
  136. }
  137. /// ---------------------------
  138. // TRANSITION BETWEEN SLIDES
  139. /// ---------------------------
  140. var isPlaying = false;
  141. var slideImages = slidesContainer.children;
  142. this.moveSlider = function( newIndex ) {
  143. isPlaying = true;
  144. var baseTimeline = new TimelineMax( { onComplete: function () {
  145. that.currentIndex = newIndex;
  146. isPlaying = false;
  147. if ( options.wacky === true ) {
  148. displacementSprite.scale.set( 1 );
  149. }
  150. },onUpdate: function() {
  151. if ( options.wacky === true ) {
  152. displacementSprite.rotation += baseTimeline.progress() * 0.02;
  153. displacementSprite.scale.set( baseTimeline.progress() * 3 );
  154. }
  155. } });
  156. baseTimeline.clear();
  157. if ( baseTimeline.isActive() ) {
  158. return;
  159. }
  160. // DEMO 4
  161. baseTimeline
  162. .to(displacementFilter.scale, 1, { x: options.displaceScale[0], y: options.displaceScale[1], ease: Power1.easeOut })
  163. .to(slideImages[that.currentIndex], 0.5, { alpha: 0, ease: Power2.easeOut }, 0.2)
  164. .to(slideImages[newIndex], 0.5, { alpha: 1, ease: Power2.easeOut }, 0.3)
  165. .to(displacementFilter.scale, 1, { x: options.displaceScaleTo[0], y: options.displaceScaleTo[1], ease: Power2.easeOut }, 0.3 );
  166. };
  167. /// ---------------------------
  168. // CLICK HANDLERS
  169. /// ---------------------------
  170. var nav = options.navElement;
  171. for ( var i = 0; i < nav.length; i++ ) {
  172. var navItem = nav[i];
  173. navItem.onclick = function( event ) {
  174. // Make sure the previous transition has ended
  175. if ( isPlaying ) {
  176. return false;
  177. }
  178. if ( this.getAttribute('data-nav') === 'next' ) {
  179. if ( that.currentIndex >= 0 && that.currentIndex < slideImages.length - 1 ) {
  180. that.moveSlider( that.currentIndex + 1 );
  181. } else {
  182. that.moveSlider( 0 );
  183. }
  184. } else {
  185. if ( that.currentIndex > 0 && that.currentIndex < slideImages.length ) {
  186. that.moveSlider( that.currentIndex - 1 );
  187. } else {
  188. that.moveSlider( spriteImages.length - 1 );
  189. }
  190. }
  191. return false;
  192. }
  193. }
  194. /// ---------------------------
  195. // INIT FUNCTIONS
  196. /// ---------------------------
  197. this.init = function() {
  198. that.initPixi();
  199. that.loadPixiSprites( options.pixiSprites );
  200. /*
  201. if ( options.fullScreen === true ) {
  202. window.addEventListener("resize", function( event ){
  203. scaleToWindow( renderer.view );
  204. });
  205. scaleToWindow( renderer.view );
  206. }
  207. */
  208. };
  209. /// ---------------------------
  210. // INTERACTIONS
  211. /// ---------------------------
  212. function rotateSpite() {
  213. displacementSprite.rotation += 0.001;
  214. rafID = requestAnimationFrame( rotateSpite );
  215. }
  216. if ( options.interactive === true ) {
  217. var rafID, mouseX, mouseY;
  218. // Enable interactions on our slider
  219. slidesContainer.interactive = true;
  220. slidesContainer.buttonMode = true;
  221. // HOVER
  222. if ( options.interactionEvent === 'hover' || options.interactionEvent === 'both' ) {
  223. slidesContainer.pointerover = function( mouseData ){
  224. mouseX = mouseData.data.global.x;
  225. mouseY = mouseData.data.global.y;
  226. TweenMax.to( displacementFilter.scale, 1, { x: "+=" + Math.sin( mouseX ) * 100 + "", y: "+=" + Math.cos( mouseY ) * 100 + "" });
  227. rotateSpite();
  228. };
  229. slidesContainer.pointerout = function( mouseData ){
  230. TweenMax.to( displacementFilter.scale, 1, { x: 0, y: 0 });
  231. cancelAnimationFrame( rafID );
  232. };
  233. }
  234. // CLICK
  235. if ( options.interactionEvent === 'click' || options.interactionEvent === 'both' ) {
  236. slidesContainer.pointerup = function( mouseData ){
  237. if ( options.dispatchPointerOver === true ) {
  238. TweenMax.to( displacementFilter.scale, 1, { x: 0, y: 0, onComplete: function() {
  239. TweenMax.to( displacementFilter.scale, 1, { x: 20, y: 20 });
  240. } });
  241. } else {
  242. TweenMax.to( displacementFilter.scale, 1, { x: 0, y: 0 });
  243. cancelAnimationFrame( rafID );
  244. }
  245. };
  246. slidesContainer.pointerdown = function( mouseData ){
  247. mouseX = mouseData.data.global.x;
  248. mouseY = mouseData.data.global.y;
  249. TweenMax.to( displacementFilter.scale, 1, { x: "+=" + Math.sin( mouseX ) * 1200 + "", y: "+=" + Math.cos( mouseY ) * 200 + "" });
  250. };
  251. slidesContainer.pointerout = function( mouseData ){
  252. if ( options.dispatchPointerOver === true ) {
  253. TweenMax.to( displacementFilter.scale, 1, { x: 0, y: 0, onComplete: function() {
  254. TweenMax.to( displacementFilter.scale, 1, { x: 20, y: 20 });
  255. } });
  256. } else {
  257. TweenMax.to( displacementFilter.scale, 1, { x: 0, y: 0 });
  258. cancelAnimationFrame( rafID );
  259. }
  260. };
  261. }
  262. }
  263. /// ---------------------------
  264. // CENTER DISPLACEMENT
  265. /// ---------------------------
  266. if ( options.displacementCenter === true ) {
  267. displacementSprite.anchor.set(0.5);
  268. displacementSprite.x = renderer.view.width / 2;
  269. displacementSprite.y = renderer.view.height / 2;
  270. }
  271. /// ---------------------------
  272. // START
  273. /// ---------------------------
  274. this.init();
  275. /// ---------------------------
  276. // HELPER FUNCTIONS
  277. /// ---------------------------
  278. function scaleToWindow( canvas, backgroundColor ) {
  279. var scaleX, scaleY, scale, center;
  280. //1. Scale the canvas to the correct size
  281. //Figure out the scale amount on each axis
  282. scaleX = window.innerWidth / canvas.offsetWidth;
  283. scaleY = window.innerHeight / canvas.offsetHeight;
  284. //Scale the canvas based on whichever value is less: `scaleX` or `scaleY`
  285. scale = Math.min(scaleX, scaleY);
  286. canvas.style.transformOrigin = "0 0";
  287. canvas.style.transform = "scale(" + scale + ")";
  288. //2. Center the canvas.
  289. //Decide whether to center the canvas vertically or horizontally.
  290. //Wide canvases should be centered vertically, and
  291. //square or tall canvases should be centered horizontally
  292. if (canvas.offsetWidth > canvas.offsetHeight) {
  293. if (canvas.offsetWidth * scale < window.innerWidth) {
  294. center = "horizontally";
  295. } else {
  296. center = "vertically";
  297. }
  298. } else {
  299. if (canvas.offsetHeight * scale < window.innerHeight) {
  300. center = "vertically";
  301. } else {
  302. center = "horizontally";
  303. }
  304. }
  305. //Center horizontally (for square or tall canvases)
  306. var margin;
  307. if (center === "horizontally") {
  308. margin = (window.innerWidth - canvas.offsetWidth * scale) / 2;
  309. canvas.style.marginTop = 0 + "px";
  310. canvas.style.marginBottom = 0 + "px";
  311. canvas.style.marginLeft = margin + "px";
  312. canvas.style.marginRight = margin + "px";
  313. }
  314. //Center vertically (for wide canvases)
  315. if (center === "vertically") {
  316. margin = (window.innerHeight - canvas.offsetHeight * scale) / 2;
  317. canvas.style.marginTop = margin + "px";
  318. canvas.style.marginBottom = margin + "px";
  319. canvas.style.marginLeft = 0 + "px";
  320. canvas.style.marginRight = 0 + "px";
  321. }
  322. //3. Remove any padding from the canvas and body and set the canvas
  323. //display style to "block"
  324. canvas.style.paddingLeft = 0 + "px";
  325. canvas.style.paddingRight = 0 + "px";
  326. canvas.style.paddingTop = 0 + "px";
  327. canvas.style.paddingBottom = 0 + "px";
  328. canvas.style.display = "block";
  329. //4. Set the color of the HTML body background
  330. document.body.style.backgroundColor = backgroundColor;
  331. //Fix some quirkiness in scaling for Safari
  332. var ua = navigator.userAgent.toLowerCase();
  333. if (ua.indexOf("safari") != -1) {
  334. if (ua.indexOf("chrome") > -1) {
  335. // Chrome
  336. } else {
  337. // Safari
  338. //canvas.style.maxHeight = "100%";
  339. //canvas.style.minHeight = "100%";
  340. }
  341. }
  342. //5. Return the `scale` value. This is important, because you'll nee this value
  343. //for correct hit testing between the pointer and sprites
  344. return scale;
  345. } // http://bit.ly/2y1Yk2k
  346. };
  347. })();