joken-前端工程师

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::
  404 随笔 :: 39 文章 :: 8 评论 :: 20万 阅读

效果查看地址

https://ui.toast.com/tui-image-editor

image

github地址

https://github.com/nhn/tui.image-editor

vue 项目安装方式

yarn add @toast-ui/image-editor

vue3 使用demo

<template>
  <div class="image-editor-container">
    <!-- TUI Image Editor 容器 -->
    <div ref="editorContainer" class="editor"></div>

    <!-- 操作按钮 -->
    <div class="controls">
      <button @click="loadImage">Load Image</button>
      <button @click="exportImage">Export Image</button>
      <button @click="resetEditor">Reset</button>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref, onBeforeUnmount } from 'vue';
import '@toast-ui/image-editor/dist/toastui-image-editor.min.css';
import { ImageEditor } from '@toast-ui/image-editor';

// 引用容器元素
const editorContainer = ref(null);
let imageEditorInstance = null;

// 初始化 TUI Image Editor
onMounted(() => {
  if (editorContainer.value) {
    imageEditorInstance = new ImageEditor(editorContainer.value, {
      includeUI: {
        loadImage: {
          path: 'https://placekitten.com/600/400', // 初始加载的图片路径
          name: 'Sample Image'
        },
        theme: 'white', // 主题颜色(可选:'black' 或 'white')
        initMenu: 'filter', // 初始打开的菜单(例如:'filter', 'crop', 'draw' 等)
        menuBarPosition: 'top' // 菜单栏位置(可选:'top' 或 'bottom')
      },
      cssMaxWidth: 700, // 编辑器的最大宽度
      cssMaxHeight: 500, // 编辑器的最大高度
      usageStatistics: false // 是否启用使用统计(默认为 true)
    });

    // 监听图像加载完成事件
    imageEditorInstance.on('load.image.complete', () => {
      console.log('Image loaded successfully!');
    });

    // 监听图像导出完成事件
    imageEditorInstance.on('export.image.complete', (event) => {
      console.log('Image exported:', event.data);
    });
  }
});

// 卸载时销毁 TUI Image Editor 实例
onBeforeUnmount(() => {
  if (imageEditorInstance) {
    imageEditorInstance.destroy();
    imageEditorInstance = null;
  }
});

// 加载图像
const loadImage = () => {
  const imageUrl = prompt('Enter the URL of the image you want to load:');
  if (imageUrl) {
    imageEditorInstance.loadImageFromURL(imageUrl, 'User Image');
  }
};

// 导出图像
const exportImage = () => {
  const exportedImage = imageEditorInstance.exportImage('jpg', 0.9);
  const link = document.createElement('a');
  link.href = exportedImage;
  link.download = 'edited-image.jpg';
  link.click();
};

// 重置编辑器
const resetEditor = () => {
  imageEditorInstance.reset();
};
</script>

<style scoped>
.image-editor-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
}

.editor {
  width: 100%;
  max-width: 700px;
  margin-bottom: 20px;
}

.controls {
  display: flex;
  gap: 10px;
}

button {
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
}
</style>
posted on   joken1310  阅读(152)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示