(精华)2020年6月28日 Canvas 元素拖动
【摘要】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<met...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>图片绘制并拖拽</title>
<style>
body,div{
margin:0;
padding:0;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="500" height="400" style="border:1px solid red;">
<p>您的系统不支持此程序!</p>
</canvas>
<a href="" id="downlink">下载图片</a>
<img src='' id="modelImg">
<script>
var canvas = document.getElementById('myCanvas');
var cansText = canvas.getContext('2d');
var img = new Image();
img.src="./images/finger.png";
img.onload = function(){
cansText.drawImage(img,50,50,150,100);
}
cansText.save();
cansText.fillStyle = "#000000";
cansText.font='15px Arial';
cansText.translate(300,350);
cansText.fillText("canvas生成图片测试",0,0);
cansText.fillText("长按保存到手机",0,30);
cansText.restore();
function dragImg(){
var moving = false; //标记是否在拖动
var movingLine = false; //涂鸦
var imgCurrent = {
x:50,
y:50
};
var dis = {};
canvas.onmousedown = function(e){
//计算鼠标落下点与被拖动的左边和上边的距离
dis={
x:e.clientX-imgCurrent.x,
y:e.clientY-imgCurrent.y
}
//判断是不是在图片上
if(dis.x>150 || dis.y>100){
return;
}
moving = true;
}
canvas.onmousemove = function(e){
if(!moving) {
return;
}
var px = e.clientX - dis.x;
var py = e.clientY - dis.y;
//不要超出边界 如何处理
// 横坐标 0 - (画布的宽度-图片的宽度)
// 纵坐标 0 - (画布的高度-图片的高度)
var movescope = {
mx:canvas.width - 150,
my:canvas.height-100
};
if(px>movescope.mx || px<0){
return;
}
if(py>movescope.my || py<0){
return;
}
cansText.clearRect(0,0,canvas.width,canvas.height);
cansText.drawImage(img,px,py,150,100);
imgCurrent= {
x:px,
y:py
}
}
canvas.onmouseup = function(){
moving = false;
movingLine = false;
}
canvas.addEventListener('mousedown',function(ev){
movingLine = true;
})
canvas.addEventListener('mousemove',function(ev){
if(!movingLine){
return;
}
// drawLine()
cansText.fillStyle = 'red';
cansText.beginPath(); //开始一条路径
cansText.arc(ev.clientX,ev.clientY,5,0,2 *Math.PI,true); //路径
cansText.fill();
})
}
dragImg();
//图片的下载 base64
document.getElementById('downlink').addEventListener('click',function(ev){
ev.preventDefault();
// this.download = 'hello.png';
this.href = canvas.toDataURL('image/png');
modelImg.src= this.href;
})
</script>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/106991778
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)