PenAnimates.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <template>
  2. <div class="animations">
  3. <template v-if="props.pen.animations.length">
  4. <t-collapse
  5. v-model="openedCollapses"
  6. :borderless="true"
  7. :expand-on-row-click="true"
  8. >
  9. <t-collapse-panel v-for="(item, i) in props.pen.animations" :value="i">
  10. <template #header>
  11. <div class="flex middle" @click.stop>
  12. <t-input
  13. :title="item.name"
  14. :value="item.name"
  15. autoWidth
  16. class="mr-8"
  17. style="max-width: 60px"
  18. @change="changeAnimateName($event, item)"
  19. />
  20. <t-icon
  21. v-if="isPlaying === i"
  22. name="stop-circle"
  23. class="hover primary"
  24. style="font-size: 16px"
  25. @click="stop"
  26. />
  27. <t-icon
  28. v-else
  29. name="play-circle"
  30. class="hover"
  31. style="font-size: 16px"
  32. @click="play(i)"
  33. />
  34. </div>
  35. </template>
  36. <template #headerRightContent>
  37. <t-space size="small" @click.stop>
  38. <t-icon
  39. v-if="!props.pen.type && item.animate"
  40. name="edit"
  41. class="hover mr-4"
  42. @click="animate = item"
  43. />
  44. <t-popconfirm
  45. content="确认删除该动画吗"
  46. placement="left"
  47. @confirm="delAnimate(i)"
  48. >
  49. <t-icon name="delete" class="hover" />
  50. </t-popconfirm>
  51. </t-space>
  52. </template>
  53. <template v-if="props.pen.type">
  54. <div class="form-item">
  55. <label>动画类型</label>
  56. <t-select v-model="item.lineAnimateType" placeholder="水流">
  57. <t-option :key="0" :value="0" label="水流" />
  58. <t-option :key="1" :value="1" label="水珠" />
  59. <t-option :key="2" :value="2" label="圆点" />
  60. </t-select>
  61. </div>
  62. <div class="form-item mt-8">
  63. <label>运动速度</label>
  64. <t-slider
  65. class="ml-12"
  66. v-model="item.animateSpan"
  67. :show-tooltip="true"
  68. :min="1"
  69. :max="10"
  70. />
  71. </div>
  72. <div class="form-item mt-8">
  73. <label>动画颜色</label>
  74. <t-color-picker
  75. class="w-full"
  76. format="CSS"
  77. :enable-alpha="true"
  78. :recent-colors="null"
  79. :swatch-colors="defaultPureColor"
  80. :color-modes="['monochrome']"
  81. :show-primary-color-preview="false"
  82. :clearable="true"
  83. v-model="item.animateColor"
  84. />
  85. </div>
  86. <div class="form-item mt-8">
  87. <label>轨迹宽度</label>
  88. <t-input-number
  89. v-model="item.animateLineWidth"
  90. theme="column"
  91. :min="1"
  92. placeholder="默认"
  93. />
  94. </div>
  95. <div class="form-item mt-8">
  96. <label>反向流动</label>
  97. <t-switch
  98. class="ml-8 mt-8"
  99. size="small"
  100. v-model="item.animateReverse"
  101. />
  102. </div>
  103. <div class="form-item mt-8">
  104. <label>播放次数</label>
  105. <t-input-number
  106. v-model="item.animateCycle"
  107. theme="column"
  108. :min="1"
  109. placeholder="无限"
  110. title="缺省无限循环播放"
  111. />
  112. </div>
  113. <div class="form-item mt-8">
  114. <label>自动播放</label>
  115. <t-switch
  116. class="ml-8 mt-8"
  117. size="small"
  118. v-model="item.autoPlay"
  119. @change="changeAnimateAutoPlay($event, item)"
  120. />
  121. </div>
  122. <div
  123. class="form-item mt-8"
  124. title="当前动画结束后自动播放下一个对象的动画"
  125. >
  126. <label>下个动画</label>
  127. <t-tree-select
  128. v-model="item.nextAnimate"
  129. :data="penTree"
  130. filterable
  131. placeholder="无"
  132. />
  133. </div>
  134. </template>
  135. <template v-else>
  136. <div class="form-item">
  137. <label>动画类型</label>
  138. <t-select
  139. v-model="item.animate"
  140. clearable
  141. placeholder="动画"
  142. :options="animateList"
  143. @change="changeAnimate(item)"
  144. />
  145. </div>
  146. <div class="form-item mt-8">
  147. <label>播放次数</label>
  148. <t-input-number
  149. v-model="item.animateCycle"
  150. theme="column"
  151. :min="1"
  152. placeholder="无限"
  153. title="缺省无限循环播放"
  154. />
  155. </div>
  156. <div class="form-item mt-8">
  157. <label>结束状态</label>
  158. <t-select
  159. v-model="item.keepanimationstate"
  160. placeholder="初始状态"
  161. >
  162. <t-option :key="false" :value="false" label="初始状态" />
  163. <t-option :key="true" :value="true" label="当前状态" />
  164. </t-select>
  165. </div>
  166. <div class="form-item mt-8">
  167. <label>线性播放</label>
  168. <t-tooltip content="仅支持数字属性匀速线性播放" placement="top">
  169. <t-select v-model="item.linear" placeholder="是">
  170. <t-option :key="true" :value="true" label="是" />
  171. <t-option :key="false" :value="false" label="否" />
  172. </t-select>
  173. </t-tooltip>
  174. </div>
  175. <div class="form-item mt-8">
  176. <label>自动播放</label>
  177. <t-switch
  178. class="ml-8 mt-8"
  179. size="small"
  180. v-model="item.autoPlay"
  181. @change="changeAnimateAutoPlay($event, item)"
  182. />
  183. </div>
  184. <div
  185. class="form-item mt-8"
  186. title="当前动画结束后自动播放下一个对象的动画"
  187. >
  188. <label>下个动画</label>
  189. <t-tree-select
  190. v-model="item.nextAnimate"
  191. :data="penTree"
  192. filterable
  193. placeholder="无"
  194. />
  195. </div>
  196. </template>
  197. </t-collapse-panel>
  198. </t-collapse>
  199. <t-divider />
  200. <div class="p-16">
  201. <t-button class="w-full" @click="addAnimate" style="height: 30px">
  202. 添加动画
  203. </t-button>
  204. </div>
  205. </template>
  206. <div class="flex column center blank" v-else>
  207. <img src="/img/blank.png" />
  208. <div class="gray center">还没有动画</div>
  209. <div class="mt-8">
  210. <t-button @click="addAnimate" style="height: 30px">添加动画</t-button>
  211. </div>
  212. </div>
  213. </div>
  214. <AnimateFrames
  215. v-if="animate"
  216. :animate="animate"
  217. @close="animate = undefined"
  218. />
  219. </template>
  220. <script lang="ts" setup>
  221. import { onBeforeMount, ref, watch, onUnmounted } from 'vue';
  222. import { getPenTree } from '@/services/common';
  223. import { deepClone } from '@meta2d/core';
  224. import AnimateFrames from './AnimateFrames.vue';
  225. import { defaultPureColor } from '@/services/defaults';
  226. import { MessagePlugin } from 'tdesign-vue-next';
  227. const props = defineProps<{
  228. pen: any;
  229. }>();
  230. function changeAnimateAutoPlay(value, item) {
  231. if (value) {
  232. props.pen.autoPlay = true;
  233. if (props.pen.animations.length === 1) {
  234. item.autoPlay = true;
  235. return;
  236. }
  237. props.pen.animations.forEach((i) => {
  238. if (i.name !== item.name) {
  239. i.autoPlay = false;
  240. }
  241. });
  242. item.autoPlay = true;
  243. } else {
  244. let allFalse = props.pen.animations.every((i) => !i.autoPlay);
  245. if (allFalse) {
  246. props.pen.autoPlay = false;
  247. props.pen.frames = null;
  248. }
  249. }
  250. }
  251. const changeAnimateName = (event, item) => {
  252. let result = props.pen.animations.filter(
  253. (animation) => animation.name === event
  254. );
  255. if (result && result.length) {
  256. MessagePlugin.warning('已存在同名动画!');
  257. return;
  258. } else {
  259. item.name = event;
  260. }
  261. };
  262. const penTree: any = ref([]);
  263. const openedCollapses = ref([0]);
  264. const animate: any = ref(undefined);
  265. const animateList = [
  266. {
  267. label: '闪烁',
  268. value: '闪烁',
  269. data: [
  270. {
  271. visible: true,
  272. duration: 100,
  273. },
  274. {
  275. visible: false,
  276. duration: 100,
  277. },
  278. ],
  279. },
  280. {
  281. label: '缩放',
  282. value: '缩放',
  283. data: [
  284. {
  285. scale: 1.1,
  286. duration: 100,
  287. },
  288. {
  289. scale: 1,
  290. duration: 400,
  291. },
  292. ],
  293. },
  294. {
  295. label: '旋转',
  296. value: '旋转',
  297. data: [
  298. {
  299. rotate: 360,
  300. duration: 1000,
  301. },
  302. ],
  303. },
  304. {
  305. label: '上下跳动',
  306. value: '上下跳动',
  307. data: [
  308. {
  309. y: -10,
  310. duration: 100,
  311. },
  312. { y: 0, duration: 100 },
  313. { y: -10, duration: 200 },
  314. ],
  315. },
  316. {
  317. label: '左右跳动',
  318. value: '左右跳动',
  319. data: [
  320. {
  321. x: -10,
  322. duration: 100,
  323. },
  324. {
  325. x: 10,
  326. duration: 80,
  327. },
  328. {
  329. x: -10,
  330. duration: 50,
  331. },
  332. {
  333. x: 10,
  334. duration: 30,
  335. },
  336. {
  337. x: 0,
  338. duration: 300,
  339. },
  340. ],
  341. },
  342. {
  343. label: '颜色变化',
  344. value: '颜色变化',
  345. data: [
  346. { color: '#4583ff', duration: 200 },
  347. { color: '#ff4000', duration: 200 },
  348. ],
  349. },
  350. {
  351. label: '背景变化',
  352. value: '背景变化',
  353. data: [
  354. { background: '#4583ff', duration: 200 },
  355. { background: '#ff4000', duration: 200 },
  356. ],
  357. },
  358. {
  359. label: '文字变化',
  360. value: '文字变化',
  361. data: [
  362. { text: '乐吾乐', duration: 200 },
  363. { text: 'le5le', duration: 200 },
  364. ],
  365. },
  366. {
  367. label: '状态变化',
  368. value: '状态变化',
  369. data: [
  370. { showChild: 0, duration: 200 },
  371. { showChild: 1, duration: 200 },
  372. ],
  373. },
  374. {
  375. label: '翻转',
  376. value: '翻转',
  377. data: [
  378. { flipX: true, flipY: true, duration: 200 },
  379. { flipX: false, flipY: false, duration: 200 },
  380. ],
  381. },
  382. {
  383. label: '自定义',
  384. value: 'custom',
  385. data: [],
  386. },
  387. ];
  388. const isPlaying = ref(-1);
  389. onBeforeMount(() => {
  390. if (!props.pen.animations) {
  391. props.pen.animations = [];
  392. }
  393. const p = meta2d.findOne(props.pen.id);
  394. if (p?.calculative?.start) {
  395. // @ts-ignore
  396. isPlaying.value = p?.currentAnimation;
  397. }
  398. penTree.value = getPenTree();
  399. });
  400. const watcher = watch(
  401. () => props.pen.id,
  402. () => {
  403. const p = meta2d.findOne(props.pen.id);
  404. if (p?.calculative?.start) {
  405. // @ts-ignore
  406. isPlaying.value = p?.currentAnimation;
  407. } else {
  408. isPlaying.value = -1;
  409. }
  410. }
  411. );
  412. const addAnimate = () => {
  413. openedCollapses.value.push(props.pen.animations.length);
  414. props.pen.animations.push({
  415. name: '动画' + (props.pen.animations.length + 1),
  416. });
  417. };
  418. const delAnimate = (i: number) => {
  419. if (isPlaying.value === i) {
  420. meta2d.stopAnimate(props.pen.id);
  421. isPlaying.value = undefined;
  422. }
  423. props.pen.animations.splice(i, 1);
  424. animate.value = undefined;
  425. };
  426. const changeAnimate = (item: any) => {
  427. const animate: any = animateList.find((elem: any) => {
  428. return elem.value === item.animate;
  429. });
  430. if (!animate) {
  431. return;
  432. }
  433. item.frames = deepClone(animate.data);
  434. };
  435. const play = (i: number) => {
  436. meta2d.startAnimate(props.pen.id, i);
  437. isPlaying.value = i;
  438. };
  439. const stop = () => {
  440. meta2d.stopAnimate(props.pen.id);
  441. isPlaying.value = -1;
  442. };
  443. onUnmounted(() => {
  444. watcher();
  445. });
  446. </script>
  447. <style lang="postcss" scoped>
  448. .animations {
  449. height: 100%;
  450. .blank {
  451. height: 70%;
  452. img {
  453. padding: 16px;
  454. opacity: 0.9;
  455. }
  456. }
  457. :deep(.t-collapse) {
  458. .t-collapse-panel__header {
  459. .t-input {
  460. border-color: transparent;
  461. &:hover {
  462. border-color: var(--color-border-input);
  463. }
  464. }
  465. }
  466. .t-collapse-panel__icon:hover {
  467. background: none;
  468. svg {
  469. color: var(--color-primary);
  470. }
  471. }
  472. }
  473. }
  474. </style>