fantasy/ui/canvas.js

43 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-11-19 15:03:05 +00:00
import UI from "./ui.js";
2018-11-18 18:04:50 +00:00
2018-11-19 15:03:05 +00:00
class Canvas extends UI {
constructor(width, height) {
2018-11-19 15:03:05 +00:00
super(document.createElement("canvas"), width, height);
2018-11-20 14:30:01 +00:00
this._blurStrength = 0;
2018-11-18 18:04:50 +00:00
this._ctx = this.element.getContext("2d");
2018-11-20 14:30:01 +00:00
this._ctx.filter = "blur(" + this._blurStrength + "px)";
2018-11-19 14:09:43 +00:00
}
clear() {
this.resetStyle();
this._ctx.clearRect(0, 0, this._width, this._height);
2018-11-18 18:09:30 +00:00
}
2018-11-18 18:04:50 +00:00
setFillColor(color) {
this._ctx.fillStyle = color.rgba();
2018-11-18 18:09:30 +00:00
}
2018-11-18 18:04:50 +00:00
resetStyle() {
this._ctx.fillStyle = "";
2018-11-20 14:30:01 +00:00
this._ctx.filter = "blur(" + this._blurStrength + "px)";
2018-11-18 18:09:30 +00:00
}
2018-11-20 15:10:46 +00:00
getData() {
this.element.toDataURL();
}
2018-11-18 18:04:50 +00:00
draw(shape) {
2018-11-18 18:20:01 +00:00
this._ctx.fillStyle = shape.color.rgba();
2018-11-18 18:04:50 +00:00
shape.draw(this._ctx);
this.resetStyle();
}
2018-11-19 15:03:05 +00:00
setSize(width, height) {
super.setSize(width, height);
this.element.width = width;
this.element.height = height;
}
2018-11-20 14:30:01 +00:00
setBlurStrength(num) {
this._blurStrength = parseInt(num) || 0;
this._ctx.filter = "blur(" + this._blurStrength + "px)";
}
2018-11-18 18:04:50 +00:00
}
export default Canvas;