H
H
hero5642015-10-14 17:17:24
JavaScript
hero564, 2015-10-14 17:17:24

Why doesn't ctx.drawImage work?

Hello, tell me why ctx.drawImage may not work (it just does not draw a picture), while ctx.fillRect works properly.

function Game(canv_width,canv_height){
  //set the canvas attributes
  this.width=canv_width,
  this.height=canv_height,
  //create canvas elements
  this.canvas=document.createElement("CANVAS"),
  //set canvas width
  this.canvas.width=this.width,
  //set canvas height
  this.canvas.height=this.height,
  //push created <canvas> tag in <body>
  document.body.appendChild(this.canvas),
  //get the context
  this.context=this.canvas.getContext('2d')
};

//Image object with draw function without else properties
//path-path to image file
function HImage(path){
  //create new Image and set path to file
  this.img=new Image(),
  this.img.src=path,
  //flag of loading image
  this.isLoad=false,
  
  //when image is load
  this.setToTrue=function(){
    //set flag to true
    this.isLoad=true;
  },
  this.img.onload=this.setToTrue(),
  
  //drawing image in position px,py. game_object - object of Game class
  this.draw=function(game_object,px,py){
    //if image is load
    if (this.isLoad==true){
      //draw the image in position
      //game_object.context.fillStyle='#FFCCFF';
      //game_object.context.fillRect(0,0,100,100);
      game_object.context.drawImage(this.img,px,py);
      
    }
  }
};

var game=new Game(800,600);
image=new HImage('img.png');
image.draw(game,200,200);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2015-10-15
@hero564

Working example https://jsfiddle.net/etdjojkj/

function Game(canv_width,canv_height){
  //set the canvas attributes
  this.width=canv_width,
  this.height=canv_height,
  //create canvas elements
  this.canvas=document.createElement("CANVAS"),
  //set canvas width
  this.canvas.width=this.width,
  //set canvas height
  this.canvas.height=this.height,
  //push created <canvas> tag in <body>
  document.body.appendChild(this.canvas),
  //get the context
  this.context=this.canvas.getContext('2d')
};

//Image object with draw function without else properties
//path-path to image file
function HImage(path){
  //create new Image and set path to file
  this.img=new Image();
  this.img.src=path;
  var _this = this;//Добавил тут кое-что
  //flag of loading image
  this.isLoad=false;
  //when image is load
  this.setToTrue=function(){
    //set flag to true
    this.isLoad=true;
  },
  this.img.onload=this.setToTrue();
  
  //drawing image in position px,py. game_object - object of Game class
  this.draw=function(game_object,px,py){
    //if image is load
    if (this.isLoad==true){
      //draw the image in position
      //game_object.context.fillStyle='#FFCCFF';
      //game_object.context.fillRect(0,0,100,100);
      game_object.context.drawImage(_this.img,px,py);//И тут заменил this На _this созданный ранее, так как в функции это не тот this о котором ты подумал
      
    }
  }
};

var game=new Game(800,600);
image=new HImage('http://facebookcovers.piz18.com/wp-content/uploads/2012/04/Audi-R8-Super-Car.jpg');
image.draw(game,200,200);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question