如何在HTML5的画布上拖动绘图
How to make drawing draggable on canvas in HTML5
我是HTML5和JavaScript的新手,我知道有很多库可以做到这一点,但我想知道是否有一种方法可以使用纯JavaScript。
我有一个画布,当画布被点击的时候,会有一个小红点出现在用户点击的地方。我想让每个点都是可拖拽的,有什么想法吗?谢谢。
下面是我的代码:
HTML:<div id="primal_plane" style="position:absolute; top:100px; left:10px;">
<canvas id="canvas_prime" onclick="drawDot('canvas_prime')" width="700" height="500"></canvas>
</div>
:
function drawDot(plane) {
var canvas = document.getElementById(plane);
var context = canvas.getContext("2d");
context.beginPath();
context.arc(mouseX * 50, mouseY * 50, 4, 0, 2 * Math.PI, false);
context.fillStyle = 'green';
context.fill();
context.lineWidth = 1;
context.strokeStyle = 'yellow';
context.stroke();
}
概述您的解决方案:
- 监听鼠标按下事件,然后(1)如果鼠标不在圆圈上,则创建一个新的圆圈;(2)如果鼠标在圆圈上,则在圆圈上开始拖动操作。
- 监听鼠标移动事件,并按鼠标自上次鼠标移动事件以来移动的距离移动所拖动的圆圈
- 监听鼠标上升事件并停止拖动操作
<!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() {
// canvas related variables
// references to canvas and its context and its position on the page
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var $canvas = $("#canvas");
var canvasOffset = $canvas.offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
var scrollX = $canvas.scrollLeft();
var scrollY = $canvas.scrollTop();
var cw = canvas.width;
var ch = canvas.height;
// flag to indicate a drag is in process
// and the last XY position that has already been processed
var isDown = false;
var lastX;
var lastY;
// the radian value of a full circle is used often, cache it
var PI2 = Math.PI * 2;
// variables relating to existing circles
var circles = [];
var stdRadius = 10;
var draggingCircle = -1;
// clear the canvas and redraw all existing circles
function drawAll() {
ctx.clearRect(0, 0, cw, ch);
for(var i=0; i<circles.length; i++){
var circle = circles[i];
ctx.beginPath();
ctx.arc(circle.x, circle.y, circle.radius, 0, PI2);
ctx.closePath();
ctx.fillStyle = circle.color;
ctx.fill();
}
}
function handleMouseDown(e) {
// tell the browser we'll handle this event
e.preventDefault();
e.stopPropagation();
// save the mouse position
// in case this becomes a drag operation
lastX = parseInt(e.clientX - offsetX);
lastY = parseInt(e.clientY - offsetY);
// hit test all existing circles
var hit = -1;
for (var i=0; i < circles.length; i++) {
var circle = circles[i];
var dx = lastX - circle.x;
var dy = lastY - circle.y;
if (dx*dx + dy*dy < circle.radius * circle.radius) {
hit = i;
}
}
// if no hits then add a circle
// if hit then set the isDown flag to start a drag
if (hit < 0) {
circles.push({x:lastX, y:lastY, radius:stdRadius, color:randomColor()});
drawAll();
} else {
draggingCircle = circles[hit];
isDown = true;
}
}
function handleMouseUp(e) {
// tell the browser we'll handle this event
e.preventDefault();
e.stopPropagation();
// stop the drag
isDown = false;
}
function handleMouseMove(e) {
// if we're not dragging, just exit
if (!isDown) { return; }
// tell the browser we'll handle this event
e.preventDefault();
e.stopPropagation();
// get the current mouse position
mouseX = parseInt(e.clientX - offsetX);
mouseY = parseInt(e.clientY - offsetY);
// calculate how far the mouse has moved
// since the last mousemove event was processed
var dx = mouseX - lastX;
var dy = mouseY - lastY;
// reset the lastX/Y to the current mouse position
lastX = mouseX;
lastY = mouseY;
// change the target circles position by the
// distance the mouse has moved since the last
// mousemove event
draggingCircle.x += dx;
draggingCircle.y += dy;
// redraw all the circles
drawAll();
}
// 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) { handleMouseUp(e); });
//////////////////////
// Utility functions
function randomColor(){
return('#' + Math.floor(Math.random()*16777215).toString(16));
}
}); // end $(function(){});
</script>
</head>
<body>
<canvas id="canvas" width=300 height=300></canvas>
</body>
</html>
您必须存储创建的点的列表,但是您必须检查点是否已经存在。如果是这样,则设置一个布尔标志,用于标识发生mousedown事件时的拖动行为,另一个标志用于标识被拖动的点。在mouseup事件中清除标志。mousemove方法应该更新存储当前鼠标位置的变量,并更新当前被拖动点的位置。使用jQuery .
相关文章:
- 调整窗口大小时,可拖动的对象会出现在容器外部
- 删除对HTML元素的拖动
- jQuery UI可排序-多连接列表拖动
- 禁用SVG拖动
- 旋转后拖动对象
- JQuery UI可拖动潜水与滚动棒到鼠标
- jsPlumb-拖动克隆而不进行复制
- 当我在节点上拖动鼠标时,我如何防止使用d3.ehavior.zoom().on(“缩放”,重绘)
- 如何使元素在可拖动元素内可单击
- 如何在使用Kinetic拖动同一对象后,在旋转动画中保持先前的偏移
- 在d3中拖动一条线
- 使元素在iframe中可拖动
- 通过拖动可以更改三维模型对象在Three.js场景中的位置
- 如何使所有绘制的(矩形、圆形、直线、多边形)都可拖动?纯JS
- 拖动开始时更改可拖动元素的大小
- 如何正确拖动jqgrid编辑表单
- 高图表可拖动的绘图线与对数轴刻度
- javascript+html5画布:在移动设备上绘图而不是拖动/滚动
- 如何在HTML5的画布上拖动绘图
- HTML 5绘图应用程序中的拖动特性