按下鼠标,自由移动绘制椭圆;当鼠标松开时,绘画结束,可以进行下一次的绘画。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>画图</title>
<style>
div{
background: rgba(255,0,0,0.5);
border-radius: 50%;
position: absolute;
}
</style>
<script>
window.onload = function(){
var tempDiv;
var startX;
var startY;
document.onmousedown = function(event){
tempDiv = document.createElement('div');
startX = event.clientX;
startY = event.clientY;
tempDiv.style.left = event.clientX+'px';
tempDiv.style.top = event.clientY+'px';
document.body.appendChild(tempDiv);
}
document.onmousemove = function(event){
if(tempDiv){
tempDiv.style.width = event.clientX-startX+'px';
tempDiv.style.height = event.clientY-startY+'px';
}
}
document.onmouseup = function(){
tempDiv = null;
}
}
</script>
</head>
<body>
按下鼠标绘制,移动鼠标改变大小
</body>
</html>
绘画界面:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190722211001969.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNTUyMzkz,size_16,color_FFFFFF,t_70)