1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| import * as TWEEN from "@tweenjs/tween.js"; import Two from "two.js"; import animations, { register } from "./index"; import { two, center } from "../common"; import { Circle } from "two.js/src/shapes/circle";
let direction = true; let animate_ins = [], animate_outs = [];
const TWO_PI = Math.PI * 2; const duration = 1000; const amount = 24; const last = amount - 1; const circles = [...Array(amount).keys()].map((i) => { const circle: Circle & { theta?: number; destination?: number; } = new Two.Circle(); circle.theta = 0; circle.destination = 0; return circle; });
const group = two.makeGroup(circles); group.noStroke().fill = "#e34f0c";
function start() { animate_ins[0].start(); }
function resize() { group.translation.copy(center); }
function reset() { if (animate_ins.length > 0) { animate_ins.forEach(stop); animate_ins.length = 0; } if (animate_outs.length > 0) { animate_outs.forEach(stop); animate_outs.length = 0; }
direction = Math.random() > 0.5; group.rotation = Math.random() * TWO_PI;
const radius = animations.minDimension / 3; const bubbleRadius = animations.minDimension / 90;
for (let i = 0; i < circles.length; i++) { let pct = i / amount; let npt = (i + 1) / amount;
const circle = circles[i]; circle.visible = false; circle.radius = bubbleRadius;
circle.destination = TWO_PI * pct; circle.theta = 0; circle.translation.set(radius, 0);
const ain = new TWEEN.Tween(circle) .to({ theta: circle.destination }, (0.2 * duration) / (i + 1)) .onStart(() => (circle.visible = true)) .onUpdate(() => { const theta = circle.theta * (direction ? 1 : -1); const x = radius * Math.cos(theta); const y = radius * Math.sin(theta);
circle.translation.set(x, y); }) .onComplete(() => { if (i >= last) { animate_outs[0].start(); return; }
const next = circles[i + 1]; const tween = animate_ins[i + 1]; next.theta = circle.theta; next.translation.copy(circle.translation); tween.start(); });
animate_ins.push(ain);
const destination = Math.min(npt * TWO_PI, TWO_PI);
const aout = new TWEEN.Tween(circle) .to( { theta: destination, }, (0.2 * duration) / (amount - (i + 1)) ) .onUpdate(() => { const theta = circle.theta * (direction ? 1 : -1); const x = radius * Math.cos(theta); const y = radius * Math.sin(theta);
circle.translation.set(x, y); }) .onComplete(() => { circle.visible = false;
if (i >= last - 1) { reset(); } else { animate_outs[i + 1].start(); } });
animate_outs.push(aout); } }
function stop(tween) { tween.stop(); }
resize(); reset();
const animation = { start: start, clear: reset, resize: resize, name: "bubbles", };
register(animation.name, animation);
|