main.js 17 KB

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