【发布时间】:2010-10-28 23:11:53
【问题描述】:
我正在尝试使用 JS 和 Canvas 元素制作一个简单的绘图工具。我的问题是我想要几个画布,用户应该能够在所有画布上画一条线。这是我做的一个小页面:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script type="text/javascript">
var act = null;
var context = null;
var draw = false;
var c = false;
function boot() {
$('.can')
.mouseenter(function(){
act = this;
context = act.getContext('2d');
// console.log(this);
})
.mouseleave(function(){
act = null;
context = null;
// console.log('out');
})
.mousedown(function(){
draw = true;
})
.mouseup(function(){
draw = false;
})
.mousemove(function(ev){
// console.log(act);
if (ev.layerX || ev.layerX == 0) { // Firefox
x = ev.layerX;
y = ev.layerY;
} else if (ev.offsetX || ev.offsetX == 0) { // Opera
x = ev.offsetX;
y = ev.offsetY;
}
if(draw && context != null)
if (!c) {
context.beginPath();
context.moveTo(x, y);
c = true;
} else {
context.lineTo(x, y);
context.stroke();
}
});
}
$(document).ready(boot);
</script>
<style>
.can {border: 1px solid blue; display:block; float:left; margin:0;}
</style>
</head>
<body>
<canvas class="can" id="c2" width="200" height="200"></canvas>
<canvas class="can" id="c1" width="200" height="200"></canvas>
<canvas class="can" id="c3" width="200" height="200"></canvas>
</body>
</html>
它部分有效:我只能在第一个画布中绘制。 我调试了它,我真的很困惑,因为上下文按预期更改,并且仅在第一个画布中启用了绘图。
任何想法是什么原因导致这种行为?
【问题讨论】:
-
这非常非常奇怪。我在我的 jsfiddle 上遇到了更不可能的问题。我会及时通知您我取得的任何进展。
标签: javascript jquery html canvas