Viewer.jsx 13 KB

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