将图像导入画布时,我遇到移动图像的问题。 我尝试了这段代码,但它不起作用。 代码只需将图像拖入画布即可,但是当我尝试移动它时,它不会移动。 任何人都可以告诉我错误在哪里。 这是代码:
<body>
<section>
<div>
<canvas id="canvas" width="300" height="200" style="border:1px solid" >
This text is displayed if your browser does not support HTML5 Canvas.
</canvas>
<input type="button" name="save" value="Drag Image" onClick="drag()"/>
</div>
<script type="text/javascript">
function drag(){
context.fillText("Drop an image onto the canvas", 50, 30);
var dx = 5;
var dy = 5;
var x = 170
var y = 100;
var img = new Image();
hasText = true,
clearCanvas = function () {
if (hasText) {context.clearRect(0, 0, canvas.width, canvas.height);
hasText = false;
}
};
img.addEventListener("load", function() {
clearCanvas();
context.drawImage(img, x, y);
}, false);
// To enable drag and drop
canvas.addEventListener("dragover", function (evt) {
evt.preventDefault();}, false);
// Handle dropped image file - only Firefox and Google Chrome
canvas.addEventListener("drop", function (evt) {
var files = evt.dataTransfer.files;
if (files.length > 0) {
var file = files[0];
if (typeof FileReader !== "undefined" && file.type.indexOf("image") != -1) {
var reader = new FileReader();
reader.onload = function (evt) {
img.src = evt.target.result;
};
reader.readAsDataURL(file);}
}
evt.preventDefault();}, false);
function doKeyDown(evt){
switch (evt.keyCode) {
case 38: /* Up arrow was pressed */
if (y - dy > 0){
y -= dy;
}
break;
case 40: /* Down arrow was pressed */
if (y + dy < c){
y += dy;
}
break;
case 37: /* Left arrow was pressed */
if (x - dx > 0){
x -= dx;
}
break;
case 39: /* Right arrow was pressed */
if (x + dx < canvas.width){
x += dx;
}
break;
}
}
function draw(){
clearCanvas();
context.drawImage(img, x, y);}
img.addEventListener('keydown',doKeyDown,true);
}
</script>
<script type="text/javascript">
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
setInterval(draw,5);
</script>
</section>
</body>
</html>
答案 0 :(得分:1)
有一些小的语法错误会导致一些错误,如果你使用的是firefox,你应该尝试使用firefox(Tools -> Error Console
)或firebug的内置javascript错误控制台或如果您使用chrome内置控制台(Tools -> Javascript console
),这应该可以帮助您调试脚本。似乎附加到画布或画布元素的keydown
事件不会触发,将keydown
事件侦听器设置为附加到窗口而不是似乎使其正常运行。此外,加载代码似乎不适用于chrome,但它可以作为一个单独的问题进行调查。
<html>
<head>
<title>Test Page</title>
</head>
<body>
<canvas id="canvas" width="300" height="200" style="border:1px solid" >
This text is displayed if your browser does not support HTML5 Canvas.
</canvas>
<input type="button" name="save" value="Drag Image" onClick="drag()"/>
<script type="text/javascript">
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
function drag(){
context.fillText("Drop an image onto the canvas", 50, 30);
}
var dx = 5;
var dy = 5;
var x = 170
var y = 100;
var img = new Image();
hasText = true;
clearCanvas = function () {
if (hasText) {
context.clearRect(0, 0, canvas.width, canvas.height);
hasText = false;
} else {
context.clearRect(0, 0, canvas.width, canvas.height);
}
};
img.addEventListener("load", function() {
clearCanvas();
context.drawImage(img, x, y);
}, false);
// To enable drag and drop
canvas.addEventListener("dragover", function (evt) {
evt.preventDefault();
}, false);
// Handle dropped image file - only Firefox and Google Chrome
canvas.addEventListener("drop", function (evt) {
var files = evt.dataTransfer.files;
if (files.length > 0) {
var file = files[0];
if (typeof FileReader !== "undefined" && file.type.indexOf("image") != -1) {
var reader = new FileReader();
reader.onload = function (evt) {
img.src = evt.target.result;
};
reader.readAsDataURL(file);
}
}
evt.preventDefault();
}, false);
function doKeyDown(evt){
console.log(evt.keyCode);
switch (evt.keyCode) {
case 38: /* Up arrow was pressed */
if (y - dy > 0){
y -= dy;
}
break;
case 40: /* Down arrow was pressed */
if (y + dy < canvas.height){
y += dy;
}
break;
case 37: /* Left arrow was pressed */
if (x - dx > 0){
x -= dx;
}
break;
case 39: /* Right arrow was pressed */
if (x + dx < canvas.width){
x += dx;
}
break;
}
draw();
}
function draw(){
clearCanvas();
context.drawImage(img, x, y);
}
window.addEventListener('keydown', doKeyDown, false);
//setInterval(draw,5);
</script>
</body>
</html>