Technology Sharing

[Domestic open source visualization engine Meta2d.js] Drag and drop

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Meta2d.js has listened to the drag event, supports receiving a valid element Json data, and creates an element object on the canvas.

Graphics library drag

1. Create a graphics library toolbar

Create a graphical library toolbar html element and bind drag events or touch events

  1. <div v-for="item in list" draggable="true"
  2. ondragstart="onDragstart($event, item.data)"
  3. onclick="onTouchstart($event, item.data)"
  4. ontouchstart="onTouchstart($event, item.data)">
  5. <img :src="item.svg" />
  6. </div>

Copy

2. Passing Data

  1. // 示例pen
  2. const pen = {
  3. name: "rectangle",
  4. text: "矩形",
  5. width: 100,
  6. height: 100,
  7. };
  8. // 拖拽添加图元
  9. onDragstart = (e, pen) => {
  10. e.dataTransfer.setData("Text", JSON.stringify(pen));
  11. };
  12. // 支持单击添加图元
  13. import { deepClone } from "@meta2d/core";
  14. onTouchstart = (e, pen) => {
  15. meta2d.canvas.addCaches = deepClone([pen]);
  16. };

Copy

Drag desktop picture

Method 1: uploadUrl

Front-end configuration: meta2d.store.options.uploadUrl.

Backend configuration:

  1. The attribute name of the accepted formData file must be file
  2. In the response returned by the backend, the first-level attribute must contain the url as the image preview address
  1. new Meta2d("meta2d", {
  2. uploadUrl: "/api/image",
  3. uploadHeaders: {
  4. token: "xxx"
  5. },
  6. uploadParams: {
  7. // 参数
  8. public: true,
  9. },
  10. });

Copy

Method 2: uploadFn

Configure meta2d.store.options.uploadFn. This method is a callback function, and its parameter is a file, that is, an image file. After uploading to the backend, it needs to return the accessible URL of the image.

By default, uploadFn converts the image into base64, which will cause the downloaded json file to be large and the copied image cannot be reused. It is recommended to pass the image to the backend and use the URL to access the image.

  1. new Meta2d("meta2d", {
  2. uploadFn: async (file: File) => {
  3. // 伪代码,复制后根据实际情况使用
  4. const formData = new FormData();
  5. formData.append("file", file);
  6. const res = await axios.post(url, file);
  7. return res.url;
  8. },
  9. });

Copy

Domestic open source

Lewule concentrates on research and development, is self-controlled, and continuously iterates and optimizes

Github:GitHub - le5le-com/meta2d.js: The meta2d.js is a real-time data exchange and interactive web 2D engine. Developers are able to build Web SCADA, IoT, Digital twins and so on. Meta2d.js is a real-time data response and interactive 2D engine that can be used in Web configuration, Internet of Things, digital twins and other scenarios.

Gitee: meta2d.js: The meta2d.js is a real-time data exchange and interactive web 2D engine. Developers are able to build Web SCADA, IoT, Digital twins and so on. Meta2d.js is a real-time data response and interactive 2D engine that can be used in Web configuration, Internet of Things, digital twins and other scenarios.

Welcome to Star, Fork, and blog support