2016-11-07 7 views
0

Ich habe versucht, auf einer Leinwand mit dem neuen Ecma6 Klassensystem zu zeichnen, aber nach viel Forschung will es einfach nicht funktionieren. Ich übergebe den Kontext als Parameter der Zeichenfunktion, selbst wenn ich den Kontext logge, heißt es nicht, dass es leer oder undefiniert ist. Kann jemand von euch nette Leute mir helfen?Klasse nicht auf Leinwand zeichnen

Dies ist mein Code:

class Canvas { 
    constructor() { 
     this.canvas = document.getElementById('canvas'); 
     this.context = this.canvas.getContext('2d'); 
     this.width = this.canvas.width; 
     this.height = this.canvas.height; 
     this.components = []; 
    } 

    draw() { 
     this.context.clearRect(0, 0, this.width, this.height); 
     this.context.globalCompositeOperation = 'hard-light'; 

     this.components.map(e => e.draw(this.context)); 

     window.requestAnimationFrame(this.draw.bind(this)); 
    } 

    add(e) { 
     this.components.push(e); 

     this.components.sort(function (a, b) { 
      return a.layer - b.layer; 
     }); 
    } 

    listeners() { 
     window.addEventListener('resize',() => { 
      this.width = this.canvas.width; 
      this.height = this.canvas.height; 
     }, false); 
    } 

    init() { 
     this.listeners(); 
     this.draw(); 
    } 
} 

class CanvasElement { 
    constructor(x, y, height, width, layer) { 
     this.position = { 
      x: x, 
      y: y 
     } 
     this.height = height; 
     this.width = width; 
     this.layer = layer; 
     this.color = "grey"; 
    } 

    draw(context) { 
     context.fillStyle = this.color; 
     context.fillRect(this.x, this.y, this.width, this.height); 
    } 
} 

class CanvasImage extends CanvasElement { 
    constructor(x, y, image, context) { 
     super(x, y, image.width, image.height, 0); 
     let self = this; 
     this.image = new Image(); 
     this.image.onload = function() { 
      self.imageReadyToUse = true; 
      self.width = this.width; 
      self.height = this.height; 
     } 
     this.image.src = image; 
     this.imageReadyToUse = false; 

    } 

    draw(context) { 
     if (this.imageReadyToUse) { 
      context.drawImage(this.image, this.x, this.y); 
     } 
    } 
} 
+0

Sie sehen Fehler in der Konsole? – Stuart

+0

Es gibt keine Fehler oder Warnungen in der Konsole. –

Antwort

0

Ihre Fehler auf der Position liegt in der drawImage Methode. Ihre this.x und this.y Werte sind undefined.

Hier ist ein funktionierendes Beispiel:

var imageURL = "data:image/gif;base64,R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj8wZAKUsAOzZz+UMAOsJAP/Z2ccMDA8PD/95eX5NWvsJCOVNQPtfX/8zM8+QePLl38MGBr8JCP+zs9myn/8GBqwpAP/GxgwJCPny78lzYLgjAJ8vAP9fX/+MjMUcAN8zM/9wcM8ZGcATEL+QePdZWf/29uc/P9cmJu9MTDImIN+/r7+/vz8/P8VNQGNugV8AAF9fX8swMNgTAFlDOICAgPNSUnNWSMQ5MBAQEJE3QPIGAM9AQMqGcG9vb6MhJsEdGM8vLx8fH98AANIWAMuQeL8fABkTEPPQ0OM5OSYdGFl5jo+Pj/+pqcsTE78wMFNGQLYmID4dGPvd3UBAQJmTkP+8vH9QUK+vr8ZWSHpzcJMmILdwcLOGcHRQUHxwcK9PT9DQ0O/v70w5MLypoG8wKOuwsP/g4P/Q0IcwKEswKMl8aJ9fX2xjdOtGRs/Pz+Dg4GImIP8gIH0sKEAwKKmTiKZ8aB/f39Wsl+LFt8dgUE9PT5x5aHBwcP+AgP+WltdgYMyZfyywz78AAAAAAAD///8AAP9mZv///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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xT/QMbabI/iBCliMLEJKX2EEkomBAUCxRi42VDADxyTYDVogV+wSChqmKxEKCDAYFDFj4OmwbY7bDGdBhtrnTQYOigeChUmc1K3QTnAUfEgGFgAWt88hKA6aCRIXhxnQ1yg3BCayK44EWdkUQcBByEQChFXfCB776aQsG0BIlQgQgE8qO26X1h8cEUep8ngRBnOy74E9QgRgEAC8SvOfQkh7FDBDmS43PmGoIiKUUEGkMEC/PJHgxw0xH74yx/3XnaYRJgMB8obxQW6kL9QYEJ0FIFgByfIL7/IQAlvQwEpnAC7DtLNJCKUoO/w45c44GwCXiAFB/OXAATQryUxdN4LfFiwgjCNYg+kYMIEFkCKDs6PKAIJouyGWMS1FSKJOMRB/BoIxYJIUXFUxNwoIkEKPAgCBZSQHQ1A2EWDfDEUVLyADj5AChSIQW6gu10bE/JG2VnCZGfo4R4d0sdQoBAHhPjhIB94v/wRoRKQWGRHgrhGSQJxCS+0pCZbEhAAOw=="; 
 

 
class Canvas { 
 
