代码在jsfiddle中工作,但在浏览器中不起作用

时间:2014-03-14 06:08:23

标签: javascript html canvas

这是我的HTML5代码。每当我尝试运行这个文件时,我会得到一个内部没有矩形的黑色画布。但它在jsfiddle中完美无缺地工作。

<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
<style>
body {
background-color:ivory;
}
#canvas {
border:10px solid red;
background-color:black;
}
 </style>
</head>
<body>
<canvas id="canvas" width="400" height="300">
</canvas>
<script type="text/javascript">
var c = document.getElementById("canvas");
var ctx = c.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 isDown = false;
var startX;
var startY;

var toggle=0;
    var x=150;
    var y=100;
    var w=100;
    var h=100;
    var wasInside=false;

ctx.fillStyle = "#04B45F";
ctx.fillRect(x, y, w, h);

function changeColor() {
if (toggle == 0) {
    ctx.fillStyle = "#FFFFFF";
    toggle = 1;
} else {
    ctx.fillStyle = "#04B45F";
    toggle = 0;
}
ctx.fillRect(x, y, w, h);
}
function handleMouseMove(e) {

e.preventDefault();
var mx = parseInt(e.clientX - offsetX);
var my = parseInt(e.clientY - offsetY);

var isInsideNow = (mx > x && mx < x + w && my > y && my <= y + h);

if (isInsideNow && !wasInside) {
    changeColor();
    wasInside = true;
} else if (!isInsideNow && wasInside) {
    wasInside = false;
}

}

$("#canvas").mousemove(function (e) {
handleMouseMove(e);
});

</script>
</body>
</html>

这里是jsfiddle http://jsfiddle.net/9GcbH/4/的链接。我在我的系统中使用确切的代码。但这对我不起作用。我可以知道确切的原因以及如何克服它吗?

3 个答案:

答案 0 :(得分:3)

您需要在远程CDN或本地

的头标记中包含js脚本
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

答案 1 :(得分:2)

您需要将jQuery代码包装在DOM ready handler $(function() {...});中。

当您包含jQuery时,jsFiddle会自动执行此任务。

$(function() {
    $("#canvas").mousemove(function (e) {
        handleMouseMove(e);
    });
});

实际上你在代码中缺少jQuery库,所以你需要包含它。所以你可以这样做:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(function() {
    $("#canvas").mousemove(function (e) {
        handleMouseMove(e);
    });
});
</script>

在关闭</body>代码之前,将上面的代码放在页面底部。

答案 2 :(得分:0)

试试这个

<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
<style>
body {
background-color:ivory;
}
#canvas {
border:10px solid red;
background-color:black;
}
 </style>
</head>
<body>
<canvas id="canvas" width="400" height="300">
</canvas>
<script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>

<script type="text/javascript">
var c = document.getElementById("canvas");
var ctx = c.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 isDown = false;
var startX;
var startY;

var toggle=0;
    var x=150;
    var y=100;
    var w=100;
    var h=100;
    var wasInside=false;

ctx.fillStyle = "#04B45F";
ctx.fillRect(x, y, w, h);

function changeColor() {
if (toggle == 0) {
    ctx.fillStyle = "#FFFFFF";
    toggle = 1;
} else {
    ctx.fillStyle = "#04B45F";
    toggle = 0;
}
ctx.fillRect(x, y, w, h);
}
function handleMouseMove(e) {

e.preventDefault();
var mx = parseInt(e.clientX - offsetX);
var my = parseInt(e.clientY - offsetY);

var isInsideNow = (mx > x && mx < x + w && my > y && my <= y + h);

if (isInsideNow && !wasInside) {
    changeColor();
    wasInside = true;
} else if (!isInsideNow && wasInside) {
    wasInside = false;
}

}

$("#canvas").mousemove(function (e) {
handleMouseMove(e);
});

</script>
</body>
</html>