diff --git a/fantasy.kdev4 b/fantasy.kdev4 deleted file mode 100644 index 8a8f87d..0000000 --- a/fantasy.kdev4 +++ /dev/null @@ -1,3 +0,0 @@ -[Project] -Name=fantasy -Manager=KDevGenericManager diff --git a/main.js b/main.js index da32f81..85ecfd7 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,10 @@ -import Shape from "./painting/shape.js" +import factory from "./painting/factory.js"; +import Canvas from "./painting/canvas.js"; -document.getElementById("main").innerText = "Yep"; +document.body.innerHTML = ""; -let s1 = new Shape(); +let canvas = new Canvas(); +document.body.appendChild(canvas.element); + +let shape = factory.createRandomShape(); +shape.draw(canvas); diff --git a/painting/canvas.js b/painting/canvas.js new file mode 100644 index 0000000..d7ef662 --- /dev/null +++ b/painting/canvas.js @@ -0,0 +1,23 @@ +import Color from "./color.js"; + +class Canvas { + constructor() { + this.element = document.createElement("canvas"); + this.element.width = 1000; + this.element.height = 1000; + this._ctx = this.element.getContext("2d"); + }, + 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(); + } +} + +export default Canvas; diff --git a/painting/color.js b/painting/color.js new file mode 100644 index 0000000..5758344 --- /dev/null +++ b/painting/color.js @@ -0,0 +1,38 @@ +class Color { + constructor(r, g, b, a) { + r = Math.max(0, r); + r = Math.min(1, r); + g = Math.max(0, g); + g = Math.min(1, g); + b = Math.max(0, b); + b = Math.min(1, b); + + if (a !== 0) { + a = a || 1; + } + a = Math.max(0, a); + a = Math.min(1, a); + + this._r = r; + this._g = g; + this._b = b; + this._a = a; + }, + rgba() { + let out = "rgba("; + out += Math.floor(this._r * 255); + out += ", "; + out += Math.floor(this._g * 255); + out += ", "; + out += Math.floor(this._b * 255); + out += ", "; + out += this._a; + out += ")"; + + return out; + }, + static random() { + return new Color(Math.random(), Math.random(), Math.random(), Math.random()); + } +} + diff --git a/painting/factory.js b/painting/factory.js new file mode 100644 index 0000000..98afa5d --- /dev/null +++ b/painting/factory.js @@ -0,0 +1,14 @@ +import Rectangle from "./rectangle.js"; + +const options = [ + Rectangle +] + +export default { + createRandomShape: function() { + let Shape = options[Math.floor(Math.random() * options.length)]; + + let opts = Shape.randomOptions(); + let shape = Shape.fromOptions(opts); + } +}; diff --git a/painting/rectangle.js b/painting/rectangle.js new file mode 100644 index 0000000..e0399c5 --- /dev/null +++ b/painting/rectangle.js @@ -0,0 +1,30 @@ +import Shape from "./shape.js"; + +class Rectangle extends Shape { + className: "Rectangle", + constructor(x, y, color, width, height) { + super(x, y, color); + + this._width = width; + this._height = height; + }, + static randomOptions() { + opts = super.randomOptions(); + + opts.width = Math.floor(Math.random() * maxWidth); + opts.height = Math.floor(Math.random() * maxHeight); + + return opts; + }, + static fromOptions(o) { + return new Rectangle(o.x, o.y, o.color, o.width, o.height); + }, + draw(context) { + context.fillRect(this._x, this._y, this._x + this._width, this._y + this._height); + } +} + +const maxWidth = 1000; +const maxHeight = 1000; + +export default Rectangle; diff --git a/painting/shape.js b/painting/shape.js index fadf35b..0ce37f9 100644 --- a/painting/shape.js +++ b/painting/shape.js @@ -1,7 +1,28 @@ +import Color from "./color.js"; + class Shape { - constructor() { - window.document.getElementById("main").innerHTML = "Works"; + className: "Shape", + constructor(x, y, color) { + this._x = x; + this._y = y; + this.color = color; + }, + draw(context) { + throw new Error("A pure virtual method call of a shape " + this.className); + }, + static randomOptions() { + return { + x: Math.floor(Math.random() * maxX), + y: Math.floor(Math.random() * maxY), + color: Color.random() + } + }, + static fromOptions(o) { + return new Shape(o.x, o.y, o.color); } } +const maxX = 1000; +const maxY = 1000; + export default Shape;