UNPKG

@zhsz/cool-design-dv

Version:

121 lines (120 loc) 4.25 kB
import { defineComponent, ref, getCurrentInstance, useAttrs, inject, computed, onMounted, onBeforeUnmount, openBlock, createBlock, unref, mergeProps, withCtx, createElementBlock, Fragment, renderList, createCommentVNode } 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 { 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", "begin"]; const __default__ = defineComponent({ name: "DvAdorn3" }); const _sfc_main = /* @__PURE__ */ defineComponent({ ...__default__, props: { duration: { type: Number, default: 3 }, reverse: Boolean, opacity: { type: Number, default: 1 }, size: { type: Number, default: 5 }, gap: { type: Number, default: 5 } }, emits: ["resize"], setup(__props, { emit: __emit }) { var _a, _b, _c; const props = __props; const points = 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 } = 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 rows = computed(() => { return Math.floor(height.value / (props.size + props.gap)); }); const columns = computed(() => { return Math.floor(width.value / (props.size + props.gap)); }); function createPoints() { const grid = props.gap + props.size; const points2 = new Array(rows.value).fill(0).map( (foo, i) => new Array(columns.value).fill(0).map((foo2, j) => [grid * j, grid * i]) ); return points2.reduce((all, item) => [...all, ...item], []); } function setData() { points.value = createPoints(); } 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-3", unref(classes)], style: unref(styles), "default-width": "300px", "default-height": "20px" }, unref($attrs)), { default: withCtx(() => [ (openBlock(), createElementBlock("svg", { width: `${unref(width)}px`, height: `${unref(height)}px` }, [ (openBlock(true), createElementBlock(Fragment, null, renderList(points.value, (point, i) => { return openBlock(), createElementBlock("rect", { key: i, fill: unref(dark), x: point[0], y: point[1], width: __props.size, height: __props.size }, [ Math.random() > 0.6 ? (openBlock(), createElementBlock("animate", { key: 0, attributeName: "fill", values: `${[unref(dark), "transparent"].join(";")}`, dur: Math.random() + 1 + "s", begin: Math.random() * 2, repeatCount: "indefinite" }, null, 8, _hoisted_3)) : createCommentVNode("", true) ], 8, _hoisted_2); }), 128)) ], 8, _hoisted_1)) ]), _: 1 }, 16, ["class", "style"]); }; } }); export { _sfc_main as default };