canvas 自定义画布,在画布上增加(边框 图片 文字 )

先说下我的功能需求:
通过画布,自定义一个区域大小,在这个区域内:添加背景图片、图中图叠加、画内外边框、设置文字(文字的字体和大小颜色)

效果图
在这里插入图片描述
背景图片就是鸣人和雏田,右下角的属于图中图叠加效果,左下和右下都是分别画的两个区域框

不需要用到上传图片功能的,这一步可以略过,直接看下面 canvasdiv() 方法
1.右下角的小图,我做的是上传图片,上面有个附图上传的按钮,这个用的是element-ui,代码我贴下面了,也可以不选择上传,自己从本地获取一张图片也可以

<el-form-item label="附图上传" prop="dh">
     <div>
          <el-upload class="upload-demo" action="" :on-preview="handlePreview" :on-remove="handleRemove"
              :before-remove="beforeRemove" :on-change="uploadTU" :auto-upload="false" :limit="1"
              list-type="picture" :on-exceed="handleExceed" :file-list="fileList">
              <el-button size="small" type="primary">点击上传附图</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
          </el-upload>
      </div>
</el-form-item>
//附图上传调用的方法,这里主要是获取了图片的地址,
//这里这个 this.futuimage 会在做附图的时候用到,所以我这里要保存一下这个图片地址
uploadTU(file, filelist) {
   this.futuimage = file.url
},

画布上图、自定义文字、画线

1.先定义一个canvas容器

<div id="div">
    <canvas id="canvas"></canvas>
</div>

2.这个方法自己写个点击事件触发,我是用按钮触发的,按钮代码我就不写了