     constructor() { 
 
      this.canvas = document.getElementById('canvas'); 
 
      this.context = this.canvas.getContext('2d'); 
 
      this.width = this.canvas.width; 
 
      this.height = this.canvas.height; 
 
      this.components = []; 
 
     } 
 
    
 
     draw() { 
 
      this.context.clearRect(0, 0, this.width, this.height); 
 
      this.context.globalCompositeOperation = 'hard-light'; 
 
    
 
      this.components.map(e => e.draw(this.context)); 
 
    
 
      window.requestAnimationFrame(this.draw.bind(this)); 
 
     } 
 
    
 
     add(e) { 
 
      this.components.push(e); 
 
    
 
      this.components.sort(function (a, b) { 
 
       return a.layer - b.layer; 
 
      }); 
 
     } 
 
    
 
     listeners() { 
 
      window.addEventListener('resize',() => { 
 
       this.width = this.canvas.width; 
 
       this.height = this.canvas.height; 
 
      }, false); 
 
     } 
 
    
 
     init() { 
 
      this.listeners(); 
 
      this.draw(); 
 
     } 
 
    } 
 
    
 
    class CanvasElement { 
 
     constructor(x, y, height, width, layer) { 
 
      this.position = { 
 
       x: x, 
 
       y: y 
 
      } 
 
      this.height = height; 
 
      this.width = width; 
 
      this.layer = layer; 
 
      this.color = "grey"; 
 
     } 
 
    
 
     draw(context) { 
 
      context.fillStyle = this.color; 
 
      context.fillRect(this.x, this.y, this.width, this.height); 
 
     } 
 
    } 
 
    
 
    class CanvasImage extends CanvasElement { 
 
     constructor(x, y, size, context) { 
 
      super(x, y, size.width, size.height, 0); 
 
      let self = this; 
 
      this.context = context; 
 
      this.image = new Image(); 
 
      this.image.onload = function() { 
 
       self.imageReadyToUse = true; 
 
       self.width = this.width; 
 
       self.height = this.height; 
 
       self.draw(); 
 
      } 
 
      this.image.src = imageURL; 
 
      this.imageReadyToUse = false; 
 
     } 
 
    
 
     draw() { 
 
      if (this.imageReadyToUse) { 
 
       console.log("YOU ARE UNDEFINED:", this.x, this.y); 
 
       this.context.drawImage(this.image, 0, 0, 61, 68); 
 
      } 
 
     } 
 
    } 
 

 
var canvas = new Canvas(); 
 
var canvasImage = new CanvasImage(0, 0, {width:61, height:68}, canvas.context); 
 
canvasImage.draw();
<canvas style="border:1px solid grey" id="canvas" width="400" height="400"></canvas> 
 
sdsdc

+0

Mein schlechtes nicht hinzufügen, welches Bild ist, Bild ist eine URI-Zeichenfolge. var canvasImage = new CanvasImage (0, 0, "images/BapeCat.jpg"); –

+0

Aber es muss ein Fehler, schauen Sie sich dieses: 'Super (x, y, image.width, image.Height, 0);' und 'this.image.src = Bild;' So aren Sie nicht in einer Schnur. Andernfalls würden Sie durch den Zugriff auf die Breite undefiniert werden. –

+0

Zuerst, das stimmt, das habe ich behoben. Allerdings würde es nach dem Onload trotzdem behoben werden. Aber das fillRect funktioniert auch nicht. –

0

So fand ich heraus, was das Problem war. Anstatt this.x und this.y zu verwenden, musste ich this.position.x und this.position.y verwenden.

Vielen Dank für Ihre Hilfe!