51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
import GeometryPoint from "../geometry/GeometryPoint.js";
|
|
|
|
export default class RetroSprite
|
|
{
|
|
constructor(image, scale = 1)
|
|
{
|
|
this.image = image;
|
|
this.canvas = document.createElement('canvas');
|
|
this.context = this.canvas.getContext('2d');
|
|
this.position = new GeometryPoint();
|
|
this.scale = scale;
|
|
|
|
this.render();
|
|
}
|
|
|
|
draw(context)
|
|
{
|
|
context.drawImage(this.canvas, this.position.x, this.position.y);
|
|
}
|
|
|
|
hasRectCollisionWith(sprite)
|
|
{
|
|
|
|
}
|
|
|
|
hasCollisionWith(sprite)
|
|
{
|
|
|
|
}
|
|
|
|
render()
|
|
{
|
|
let canvasTemp = document.createElement('canvas');
|
|
let contextTemp = canvasTemp.getContext('2d');
|
|
contextTemp.drawImage(this.image, 0, 0);
|
|
|
|
this.canvas.width = this.image.width * this.scale;
|
|
this.canvas.height = this.image.height * this.scale;
|
|
|
|
this.context.clearRect(0, 0, this.image.width * this.scale, this.image.height * this.scale);
|
|
|
|
for (let y = 0; y < this.image.height; y++) {
|
|
for (let x = 0; x < this.image.width; x++) {
|
|
let pixel = contextTemp.getImageData(x, y, 1, 1).data;
|
|
this.context.globalAlpha = pixel[3] / 255;
|
|
this.context.fillStyle = 'rgb(' + pixel[0] + ',' + pixel[1] + ',' + pixel[2] + ')';
|
|
this.context.fillRect(x * this.scale, y * this.scale, this.scale, this.scale);
|
|
}
|
|
}
|
|
}
|
|
} |