用Canvas怎样制作一个涂鸦批改的插件
Admin 2022-07-11 群英技术资讯 786 次浏览
今天这篇我们来学习和了解“用Canvas怎样制作一个涂鸦批改的插件”,下文的讲解详细,步骤过程清晰,对大家进一步学习和理解“用Canvas怎样制作一个涂鸦批改的插件”有一定的帮助。有这方面学习需要的朋友就继续往下看吧!今天公司有一个新的需求,就是要在返回的作业照片里面可以涂鸦批改,批改完后就连同批改后的照片上传到服务器。这对我不怎么熟悉canvas的人来说是个挑战。
需求分析
技术上的实现思路
在听到这需求后的第一反应就是用canvas来做,所以我在w3school阅读了 canvas的API .
1.将图片转到canvas,用到API: drawImage()
2画笔的实现
3.清除功能:讲原始的图片再次用drawImage()函数来重置
4.撤回功能:在每次按下鼠标那时候,用getImageData()函数获取当前的图像记录到数组里面,然后按撤回则使用putImageData()函数放在canvas
5.画笔的颜色:在mousemove里面改变strokeStyle笔的颜色
代码实现
移动鼠标画出线条的代码
let self = this;
this.canvasNode = document.createElement('canvas');
let styleString = this.utils.formatStyle(CANVAS_STYLE); // CANVAS_STYLE是canvas的样式
this.canvasNode.setAttribute('id','canvas');
// 一定要设置这width 和 height
let ratio = this.imgNode.width / this.imgNode.height, height = this.imgNode.height, width = this.imgNode.width;
let tempWidth , tempHeight;
// 按比例伸缩
if(ratio >= window.innerWidth / window.innerHeight){
if(width > window.innerWidth){
tempWidth = window.innerWidth;
tempHeight = height * window.innerWidth / width;
} else {
tempWidth = width;
tempHeight = height;
}
}else{
if(height > window.innerHeight){
tempWidth = width * window.innerHeight / width;
tempHeight = window.innerHeight;
}else{
tempWidth = width;
tempHeight = height;
}
}
this.canvasNode.height = tempHeight;
this.canvasNode.width = tempWidth;
styleString = Object.assign({'width': tempWidth, 'height': tempHeight}, CANVAS_STYLE);
this.canvasNode.setAttribute('style', styleString);
let ctx = this.canvasNode.getContext('2d'), startX = 0, startY = 0;
let image = new Image() ;
image.setAttribute("crossOrigin",'Anonymous')
// 加时间戳因为这图片的域名没设置跨域https://www.jianshu.com/p/c3aa975923de
image.src = this.imgNode.src + '?t=' + new Date().getTime();
image.height = tempHeight;
image.width = tempWidth;
image.onload = function(){
ctx.drawImage(image, 0, 0, tempWidth, tempHeight);
}
// 鼠标移动事件
let mousemoveFn = function(e) {
ctx.beginPath();
ctx.lineWidth = 3;
ctx.strokeStyle = self.currentColor;
if(startX == e.clientX - self.canvasNode.offsetLeft || startY === e.clientY - self.canvasNode.offsetTop ) return
ctx.moveTo(startX,startY);
ctx.lineTo(e.clientX - self.canvasNode.offsetLeft , e.clientY - self.canvasNode.offsetTop );
ctx.stroke();
startX = e.clientX - self.canvasNode.offsetLeft;
startY = e.clientY - self.canvasNode.offsetTop ; // 37是header的高度
}
// 鼠标按下事件
this.canvasNode.addEventListener("mousedown",function(e){
startX = e.clientX - self.canvasNode.offsetLeft;
startY = e.clientY - self.canvasNode.offsetTop ;
// 如果在mouseup那里记录 则在撤回时候要做多一个步骤
let imageData = ctx.getImageData(0,0, self.canvasNode.width, self.canvasNode.height);
self.imageDataArray.push(imageData); // 这imageDataArray用来记录画笔的笔画
self.canvasNode.addEventListener("mousemove", mousemoveFn, false);
},false);
this.canvasNode.addEventListener('mouseup', function(e){
self.canvasNode.removeEventListener('mousemove', mousemoveFn);
});
this.bgNode.appendChild(this.canvasNode);
遇到的问题
1.图片的跨域问题 因为这个域名只设置了192.168.6.*的跨域,所以我localhost的域名会报跨域的问题(只对192.168.6.*的跨域是同事告诉我的,不然我还在傻乎乎的查问题)
解决办法:设置vue.congfig.js文件的dev下的host
2.图片的按比例伸缩完按保存后图片的尺寸变了 我用toDataURL()方法输出的base64后的图片尺寸变了。原因:在我把图片draw上canvas上时候,用了上面代码的图片那比例伸缩的算法把图片变小了,所以画在canvas上的图片也变小了...
解决办法:(待解决)
总结
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
在前端开发中我们会经常提到渲染这个词,CSS网页渲染是浏览器工作,对于渲染简单的理解就是展示,将网站后台的数据渲染给用户看。我们能通过提升网页渲染速度来提升网页速度。
单纯用css实现切角+边框+投影+内容背景色渐变所有效果,因为UI没给背景切图,寻思这个理论上用css就能实现。看一下最终要实现的效果:首先不谈内容紫蓝色渐变,一个单纯的四切角+黑
HTML5中basecss的有关操作html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,ins,kbd,q,s,samp,sma
这篇文章给大家分享的是CSS实现下凹字体的方法。实现下凹字体并不难,文中的示例代码介绍得很详细,有需要的朋友可以参考,对新手学习CSS的使用会有帮助,接下来就跟随小编一起了解看看吧。
css3框模型有5种属性:1、width属性,设置内容的宽度;2、height属性,设置内容的高度;3、padding属性,设置内边距;4、margin属性,设置外边距;5、border属性,设置边框。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008