首页 > 其他分享 >vue.js3:分割图片为四宫格九宫格等形式([email protected])

vue.js3:分割图片为四宫格九宫格等形式([email protected])

时间:2022-11-10 18:35:19浏览次数:71  
标签:canvas vue const img 九宫格 value let 宫格

一,js代码:

<template>
<div>
  <div style="width:800px;margin: auto;display: flex;flex-direction: column;">
    <div>请选择上传图片:
      <input type="file" id="back" ref="backfile" accept="image/*" @change="handleFile" /></div>

    <div id="imgContainer" 
style="position: relative;margin-left:150px;margin-top:10px;width:500px;height:500px;overflow: hidden;background: lightgray;" > <img id="img" :src="imgSrc" style="" /> </div> <img id="img2" style="" /> <div style="margin-left:150px;"> <template v-for='(items, index) in imgList' :key="index"> <div style="font-size:0;"> <template v-for='(itemOne, indexOne) in items' :key="indexOne"> <img :src="itemOne" style="width:200px;display: block;float:left;margin-left: 5px;margin-top: 5px;" /> <!--{{itemOne}}--> </template> </div> </template> </div> <div id="dpiBtn" style="display: none;"> <input type="button" value="分割图片" @click="split" /> <input type="button" value="打包下载图片" @click="downZip" style="margin-left: 20px;" /> </div> </div> </div> </template> <script> import {ref} from "vue"; export default { name: "ImageSplit", setup() { //图片的原宽高 const imgWidth = ref(0); const imgHeight = ref(0); //图片内容 const imgSrc = ref(""); //选中的图片文件,保存在数组中 const imgFile = ref(); const roundMax = ref(0); //选中图片后的处理 const handleFile = () => { let filePaths = window.event.target.files; //清空原有缩略图 if (filePaths.length === 0) { //未选择,则返回 return } //把新选中的图片加入数组 imgFile.value = filePaths[0]; imgSrc.value = URL.createObjectURL(imgFile.value); let reader = new FileReader(); reader.readAsDataURL(imgFile.value); reader.onload = () =>{ //显示图片 imgSrc.value = reader.result; //得到宽高 let img = new Image(); img.src= reader.result; img.onload = () => { //保存宽高 imgWidth.value = img.width; imgHeight.value = img.height; if (img.width >= img.height) { roundMax.value = img.height/2; let rate = 500 / img.width; let left = (500-img.width) / 2; let top = (500 -img.height) / 2; let styleStr = 'display: inline-block;position:absolute;transform:scale('+rate+');left:'+left+'px;display: block;top:'+top+'px;'; document.getElementById('img').style = styleStr; } else { roundMax.value = img.width/2; let rate = 500 / img.height; let left = (500-img.width) / 2; let top = (500 -img.height) / 2; let styleStr = 'display: inline-block;position:absolute;transform:scale('+rate+');left:'+left+'px;display: block;top:'+top+'px;'; document.getElementById('img').style = styleStr } //显示btn document.getElementById('dpiBtn').style.display=""; } } } const imgList = ref([]); //行数 const rows = ref(2); //列数 const columns = ref(2); //拆分图片 const split = () => { var canvas = document.createElement("canvas"); canvas.width = imgWidth.value; canvas.height = imgHeight.value; let ctx = canvas.getContext('2d'); let img = document.getElementById('img'); ctx.drawImage(img, 0, 0, imgWidth.value, imgHeight.value); let list = getList(ctx); imgList.value = list; console.log(imgList.value); } //得到切割后的图片列表 const getList = (ctx) => { let destWidth = ctx.canvas.width / columns.value; let destHeight = ctx.canvas.height / rows.value; let list = []; for (let i=0;i<rows.value;i++) { let listOne = []; for (let k=0;k<columns.value;k++) { console.log("i:"+i+";k:"+k); let url = getPart(ctx,k*destWidth,i*destHeight,destWidth,destHeight); listOne.push(url); } list.push(listOne); } return list; } //得到canvas的指定部分 const getPart = (ctx,x,y,w,h) => { let canvas = document.createElement("canvas"); canvas.width = w; canvas.height = h; console.log("x:"+x+";y:"+y+";w:"+w+";h:"+h); let data = ctx.getImageData(x,y,w,h); let context = canvas.getContext("2d"); context.putImageData(data,0,0); return canvas.toDataURL("image/png",1); } return { handleFile, imgSrc, imgList, rows, columns, split, } } } </script> <style scoped> </style>

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: [email protected]

二,测试效果

原图:

2x2

3x3

三,查看vue框架的版本:

root@lhdpc:/data/vue/imgtouch# npm list vue
[email protected] /data/vue/imgtouch
├─┬ @vue/[email protected]
│ └─┬ @vue/[email protected]
│   └── [email protected] deduped
└─┬ [email protected]
  └─┬ @vue/[email protected]
    └── [email protected] deduped 

 

标签:canvas,vue,const,img,九宫格,value,let,宫格
From: https://www.cnblogs.com/architectforest/p/16878005.html

相关文章

  • idea 打开vue项目 不能识别@路径
    第一步根目录自定义一个文件:webpath.config.js,内容为:'usescript'constpath=require('path')functionresolve(dir){returnpath.join(__dirname,dir)}mod......
  • vue中rsa加密
    1.下载组件npminstall--savejsencrypt 2.在main中引入importVuetifyfrom'vuetify';import'vuetify/dist/vuetify.min.css';Vue.use(Vuetify) 3、页......
  • Vue学习笔记之使用正则表达式提示Single character alternation in regex
    0x00概述在WebStrom中使用正则表达式,工具提示Singlecharacteralternationinregex 0x01问题Vue页面需要处理多选产生的列表,["a","b","c","d"]转换成如下......
  • vue el-tree树形结构选中子节点,保持父节点选中
    <!--菜单分配弹窗--><el-dialogtitle="菜单分配":visible.sync="menuDialogVisible"width="30%"><el-tree:props="props"......
  • Vue中v-if和v-for一起使用时的优先级
     问题:Vue2.0中v-if和v-for一起使用时报错,怎么解决呢?代码和报错信息如下  原因和解决办法:  在处于同一节点的时候,v-for优先级比v-if高。这意味着v-if将分别......
  • vue3:elementPlus的dailog组件title标题位置设置
     按照官网打开弹出窗想要的是这种效果但自己出现的是下面这种,title跑中间了,body区唯一一个控件,也跑到下面了   设置el-dialog__header高度后,高度的确发生了变......
  • vue前端分页
    偷懒了,其他都是老老实实后端分页的,就这一个,前端涉及音频和三个接口的获取,之前写在一起了没分开。要是还用后端分页,要重新写,想着数据量不大,干脆在前端分页了。 分页用的e......
  • vue实现音频列表,播放互斥
    用了最暴力的方法,直接遍历。先判断是否是需要播放的音频,不是的话再判断它是否正在播放,是的话就关掉这条音频。没什么逻辑可言,但是不知道怎么控制这条音频关闭,花了点时间写......
  • vuecli之dodo底部统计功能
    footer<template><divclass="todo-footer"><label><inputtype="checkbox"/></label><span>......
  • vue修改node_modules包代码部署提交
    1、安装patch-packagepatch-package包可以通过npm进行安装。npmipatch-package--save-dev或者也可以通过yarn进行安装。yarnadd--devpatch-packagepostinsta......