Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/native. An initiative which aims to make it easy to create complex, light-weight, and perfor okikio.github.io/native
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346import { methodCall } from "@okikio/manager";import { animate, tweenAttr } from "@okikio/animate";import { interpolate } from "polymorph-js";
import type { IAnimationOptions, TypePlayStates } from "@okikio/animate";
// I added extra code to the demo to support Chrome 77 and belowlet playbackFn = (containerSel, anims) => { let playstateEl = document.querySelector(`${containerSel} #playstate-toggle`) as HTMLInputElement; let progressEl = document.querySelector(`${containerSel} #progress`) as HTMLInputElement;
let progressOutputEl = document.querySelector(`${containerSel} #progress-output`); let oldState: "pending" | TypePlayStates;
let updatePlayState = () => { oldState = anims[0].getPlayState(); if (oldState == "idle") oldState = "paused"; else if (oldState == "pending") oldState = "running"; playstateEl.setAttribute("data-playstate", oldState); };
anims[0] .on("finish begin", updatePlayState) .on("update", (progress) => { progress = progress.toFixed(4); progressEl.value = `` + progress; progressOutputEl.textContent = `${Math.round(progress)}%`; });
let clickFn = () => { if (anims[0].is("running")) methodCall(anims, "pause"); else if (anims[0].is("finished")) methodCall(anims, "reset"); else methodCall(anims, "play");
updatePlayState(); };
let inputFn = () => { let percent = Number(progressEl.value); methodCall(anims, "setProgress", percent); methodCall(anims, "pause"); }
let changeFn = () => { oldState !== "paused" ? methodCall(anims, "play") : methodCall(anims, "pause");
updatePlayState(); }
playstateEl.addEventListener("click", clickFn); progressEl.addEventListener("input", inputFn); progressEl.addEventListener("change", changeFn);
anims[0].on("stop", () => { playstateEl.removeEventListener("click", clickFn); progressEl.removeEventListener("input", inputFn); progressEl.removeEventListener("change", changeFn); anims = null; });
}
let random = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min;
/* Properties Section */// Playback Controls Demoexport let svgAnim1, svgAnim2, empty, anim, motionPath, getTotalLength;export let run = () => { // Based on an example by animateplus (() => { let containerSel = ".morph-demo"; let usingDPathEl = document.querySelector(`${containerSel} #using-d`); let usingPolymorphPathEl = document.querySelector(`${containerSel} #using-polymorph-js`);
if (usingDPathEl) { let InitialStyle = getComputedStyle(usingDPathEl); svgAnim1 = animate({ target: usingDPathEl, duration: 1800, easing: "ease", loop: 4, direction: "alternate", "d": [ InitialStyle.getPropertyValue("d"), `path("M2,5 S2,14 4,5 S7,8 8,4")` ], stroke: [ InitialStyle.getPropertyValue("stroke"), `rgb(96, 165, 250)` ], }); }
if (usingDPathEl && usingPolymorphPathEl) { let InitialStyle = getComputedStyle(usingPolymorphPathEl); let startPath = usingPolymorphPathEl.getAttribute("d"); let endPath = "M12 3v10.55c-.59-.34-1.27-.55-2-.55-2.21 0-4 1.79-4 4s1.79 4 4 4 4-1.79 4-4V7h4V3h-6z";
let options: IAnimationOptions = { duration: 1800, easing: "ease", loop: 4, fillMode: "both", direction: "alternate", };
svgAnim2 = animate({ target: usingPolymorphPathEl, options, fill: [ InitialStyle.getPropertyValue("stroke"), `rgb(96, 165, 250)` ],
// You can also use custom properties when animating // I chose to use them for this example // Read more here [https://css-tricks.com/a-complete-guide-to-custom-properties/] "--stroke": [ InitialStyle.getPropertyValue("stroke"), `rgb(96, 165, 250)` ], });
let morph = interpolate([startPath, endPath], { addPoints: 0, origin: { x: 0, y: 0 }, optimize: "fill", precision: 3 });
empty = tweenAttr({ target: usingPolymorphPathEl, options, d: progress => morph(progress), easing: "linear" });
playbackFn(containerSel, [empty, svgAnim1, svgAnim2]); } })();
(() => { let containerSel = ".playback-demo"; let DOMNodes = document.querySelectorAll(`${containerSel} .el`); if (DOMNodes.length) { anim = animate({ target: DOMNodes, // keyframes(_, total, target) { // let bgColor = getComputedStyle(target).getPropertyValue("background-color"); // let [r, g, b] = [ // random(0, 255), // random(0, 255), // random(0, 255) // ];
// return [ // { // translateX: 0, // translateY: 0, // scale: 1, // opacity: 0.5, // rotate: 0, // "background-color": bgColor // }, // { // "translate-x": random(50, 400), // translateY: (random(-50, 50) * total), // scale: 1 + random(0.025, 1.75), // opacity: 0.5 + Math.min(random(0.025, total) / total, 0.5), // rotate: random(-360, 360), // backgroundColor: `rgb(${r}, ${g}, ${b})` // } // ]; // },
"background-color": (...args) => { let [, , target] = args; let [r, g, b] = [ random(0, 255), random(0, 255), random(0, 255) ] return [ getComputedStyle(target).getPropertyValue("background-color"), `rgb(${r}, ${g}, ${b})` ]; },
"translate-x": () => [0, random(50, 400)], translateY(...args) { let [, total] = args; return [0, (random(-50, 50) * total)]; }, scale() { return [1, 1 + random(0.025, 1.75)]; }, opacity(...args) { let [, total] = args; return [0.5, 0.5 + Math.min(random(0.025, total) / total, 0.5)]; }, rotate: () => [0, random(-360, 360)], borderRadius: () => ["3px", `${random(10, 35)}%`], duration: () => random(1200, 1800), delay: () => random(0, 400),
// It is best to use the onfinish() method, but in this situation fillMode works best loop: 2, speed: (i) => 1.5 - (i * 0.125),
fillMode: "both", direction: "alternate", easing: "in-out-back", padEndDelay: true, autoplay: true });
let addBtn = document.querySelector("#add-el") as HTMLElement; let removeBtn = document.querySelector("#remove-el") as HTMLElement; let elPlacement = document.querySelector(".el-placement") as HTMLElement;
let contain = document.createElement("div"); contain.className = "contain"; contain.innerHTML = ` <div class="el"></div> <div class="el-initial"></div>`.trim();
addBtn.onclick = () => { let _contain = contain.cloneNode(true) as HTMLElement; let el = _contain.querySelector(".el"); elPlacement.appendChild(_contain);
anim.add(el);
// To support older browsers I can't use partial keyframes let transition = animate({ target: _contain, opacity: [0, 1], height: [0, "4vmin"], marginBottom: [0, 5], fillMode: "forwards", duration: 400, easing: "out" }).then(() => { transition.stop(); transition = null; _contain = null; el = null; });
};
removeBtn.onclick = () => { let contain = elPlacement.querySelector(".contain") as HTMLElement; let el = contain?.querySelector(".el") as HTMLElement;
anim.remove(el);
// To support older browsers I can't use partial keyframes let style = getComputedStyle(contain); let marginBottom = style.getPropertyValue("margin-bottom"); let height = style.getPropertyValue("height"); let transition = animate({ target: contain, opacity: [1, 0], height: [height, 0], "margin-bottom": [marginBottom, 0], fillMode: "forwards", duration: 400, easing: "out" }).then(() => { contain?.remove(); transition.stop();
transition = null; contain = null; el = null; }); };
playbackFn(containerSel, [anim]); } })();
(() => { let options: IAnimationOptions = { padEndDelay: true, easing: "linear", duration: 2000, loop: 4, speed: 1, };
let containerSel = ".motion-path-demo"; let el = document.querySelector('.motion-path .el-1') as HTMLElement; if (el) { // To support older browsers I can't use partial keyframes motionPath = animate({ target: el, "offsetDistance": ["0%", "100%"], ...options }); }
let path = document.querySelector('.motion-path path') as SVGPathElement; let el2 = document.querySelector('.motion-path .el-2') as HTMLElement; if (path && el2) { let pts: Set<number[]> = new Set(); let rotateArr: number[] = []; let len = path.getTotalLength();
let ptAtZero = path.getPointAtLength(0); for (var i = 0; i < len; i++) { let { x, y } = path.getPointAtLength(i); pts.add([x, y]);
let { x: x0, y: y0 } = i - 1 >= 1 ? path.getPointAtLength(i - 1) : ptAtZero; let { x: x1, y: y1 } = i + 1 >= 1 ? path.getPointAtLength(i + 1) : ptAtZero; let calc = +(Math.atan2(y0 - y1, x0 - x1) * 180 / Math.PI); rotateArr.push(calc); }
getTotalLength = animate({ target: el2, translate: [...pts], rotate: rotateArr, fillMode: "both", ...options, });
playbackFn(containerSel, [motionPath, getTotalLength]); } })();};
export let stop = () => { svgAnim1?.stop(); svgAnim2?.stop(); empty?.stop(); anim?.stop(); motionPath?.stop(); getTotalLength?.stop();
}