UNPKG

@zhsz/cool-design-dv

Version:

133 lines (132 loc) 5.14 kB
import { defineComponent, ref, getCurrentInstance, useAttrs, inject, computed, onMounted, onBeforeUnmount, openBlock, createBlock, unref, mergeProps, withCtx, createElementBlock, Fragment, renderList, createElementVNode } from "vue"; import Box from "../dv-box/index.mjs"; import { dvPageSymbols, mittSymbol } from "../../symbols/index.mjs"; import { addResizeListener, removeResizeListener } from "../../utils/resize-event.mjs"; import { random } from "../../utils/util.mjs"; import { useAdorn } from "../../hooks/useAdorn.mjs"; import { baseConfig } from "../../hooks/useConfig.mjs"; const _hoisted_1 = ["width", "height"]; const _hoisted_2 = ["fill", "x", "y", "width", "height"]; const _hoisted_3 = ["values", "dur"]; const _hoisted_4 = ["values", "dur"]; const __default__ = defineComponent({ name: "DvAdorn6" }); const _sfc_main = /* @__PURE__ */ defineComponent({ ...__default__, props: { duration: { type: Number, default: 3 }, reverse: Boolean, opacity: { type: Number, default: 1 }, size: { type: Number, default: 8 }, gap: { type: Number, default: 2 } }, emits: ["resize"], setup(__props, { emit: __emit }) { var _a, _b, _c; const props = __props; const points = ref([]); const heights = ref([]); const minHeights = ref([]); const randoms = ref([]); const instance = (_a = getCurrentInstance()) == null ? void 0 : _a.proxy; const emits = __emit; const $attrs = useAttrs(); const page = inject(dvPageSymbols, null); const mitt = inject(mittSymbol, null); const { resize, classes, styles, width, height, dark, light } = useAdorn( emits, ((_c = (_b = page == null ? void 0 : page.settings) == null ? void 0 : _b.value) == null ? void 0 : _c.color) ?? baseConfig.color, props, page ); const columns = computed(() => { return Math.floor(width.value / (props.size + props.gap)); }); function setData() { points.value = new Array(columns.value).fill(0).map((n, i) => i * (props.size + props.gap)); const _heights = heights.value = new Array(columns.value).fill(0).map( (foo) => Math.random() > 0.8 ? random(0.7 * height.value, height.value) : random(0.2 * height.value, 0.5 * height.value) ); minHeights.value = new Array(columns.value).fill(0).map((foo, i) => _heights[i] * Math.random()); randoms.value = new Array(columns.value).fill(0).map((foo) => Math.random() + 1.5); } onMounted(() => { resize(instance == null ? void 0 : instance.$el); setData(); addResizeListener(instance == null ? void 0 : instance.$el, () => resize(instance == null ? void 0 : instance.$el)); if (mitt) { mitt.on("resize", setData); } }); onBeforeUnmount(() => { removeResizeListener(instance == null ? void 0 : instance.$el, () => resize(instance == null ? void 0 : instance.$el)); if (mitt) { mitt.off("resize", setData); } }); return (_ctx, _cache) => { return openBlock(), createBlock(unref(Box), mergeProps({ class: ["my-dv-adorn-6", unref(classes)], style: unref(styles), "default-width": "300px", "default-height": "60px" }, unref($attrs)), { default: withCtx(() => [ (openBlock(), createElementBlock("svg", { width: `${unref(width)}px`, height: `${unref(height)}px` }, [ (openBlock(true), createElementBlock(Fragment, null, renderList(points.value, (x, i) => { return openBlock(), createElementBlock("rect", { key: i, fill: Math.random() > 0.5 ? unref(light) : unref(dark), x, y: (unref(height) - heights.value[i]) / 2, width: __props.size, height: heights.value[i] }, [ createElementVNode("animate", { attributeName: "y", values: `${(unref(height) - minHeights.value[i]) / 2};${(unref(height) - heights.value[i]) / 2};${(unref(height) - minHeights.value[i]) / 2}`, dur: `${randoms.value[i]}s`, keyTimes: "0;0.5;1", calcMode: "spline", keySplines: "0.42,0,0.58,1;0.42,0,0.58,1", begin: "0s", repeatCount: "indefinite" }, null, 8, _hoisted_3), createElementVNode("animate", { attributeName: "height", values: `${minHeights.value[i]};${heights.value[i]};${minHeights.value[i]}`, dur: `${randoms.value[i]}s`, keyTimes: "0;0.5;1", calcMode: "spline", keySplines: "0.42,0,0.58,1;0.42,0,0.58,1", begin: "0s", repeatCount: "indefinite" }, null, 8, _hoisted_4) ], 8, _hoisted_2); }), 128)) ], 8, _hoisted_1)) ]), _: 1 }, 16, ["class", "style"]); }; } }); export { _sfc_main as default };