Viewer.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. import { Component, createRef } from "react";
  2. import PropTypes from "prop-types";
  3. import { gsap, ScrollTrigger } from "gsap/all";
  4. import LazyLoad from "react-lazyload";
  5. import { css } from "@emotion/react";
  6. const isDebug = Number(import.meta.env.VITE_APP_DEBUG) === 1;
  7. console.log("isDebug", isDebug);
  8. export default class Viewer extends Component {
  9. constructor() {
  10. super();
  11. console.log("hello constructor");
  12. //ref
  13. this.containerRef = createRef(null);
  14. this.viewerRef = createRef(null);
  15. this.canvasContainerRef = createRef(null);
  16. this.loadingWrap = createRef(null);
  17. this.viewerOffsetRef = createRef(null);
  18. this.canvasRef = createRef(null);
  19. this.processingRef = createRef(null);
  20. this.preProcessingRef = createRef(null);
  21. this.processBarRef = createRef(null);
  22. this.sequence = [];
  23. this.loadedRenderPool = [];
  24. this.enterTimeline = false;
  25. this.exitTimeline = false;
  26. this.loadComplete = false;
  27. this.playBarTween = false;
  28. this.playPreBarTween = false;
  29. this.loadedCount = 0;
  30. this.progress = 0;
  31. this.lastFrame = -1;
  32. this.floatFrame = 0;
  33. this.frame = 0;
  34. this.loadedRenderTimeout = null;
  35. this.poolAnimateDelay = 40;
  36. this.context = null;
  37. this.width = 1552;
  38. this.height = 873;
  39. this.justScrolled = false;
  40. this.lastProgress = false;
  41. this.isBelow = true;
  42. this.isAbove = false;
  43. }
  44. static propTypes = {
  45. name: PropTypes.string,
  46. height: PropTypes.string,
  47. path: PropTypes.string,
  48. frameCount: PropTypes.number,
  49. startFrame: PropTypes.number,
  50. enterTween: PropTypes.object,
  51. exitTween: PropTypes.object,
  52. canvasSize: PropTypes.array,
  53. pause: PropTypes.object,
  54. children: PropTypes.object,
  55. };
  56. componentWillUnmount() {
  57. console.error("remove-timeline");
  58. if (this.timeline) {
  59. this.timeline.kill(true);
  60. }
  61. }
  62. componentDidMount() {
  63. this.fullFrameCount = this.props.frameCount;
  64. this.frame = this.props.startFrame || 0;
  65. if (this.props.pause) {
  66. Object.keys(this.props.pause).forEach((index) => {
  67. this.fullFrameCount += this.props.pause[index];
  68. });
  69. }
  70. this.loadAssets();
  71. this.canvasRef.current && this.initializeCanvas();
  72. if (!this.timeline) {
  73. this.initializeTimeline();
  74. this.setTimeline();
  75. }
  76. if (!this.enterTimeline && this.props.enterTween) {
  77. this.initializeEnterTween();
  78. }
  79. if (!this.exitTimeline && this.props.exitTween) {
  80. this.initializeExitTween();
  81. }
  82. ScrollTrigger.refresh();
  83. }
  84. loadImage(index) {
  85. const img = new Image();
  86. img.retried = 0;
  87. img.src = this.getSourcePath(index);
  88. img.ogSrc = img.src;
  89. if (this.props.pause && index + "" in this.props.pause) {
  90. for (var r = this.props.pause[index]; r--; ) {
  91. this.sequence.push(img);
  92. }
  93. }
  94. this.sequence.push(img);
  95. img.onload = () => {
  96. index === 1 && this.renderImageToCanvas(0);
  97. if (this.frame > index && this.timeline.scrollTrigger.isActive) {
  98. this.poolNewFrames(index - 1);
  99. }
  100. // var t = 100 - (this.frame / this.fullFrameCount) * 100 + "%";
  101. // this.loadingProgress.current.style.width
  102. this.loadedCount += 1;
  103. if (this.loadedCount === parseFloat(this.props.frameCount) - 1) {
  104. this.loadingComplete();
  105. }
  106. };
  107. }
  108. getSourcePath(index) {
  109. const defaultPrefix = import.meta.env.VITE_APP_SOURCE;
  110. return `${defaultPrefix}${this.props.path}/${"".concat(
  111. index.toString().padStart(4, "0")
  112. )}.webp`;
  113. }
  114. loadAssets() {
  115. this.loadImage(1);
  116. setTimeout(() => {
  117. for (var t = 2; t <= this.props.frameCount; t += 1) {
  118. this.loadImage(t);
  119. }
  120. }, 60);
  121. }
  122. loadingComplete() {
  123. console.log(this.props.path, "loading complete");
  124. this.loadComplete = true;
  125. this.isAbove && this.renderImageToCanvas(this.loadedCount - 1);
  126. }
  127. initializeCanvas() {
  128. this.context = this.canvasRef.current.getContext("2d", {
  129. alpha: false,
  130. desynchronized: true,
  131. powerPreference: "high-performance",
  132. });
  133. this.context.imageSmoothingEnabled = true;
  134. this.context.imageSmoothingQuality = "high";
  135. }
  136. initializeTimeline() {
  137. // const openLoading = () => {
  138. // gsap.to(this.loadingWrap.current, {
  139. // autoAlpha: 1,
  140. // });
  141. // };
  142. // const closeLoading = () => {
  143. // gsap.to(this.loadingWrap.current, {
  144. // autoAlpha: 0,
  145. // });
  146. // };
  147. // closeLoading();
  148. this.timeline = gsap.timeline({
  149. scrollTrigger: {
  150. trigger: this.containerRef.current,
  151. pin: this.viewerRef.current,
  152. scrub: 0.66,
  153. start: "top top",
  154. end: "bottom bottom",
  155. ease: "none",
  156. markers: isDebug,
  157. onUpdate: (trigger) => {
  158. //处理processloading
  159. if (!this.lastProgress) {
  160. this.lastProgress = trigger.progress;
  161. // console.log("lastProgress", this.lastProgress);
  162. } else {
  163. if (this.lastProgress !== this.progress) {
  164. this.justScrolled = true;
  165. this.lastProgress = trigger.progress;
  166. }
  167. }
  168. },
  169. onScrubComplete: () => {
  170. this.justScrolled = true;
  171. },
  172. onEnter: () => {
  173. this.isAbove = false;
  174. console.log(this.props.path, "onEnter");
  175. this.enterShowElements();
  176. },
  177. onEnterBack: () => {
  178. // openLoading();
  179. console.log(this.props.path, "onEnterBack");
  180. this.isBelow = false;
  181. this.enterShowElements();
  182. },
  183. onLeave: () => {
  184. console.log(this.props.path, "onLeave");
  185. this.isAbove = true;
  186. this.leaveHideElements();
  187. },
  188. onLeaveBack: () => {
  189. console.log(this.props.path, "onLeaveBack");
  190. this.isBelow = true;
  191. this.leaveHideElements();
  192. },
  193. },
  194. });
  195. }
  196. setTimeline() {
  197. this.timeline.to(this, {
  198. floatFrame: this.fullFrameCount - 1,
  199. ease: "none",
  200. onUpdate: () => {
  201. this.frame = Math.floor(this.floatFrame);
  202. if (
  203. this.lastFrame === this.frame ||
  204. this.loadedRenderPool.length === 0
  205. ) {
  206. this.renderImageToCanvas(this.frame);
  207. }
  208. },
  209. });
  210. }
  211. initializeEnterTween() {
  212. const duration = this.props.enterTween.duration || 1;
  213. let openPin = false;
  214. console.warn("this.props.enterTween", duration, this.props.enterTween);
  215. gsap.set(this.viewerRef.current, {
  216. yPercent: -100 * duration,
  217. });
  218. if (void 0 !== this.props.enterTween.pin) {
  219. openPin = this.props.enterTween.pin;
  220. }
  221. this.enterTimeline = gsap.timeline({
  222. scrollTrigger: {
  223. trigger: this.viewerRef.current,
  224. scrub: true,
  225. pin: openPin,
  226. start: function () {
  227. return "top top";
  228. },
  229. end: function () {
  230. return "top top-=" + window.innerHeight * duration;
  231. },
  232. },
  233. });
  234. if (this.props.enterTween.to) {
  235. this.enterTimeline.to(
  236. this.viewerRef.current,
  237. Object.assign(
  238. {
  239. ease: "none",
  240. },
  241. this.props.enterTween.to
  242. )
  243. );
  244. }
  245. if (this.props.enterTween.from) {
  246. this.enterTimeline.from(
  247. this.viewerRef.current,
  248. Object.assign(
  249. {
  250. ease: "none",
  251. },
  252. this.props.enterTween.from
  253. )
  254. );
  255. }
  256. }
  257. initializeExitTween() {
  258. console.log(this.props.path, "initializing exit tween ");
  259. this.exitTimeline = gsap.timeline({
  260. scrollTrigger: {
  261. scrub: true,
  262. trigger: this.containerRef.current,
  263. pin: this.viewerRef.current,
  264. onLeave: this.props.exitTween.onLeave,
  265. onLeaveBack: this.props.exitTween.onLeaveBack,
  266. onEnterBack: this.props.exitTween.onEnterBack,
  267. start: function () {
  268. return "bottom bottom";
  269. },
  270. end: function () {
  271. return "bottom top";
  272. },
  273. },
  274. });
  275. if (this.props.exitTween.from) {
  276. this.exitTimeline.from(
  277. this.viewerRef.current,
  278. Object.assign(
  279. {
  280. ease: "none",
  281. },
  282. this.props.exitTween.from
  283. )
  284. );
  285. }
  286. if (this.props.exitTween.to) {
  287. this.exitTimeline.to(
  288. this.viewerRef.current,
  289. Object.assign(
  290. {
  291. ease: "none",
  292. },
  293. this.props.exitTween.to
  294. )
  295. );
  296. }
  297. }
  298. poolNewFrames(index) {
  299. this.loadedRenderPool.unshift(index);
  300. this.loadedRenderPool.sort(function (a, b) {
  301. return b - a;
  302. });
  303. this.animatePool();
  304. }
  305. animatePool() {
  306. if (!this.loadedRenderTimeout && this.loadedRenderPool.length) {
  307. this.loadedRenderTimeout = setTimeout(() => {
  308. this.loadedRenderTimeout = false;
  309. var poolFrame = this.loadedRenderPool[this.loadedRenderPool.length - 1];
  310. if (poolFrame <= this.frame) {
  311. var remainFrame = this.loadedRenderPool.pop();
  312. this.renderImageToCanvas(remainFrame);
  313. this.animatePool();
  314. }
  315. if (this.frame < poolFrame) {
  316. this.loadedRenderPool = [];
  317. }
  318. }, this.poolAnimateDelay);
  319. }
  320. }
  321. renderImageToCanvas(index) {
  322. if (this.sequence[index]) {
  323. if (this.context.drawImage) {
  324. this.context.drawImage(this.sequence[index], 0, 0);
  325. this.lastFrame = index;
  326. this.handleSyncProessBar(index);
  327. } else {
  328. this.initializeCanvas();
  329. }
  330. }
  331. }
  332. handleSyncProessBar(index) {
  333. const progressingPreload =
  334. 100 - (this.frame / this.fullFrameCount) * 100 + "%";
  335. const progressing = 100 - (index / this.fullFrameCount) * 100 + "%";
  336. // console.log("handleSyncProessBar", this.processingRef.current);
  337. this.playPreBarTween = gsap.to(this.preProcessingRef.current, {
  338. duration: 0.05,
  339. right: progressingPreload,
  340. ease: "none",
  341. });
  342. this.playBarTween = gsap.to(this.processingRef.current, {
  343. duration: 0.05,
  344. right: progressing,
  345. ease: "none",
  346. });
  347. }
  348. enterShowElements() {
  349. gsap.set(this.processBarRef.current, {
  350. autoAlpha: 1,
  351. });
  352. }
  353. leaveHideElements() {
  354. gsap.set(this.processBarRef.current, {
  355. autoAlpha: 0,
  356. });
  357. }
  358. render() {
  359. return (
  360. <>
  361. <div
  362. className="processBar"
  363. ref={this.processBarRef}
  364. css={css`
  365. position: fixed;
  366. top: calc(100vh - 4px);
  367. // top: calc(var(--vh, 1vh) * 100 - 4px);
  368. left: 0;
  369. width: 100vw;
  370. max-width: 100%;
  371. border-top: 1px solid rgba(33, 33, 44, 0.6);
  372. background-color: rgba(17, 17, 34, 0.6);
  373. height: 4px;
  374. z-index: 9;
  375. `}
  376. >
  377. <div
  378. css={css`
  379. position: absolute;
  380. width: auto;
  381. height: 4px;
  382. left: 0;
  383. bottom: 0;
  384. z-index: 10;
  385. background-color: rgba(120, 120, 163, 0.33);
  386. `}
  387. ref={this.preProcessingRef}
  388. ></div>
  389. <div
  390. css={css`
  391. position: absolute;
  392. width: auto;
  393. height: 4px;
  394. left: 0;
  395. bottom: 0;
  396. z-index: 10;
  397. background-color: hsla(0, 0%, 79.2%, 0.5);
  398. `}
  399. ref={this.processingRef}
  400. ></div>
  401. </div>
  402. <div
  403. css={css`
  404. position: relative;
  405. margin: auto;
  406. text-align: center;
  407. pointer-events: none;
  408. max-width: 100vw;
  409. `}
  410. ref={this.containerRef}
  411. style={{ height: this.props.height || "500vh" }}
  412. >
  413. <div ref={this.viewerRef}>
  414. <div style={{ overflow: "hidden" }}>
  415. <canvas
  416. css={css`
  417. width: auto;
  418. margin-left: 50%;
  419. transform: translateX(-50%);
  420. height: 100vh;
  421. height: calc(var(--vh, 1vh) * 100);
  422. `}
  423. ref={this.canvasRef}
  424. width={this.width}
  425. height={this.height}
  426. ></canvas>
  427. </div>
  428. </div>
  429. <>{this.props.children}</>
  430. </div>
  431. </>
  432. );
  433. }
  434. }