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

基于ueditor编辑器的功能开发之给编辑器图片增加水印功能

用户需求,双击编辑器中的图片的时候,出现弹框,用户可以选择水印缩放倍数、距离以及水印所放置的方位(当然有很多水印插件,位置大小透明度用户都能够自定义,但是用户需求如此,就自己写了)

编辑器内部已经实现了一个方法,点击图片的时候,图片四周会出现8个点点,用于拖动图片大小,找到百度编辑器注册的事件,这个事件构建了图片点击时候,8个点的html结构以及给点位注册了各种事件,点击图片之后页面会出现他们构建的dom结构,我们在此基础上去修改源码

 

 

然后再init方法中,对图片蒙版点击双击事件,打开vue的图片编辑弹框,代码如下

 

弹框打开,我们利用canvas绘制图片和图片水印功能,广播通信中已经将流媒体地址拿到,直接绘制需要添加水印的图片

    imgtocanvas(src){
      // 创建一个图片
      const img1 = document.createElement('img')
      img1.src =  src
      this.bgcsrc = src
      const canvas = document.getElementById('mergedCanvas');
      // 首先清空画布
      const ctx = canvas.getContext('2d');
      img1.onload = (e)=>{
        console.log(e,img1.naturalWidth,img1.naturalHeight,img1,'当前图片元素的信息')
        this.canvasWidth = img1.naturalWidth
        this.canvasHeight = img1.naturalHeight
        canvas.width = this.canvasWidth
        canvas.height =  this.canvasHeight
        ctx.drawImage(img1, 0, 0);
      }
    },

 绘制后,点击方位键绘制水印小图片

    handelDreation(item){
      // // 获取canvas画布
      this.currentDreation = item
      const canvas = document.getElementById('mergedCanvas');
      let canvasWidth = canvas.getAttribute('width') * 1
      let canvasHeigth = canvas.getAttribute('height') * 1
      const ctx = canvas.getContext('2d');
      const img1 = document.getElementById('img1');
      ctx.clearRect(0,0,this.canvasWidth,this.canvasHeight);
      // 重新绘制一下背景图
      ctx.drawImage(img1, 0, 0);
      let img2 = document.querySelector('.active-img') //需要绘制的水印图片
      console.log(img2.naturalWidth,img2.naturalHeight,canvasWidth,canvasHeigth,'图片原始尺寸')
      let dx = 0 //绘制横坐标
      let dy = 0 //绘制y坐标
      let dw = img2.naturalWidth / this.ruleForm.imgscale //绘制图像宽度
      let dh = img2.naturalHeight / this.ruleForm.imgscale  //绘制图像宽度
      // 每次都重新绘制一张画布
      switch(item.className){
        case 'icon-left_top': //左上
          dx = this.ruleForm.distinct
          dy = this.ruleForm.distinct
          ctx.drawImage(img2, dx, dy,dw,dh);
        // wrapCanvas.drawImage(0,0,50,50);
        break;
        case 'icon-top': //上
          dx = canvasWidth / 2 - dw / 2
          dy = this.ruleForm.distinct
          ctx.drawImage(img2, dx, dy,dw,dh);
        break;
        case 'icon-fangwei': //右上
          dx = canvasWidth - dw - this.ruleForm.distinct
          dy = this.ruleForm.distinct
          ctx.drawImage(img2, dx, dy,dw,dh);
        break;
        case 'icon-left1': //左
          dx = this.ruleForm.distinct
          dy = canvasHeigth / 2 - dh / 2
          ctx.drawImage(img2, dx, dy,dw,dh);
          break;
        case 'icon-fangwei-01': //居中
          dx = canvasWidth / 2 - dw / 2
          dy = canvasHeigth / 2 - dh / 2
          ctx.drawImage(img2, dx, dy,dw,dh);
          break;
        case 'icon-left': //右
          dx = canvasWidth - dw - this.ruleForm.distinct
          dy = canvasHeigth / 2 - dh / 2
          ctx.drawImage(img2, dx, dy,dw,dh);
          break;
        case 'icon-left-bottom': //左下
          dx = this.ruleForm.distinct
          dy = canvasHeigth  - this.ruleForm.distinct - dh
          ctx.drawImage(img2, dx, dy,dw,dh);
          break;
        case 'icon-bottom': //下
          dx = canvasWidth / 2 - dw / 2
          dy = canvasHeigth  - this.ruleForm.distinct - dh
          ctx.drawImage(img2, dx, dy,dw,dh);
          break;
        case 'icon-right_bottom': //右下
          dx = canvasWidth - dw - this.ruleForm.distinct
          dy = canvasHeigth  - this.ruleForm.distinct - dh
          ctx.drawImage(img2, dx, dy,dw,dh);
          break;
      }
    },

http://www.dtcms.com/a/122932.html

相关文章:

  • 智能防灾网:气象灾害风险管理系统的科技力量与未来蓝图
  • Mybatis的springboot项目使用
  • 【leetcode hot 100 198】打家劫舍
  • 【力扣hot100题】(074)前 K 个高频元素
  • Nginx之https重定向为http
  • 【DvAdmin】接口返回 emoji 显示 ?解决方法
  • ArkTS语言入门之接口、泛型、空安全、特殊运算符等
  • vue3的router.ts中,在children和不在的区别
  • Python 爬取 1688.item_get_factory 接口:获取工厂档案信息实战指南
  • MySQL中FIND_IN_SET函数与INSTR函数用法解析
  • 鸿蒙小案例---心情日记
  • VSCode解决中文乱码方法
  • 【c语言】深度剖析数据在内存中的存储
  • SpringAI调用硅基流动免费模型
  • 应急响应-进程排查
  • Ceph异地数据同步之-Cephfs异地同步复制
  • 【图书管理系统】全栈开发图书管理系统获取图书列表接口(后端:计算图书页数、查询当前页展示的书籍)
  • 前端三件套—HTML入门
  • 数论学习笔记:素数筛
  • 数据库事务隔离级别
  • 前端性能指标详解
  • 【Leetcode-Hot100】盛最多水的容器
  • React 响应事件
  • 如何实现文本回复Ai ChatGPT DeepSeek 式文字渐显效果?前端技术详解(附完整代码)
  • 【MySQL】安装
  • CD25.【C++ Dev】类和对象(16) static成员(上)
  • redis(2)-mysql-锁
  • 经典算法 最近点对问题
  • 猜猜乐游戏(python)
  • Trae AI 保姆级教程:从安装到调试全流程指南