【发布时间】:2017-11-05 00:29:57
【问题描述】:
我在使用画布/鼠标移动时遇到问题。每当鼠标使用 mousemove 绘图/绘画工具移动时,我希望能够在整个页面上绘图,但也仍然单击出现在各种其他 div 中的文本链接。我遇到的问题是当前已修复的画布具有透明的背景颜色,并且设置为 100% 的宽度和高度以较低的 z-index 阻挡了下面的 div,这意味着无法单击链接。在画布上使用 pointer-events:none 不是解决方案,因为它会禁用鼠标移动效果。如果我使用要单击的链接使画布 z-index 低于 div,则绘图将仅出现在 div 之外。
我需要添加或更改哪些内容才能完成这项工作?我基本上只是想要一个具有鼠标悬停效果的功能网页,只要它移动就会在页面上绘制。
下面是我正在使用的脚本。这是一个例子http://jsfiddle.net/zAF4d/1/
$(function() {
var letsdraw = false;
var theCanvas = document.getElementById('paint');
var ctx = theCanvas.getContext('2d');
theCanvas.width = window.innerWidth;
theCanvas.height = window.innerHeight;
var canvasOffset = $('#paint').offset();
$('#paint').mousemove(function(e) {
if (letsdraw === true) {
ctx.lineTo(e.pageX - canvasOffset.left, e.pageY - canvasOffset.top);
ctx.stroke();
}
});
$('#paint').mousemove(function(e) {
$('.v').css('left', e.clientX + 'px');
$('.h').css('top', e.clientY + 'px');
letsdraw = true;
ctx.strokeStyle = 'blue';
ctx.lineWidth = 0.5;
ctx.lineCap = 'round';
ctx.beginPath();
ctx.moveTo(e.pageX - canvasOffset.left, e.pageY - canvasOffset.top);
});
$(window).mouseout(function(e) {
// bind to the window mouse up, that way if you mouse up and you're not over
// the canvas you'll still get the release of the drawing.
letsdraw = true;
});
});
【问题讨论】:
标签: javascript jquery canvas draw mousemove