【Vue】fabricjs 实现局部截图及el-image-viewer大图预览

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>使用fabric.js裁剪和显示图片</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/4.5.0/fabric.min.js"></script>

</head>
<body>
    <canvas id="canvas" ></canvas>
    <img id="sourceImage" src="YOUR URL" style="display: none;">

    <script>
        // var canvas = new fabric.Canvas('canvas');
        var canvas = new fabric.StaticCanvas("canvas");
        var sourceImage = document.getElementById('sourceImage');
        //截图位置
        const cropXywh = [821,3799,97,90]
        fabric.Image.fromURL(sourceImage.src, function(img) {
            canvas.add(img);
            var text = new fabric.Text('这里是文本描述', {
                left: 0,
                top: 0,
                fontSize: 16,
                fill: 'red'
            });
            var textWidth = text.width; // 获取文本的宽度
            var textHeight = text.height; // 获取文本的高度
            img.set({
                left: 0,
                top: textHeight,
                width: cropXywh[2],
                height: cropXywh[3],
                cropX: cropXywh[0], // 裁剪区域左上角x坐标
                cropY: cropXywh[1], // 裁剪区域左上角y坐标
                cropWidth: cropXywh[2], // 裁剪区域宽度
                cropHeight: cropXywh[3] // 裁剪区域高度
            });
            
           
            let widths = [cropXywh[2],textWidth];
            let maxWidth = Math.max(...widths);
            let maxHeight = cropXywh[3]+textHeight;
            canvas.setWidth(maxWidth);
            canvas.setHeight(maxHeight);
            canvas.add(text);
            canvas.renderAll(); // 更新Canvas
            //若使用elementui可以显示局部图放大效果
            //let imgData = canvas.toDataURL({ format: "png", quality: 0.8 });
            // imageData.value = [imgData]
            // showImageViewer.value = true
        });
    </script>
</body>
</html>

效果图:

再结合el-image-viewer可以实现大图预览局部图的效果。

html 复制代码
// 图片预览 
      <el-image-viewer
        style="z-index:1500"
        v-if="showImageViewer"
        @close="()=>{showImageViewer = false}"
        :url-list="imageData"
      >
javascript 复制代码
onMounted(() => {
      // 通过遮罩层关闭图片预览
    document.addEventListener('click',function(e){
        if(e.target.className=="el-image-viewer__mask"){
            let close = document.querySelector(".el-image-viewer__close");
            if(close){
              close.click();
              showImageViewer.value = false
            }else{
             close = document.querySelector(".el-icon-circle-close"); 
              if(close){
                close.click();
                showImageViewer.value = false
              }
            }
        }
    });
 });
相关推荐
初遇你时动了情3 小时前
css中backdrop-filter 详细使用 ios毛玻璃效果、filter和backdrop-filter使用说明
前端·css
景彡先生4 小时前
Python Selenium详解:从入门到实战,Web自动化的“瑞士军刀”
前端·python·selenium
Liudef065 小时前
DeepseekV3.2 实现构建简易版Wiki系统:从零开始的HTML实现
前端·javascript·人工智能·html
景早7 小时前
vue 记事本案例详解
前端·javascript·vue.js
wangjialelele8 小时前
Qt中的常用组件:QWidget篇
开发语言·前端·c++·qt
乔冠宇8 小时前
vue需要学习的点
前端·vue.js·学习
用户47949283569159 小时前
同样是 #,锚点和路由有什么区别
前端·javascript
Hero_11279 小时前
在pycharm中install不上需要的包
服务器·前端·pycharm
爱上妖精的尾巴9 小时前
5-26 WPS JS宏数组元素添加删除应用
开发语言·前端·javascript·wps·js宏
是谁眉眼9 小时前
wpsapi
前端·javascript·html