From 0ef6ec5b28f145603be208f744adb5776c33c71c Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Wed, 19 Sep 2018 18:31:21 +0200 Subject: [PATCH] Sapphire.js cleanup Make sapphire.js follow new javascript conventions. Also fix a dirty JS in base.html. --- static/js/sapphire.js | 465 ++++++++++++++++++++++++++++-------------- templates/base.html | 7 +- 2 files changed, 315 insertions(+), 157 deletions(-) diff --git a/static/js/sapphire.js b/static/js/sapphire.js index db65fdd1..4f2e6c29 100644 --- a/static/js/sapphire.js +++ b/static/js/sapphire.js @@ -21,189 +21,332 @@ // General options //===================================== // Times the canvas is refreshed a second -var FPS = 30; +let FPS = 30; // Determine the length of the trail (0=instant disappear, maximum=window.innerHeight=no disappear) -var TRAIL_TIME = 5; +let TRAIL_TIME = 5; // The color of the characters -var RAIN_COLOR = "#00F"; +let RAIN_COLOR = "#00F"; // The characters displayed -var CHARACTERS = "田由甲申甴电甶男甸甹町画甼甽甾甿畀畁畂畃畄畅畆畇畈畉畊畋界畍畎畏畐畑".split(""); +let CHARACTERS = "田由甲申甴电甶男甸甹町画甼甽甾甿畀畁畂畃畄畅畆畇畈畉畊畋界畍畎畏畐畑".split(""); // The font size used to display the characters -var FONT_SIZE = 10; +let FONT_SIZE = 10; // The maximum number of characters displayed by column -var MAX_CHAR = 7; +let MAX_CHAR = 7; -var Sapphire = function () { - var sapphire = { +let Sapphire; + +Sapphire = function () { + let sapphire = { triggerHandle: undefined, - activated: false, - runOnce: false, + activated: false, + runOnce: false, - getClass: function(elt, main, name) { elt.obj = main.getElementsByClassName(name); }, - getTag: function(elt, main, name) { elt.obj = main.getElementsByTagName(name); }, + getClass: function (elt, main, name) { + elt.obj = main.getElementsByClassName(name); + }, + getTag: function (elt, main, name) { + elt.obj = main.getElementsByTagName(name); + }, - getProp: function(elt) { - for (var i=0 ; i sapphire.canvas.height && Math.random() > 0.975) + for (let i = 0; i < sapphire.drops.length; i++) { + for (let j = 0; j < sapphire.drops[i].length; j++) { + let text = CHARACTERS[Math.floor(Math.random() * CHARACTERS.length)]; + ctx.fillText(text, i * FONT_SIZE, sapphire.drops[i][j] * FONT_SIZE); + if (sapphire.drops[i][j] * FONT_SIZE > sapphire.canvas.height && Math.random() > 0.975) sapphire.drops[i][j] = 0; sapphire.drops[i][j]++; } @@ -278,19 +425,29 @@ var Sapphire = function () { sapphire.resize(); window.addEventListener('resize', sapphire.resize); - sapphire.triggerHandle = setInterval(drawEverything, 1000/FPS); + sapphire.triggerHandle = setInterval(drawEverything, 1000 / FPS); }, - stop: function() { + stop: function () { window.removeEventListener('resize', sapphire.resize); clearInterval(sapphire.triggerHandle); sapphire.canvas.parentNode.removeChild(sapphire.canvas); }, - alterElts: function() { for (var e in sapphire.elts) { sapphire.elts[e].alter(main); } }, - revertElts: function() { for (var e in sapphire.elts) { sapphire.elts[e].revert(main); } }, + alterElts: function () { + for (let e in sapphire.elts) { + let main = document.getElementById("main"); + sapphire.elts[e].alter(main); + } + }, + revertElts: function () { + for (let e in sapphire.elts) { + let main = document.getElementById("main"); + sapphire.elts[e].revert(main); + } + }, - activate: function() { + activate: function () { if (!sapphire.runOnce) { sapphire.runOnce = true; sapphire.init(); @@ -306,11 +463,7 @@ var Sapphire = function () { sapphire.revertElts(); } } - } + }; return sapphire; -} - -var s = Sapphire(); -Konami(s.activate); - +}; \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index fc79ed8c..f6cf3c39 100644 --- a/templates/base.html +++ b/templates/base.html @@ -269,7 +269,12 @@ with this program; if not, write to the Free Software Foundation, Inc., - + +