What#39;s wrong with the addEventListeners(AddEventListeners有什么问题)
本文介绍了AddEventListeners有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
该向量应该能够被拉出和重新定位。啊!我有小提琴在
jsFiddle 数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
var canvas = document.getElementById('cv2'),
c = canvas.getContext('2d');
var wide = canvas.width;
var high = canvas.height;
var p0 = {
x: 50,
y: 250
};
var p1 = {
x: 250,
y: 270
};
var p2 = {
x: 250,
y: 150
};
draw();
function draw() {
c.clearRect(0, 0, canvas.width, canvas.height);
drawPoint(p0);
drawPoint(p1);
drawPoint(p2);
drawLines();
}
function drawPoint(p) {
c.beginPath();
c.lineWidth = 2;
c.arc(p.x, p.y, 10, 0, 2 * Math.PI, false);
c.stroke();
c.fill();
}
function drawLines() {
c.beginPath();
c.lineWidth = 2;
c.moveTo(p1.x, p1.y);
c.lineTo(p0.x, p0.y);
c.lineTo(p2.x, p2.y);
c.stroke();
}
canvas.addEventListener('mousedown', onMouseDown);
var dragPoint;
function findDragPoint(x, y) {
if (hitTest(p0, x, y)) return p0;
if (hitTest(p1, x, y)) return p1;
if (hitTest(p2, x, y)) return p2;
return null;
}
function onMouseDown(event) {
dragPoint = findDragPoint(event.clientX, event.clientY);
if (dragPoint) {
dragPoint.x = event.clientX;
dragPoint.y = event.clientY;
draw();
canvas.addEventListener("mousemove", onMouseMove);
canvas.addEventListener("mouseup", onMouseUp);
}
}
function onMouseMove(event) {
dragPoint.x = event.clientX;
dragPoint.y = event.cleintY;
draw();
}
function onMouseUp() {
canvas.removeEventListener("mousemove", onMouseMove);
canvas.removeEventListener("mouseup", onMouseUp);
}
function hitTest(p, x, y) {
var dx = p.x - x,
dy = p.y - y;
return Math.sqrt(dx * dx + dy * dy) <= 10;
}
<canvas id='cv2' width=800 height=500></canvas>
推荐答案
没有什么乱七八糟的东西,你只是需要更多的练习...
正如他们在注释中指出的那样,您的代码中几乎没有什么东西,您有一个拼写错误cleintY
,您还必须减去画布。偏移以获得鼠标的正确位置。
这些点应该是一个数组,这样您就可以添加更多,一切都会正常工作。
以下是您的代码正在运行
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">var canvas = document.getElementById('cv2');
canvas.addEventListener('mousedown', onMouseDown);
var c = canvas.getContext('2d');
var points = [{x:18, y:12},{x:50, y:50},{x:180, y:90},{x:250, y:50}];
var dragPoint = null;
draw();
function draw() {
c.clearRect(0, 0, canvas.width, canvas.height);
points.forEach(p => drawPoint(p));
drawLines();
}
function drawPoint(p) {
c.beginPath();
c.lineWidth = 2;
c.arc(p.x, p.y, 10, 0, 2 * Math.PI, false);
c.stroke();
c.fill();
}
function drawLines() {
c.beginPath();
c.lineWidth = 2;
points.forEach(p => c.lineTo(p.x, p.y));
c.stroke();
}
function findDragPoint(x, y) {
for (i = 0; i < points.length; i++) {
if (hitTest(points[i], x, y)) return points[i];
};
return null;
}
function onMouseDown(event) {
dragPoint = findDragPoint(event.clientX- canvas.offsetLeft, event.clientY- canvas.offsetTop);
if (dragPoint) {
dragPoint.x = event.clientX- canvas.offsetLeft;
dragPoint.y = event.clientY- canvas.offsetTop;
draw();
canvas.addEventListener("mousemove", onMouseMove);
canvas.addEventListener("mouseup", onMouseUp);
}
}
function onMouseMove(event) {
dragPoint.x = event.clientX- canvas.offsetLeft;
dragPoint.y = event.clientY- canvas.offsetTop;
draw();
}
function onMouseUp() {
canvas.removeEventListener("mousemove", onMouseMove);
canvas.removeEventListener("mouseup", onMouseUp);
}
function hitTest(p, x, y) {
var dx = p.x - x, dy = p.y - y;
return Math.sqrt(dx * dx + dy * dy) <= 10;
}
<canvas id='cv2' width=400 height=120></canvas>
这篇关于AddEventListeners有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:AddEventListeners有什么问题
基础教程推荐
猜你喜欢
- 在for循环中使用setTimeout 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 动态更新多个选择框 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01