From 0dc789a168828ede4ad5c3e86db97c6f94d5698f Mon Sep 17 00:00:00 2001 From: Arne van Iterson Date: Tue, 24 Mar 2020 20:29:33 +0100 Subject: [PATCH] Added lightsource --- lib/Lightsource.js | 9 +++++++++ lib/index.js | 2 ++ lib/renderer/CanvasRenderer.js | 9 +++++++++ 3 files changed, 20 insertions(+) create mode 100644 lib/Lightsource.js diff --git a/lib/Lightsource.js b/lib/Lightsource.js new file mode 100644 index 0000000..43f533a --- /dev/null +++ b/lib/Lightsource.js @@ -0,0 +1,9 @@ +class Lightsource { + constructor(radius, style = { start: "rgba(255,255,255,0.5)", stop: "rgba(0,0,0,0.9)" }) { + this.pos = { x: 0, y: 0 }; + this.radius = radius; + this.style = style; + } +} + +module.exports = Lightsource; \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index a4639a7..5d974b9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,6 +8,7 @@ var AnimManager = require("./AnimManager.js"), deadInTracks = require("./movement/deadInTracks.js"), wallslide = require("./movement/wallslide.js"), Rect = require("./Rect.js"), + Lightsource = require("./Lightsource.js"), KeyControls = require("./controls/KeyControls.js"), MouseControls = require("./controls/MouseControls.js"), Sprite = require("./Sprite.js"), @@ -31,6 +32,7 @@ module.exports = { deadInTracks, wallslide, Rect, + Lightsource, KeyControls, MouseControls, Sprite, diff --git a/lib/renderer/CanvasRenderer.js b/lib/renderer/CanvasRenderer.js index 9f22141..0a91af3 100644 --- a/lib/renderer/CanvasRenderer.js +++ b/lib/renderer/CanvasRenderer.js @@ -82,6 +82,15 @@ class CanvasRenderer { } else if (child.style && child.w && child.h) { ctx.fillStyle = child.style.fill; ctx.fillRect(0, 0, child.w, child.h); + } else if (child.style && child.radius) { + var gradient = ctx.createRadialGradient(0, 0, 50, 0, 0, 50); + gradient.addColorStop(0, child.style.start); + gradient.addColorStop(1, child.style.stop); + + ctx.arc(0, 0, child.radius, 0, 2 * Math.PI); + ctx.fillStyle = gradient; + + ctx.fill(); } // Handle children with children