//图片生成pdn的方法
       canvasdiv(dataUrl) {
            var canvas = document.getElementById("canvas");
            canvas.width = 1545;
            canvas.height = 855;
            var ctx = canvas.getContext("2d");

			//这里就是背景图,我获取的是本地图片
            var image = new Image();
            //这里可以放png图片,也可以放图片的base64
            image.src = './img/beijingtu.png'; 

			//图例图片
            var tuli = new Image();
            tuli.src = './img/xiao.png';
			
			//附图图片
            var futu = new Image();
            futu.src = this.futuimage;	//这个我放的是图片的base64,主要给大家说明一下可以放这种类型数据
			
			//从这一步开始,就开始画布了,在画布里设置图片呀 文字呀 边框呀
            image.onload = function () {
                var imgwidth = 1400;	//背景图宽度
                var imgheight = 750;	//背景图高度
                var left = (canvas.width - imgwidth) / 2;
                var top = (canvas.height - imgheight) / 2;
                
				//第一个参数image就是我读取的本地png图片,这里我试过放base64 也可以
				//第二个参数left 就是 x坐标,第三个参数top就是 y坐标 ,坐标轴懂吧 x和y
				//第四个和第五个参数就是背景图的宽和高,单位是像素px
				//多说一句第一个参数 image 看你自己需求放什么,但是我发现base64图片不能自适应
                ctx.drawImage(image, left, top, imgwidth, imgheight);	//背景图上图到画布
                //外框
                ctx.moveTo(left - 20, top - 20); //将画笔移动到坐标 x和y
                ctx.lineTo(left + imgwidth + 20, top - 20); //从画笔位置绘制直线到坐标 x和y
                ctx.lineTo(left + imgwidth + 20, top + imgheight + 20); //从当前位置绘制直线到
                ctx.lineTo(left - 20, top + imgheight + 20); //.从当前位置...
                ctx.closePath(); //闭合线条
                ctx.lineWidth = 3; //线宽
                ctx.strokeStyle = 'black'; //描边颜色
                ctx.stroke(); //渲染直线(描边)

                //最内的框
                ctx.moveTo(left, top); //将画笔移动到坐标
                ctx.lineTo(left + imgwidth, top); //从画笔位置绘制直线到坐标
                ctx.lineTo(left + imgwidth, top + imgheight); //从当前位置绘制直线到
                ctx.lineTo(left, top + imgheight); // 从当前位置
                ctx.closePath(); //闭合线条
                ctx.lineWidth = 1; //线宽
                ctx.strokeStyle = 'black'; //描边颜色
                ctx.stroke(); //渲染直线(描边)
				
				//看一下效果图左下角有个图例,背景色是白色,主要是填充区域颜色需要加这一行代码
                ctx.beginPath(); //开启路径绘制,不加它无法填充区域颜色,
                ctx.moveTo(left, imgheight / 4 * 3 + top); //将画笔移动到坐标
                ctx.lineTo(left + this.tuli_width, imgheight / 4 * 3 + top); //从画笔位置绘制直线到坐标
                ctx.lineTo(left + this.tuli_width, imgheight + top); //从当前位置绘制直线到
                ctx.lineTo(left, imgheight + top); //.从当前位置
                ctx.closePath(); //闭合线条
                ctx.fillStyle = 'white';
                ctx.fill(); //设置封闭图形填充
                ctx.lineWidth = 1; //线宽
                ctx.strokeStyle = 'black'; //描边颜色
                ctx.stroke(); //渲染直线(描边)

                //附图
                ctx.beginPath(); //开启路径绘制,不加它无法填充区域颜色
                ctx.moveTo(imgwidth - 300 + left, imgheight / 4 * 3 + top); //将画笔移动到坐标
                ctx.lineTo(left + imgwidth, imgheight / 4 * 3 + top); //从画笔位置绘制直线到坐标
                ctx.lineTo(left + imgwidth, imgheight + top); //从当前位置绘制直线到
                ctx.lineTo(imgwidth - 300 + left, imgheight + top); //.从当前位置
                ctx.lineWidth = 1; //线宽
                ctx.strokeStyle = 'black'; //描边颜色
                ctx.fillStyle = 'white';
                ctx.fill(); //设置封闭图形填充
                ctx.stroke(); //渲染直线(描边)
                ctx.closePath(); //闭合线条
                ctx.drawImage(futu, imgwidth - 300 + left, imgheight / 4 * 3 + top + 20, 300, imgheight / 4 - 20); //添加附图图片

                //附图俩字下面的横线
                ctx.moveTo(imgwidth - 300 + left, imgheight / 4 * 3 + top + 25); //将画笔移动到坐标
                ctx.lineTo(left + imgwidth, imgheight / 4 * 3 + top + 25);
                ctx.lineWidth = 1; //线宽
                ctx.strokeStyle = 'black'; //描边颜色
                ctx.stroke(); //渲染直线(描边)

                // 绘制文字(文本内容,x坐标,y坐标)
                ctx.font = "13px serif";    //文字大小
                ctx.fillStyle = "black";    //文字颜色
                ctx.textAlign = "center";   //文字居中

                ctx.font = "23px 宋简体"
                ctx.fillText("标题", canvas.width / 2, canvas.height - 835);

                ctx.font = "15px 黑体"
				ctx.fillText("图例", left + 75, imgheight / 4 * 3 + top + 17);
                ctx.fillText("小图区域", imgwidth - 150 + left, imgheight / 4 * 3 + top + 17);

                ctx.font = "13px 黑体"
                ctx.fillText("自定义文字", left - 10, canvas.height - 835);
                ctx.fillText("1:50000", canvas.width / 2, canvas.height - 12);
                ctx.fillText(that.getCurrentTime(), left + imgwidth - 20, canvas.height - 12);
                ctx.fillText("XXX", left - 10, canvas.height - 12);


                //展示生成后的图片效果--第二张图
                var base64 = canvas.toDataURL();
                document.getElementById("base64Img").src = base64;

                const download = document.getElementById("download");
                var ba64 = canvas.toDataURL("image/png");

                // 点击图片下载
                // canvas.onclick = function () {
                //     //download.click();
                //     var input1 = document.getElementById("upload");

                //     if (typeof FileReader === 'undefined') {
                //         alert("抱歉,你的浏览器不支持 FileReader");
                //         input1.setAttribute('disabled', 'disabled');
                //     } else {
                //         // console.log(typeof FileReader);
                //         input1.addEventListener('change', that.readFile(), false);
                //         /*input1.addEventListener('change',function (e){
                //       // console.log(e.target.files[0]);
                //       console.log(this.files[0]);
                //       },false); */
                //     }
                // };
            }
        },
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七月吃橘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值