GSAP 사례 연구
Generating Randomness
데이터 · MIT (local companion); GSAP Standard No Charge License (runtime)
갤러리 안에서는 화면을 벗어나거나 움직임을 멈추면 예제도 닫힙니다. 다시 재생할 때는 처음부터 시작해요.
저장한 HTML에서는 갤러리의 재생 설정이 적용되지 않아요.
SOURCE FILES
독립 예제 코드 읽기
사례의 구도나 문서의 원리를 바탕으로 새로 만든 예제입니다. 아래 코드를 복사해 직접 응용할 수 있습니다. 실행 HTML에는 미리보기를 위한 실행 환경이 들어 있습니다.
vendor/gsap-previews/sources/gsap-2a414d7d8e07/source-input.js
/// random number between -100 and 100
let randomNum = gsap.utils.random(-100, 100);
console.log("random number between -100 and 100", randomNum)
// snapped to closest increment of 5
let randomSnap = gsap.utils.random(-100, 100, 5);
console.log("snapped to closest increment of 5", randomSnap)
// get a random value from an array of colors
let randomValue = gsap.utils.random(["red", "blue", "green"]);
console.log("a random value from an array of colors", randomValue)
let colors = ["#0ae448", "#fec5fb", "#ff8709","#9d95ff", "#abff84", "#00bae2"]
// use the shortcut string syntax or call the util methods in a tween
gsap.to('.box', {
x: "random(-400, 400, 5)",
backgroundColor: () => gsap.utils.random(colors),
repeat: -1,
repeatRefresh: true,
duration: 1,
repeatDelay: 0.2
})vendor/gsap-previews/sources/gsap-2a414d7d8e07/index.html실행 안내·자료
<main class="study"><p class="study-label">공식 문서 기반 · 독립 예제</p><header><h2>다음 반복에서 다시 고르기</h2><p>처음 생성한 값 세 개와 반복할 때 다시 선택하는 위치·색을 구분합니다.</p></header><div class="values"><div>범위 값<strong id="random-number"></strong></div><div>5단위 값<strong id="random-snap"></strong></div><div>배열 선택<strong id="random-color"></strong></div></div><div class="stage" id="random-stage"><div class="random-world"><div class="axis"></div><div class="box"></div></div></div><div class="controls"><button id="toggle" type="button">정지</button></div><p class="note">이동 좌표는 원문과 같은 −400…400, 5단위입니다. 좁은 화면에서는 관찰 표면 전체를 축소합니다.</p></main>함께 쓰는 파일 3개 보기
vendor/gsap-previews/sources/gsap-2a414d7d8e07/style.css실행 안내·자료
*{box-sizing:border-box}body{background:#111522;color:#f5f2eb;font-family:system-ui,sans-serif;margin:0}button{font:inherit}.study{margin:0 auto;max-width:1100px;min-height:100vh;padding:clamp(20px,4vw,42px)}.study-label{color:#95efd2;font-size:12px;letter-spacing:.08em;margin:0 0 24px}header h2{font-size:clamp(24px,4vw,40px);letter-spacing:-.04em;margin:0 0 12px}header p{color:#aeb7c9;line-height:1.6;margin:0 0 24px;max-width:62ch}.controls{display:flex;flex-wrap:wrap;gap:8px;margin:20px 0}button{background:#26314b;border:1px solid #617394;border-radius:9px;color:#f5f2eb;cursor:pointer;padding:9px 14px}button:hover{background:#334261}button:focus-visible{outline:3px solid #95efd2;outline-offset:3px}button:disabled{cursor:default;opacity:.45}.stage{align-items:center;background:radial-gradient(ellipse at top,#243253,#141c2d);border:1px solid #3d4c68;border-radius:20px;display:flex;justify-content:center;min-height:220px;overflow:hidden;position:relative}.note{color:#aeb7c9;font-size:13px;line-height:1.5}.readout{color:#95efd2;font-family:ui-monospace,monospace;font-size:13px;min-height:2em}.stage svg{display:block;max-width:100%}code{font-family:ui-monospace,monospace}button[aria-pressed="true"]{background:#95efd2;color:#111522}
.values{display:flex;flex-wrap:wrap;gap:12px;margin:20px 0}.values div{background:#202b42;border-radius:12px;color:#aeb7c9;flex:1;min-width:110px;padding:16px}.values strong{color:#f5f2eb;display:block;font-family:ui-monospace,monospace;font-size:20px;margin-top:8px}.random-world{height:120px;left:50%;position:absolute;top:50%;transform:translate(-50%,-50%);transform-origin:center;width:940px}.axis{border-top:1px dashed #798bad;left:20px;position:absolute;right:20px;top:60px}.box{background:#95efd2;border-radius:18px;height:72px;left:434px;position:absolute;top:24px;width:72px}vendor/gsap-previews/sources/gsap-2a414d7d8e07/script.js실행 안내·자료
gsap.registerPlugin();
/// random number between -100 and 100
let randomNum = gsap.utils.random(-100, 100);
console.log("random number between -100 and 100", randomNum)
// snapped to closest increment of 5
let randomSnap = gsap.utils.random(-100, 100, 5);
console.log("snapped to closest increment of 5", randomSnap)
// get a random value from an array of colors
let randomValue = gsap.utils.random(["red", "blue", "green"]);
console.log("a random value from an array of colors", randomValue)
let colors = ["#0ae448", "#fec5fb", "#ff8709","#9d95ff", "#abff84", "#00bae2"]
// use the shortcut string syntax or call the util methods in a tween
gsap.to('.box', {
x: "random(-400, 400, 5)",
backgroundColor: () => gsap.utils.random(colors),
repeat: -1,
repeatRefresh: true,
duration: 1,
repeatDelay: 0.2
})
document.getElementById('random-number').textContent=randomNum.toFixed(2);document.getElementById('random-snap').textContent=randomSnap;document.getElementById('random-color').textContent=randomValue;
const stage=document.getElementById('random-stage');const world=document.querySelector('.random-world');
new ResizeObserver(()=>world.style.transform='translate(-50%,-50%) scale('+Math.min(1,stage.clientWidth/940)+')').observe(stage);
let paused=false;document.getElementById('toggle').addEventListener('click',e=>{paused=!paused;gsap.getTweensOf('.box').forEach(t=>t.paused(paused));e.currentTarget.textContent=paused?'재생':'정지'});LICENSE실행 안내·자료
Locally authored companion HTML/CSS/control wiring: MIT License.
Copyright (c) 2026 StyleGallery contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The separately retained official documentation excerpt is source input, not a recovered CodePen source set; this local MIT notice does not assign a new license to that excerpt.
GSAP 3.15.0: Copyright 2026, GreenSock. All rights reserved. Subject to https://gsap.com/standard-license. Original library notices are preserved.
BEHIND THE EXAMPLE
이 장면을 만드는 원리
처음 생성한 값과 반복마다 생성한 값
값이 생성되는 시점과 화면이 갱신되는 시점을 분리해 설명합니다.
- 이 예제에서는
- 세 utils.random 호출은 실행 시 한 번 값을 만듭니다. 상자의 x는 -400…400을 5단위로 고르고 색은 함수에서 선택하며 repeatRefresh:true가 다음 반복에서 값을 다시 계산합니다. 동반 예제의 값 패널은 처음의 세 값을 유지하고 정지 버튼은 상자 트윈만 제어합니다.
코드와 함께 확인하기
코드에서 찾기
repeatRefresh: truesource-input.js반복마다 함수·난수 입력을 다시 평가하는 문서 설정입니다.
gsap.utils.random(-100, 100, 5)source-input.js처음 생성하는 5단위 범위 값입니다.
gsap.getTweensOf('.box')script.js동반 예제의 정지는 입출력 값이 아니라 움직임을 대상으로 합니다.
직접 해보기
여러 반복을 기다립니다.
살펴볼 변화상자 위치와 색은 다시 선택되지만 위쪽의 최초 값은 바뀌지 않아야 합니다.
정지 후 재생을 누릅니다.
살펴볼 변화최초 값들을 새로 생성하는 동작과 트윈 재생을 구분합니다.
