当前位置: 首页 > news >正文

在Vue中使用Excalidraw实现在线画板

概述

Excalidraw是一个非常好用的画图板工具,但是是用React写的,本文分享一种在Vue项目中使用的方法。

效果

image.png

image.png

实现

Excalidraw简介

这篇文章(Excalidraw 完美的绘图工具:https://zhuanlan.zhihu.com/p/684940131)介绍的很全面,大家移步可以过去看看。

使用

  • 官方文档:https://docs.excalidraw.com/docs

1. 引入依赖

npm install react react-dom @excalidraw/excalidraw
# 或
yarn add react react-dom @excalidraw/excalidraw

2.添加配置

修改vite.config.js,添加如下配置:

export default defineConfig({...,define: {'process.env': {}},
})

3.页面使用

在Vue文件中的使用方式如下:

<template><div class="container"><div class="header">LZUGIS<button style="float: right" @click="save">Save</button></div><div class="excalidraw" id="excalidraw"></div><div class="footer">@lzugis 2024</div></div>
</template><script>
import { createRoot } from "react-dom/client";
import React from "react";
import { Excalidraw } from "@excalidraw/excalidraw";let root = null,app = null;export default {data() {return {};},mounted() {root = createRoot(document.getElementById("excalidraw"));const elements = JSON.parse(localStorage.getItem("excalidraw-elements"));const libs = JSON.parse(localStorage.getItem("excalidraw-libs"));const state = JSON.parse(localStorage.getItem("excalidraw-state"));const {theme,activeTool,name,scrollX,scrollY,zoom,offsetLeftm,offsetTop,} = state;root.render(React.createElement(Excalidraw, {name: "我的画板",initialData: {elements: elements,libraryItems: libs,appState: {theme,activeTool,name,scrollX,scrollY,zoom,offsetLeftm,offsetTop,},},langCode: "zh-CN",onChange: this.onChange,onLibraryChange: this.onLibraryChange,excalidrawAPI: this.excalidrawAPI,// props}));},unmounted() {root.unmount();},methods: {save() {if (app) {localStorage.setItem("excalidraw-state",JSON.stringify(app.getAppState()));localStorage.setItem("excalidraw-elements",JSON.stringify(app.getSceneElements()));}},onChange(e) {localStorage.setItem("excalidraw-elements", JSON.stringify(e));},onLibraryChange(e) {localStorage.setItem("excalidraw-libs", JSON.stringify(e));},excalidrawAPI(e) {app = e;window.app = e;},},
};
</script><style scoped lang="scss">
.container {width: 100%;height: 100vh;overflow: hidden;display: flex;flex-direction: column;.header {height: 3rem;line-height: 3rem;padding: 0 1rem;font-size: 1.2rem;background-color: #038fe5;color: white;}.footer {height: 2rem;line-height: 2rem;text-align: center;background-color: #038fe5;color: white;}.excalidraw {flex-grow: 1;}
}
</style>
http://www.lryc.cn/news/488162.html

相关文章:

  • 游戏+AI的发展历程,AI技术在游戏行业的应用有哪些?
  • Methode Electronics EDI 需求分析
  • 2023AE软件、Adobe After Effects安装步骤分享教程
  • 【前端】JavaScript 变量引用、内存与数组赋值:深入解析三种情景
  • 本地项目运行提示跨域问题
  • C++ —— string类(上)
  • React Native Mac 环境搭建
  • Python Web 开发的路径管理艺术:FastAPI 项目中的最佳实践与问题解析20241119
  • Rust derive macro(Rust #[derive])Rust派生宏
  • springboot嗨玩旅游网站
  • 杰发科技AC7840——EEP中RAM的配置
  • 从零开始的c++之旅——map_set的使用
  • Docker中的一些常用命令
  • 自存 sql常见语句和实际应用
  • python | argparse模块在命令行的使用中的重要作用
  • 【HCIP]——OSPF综合实验
  • PW系列工控电脑复制机:效率与精度双重提升
  • 学习QT第二天
  • 11.20作业
  • Ubuntu Linux使用前准备动作_使用root登录图形化界面
  • DICOM核心概念:显式 VR(Explicit VR)与隐式 VR(Implicit VR)在DICOM中的定义与区别
  • 源码分析Spring Boot (v3.3.0)
  • IPv6 NDP 记录
  • linux常用命令(文件操作)
  • 内存管理 I(内存管理的基本原理和要求、连续分配管理方式)
  • 【Redis】基于Redis实现秒杀功能
  • Hadoop 使用过程中 15 个常见问题的详细描述、解决方案
  • 【Flutter 问题系列第 84 篇】如何清除指定网络图片的缓存
  • 【UE5】使用基元数据对材质传参,从而避免新建材质实例
  • 鸿蒙动画开发07——粒子动画