index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <template>
  2. <div id="left">
  3. <div class="tit">图案库</div>
  4. <div id="patternLib">
  5. <div class="patternWrapper" v-for="item in patternsRef">
  6. <img class="pattern" :src="item" alt="" @click="addPattern" />
  7. </div>
  8. </div>
  9. </div>
  10. <div id="center" ref="editorDomRef" :style="{ cursor }">
  11. <div class="tips">
  12. <b>添加图案</b>: 点选图案库中的图案; <b>选择编辑视口中的图案</b>: 单击图案或单击相应图层; <b>中心缩放</b>: alt+缩放操作; <b>等比缩放</b>: shift+缩放操作; <b>等量旋转</b>: shift+旋转操作; <b>删除图案</b>: Del; <b>取消变换</b>: Esc 或点击空白处;
  13. <b>确认变换</b>: Enter; <b>改变图案排列顺序</b>: 拖拽图层; <b>缩放视口</b>: 滑动鼠标滚轮; <b>平移视口</b>: 按住鼠标滚轮移动鼠标。<b>兼容性</b>: PC端
  14. </div>
  15. </div>
  16. <div id="right">
  17. <div id="effect" ref="effectDomRef"></div>
  18. <ul id="layers">
  19. <template v-for="(item, index) in layersRef" :key="item.uuid">
  20. <li class="layer" :class="{ layerActive: item.active }" :data-uuid="item.uuid" :index="index" @click="selectLayer(index)" draggable="true" @dragover.prevent @dragstart="dragstart($event, index)" @drop="drop($event, index)">
  21. <img class="thumbnail" :src="item.src" alt="" />
  22. {{ item.name }}
  23. </li>
  24. </template>
  25. </ul>
  26. </div>
  27. </template>
  28. <script setup lang="ts">
  29. import { onMounted, ref } from "vue";
  30. import { Editor } from "./Editor";
  31. import { Effector } from "./Effector";
  32. /* 模拟fetch 请求后端图案库数据 */
  33. const patternData = new Promise<string[]>((resolve) => {
  34. const arr: string[] = [];
  35. for (let i = 1; i <= 8; i++) {
  36. arr.push(`https://yxyy-pandora.oss-cn-beijing.aliyuncs.com/stamp-images/${i}.png`);
  37. }
  38. resolve(arr);
  39. });
  40. /* 图案类型 */
  41. type ImgType = {
  42. src?: string;
  43. globalCompositeOperation: GlobalCompositeOperation;
  44. };
  45. /* 模拟fetch请求后端T恤数据 */
  46. const TShirtData = new Promise<{
  47. designImgSrc: string;
  48. effectImgData: ImgType[];
  49. }>((resolve) => {
  50. resolve({
  51. /* 设计图 */
  52. designImgSrc: "https://yxyy-pandora.oss-cn-beijing.aliyuncs.com/stamp-images/design.png",
  53. /* 效果图素材 */
  54. effectImgData: [
  55. {
  56. src: "",
  57. globalCompositeOperation: "source-over",
  58. },
  59. {
  60. src: "https://yxyy-pandora.oss-cn-beijing.aliyuncs.com/stamp-images/shirt-mask.png",
  61. globalCompositeOperation: "destination-in",
  62. },
  63. {
  64. src: "https://yxyy-pandora.oss-cn-beijing.aliyuncs.com/stamp-images/shirt-shadow.png",
  65. globalCompositeOperation: "multiply",
  66. },
  67. // {
  68. // src: '',
  69. // globalCompositeOperation: 'destination-in',
  70. // },
  71. // {
  72. // src: 'https://yxyy-pandora.oss-cn-beijing.aliyuncs.com/stamp-images/shirt-origin.jpg',
  73. // globalCompositeOperation: 'destination-over',
  74. // },
  75. {
  76. src: "https://yxyy-pandora.oss-cn-beijing.aliyuncs.com/stamp-images/shirt-noPattern.png",
  77. globalCompositeOperation: "source-over",
  78. },
  79. ],
  80. });
  81. });
  82. /* 图案库 */
  83. const patternsRef = ref<string[]>([]);
  84. /* 编辑器的DOMRef */
  85. const editorDomRef = ref<HTMLDivElement>();
  86. /* 效果图的DOMRef */
  87. const effectDomRef = ref<HTMLDivElement>();
  88. /* 图案编辑器 */
  89. const editor = new Editor();
  90. const {
  91. cursor,
  92. transformControler,
  93. group: { children },
  94. group,
  95. } = editor;
  96. transformControler.addEventListener("selected", ({ obj }) => {
  97. // 更新图层选择状态
  98. activateLayer(children.length - 1 - children.indexOf(obj));
  99. });
  100. group.addEventListener("remove", ({ obj: { uuid } }) => {
  101. // 删除图层
  102. removeLayer(uuid);
  103. });
  104. /* 效果图 */
  105. const effector = new Effector();
  106. /* 渲染效果图 */
  107. editor.addEventListener("render", () => {
  108. effector.render();
  109. });
  110. /* 图层数据类型 */
  111. interface Layer {
  112. src: string;
  113. name: string;
  114. uuid: string;
  115. active: boolean;
  116. }
  117. /* 图层集合 */
  118. const layersRef = ref<Layer[]>([]);
  119. onMounted(() => {
  120. /* 图案库 */
  121. patternData.then((data) => {
  122. patternsRef.value = data;
  123. });
  124. if (!editorDomRef.value || !effectDomRef.value) {
  125. return;
  126. }
  127. /* 图案编辑器 */
  128. editor.onMounted(editorDomRef.value, effectDomRef.value);
  129. /* 效果图 */
  130. effector.onMounted(effectDomRef.value);
  131. /* T恤数据 */
  132. TShirtData.then(({ designImgSrc, effectImgData }) => {
  133. editor.setDesignImg(designImgSrc);
  134. effector.addImgs(effectImgData, editor.resultScene.canvas);
  135. });
  136. });
  137. /* 当点击图像库中的图案时,将图案添加到图案编辑器和图层中 */
  138. function addPattern({ target }: MouseEvent) {
  139. if (!(target instanceof Image)) {
  140. return;
  141. }
  142. /* 将图片添加到编辑器中 */
  143. const { uuid, name } = editor.addImg(target);
  144. /* 取消当前图层的选择 */
  145. for (let layer of layersRef.value) {
  146. if (layer.active) {
  147. layer.active = false;
  148. break;
  149. }
  150. }
  151. /* 添加图像到layersRef中 */
  152. layersRef.value.unshift({
  153. src: target.src,
  154. active: true,
  155. uuid,
  156. name,
  157. });
  158. }
  159. /* 选择图层 */
  160. function selectLayer(index: number) {
  161. const { value } = layersRef;
  162. // 激活图层
  163. activateLayer(index);
  164. // 更新图像的控制状态
  165. editor.selectImgByUUID(value[index].uuid);
  166. }
  167. /* 激活图层 */
  168. function activateLayer(index: number) {
  169. const { value } = layersRef;
  170. for (let i = 0, len = value.length; i < len; i++) {
  171. value[i].active = i === index;
  172. }
  173. }
  174. /* 删除图层 */
  175. function removeLayer(uuid: string) {
  176. const { value } = layersRef;
  177. for (let i = 0, len = value.length; i < len; i++) {
  178. if (value[i].uuid === uuid) {
  179. value.splice(i, 1);
  180. break;
  181. }
  182. }
  183. }
  184. /* 开始拖拽 */
  185. function dragstart({ dataTransfer }: DragEvent, index: number) {
  186. dataTransfer && dataTransfer.setData("index", index.toString());
  187. }
  188. /* 置入 */
  189. function drop({ dataTransfer }: DragEvent, index: number) {
  190. if (!dataTransfer) {
  191. return;
  192. }
  193. /* 激活图层 */
  194. const dragIndex = parseInt(dataTransfer.getData("index"));
  195. activateLayer(dragIndex);
  196. const { value } = layersRef;
  197. /* 选择图层 */
  198. editor.selectImgByUUID(value[dragIndex].uuid);
  199. /* 置换图层 */
  200. [value[dragIndex], value[index]] = [value[index], value[dragIndex]];
  201. /* 置换图案 */
  202. const len = value.length - 1;
  203. editor.replaceImg(len - dragIndex, len - index);
  204. }
  205. </script>
  206. <style scoped>
  207. * {
  208. box-sizing: border-box;
  209. }
  210. #left {
  211. display: flex;
  212. flex-direction: column;
  213. border-right: 1px solid #ddd;
  214. }
  215. .tit {
  216. font-weight: bold;
  217. font-size: 18px;
  218. padding: 15px 15px 6px 15px;
  219. border-bottom: 1px solid #ddd;
  220. }
  221. #patternLib {
  222. padding: 15px;
  223. width: 200px;
  224. height: 100%;
  225. overflow-y: scroll;
  226. }
  227. .patternWrapper {
  228. flex-wrap: wrap;
  229. margin: 15px 0px;
  230. }
  231. .pattern {
  232. width: 100%;
  233. cursor: pointer;
  234. transition: transform 100ms;
  235. }
  236. .pattern:hover {
  237. transform: scale(1.1);
  238. }
  239. #center {
  240. flex: 1;
  241. background-image: url("https://yxyy-pandora.oss-cn-beijing.aliyuncs.com/stamp-images/alpha_back.png");
  242. position: relative;
  243. }
  244. .tips {
  245. background-color: rgba(0, 0, 0, 0.6);
  246. color: #fff;
  247. padding: 15px;
  248. line-height: 28px;
  249. position: absolute;
  250. bottom: 0;
  251. }
  252. #right {
  253. display: flex;
  254. flex-direction: column;
  255. width: 350px;
  256. border-left: 1px solid #ddd;
  257. }
  258. #effect {
  259. background-color: antiquewhite;
  260. height: 350px;
  261. }
  262. #layers {
  263. flex: 1;
  264. padding: 0;
  265. margin: 0;
  266. overflow-y: scroll;
  267. }
  268. .layer {
  269. display: flex;
  270. align-items: center;
  271. padding: 3px;
  272. list-style: none;
  273. cursor: pointer;
  274. transition: all 100ms;
  275. border-bottom: 1px solid #ddd;
  276. line-height: 0;
  277. }
  278. .layerActive {
  279. background-color: #f5f5f5;
  280. box-shadow: inset #ccc 0 1px 2px;
  281. }
  282. .thumbnail {
  283. width: 50px;
  284. background-image: url("https://yxyy-pandora.oss-cn-beijing.aliyuncs.com/stamp-images/alpha_back.png");
  285. background-size: 8px;
  286. margin-right: 12px;
  287. }
  288. /* 滚动条样式 */
  289. ul::-webkit-scrollbar,
  290. div::-webkit-scrollbar {
  291. width: 8px;
  292. }
  293. ul::-webkit-scrollbar-thumb,
  294. div::-webkit-scrollbar-thumb {
  295. -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  296. }
  297. ul::-webkit-scrollbar-track,
  298. div::-webkit-scrollbar-track {
  299. -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
  300. }
  301. </style>