fantasy/painting/canvas.js
2018-11-19 17:09:43 +03:00

36 lines
899 B
JavaScript

import Color from "./color.js";
class Canvas {
constructor(width, height) {
this.element = document.createElement("canvas");
this.element.width = width;
this.element.height = height;
this._ctx = this.element.getContext("2d");
this._width = width;
this._height = height;
}
clear() {
this.resetStyle();
this._ctx.clearRect(0, 0, this._width, this._height);
}
setFillColor(color) {
this._ctx.fillStyle = color.rgba();
}
resetStyle() {
this._ctx.fillStyle = "";
}
draw(shape) {
this._ctx.fillStyle = shape.color.rgba();
shape.draw(this._ctx);
this.resetStyle();
}
resize(width, height) {
this.element.width = width;
this.element.height = height;
this._width = width;
this._height = height;
}
}
export default Canvas;