html上的Javascript Canvas元素问题

Javascript on html Canvas element problem

本文关键字:元素 问题 Canvas Javascript 上的 html      更新时间:2023-09-26

所以我试图创建一个方法,在该方法中,您可以在画布元素周围移动图像。这是相关的,因为在创建许多类型的游戏时,你需要一个背景图像来正确地在画布和玩家的移动中移动。问题是,您总是相对于画布左上角的(0,0(点进行绘制。因此,我要做的是在概念化中,向右按压(例如(会被认为是向右移动画布,而实际上你是在向左移动图像。可以说这是不必要的,但老实说,从另一个角度考虑这件事有点让我头疼。我认为这种将所有事物与更大的绝对场联系起来的方式更容易用大量对象进行编程。

问题是,我在Pycharm中处理了我的代码,但我一直得到画布未定义和类似的错误。请帮我把这个修好!事不宜迟,这是我的代码!(任何其他清理代码的方法都很感激,我对JS还很陌生!(

//Animates a moving black dot on the canvas.
//Variables for parameters
var gameloopId;
var speed=6;
var canvas;
var background;
var circle;
var ctx;
//Wait for document to be ready then start
$(document).ready(function(){
    console.log('document is ready');
    init();
});
//Holds the relative coordinates.
function Canvas(){
    this.x=0;//relative X
    this.y=0;//relative Y
    //Calulate screen height and width
    this.width = parseInt($("#canvas").attr("width"));
    this.height = parseInt($("#canvas").attr("height"));
}
canvas=new Canvas();
//Define an object
function Object(){
    this.absX=0;
    this.absY=0;
    this.x=this.absX-canvas.x;
    this.y=this.absY-canvas.y;
}
//Circle Object
function Circle(radius){
    this.radius=radius;
}
Circle.prototype= new Object(); //Circle is an Object
function drawCircle(){
        // Create the circle
        ctx.strokeStyle = "#000000";
        ctx.fillStyle = "#000000";
        ctx.beginPath();
        ctx.arc(circle.x,circle.y,circle.radius,0,Math.PI*2,true);
        ctx.closePath();
        ctx.stroke();
        ctx.fill();
    }
Background= Image();
Background.prototype=new Object(); //Background is an Object
background= new Background()
function drawBackground(){
        //draw the background
        ctx.drawImage(background,background.x,background.y);
    }
function init(){
    console.log('function init()');
    initSettings();

    //Insert event handler for keyboard movement of circle (space clearInterval)
    $(document).keydown(function(e){
        if(e.keyCode=='37'){    //Left key
            circle.absX+=speed;
            canvas.x+=speed;}
        if(e.keyCode=='38'){    //Up key
            circle.absY-=speed;
            canvas.y-=speed;}
        if(e.keyCode=='39'){    //Right key
            circle.absX+=speed;
            canvas.x+=speed;}
        if(e.keyCode=='40'){    //Down key
            circle.absX+=speed;
            canvas.y+=speed;}
        if(e.keyCode=='32'){    //Space Bar
            console.log('spacebar');
            clearInterval(gameloopId);
            initSettings();
            gameloopId = setInterval(gameLoop,10);
        }
    });
    $(document).keyup(function(e){
        if(e.keyCode=='37'){
        console.log('left');}//Left key
        if(e.keyCode=='38'){
        console.log('up');}//Up key
        if(e.keyCode=='39'){
        console.log('right');}//Right key
        if(e.keyCode=='40'){
        console.log('down');}//Down key
    });
    //Initialize loop of "game"
    gameloopId = setInterval(gameLoop,10);
}
function initSettings(){
    console.log('initSettings');
    //Set up canvas
    ctx = document.getElementById('canvas').getContext('2d');
    //center circle on the horizontal axis
    console.log('setting circle coords');
    circle = new Circle(15);
    circle.x = parseInt(canvas.width/2);
    circle.y = canvas.height - 40;
    //Put background at (0,0)
    background.x=0;
    background.y=0;
    background.src="http://127.0.0.1:8000/static/back.jpg";
    console.log("background width:"+background.width);
    console.log("background height:"+background.height);
}
function gameLoop(){
    //console.log('function gameLoop()');
    //Has it reached far left side?
    if(circle.x<circle.radius)
    {
        circle.x=circle.radius
    }
    //Has it reached far right side?
    if(circle.x>canvas.width - circle.radius)
    {
        circle.x=canvas.width - circle.radius
    }
    //Has it reached top?
    if(circle.y<circle.radius)
    {
        circle.y=circle.radius
    }
    //has it reached bottom?
    if(circle.y>canvas.height - circle.radius)
    {
        circle.y=canvas.height - circle.radius
    }
    //has background reached left?
    if(background.x < canvas.width-background.width)
    {
        background.x= canvas.width-background.width;
    }
    //has background reached right?
    if(background.x>0)
    {
        background.x=0;
    }
    //has background reached top?
    if(background.y < canvas.height-background.height)
    {
        background.y = canvas.height-background.height;
    }
    //has background reached bottom?
    if(background.y>0)
    {
        background.y=0;
    }
    //Clear the screen (i.e. a draw a clear rectangle the size of the screen)
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.save();
    //draw background
    drawBackground();
    // draw the circle
    drawCircle();

    ctx.restore();
}

编辑:(更新代码!(

//Animates a moving black dot on the canvas.
//Variables for parameters
var gameloopId;
var speed=6;
var canvas;
var background;
var circle;
var ctx;
//Wait for document to be ready then start
$(document).ready(function(){
    console.log('document is ready');
    init();
});
//Holds the relative coordinates.
function Canvas(){
    this.x=0;//relative X
    this.y=0;//relative Y
    //Calulate screen height and width
    this.width = parseInt($("#canvas").attr("width"));
    this.height = parseInt($("#canvas").attr("height"));
}
//Define an object
function MyObject(){
    this.absX=0;
    this.absY=0;
    this.x=this.absX-canvas.x;
    this.y=this.absY-canvas.y;
}
//Circle MyObject
function Circle(radius){
    this.radius=radius;
}
Circle.prototype= new MyObject(); //Circle is an MyObject
function drawCircle(){
        // Create the circle
        ctx.strokeStyle = "#000000";
        ctx.fillStyle = "#000000";
        ctx.beginPath();
        ctx.arc(circle.x,circle.y,circle.radius,0,Math.PI*2,true);
        ctx.closePath();
        ctx.stroke();
        ctx.fill();
    }
function Background(){
    this.img= Image();
}
Background.prototype=new MyObject(); //Background is an MyObject
function drawBackground(){
        //draw the background
        ctx.drawImage(background,background.x,background.y);
    }
function init(){
    console.log('function init()');
    initSettings();

    //Insert event handler for keyboard movement of circle (space clearInterval)
    $(document).keydown(function(e){
        if(e.keyCode=='37'){    //Left key
            circle.absX+=speed;
            canvas.x+=speed;}
        if(e.keyCode=='38'){    //Up key
            circle.absY-=speed;
            canvas.y-=speed;}
        if(e.keyCode=='39'){    //Right key
            circle.absX+=speed;
            canvas.x+=speed;}
        if(e.keyCode=='40'){    //Down key
            circle.absX+=speed;
            canvas.y+=speed;}
        if(e.keyCode=='32'){    //Space Bar
            console.log('spacebar');
            clearInterval(gameloopId);
            initSettings();
            gameloopId = setInterval(gameLoop,10);
        }
    });
    $(document).keyup(function(e){
        if(e.keyCode=='37'){
        console.log('left');}//Left key
        if(e.keyCode=='38'){
        console.log('up');}//Up key
        if(e.keyCode=='39'){
        console.log('right');}//Right key
        if(e.keyCode=='40'){
        console.log('down');}//Down key
    });
    //Initialize loop of "game"
    gameloopId = setInterval(gameLoop,10);
}
function initSettings(){
    console.log('initSettings');
    //Set up canvas
    canvas=new Canvas();
    ctx = document.getElementById('canvas').getContext('2d');
    //center circle on the horizontal axis
    console.log('setting circle coords');
    circle = new Circle(15);
    circle.x = parseInt(canvas.width/2);
    circle.y = canvas.height - 40;
    //Put background at (0,0)
    background= new Background();
    background.x=0;
    background.y=0;
    background.img.src="http://127.0.0.1:8000/static/back.jpg";
    console.log("background width:"+background.width);
    console.log("background height:"+background.height);
}
function gameLoop(){
    //console.log('function gameLoop()');
    //Has it reached far left side?
    if(circle.x<circle.radius)
    {
        circle.x=circle.radius
    }
    //Has it reached far right side?
    if(circle.x>canvas.width - circle.radius)
    {
        circle.x=canvas.width - circle.radius
    }
    //Has it reached top?
    if(circle.y<circle.radius)
    {
        circle.y=circle.radius
    }
    //has it reached bottom?
    if(circle.y>canvas.height - circle.radius)
    {
        circle.y=canvas.height - circle.radius
    }
    //has background reached left?
    if(background.x < canvas.width-background.width)
    {
        background.x= canvas.width-background.width;
    }
    //has background reached right?
    if(background.x>0)
    {
        background.x=0;
    }
    //has background reached top?
    if(background.y < canvas.height-background.height)
    {
        background.y = canvas.height-background.height;
    }
    //has background reached bottom?
    if(background.y>0)
    {
        background.y=0;
    }
    //Clear the screen (i.e. a draw a clear rectangle the size of the screen)
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.save();
    //draw background
    drawBackground();
    // draw the circle
    drawCircle();

    ctx.restore();
}

enter code here

我认为你不能编写自己的Object你绝对不能使用Object,这是一个保留关键字。对象是所有对象从中继承的内置javascript对象。你基本上已经覆盖了它。这可能是你的问题。

尝试将其称为myObject以检查是否存在此问题。

//Define an myObject
function myObject(){
    this.absX=0;
    this.absY=0;
    this.x=this.absX-canvas.x;
    this.y=this.absY-canvas.y;
}
Circle.prototype= new myObject(); //Circle is a myObject
Background= Image();
Background.prototype=new Object(); //Background is an Object
background= new Background()

看起来可疑。edit:背景是一个元素。您添加了一个原型,即使它不是一个函数。然后您调用Background作为构造函数,但它不是。

所以背景可能是不确定的。我很惊讶背景。x给你0。顺便说一句,你应该用parseInt(arg, 10)来获得十进制而不是八进制的结果。

我想通了!我的颂歌中有很多愚蠢的东西和很多错误-例如background.img是一个图像,但我一直在尝试获得background.width而不是background.img.width。我还重构了几个函数,使其更漂亮(至少对他们来说(。感谢上面的帮助!这是我的"最终"代码,至少到目前为止:

//Animates a moving black dot on the canvas.
//Variables for parameters
var gameloopId;
var speed=6;
//var canvas;
var background;
var circle;
var ctx;
//Wait for document to be ready then start
$(document).ready(function(){
    console.log('document is ready');
    init();
});
//Holds the relative coordinates.
var canvas = new function Canvas(){
    this.x=0;//relative X
    this.y=0;//relative Y
    //Calulate screen height and width
    this.width = parseInt($("#canvas").attr("width"));
    this.height = parseInt($("#canvas").attr("height"));
};
//Define an object
function MyObject(){
    this.absX=0;
    this.absY=0;
    this.x=this.absX-canvas.x;
    this.y=this.absY-canvas.y;
    this.updateplace = function (){
        this.x=this.absX-canvas.x;
        this.y=this.absY-canvas.y;
    };
}
//Circle MyObject
function Circle(radius){
    this.radius=radius;
    this.draw=function(){
        // Create the circle
        ctx.strokeStyle = "#000000";
        ctx.fillStyle = "#000000";
        ctx.beginPath();
        ctx.arc(circle.x,circle.y,circle.radius,0,Math.PI*2,true);
        ctx.closePath();
        ctx.stroke();
        ctx.fill();
    }
}
Circle.prototype= new MyObject(); //Circle is an MyObject
function Background(){
    this.img= Image();
    this.draw=function(){
        ctx.drawImage(background.img,background.x,background.y);
    }
}
Background.prototype=new MyObject(); //Background is an MyObject
function init(){
    console.log('function init()');
    initSettings();

    //Insert event handler for keyboard movement of circle (space clearInterval)
    $(document).keydown(function(e){
        if(e.keyCode=='37'){    //Left key
            circle.absX-=speed;
            canvas.x-=speed;}
        if(e.keyCode=='38'){    //Up key
            circle.absY-=speed;
            canvas.y-=speed;}
        if(e.keyCode=='39'){    //Right key
            circle.absX+=speed;
            canvas.x+=speed;}
        if(e.keyCode=='40'){    //Down key
            circle.absY+=speed;
            canvas.y+=speed;}
        if(e.keyCode=='32'){    //Space Bar
            console.log('spacebar');
            clearInterval(gameloopId);
            initSettings();
            gameloopId = setInterval(gameLoop,10);
        }
    });
    $(document).keyup(function(e){
        if(e.keyCode=='37'){
        console.log('left');}//Left key
        if(e.keyCode=='38'){
        console.log('up');}//Up key
        if(e.keyCode=='39'){
        console.log('right');}//Right key
        if(e.keyCode=='40'){
        console.log('down');}//Down key
    });
    //Initialize loop of "game"
    gameloopId = setInterval(gameLoop,10);
}
function initSettings(){
    console.log('initSettings');
    //Set up canvas
    ctx = document.getElementById('canvas').getContext('2d');
    canvas.width = parseInt($("#canvas").attr("width"));
    canvas.height = parseInt($("#canvas").attr("height"));
    //center circle on the horizontal axis
    console.log('setting circle coords');
    circle = new Circle(15);
    circle.absX = parseInt(canvas.width/2);
    circle.absY = canvas.height - 40;
    //Put background at (0,0)
    background= new Background();
    background.x=0;
    background.y=0;
    background.img.src="http://127.0.0.1:8000/static/back.jpg";
    console.log("background width:"+background.img.width);
    console.log("background height:"+background.img.height);
    console.log("Right Bound:"+(background.img.width- canvas.width))
}
function gameLoop(){
    //console.log('function gameLoop()');
    //Has it reached far left side?
    if(circle.absX<circle.radius)
    {
        circle.absX=circle.radius
    }
    //Has it reached far right side?
    if(circle.absX>background.img.width - circle.radius)
    {
        circle.absX=background.img.width - circle.radius
    }
    //Has it reached top?
    if(circle.absY<circle.radius)
    {
        circle.absY=circle.radius
    }
    //has it reached bottom?
    if(circle.absY>background.img.height - circle.radius)
    {
        circle.absY=background.img.height - circle.radius
    }
    //has canvas reached right bound?
    if(canvas.x > background.img.width- canvas.width)
    {
        canvas.x= background.img.width- canvas.width;
    }
    //has canvas reached left bound?
    if(canvas.x<0)
    {
        canvas.x=0;
    }
    //has background reached bottom bound?
    if(canvas.y > background.img.height - canvas.height)
    {
        canvas.y = background.img.height - canvas.height;
    }
    //has background reached top bound?
    if(canvas.y<0)
    {
        canvas.y=0;
    }
    //Clear the screen (i.e. a draw a clear rectangle the size of the screen)
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.save();
    //draw background
    background.updateplace();
    background.draw();
    // draw the circle
    circle.updateplace();
    circle.draw();

    ctx.restore();
}