javascript-如何像在油漆上一样在画布上绘制矩形?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-如何像在油漆上一样在画布上绘制矩形?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2914字,纯文字阅读大概需要5分钟。
内容图文
![javascript-如何像在油漆上一样在画布上绘制矩形?](/upload/InfoBanner/zyjiaocheng/691/4ff00463fa5640a386d5a6e478a444e7.jpg)
说我想在画布上画一个矩形.我希望能够从用户的鼠标中获取坐标.理想的情况是用户单击某个点然后向下拖动到另一端,就像我们使用绘画绘制的矩形一样.如何通过拖动鼠标来像绘制颜料一样绘制矩形? (如何获得鼠标单击并离开时的坐标?)
解决方法:
以下是如何在画布上拖动矩形的概述:
在mousedown中:
>保存鼠标的起始位置
>设置标志以指示拖动已开始
在mousemove中:
>清除上一个矩形的画布
>根据开始位置与当前鼠标位置计算矩形的宽度/高度
>从起始XY到当前鼠标位置绘制一个矩形
在mouseup中:
>清除拖动标记,因为拖动结束
这是示例代码和演示:http://jsfiddle.net/m1erickson/6E2yd/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
body{ background-color: ivory; }
#canvas{border:1px solid red;}
</style>
<script>
$(function(){
// get references to the canvas and context
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
// style the context
ctx.strokeStyle = "blue";
ctx.lineWidth=3;
// calculate where the canvas is on the window
// (used to help calculate mouseX/mouseY)
var $canvas=$("#canvas");
var canvasOffset=$canvas.offset();
var offsetX=canvasOffset.left;
var offsetY=canvasOffset.top;
var scrollX=$canvas.scrollLeft();
var scrollY=$canvas.scrollTop();
// this flage is true when the user is dragging the mouse
var isDown=false;
// these vars will hold the starting mouse position
var startX;
var startY;
function handleMouseDown(e){
e.preventDefault();
e.stopPropagation();
// save the starting x/y of the rectangle
startX=parseInt(e.clientX-offsetX);
startY=parseInt(e.clientY-offsetY);
// set a flag indicating the drag has begun
isDown=true;
}
function handleMouseUp(e){
e.preventDefault();
e.stopPropagation();
// the drag is over, clear the dragging flag
isDown=false;
}
function handleMouseOut(e){
e.preventDefault();
e.stopPropagation();
// the drag is over, clear the dragging flag
isDown=false;
}
function handleMouseMove(e){
e.preventDefault();
e.stopPropagation();
// if we're not dragging, just return
if(!isDown){return;}
// get the current mouse position
mouseX=parseInt(e.clientX-offsetX);
mouseY=parseInt(e.clientY-offsetY);
// Put your mousemove stuff here
// clear the canvas
ctx.clearRect(0,0,canvas.width,canvas.height);
// calculate the rectangle width/height based
// on starting vs current mouse position
var width=mouseX-startX;
var height=mouseY-startY;
// draw a new rect from the start position
// to the current mouse position
ctx.strokeRect(startX,startY,width,height);
}
// listen for mouse events
$("#canvas").mousedown(function(e){handleMouseDown(e);});
$("#canvas").mousemove(function(e){handleMouseMove(e);});
$("#canvas").mouseup(function(e){handleMouseUp(e);});
$("#canvas").mouseout(function(e){handleMouseOut(e);});
}); // end $(function(){});
</script>
</head>
<body>
<h4>Drag the mouse to create a rectangle</h4>
<canvas id="canvas" width=300 height=300></canvas>
</body>
</html>
>清除
内容总结
以上是互联网集市为您收集整理的javascript-如何像在油漆上一样在画布上绘制矩形?全部内容,希望文章能够帮你解决javascript-如何像在油漆上一样在画布上绘制矩形?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。