|
| 1 | +import type { Point } from '@lightningjs/renderer'; |
| 2 | +import type { ExampleSettings } from '../common/ExampleSettings.js'; |
| 3 | +import type { CoreNode } from '../../dist/src/core/CoreNode.js'; |
| 4 | + |
| 5 | +const getRandomValue = (min: number, max: number) => { |
| 6 | + return Math.random() * (max - min) + min; |
| 7 | +}; |
| 8 | + |
| 9 | +const getRandomColor = () => { |
| 10 | + const randomInt = Math.floor(Math.random() * Math.pow(2, 24)); // Use 24 bits for RGB |
| 11 | + const hexString = randomInt.toString(16).padStart(6, '0'); // RGB hex without alpha |
| 12 | + return parseInt(hexString + 'FF', 16); // Append 'FF' for full alpha |
| 13 | +}; |
| 14 | + |
| 15 | +const getRandomBezierCurve = () => { |
| 16 | + // Generate random values for control points within specified ranges |
| 17 | + const x1 = Math.random(); // 0 to 1 |
| 18 | + const y1 = Math.random() * 2; // Allow values above 1 |
| 19 | + const x2 = Math.random(); // 0 to 1 |
| 20 | + const y2 = Math.random() * 2 - 1; // Allow values between -1 and 1 |
| 21 | + |
| 22 | + // Return the Bezier curve in the required format |
| 23 | + return `cubic-bezier(${x1.toFixed(2)}, ${y1.toFixed(2)}, ${x2.toFixed( |
| 24 | + 2, |
| 25 | + )}, ${y2.toFixed(2)})`; |
| 26 | +}; |
| 27 | + |
| 28 | +export default async function ({ renderer, testRoot }: ExampleSettings) { |
| 29 | + const holder = renderer.createNode({ |
| 30 | + x: 0, |
| 31 | + y: 0, |
| 32 | + width: 1920, |
| 33 | + height: 1080, |
| 34 | + color: 0x000000ff, |
| 35 | + parent: testRoot, |
| 36 | + }); |
| 37 | + |
| 38 | + // Copy source texture from rootRenderToTextureNode |
| 39 | + for (let i = 0; i < 50; i++) { |
| 40 | + const dimension = getRandomValue(30, 150); |
| 41 | + const node = renderer.createNode({ |
| 42 | + parent: holder, |
| 43 | + x: getRandomValue(0, 1820), |
| 44 | + y: getRandomValue(0, 980), |
| 45 | + width: dimension, |
| 46 | + height: dimension, |
| 47 | + color: getRandomColor(), |
| 48 | + interactive: true, |
| 49 | + zIndex: getRandomValue(0, 100), |
| 50 | + }); |
| 51 | + |
| 52 | + node |
| 53 | + .animate( |
| 54 | + { |
| 55 | + x: getRandomValue(0, 1820), |
| 56 | + y: getRandomValue(0, 980), |
| 57 | + }, |
| 58 | + { |
| 59 | + duration: getRandomValue(8000, 12000), |
| 60 | + delay: getRandomValue(0, 5000), |
| 61 | + stopMethod: 'reverse', |
| 62 | + loop: true, |
| 63 | + easing: getRandomBezierCurve(), |
| 64 | + }, |
| 65 | + ) |
| 66 | + .start(); |
| 67 | + } |
| 68 | + |
| 69 | + document.addEventListener('touchstart', (e: TouchEvent) => { |
| 70 | + const { changedTouches } = e; |
| 71 | + if (changedTouches.length) { |
| 72 | + const touch = changedTouches.item(0); |
| 73 | + |
| 74 | + const x = touch?.clientX ?? 0; |
| 75 | + const y = touch?.clientY ?? 0; |
| 76 | + |
| 77 | + const eventData: Point = { |
| 78 | + x, |
| 79 | + y, |
| 80 | + }; |
| 81 | + // const nodes: CoreNode[] = renderer.stage.findNodesAtPoint(eventData); |
| 82 | + const topNode: CoreNode | null = |
| 83 | + renderer.stage.getNodeFromPosition(eventData); |
| 84 | + |
| 85 | + if (topNode) { |
| 86 | + topNode.scale = 1.5; |
| 87 | + setTimeout(() => { |
| 88 | + topNode.scale = 1; |
| 89 | + }, 150); |
| 90 | + } |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + document.addEventListener('mousemove', (e: MouseEvent) => { |
| 95 | + const x = e?.clientX ?? 0; |
| 96 | + const y = e?.clientY ?? 0; |
| 97 | + |
| 98 | + const eventData: Point = { |
| 99 | + x, |
| 100 | + y, |
| 101 | + }; |
| 102 | + |
| 103 | + const topNode: CoreNode | null = |
| 104 | + renderer.stage.getNodeFromPosition(eventData); |
| 105 | + if (topNode) { |
| 106 | + topNode.scale = 1.5; |
| 107 | + setTimeout(() => { |
| 108 | + topNode.scale = 1; |
| 109 | + }, 150); |
| 110 | + } |
| 111 | + }); |
| 112 | +} |
0 commit comments