A HTML Cypher Game

This is a game to be played by two or more people.
The first person writes a word or phrase in the first textarea and clicks start.
The writing is then coded and copied into the second textarea.
It is up fo others to decode the letters in the second box.
If correct a box pops up and after a while everything is reset.

Click to try

This is the code:

<!DOCTYPE html>
<html>
<head>
<title>Cipher Game</title>
<style>
body {margin-left:0;margin-right:0;font:normal normal normal 15px Arial;background: gray}
a{ text-decoration: }:link { color: rgb(0, 0, 255) }:visited {color :rgb(100, 0,100) }:hover { }:active { }
textarea{padding: 10px;font:normal normal 700 24px Arial; background: #ffeed5; }
</style>
</head>
<body>
<center><input type=”text” id=”t1″ style=”position: relative; width:500px” value=”” placeholder=”hint” /> <input type=”button” id=”b1″ style=”position: relative” value=”Start” onclick=’cipher();’ /></center><br />
<textarea id=”ta1″ style=”position: relative; width: 100%; height: 320px” ></textarea>
<textarea id=”ta2″ style=”position: relative; width: 100%; height: 320px” onkeyup=’chk();’></textarea>
<div id=”d1″ style = “position: absolute; width: 480px; height: 80px; top: 150px; text-align: center; left: 50%; margin-left: -240px; visibility: hidden; background: #aafff8; color: red;border: 4px solid black; border-radius: 12px; font:italic normal 800 64px Georgia “>You Got It!!</div>

<script>
var lngth; var ta1 = document.getElementById(“ta1”); var ta2 = document.getElementById(“ta2”); var str1 = “”; var inc = 0; var str = “”;
ta1.focus();
function cipher() {
str = ta1.value;
inc = 20*Math.random() + 1;
for (var i=0; i < ta1.value.length; i ++) {
var let = ta1.value.substr(i,1);
var no = let.charCodeAt(0);
if (no > 96 && no < 123 ) {
no += inc;
if (no > 123) no -= 25;
}
var let2 = String.fromCharCode(no);
str1 += let2;
}
ta1.value = str1;
ta2.value = str1;
ta2.focus();
}

function chk() {
if (ta2.value == str) {
document.getElementById(“d1”).style.visibility = “visible”;
setTimeout(“rload()”,6000);
}
}

function rload() {
str1 = “”;str = “”; ta1.value = “”; ta2.value = “”; document.getElementById(“t1”).value = “”;
document.getElementById(“d1”).style.visibility = “hidden”;
ta1.focus();
}

</script>
</body></html>

There are three functions.
rload() resets the variables and hides the popup:
function rload() {
str1 = “”;str = “”; ta1.value = “”; ta2.value = “”; document.getElementById(“t1”).value = “”;
document.getElementById(“d1”).style.visibility = “hidden”;
ta1.focus();
}

chk() displays the popup and starts a timer to reload:
function chk() {
if (ta2.value == str) {
document.getElementById(“d1”).style.visibility = “visible”;
setTimeout(“rload()”,6000);
}
}

cipher() codes the text and copies the coded text into the second textarea:
function cipher() {
str = ta1.value;
inc = 20*Math.random() + 1;
for (var i=0; i < ta1.value.length; i ++) {
var let = ta1.value.substr(i,1);
var no = let.charCodeAt(0);
if (no > 96 && no < 123 ) {
no += inc;
if (no > 123) no -= 25;
}
var let2 = String.fromCharCode(no);
str1 += let2;
}
ta1.value = str1;
ta2.value = str1;
ta2.focus();
}

Get Character at Selected Point

This is a very simple app that selects the character before the cursor.
This is an example:

CursorPosText

Click to try

This is the code:

<html>
<body>
<input type=text id=t1 value=abcd>
<button onclick=”f1(document.getElementById(‘t1’))”>check position</button>
<script>
function f1(el) {
var val = el.value;
var pos = el.selectionStart;
alert(val.substr(pos-1,1));
}
</script>

</body>
</html>

The selection is done by the function f1(el), where el is the id of the text element:
var val = el.value;
var pos = el.selectionStart;

pos gives the location of the cursor.

The character ids obtained by the following:
val.substr(pos-1,1)

HTML Virtual Keyboard

This is a virtual keyboard which can insert non English characters. This example only has a Greek keyboard, but a future one will be more comprehensive.
This is a example:

GreekKeyboardText

Click to try

It consists of a js file with most of the code and a minimal html file.

This is the html which sets up a table to hold the virtual keyboard, as well as a textarea to enter tghe documdent:

<!DOCTYPE html>
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head profile=”http://gmpg.org/xfn/11″&gt;
<title>Virtual Keyboard</title>
<style>
body {margin-left:0;margin-right:0;font:normal normal normal 15px Arial;}
a{ text-decoration: }:link { color: rgb(0, 0, 255) }:visited {color :rgb(100, 0,100) }:hover { }:active { }
#tb1{position: absolute; width: 100%; border: 1px solid;}
#ta2{position: relative; width: 100%; height: 600px; font:normal normal normal 24px Arial; background: white;}
</style>
</head>
<body>
<center>
<select id = “s1” onchange=’ini();’><option>Symbol</option><option>Greek</option></select></center><br />
<div id=”pad” style = “white-space: pre-wrap;”></div>
<textarea id=”ta2″ ondblclick=’exitFullscreen(); document.getElementById(“s1”).value = “Symbol”;’> </textarea>

<table id= “tb1” ></table>
http://GreekKeyboard.js
</body></html>

This is the js file:

var sg = [“&alpha;”,”&beta;”,”&gamma;”,”&delta;”,”&epsilon;”,”&zeta;”,”&eta;”,”&theta;”,”&iota;”,”&kappa;”,”&lambda;”,”&mu;”,”&nu;”,”&xi;”,”&omicron;”,”&pi;”,”&rho;”,”&sigma;”,”&tau;”,”&upsilon;”,”&phi;”,”&chi;”,”&psi;”,”&omega;”];
var lg = [“&Alpha;”,”&Beta;”,”&Gamma;”,”&Delta;”,”&Epsilon;”,”&Zeta;”,”&Eta;”,”&Theta;”,”&iota;”,”&Kappa;”,”&Lambda;”,”&Mu;”,”&Nu;”,”&Xi;”,”&Omicron;”,”&Pi;”,”&Rho;”,”&Sigma;”,”&Tau;”,”&Upsilon;”,”&Phi;”,”&Chi;”,”&Psi;”,”&Omega;”];
var str;

var tbl = document.getElementById(“tb1”); var ta = document.getElementById(“ta2”);

function ini() {
tbl.innerHTML = “”;
str = “”;
tbl.style.height = “50px”;
tbl.style.top = (screen.height – 130) + “px”;
str= ‘<tr>’;
if (document.getElementById(“s1”).value == “Greek”) {
for (var i = 0; i < sg.length; i ++) {
str += ‘<td id = “td’ + i + ‘” style=”font-size: 24px” valign = “top” onclick=\’insrt(this.id);\’>’ + sg[i] + ‘</td>’;
}
str += ‘</tr><tr>’;
for (var i = 0; i < lg.length; i ++) {
str += ‘<td id = “td’ + (i + sg.length) + ‘” style=”font-size: 24px” valign = “top” onclick=\’insrt(this.id);\’>’ + lg[i] + ‘</td>’;
}
}

str += ‘</tr>’;
tbl.innerHTML = str;
ta.focus();
}

 

function insrt(par) {
var selectionText = ta.value.substr(ta.selectionStart, ta.selectionEnd – ta.selectionStart);
var beginningText = ta.value.substr(0, ta.selectionStart);
var remainingText = ta.value.substr(ta.selectionEnd, ta.value.length – ta.selectionEnd);
ta2.value = beginningText + document.getElementById(par).innerHTML + selectionText + remainingText;
ta.focus();
}

The characters are held in arrays sg and lg.

The function ini() creates the virtual keyboard:
function ini() {
tbl.innerHTML = “”;
str = “”;
tbl.style.height = “50px”;
tbl.style.top = (screen.height – 130) + “px”;
str= ‘<tr>’;
if (document.getElementById(“s1”).value == “Greek”) {
for (var i = 0; i < sg.length; i ++) {
str += ‘<td id = “td’ + i + ‘” style=”font-size: 24px” valign = “top” onclick=\’insrt(this.id);\’>’ + sg[i] + ‘</td>’;
}
str += ‘</tr><tr>’;
for (var i = 0; i < lg.length; i ++) {
str += ‘<td id = “td’ + (i + sg.length) + ‘” style=”font-size: 24px” valign = “top” onclick=\’insrt(this.id);\’>’ + lg[i] + ‘</td>’;
}
}

str += ‘</tr>’;
tbl.innerHTML = str;
ta.focus();
}

The elements of the arrays are added to the table cells:
str= ‘<tr>’;
if (document.getElementById(“s1”).value == “Greek”) {
for (var i = 0; i < sg.length; i ++) {
str += ‘<td id = “td’ + i + ‘” style=”font-size: 24px” valign = “top” onclick=\’insrt(this.id);\’>’ + sg[i] + ‘</td>’;
}
str += ‘</tr><tr>’;
for (var i = 0; i < lg.length; i ++) {
str += ‘<td id = “td’ + (i + sg.length) + ‘” style=”font-size: 24px” valign = “top” onclick=\’insrt(this.id);\’>’ + lg[i] + ‘</td>’;
}

The function insrt(par) places the Greek characters in the texarea:
function insrt(par) {
var selectionText = ta.value.substr(ta.selectionStart, ta.selectionEnd – ta.selectionStart);
var beginningText = ta.value.substr(0, ta.selectionStart);
var remainingText = ta.value.substr(ta.selectionEnd, ta.value.length – ta.selectionEnd);
ta2.value = beginningText + document.getElementById(par).innerHTML + selectionText + remainingText;
ta.focus();
}

A Complete Horizontal Bar Graph Generator

This is a complete horizontal bar generator. Here is an example:

hbar2Text

It works with lists of bar sizes, labels, and colors. Colors can be chosen from an input, entered directly by name, or entered from a complete list od named colors. To see how the colors look before entering, the chosen color is viewed in a small box, which when clicked enters gthe value.
The chart can be saved to a file and also saved to local storage, so it can be restored and added to when needed.

This is the code;

<!DOCTYPE html>
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head profile=”http://gmpg.org/xfn/11″&gt;
<title>Horizontal Bar Graph</title>
<style>
body {margin-left:0;margin-right:0;font:normal normal normal 15px Arial;background:#dddddd}
a({) text-decoration: }:link ({) color: rgb(0, 0, 255) }:visited ({)color :rgb(100, 0,100) }:hover ({) }:active ({) }
#tb1{position: relative; top: 20px; border-collapse:collapse}
td{position: relative; vertical-align: bottom; height:30px}
#td1{font-weight: 600; text-align: right}
#b3{position relative; width: 20px; height:20px;background:cyan;border:1px solid}
</style>
</head>
<body>
<center><input type=”text” id=”t2″ style=”width: 32%” value=”” placeholder=”Value as Percent” /><input type=”text” id=”t3″ style=”width: 33%” value=”” placeholder=”Text” /><input type=”text” id=”t4″ style=”width: 32%” value=”” placeholder=”Color” /><br />
<center><select id = “s1″ onclick=’makeColor();’><option>Choose Color</option><option>AliceBlue</option><option>AntiqueWhite</option><option>Aqua</option><option>Aquamarine</option><option>Azure</option><option>Beige</option><option>Bisque</option><option>Black</option><option>BlanchedAlmond</option><option>Blue</option><option>BlueViolet</option><option>Brown</option><option>BurlyWood</option><option>CadetBlue</option><option>Chartreuse</option><option>Chocolate</option><option>Coral</option><option>CornflowerBlue</option><option>Cornsilk</option><option>Crimson</option><option>Cyan</option><option>DarkBlue</option><option>DarkCyan</option><option>DarkGoldenRod</option><option>DarkGray</option><option>DarkGrey</option><option>DarkGreen</option><option>DarkKhaki</option><option>DarkMagenta</option><option>DarkOliveGreen</option><option>DarkOrange</option><option>DarkOrchid</option><option>DarkRed</option><option>DarkSalmon</option><option>DarkSeaGreen</option><option>DarkSlateBlue</option><option>DarkSlateGray</option><option>DarkSlateGrey</option><option>DarkTurquoise</option><option>DarkViolet</option><option>DeepPink</option><option>DeepSkyBlue</option><option>DimGray</option><option>DimGrey</option><option>DodgerBlue</option><option>FireBrick</option><option>FloralWhite</option><option>ForestGreen</option><option>Fuchsia</option><option>Gainsboro</option><option>GhostWhite</option><option>Gold</option><option>GoldenRod</option><option>Gray</option><option>Grey</option><option>Green</option><option>GreenYellow</option><option>HoneyDew</option><option>HotPink</option><option>IndianRed</option><option>Indigo</option><option>Ivory</option><option>Khaki</option><option>Lavender</option><option>LavenderBlush</option><option>LawnGreen</option><option>LemonChiffon</option><option>LightBlue</option><option>LightCoral</option><option>LightCyan</option><option>LightGoldenRodYellow</option><option>LightGray</option><option>LightGrey</option><option>LightGreen</option><option>LightPink</option><option>LightSalmon</option><option>LightSeaGreen</option><option>LightSkyBlue</option><option>LightSlateGray</option><option>LightSlateGrey</option><option>LightSteelBlue</option><option>LightYellow</option><option>Lime</option><option>LimeGreen</option><option>Linen</option><option>Magenta</option><option>Maroon</option><option>MediumAquaMarine</option><option>MediumBlue</option><option>MediumOrchid</option><option>MediumPurple</option><option>MediumSeaGreen</option><option>MediumSlateBlue</option><option>MediumSpringGreen</option><option>MediumTurquoise</option><option>MediumVioletRed</option><option>MidnightBlue</option><option>MintCream</option><option>MistyRose</option><option>Moccasin</option><option>NavajoWhite</option><option>Navy</option><option>OldLace</option><option>Olive</option><option>OliveDrab</option><option>Orange</option><option>OrangeRed</option><option>Orchid</option><option>PaleGoldenRod</option><option>PaleGreen</option><option>PaleTurquoise</option><option>PaleVioletRed</option><option>PapayaWhip</option><option>PeachPuff</option><option>Peru</option><option>Pink</option><option>Plum</option><option>PowderBlue</option><option>Purple</option><option>RebeccaPurple</option><option>Red</option><option>RosyBrown</option><option>RoyalBlue</option><option>SaddleBrown</option><option>Salmon</option><option>SandyBrown</option><option>SeaGreen</option><option>SeaShell</option><option>Sienna</option><option>Silver</option><option>SkyBlue</option><option>SlateBlue</option><option>SlateGray</option><option>SlateGrey</option><option>Snow</option><option>SpringGreen</option><option>SteelBlue</option><option>Tan</option><option>Teal</option><option>Thistle</option><option>Tomato</option><option>Turquoise</option><option>Violet</option><option>Wheat</option><option>White</option><option>WhiteSmoke</option><option>Yellow</option><option>YellowGreen</option></select> <input type=”color” id=”clr” onchange=’addColor(“c”);’/> <input type=”button” id=”b3″ onclick=’addColor(“s”);’/> <input type=”button” id=”b1″ value=”Run” onclick=’rn();’/> <input type=”button” id=”b2″ value=”Save” onclick=’sve(); sve2()’/> <input type=”button” id=”brec” value=”Recall” onclick=’recl()’/></center>
<center><table id= “tb1” ></table></center>

http://store.js
<script type=”text/javascript”>
var arr = []; var arr2 = []; var arr3 = []; var cats = 3; var str = “”; var cma = false; var oldURL = “bg.html”; var exp = false; var oldURL2 = “store.js”

document.onkeyup = chooseColorName;
function chooseColorName(e) {
if (exp) {
document.getElementById(“s1”).size = 20;
document.getElementById(“tb1”).style.visibility = “hidden”;
}

if (e.keyCode == 49) document.getElementById(“s1”).selectedIndex = 0;
if (e.keyCode == 50) document.getElementById(“s1”).selectedIndex = 20;
if (e.keyCode == 51) document.getElementById(“s1”).selectedIndex = 40;
if (e.keyCode == 52) document.getElementById(“s1”).selectedIndex = 60;
if (e.keyCode == 53) document.getElementById(“s1”).selectedIndex = 80;
if (e.keyCode == 54) document.getElementById(“s1”).selectedIndex = 100;
if (e.keyCode == 55) document.getElementById(“s1”).selectedIndex = 120;
if (e.keyCode == 56) document.getElementById(“s1”).selectedIndex = 140;
if (e.keyCode == 18) {
document.getElementById(“s1”).size = 1;
exp = false;
}
if (e.keyCode == 17) exp = true;
}

function makeColor() {
document.getElementById(“b3”).style.backgroundColor = document.getElementById(“s1”).value;
}

function addColor(par) {
if (par == “c”) document.getElementById(“t4”).value += document.getElementById(“clr”).value + “,”;
if (par == “s”) document.getElementById(“t4”).value += document.getElementById(“s1”).value + “,”;
}
function rn() {
document.getElementById(“s1”).size = 1;
if (document.getElementById(“t4”).value.substr(document.getElementById(“t4”).value.length – 1,1) != “,”) document.getElementById(“t4”).value += “,”;
document.getElementById(“t4”).value = document.getElementById(“t4”).value.substr(0,document.getElementById(“t4”).value.length – 1)
arr = document.getElementById(“t2”).value.split(“,”);
arr2 = document.getElementById(“t3”).value.split(“,”);
arr3 = document.getElementById(“t4”).value.split(“,”);
document.getElementById(“t4”).value += “,”;
cats = arr.length;
str = “”;

for (var i = 0; i < cats; i ++ ) {
str += ‘<tr> <td id = “td1″>’ + arr2[i] + ‘</td> <td><div style=” border: 2px solid black; background:’ + arr3[i] + ‘;width: ‘ + (arr[i] * 11) + ‘px; height: 30px” ></div></td> </tr>’;
}
document.getElementById(“tb1”).innerHTML = str;
document.getElementById(“tb1”).style.visibility = “visible”;
}

function sve() {
var textToSave = ‘<html><style> body({background: #dddddd} td({position: relative; vertical-align: bottom; height:30px} #td1({font-weight: 600; text-align: right} </style> <body> <center><table id= “tb1” style = “position: relative; top: 100px; border-collapse:collapse”>’ + document.getElementById(“tb1”).innerHTML + ‘ </table></center> </body> </html>’;
alert(textToSave);
var textToSaveAsBlob = new Blob([textToSave], {type:”text/plain”});
var textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);
fileNameToSaveAs = prompt(“FileName to save as”,oldURL);
oldURL = fileNameToSaveAs;
var downloadLink = document.createElement(“a”);
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = “Download File”;
downloadLink.href = textToSaveAsURL;
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = “none”;
document.body.appendChild(downloadLink);
downloadLink.click();
}
function sve2() {
var textToSave = ‘var store1 = “‘ + document.getElementById(“t2″).value + ‘”; var store2 = “‘ + document.getElementById(“t3″).value + ‘”; var store3 = “‘ + document.getElementById(“t4″).value + ‘”;’;
var textToSaveAsBlob = new Blob([textToSave], {type:”text/plain”});
var textToSaveAsURL = window.URL.createObjectURL(textToSaveAsBlob);
fileNameToSaveAs = prompt(“FileName to save as”,oldURL2);
oldURL = fileNameToSaveAs;
var downloadLink = document.createElement(“a”);
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = “Download File”;
downloadLink.href = textToSaveAsURL;
downloadLink.onclick = destroyClickedElement;
downloadLink.style.display = “none”;
document.body.appendChild(downloadLink);
downloadLink.click();
}
function destroyClickedElement(event) {
document.body.removeChild(event.target);
}

function recl() {
document.getElementById(“t2”).value = store1;
document.getElementById(“t3”).value = store2;
document.getElementById(“t4”).value = store3;
}
</script>
</body></html>

Since the list of named colors is long, the method for scanning only small parts is applied:
if (e.keyCode == 49) document.getElementById(“s1”).selectedIndex = 0;
if (e.keyCode == 50) document.getElementById(“s1”).selectedIndex = 20;
if (e.keyCode == 51) document.getElementById(“s1”).selectedIndex = 40;
if (e.keyCode == 52) document.getElementById(“s1”).selectedIndex = 60;
if (e.keyCode == 53) document.getElementById(“s1”).selectedIndex = 80;
if (e.keyCode == 54) document.getElementById(“s1”).selectedIndex = 100;
if (e.keyCode == 55) document.getElementById(“s1”).selectedIndex = 120;
if (e.keyCode == 56) document.getElementById(“s1”).selectedIndex = 140;
if (e.keyCode == 18) {
document.getElementById(“s1”).size = 1;
exp = false;
}
if (e.keyCode == 17) exp = true;
}

The boolean variable exp expands the select to 20.

makeColor() puts a color in a box that is taken from the select:
function makeColor() {
document.getElementById(“b3”).style.backgroundColor = document.getElementById(“s1”).value;
}

addColor(par) adds ro the color list:
function addColor(par) {
if (par == “c”) document.getElementById(“t4”).value += document.getElementById(“clr”).value + “,”;
if (par == “s”) document.getElementById(“t4”).value += document.getElementById(“s1”).value + “,”;
}

The graph is created by the function rn():
function rn() {
document.getElementById(“s1”).size = 1;
if (document.getElementById(“t4”).value.substr(document.getElementById(“t4”).value.length – 1,1) != “,”) document.getElementById(“t4”).value += “,”;
document.getElementById(“t4”).value = document.getElementById(“t4”).value.substr(0,document.getElementById(“t4”).value.length – 1);
arr = document.getElementById(“t2”).value.split(“,”);
arr2 = document.getElementById(“t3”).value.split(“,”);
arr3 = document.getElementById(“t4”).value.split(“,”);
document.getElementById(“t4”).value += “,”;
cats = arr.length;
str = “”;

for (var i = 0; i < cats; i ++ ) {
str += ‘<tr> <td id = “td1″>’ + arr2[i] + ‘</td> <td><div style=” border: 2px solid black; background:’ + arr3[i] + ‘;width: ‘ + (arr[i] * 11) + ‘px; height: 30px” ></div></td> </tr>’;
}
document.getElementById(“tb1”).innerHTML = str;
document.getElementById(“tb1”).style.visibility = “visible”;
}

Since there is more than one way to enter a color, to be consistent, all color lists must end with a comma, which is then removed:
if (document.getElementById(“t4”).value.substr(document.getElementById(“t4”).value.length – 1,1) != “,”) document.getElementById(“t4”).value += “,”;
document.getElementById(“t4”).value = document.getElementById(“t4”).value.substr(0,document.getElementById(“t4”).value.length – 1);

The text inputs are converted to arrays:
arr = document.getElementById(“t2”).value.split(“,”);
arr2 = document.getElementById(“t3”).value.split(“,”);
arr3 = document.getElementById(“t4”).value.split(“,”);

A string for a series of table cells is created:
for (var i = 0; i < cats; i ++ ) {
str += ‘<tr> <td id = “td1″>’ + arr2[i] + ‘</td> <td><div style=” border: 2px solid black; background:’ + arr3[i] + ‘;width: ‘ + (arr[i] * 11) + ‘px; height: 30px” ></div></td> </tr>’;
}

and added to a table, which is nade visible:
document.getElementById(“tb1”).innerHTML = str;to expand the grapg through the
document.getElementById(“tb1”).style.visibility = “visible”;

The Save button creates two files, a html file from sve() and a js file from sve2(), which is used to expand the graph through the recl() function:
function recl() {
document.getElementById(“t2”).value = store1;
document.getElementById(“t3”).value = store2;
document.getElementById(“t4”).value = store3;
}

Automatically create the complement to a color input

In the example below a cube is drawn from several polygons. It is given no fill and a white stroke to contrast with a black background, giving a wireframe:

ComplColrText

In the second example a near white fill ia given. If the stroke were to remain white, it would be almost impossible to see and the cube would no longer appear so. It is therefor given a color complementary to the fill color to provide contrast.

ComplColrText2

This descibes how to do this.

This is the code:

<!DOCTYPE html>
<html>
<style>body{background-color: black}</style>
<body>
<center><input type=”color” id=”clr1″ style=”position:relative ” value=”#000000″ onchange=”setColor();”> <input type=”text” id= “t1″ style=”position:relative ” value=”” placeholder = “rgb or hex” /><br>
<svg id=”svg1″ style=”position: relative; width: 100%; height: 700px; “> </svg></center>
<script>
var ptX = [600,800,800,600]; var ptY = [100,100,300,300]; var ptX2 = [640,840,840,640]; var ptY2 = [70,70,270,270]; var ptX3 = [800,840,840,800]; var ptY3 = [100,70,270,300]; var ptX4 = [600,600,640,640]; var ptY4 = [300,100,70,270]; var ptX5 = [600,640,840,800]; var ptY5 = [100,70,70,100]; var ptX6 = [600,640,840,800]; var ptY6 = [300,270,270,300];

str = ‘<polygon id = “poly1” points = “‘;
for (var i = 0; i <=3 ; i ++ ) {
str+= ptX2[i] + ‘,’ + ptY2[i] + ‘ ‘;
}
str += ‘ ” style=”fill: none; stroke:white; stroke-width: 2″ />’;
document.getElementById(“svg1”).innerHTML = str;

str = ‘<polygon id = “poly4” points = “‘;
for (var i = 0; i <=3 ; i ++ ) {
str+= ptX5[i] + ‘,’ + ptY5[i] + ‘ ‘;
}
str += ‘ ” style=”fill:none; stroke:white; stroke-width: 2″ />’;
document.getElementById(“svg1”).innerHTML += str;

str = ‘<polygon id = “poly5” points = “‘;
for (var i = 0; i <=3 ; i ++ ) {
str+= ptX6[i] + ‘,’ + ptY6[i] + ‘ ‘;
}
str += ‘ ” style=”fill:none; stroke:white; stroke-width: 2″ />’;
document.getElementById(“svg1”).innerHTML += str;

str = ‘<polygon id = “poly2” points = “‘;
for (var i = 0; i <=3 ; i ++ ) {
str+= ptX3[i] + ‘,’ + ptY3[i] + ‘ ‘;
}
str += ‘ ” style=”fill:none; stroke: none; stroke-width: 2″ />’;
document.getElementById(“svg1”).innerHTML += str;

str = ‘<polygon id = “poly3” points = “‘;
for (var i = 0; i <=3 ; i ++ ) {
str+= ptX4[i] + ‘,’ + ptY4[i] + ‘ ‘;
}
str += ‘ ” style=”fill:none; stroke: none; stroke-width: 2″ />’;
document.getElementById(“svg1”).innerHTML += str;

str = ‘<polygon id = “poly0” points = “‘;
for (var i = 0; i <=3 ; i ++ ) {
str+= ptX[i] + ‘,’ + ptY[i] + ‘ ‘;
}
str += ‘ ” style=”fill:none; stroke:white; stroke-width: 2″ />’;
document.getElementById(“svg1”).innerHTML += str;

function setColor() {
var tmp = document.getElementById(“clr1”).value;
for (var i = 0; i <=5; i ++) {
document.getElementById(“poly” + i).style.fill = tmp;
}
var typ = document.getElementById(“t1”).value;
var colstr = getHexComplement(tmp,typ);
for (var i = 0; i <=5; i ++) {
if (i != 2 && i != 3) {
document.getElementById(“poly” + i).style.stroke = colstr;
}
}
}

function getHexComplement(val,type) {
var res;
if (type == “rgb”) {
res = “rgb(” + (255 – parseInt(val.substr(1,2),16)) + “,” + (255 – parseInt(val.substr(3,2),16)) + “,” + (255 – parseInt(val.substr(5,2),16)) + “)”;
} else {
var r = (255 – parseInt(val.substr(1,2),16)).toString(16);
if (r.length < 2) r = “0” + r;
var g = (255 – parseInt(val.substr(3,2),16)).toString(16);
if (g.length < 2) g = “0” + g;
var b = (255 – parseInt(val.substr(5,2),16)).toString(16);
if (b.length < 2) b = “0” + b;
res = “#” + r + g + b;
}
return res;
}

</script></body></html>

The first part draws the cube:
str = ‘<polygon id = “poly1” points = “‘;
for (var i = 0; i <=3 ; i ++ ) {
str+= ptX2[i] + ‘,’ + ptY2[i] + ‘ ‘;
}
str += ‘ ” style=”fill: none; stroke:white; stroke-width: 2″ />’;
document.getElementById(“svg1”).innerHTML = str;

str = ‘<polygon id = “poly4” points = “‘;
for (var i = 0; i <=3 ; i ++ ) {
str+= ptX5[i] + ‘,’ + ptY5[i] + ‘ ‘;
}
str += ‘ ” style=”fill:none; stroke:white; stroke-width: 2″ />’;
document.getElementById(“svg1”).innerHTML += str;

str = ‘<polygon id = “poly5” points = “‘;
for (var i = 0; i <=3 ; i ++ ) {
str+= ptX6[i] + ‘,’ + ptY6[i] + ‘ ‘;
}
str += ‘ ” style=”fill:none; stroke:white; stroke-width: 2″ />’;
document.getElementById(“svg1”).innerHTML += str;

str = ‘<polygon id = “poly2” points = “‘;
for (var i = 0; i <=3 ; i ++ ) {
str+= ptX3[i] + ‘,’ + ptY3[i] + ‘ ‘;
}
str += ‘ ” style=”fill:none; stroke: none; stroke-width: 2″ />’;
document.getElementById(“svg1”).innerHTML += str;

str = ‘<polygon id = “poly3” points = “‘;
for (var i = 0; i <=3 ; i ++ ) {
str+= ptX4[i] + ‘,’ + ptY4[i] + ‘ ‘;
}
str += ‘ ” style=”fill:none; stroke: none; stroke-width: 2″ />’;
document.getElementById(“svg1”).innerHTML += str;

str = ‘<polygon id = “poly0” points = “‘;
for (var i = 0; i <=3 ; i ++ ) {
str+= ptX[i] + ‘,’ + ptY[i] + ‘ ‘;
}
str += ‘ ” style=”fill:none; stroke:white; stroke-width: 2″ />’;
document.getElementById(“svg1”).innerHTML += str;

Changing the color input calls setColor():
function setColor() {
var tmp = document.getElementById(“clr1”).value;
for (var i = 0; i <=5; i ++) {
document.getElementById(“poly” + i).style.fill = tmp;
}
var typ = document.getElementById(“t1”).value;
var colstr = getHexComplement(tmp,typ);
for (var i = 0; i <=5; i ++) {
if (i != 2 && i != 3) {
document.getElementById(“poly” + i).style.stroke = colstr;
}
}
}

The fill is set from the color input and a function getHexComplement(tmp,typ) determines the complementary color to be used for the stroke:
function getHexComplement(val,type) {
var res;
if (type == “rgb”) {
res = “rgb(” + (255 – parseInt(val.substr(1,2),16)) + “,” + (255 – parseInt(val.substr(3,2),16)) + “,” + (255 – parseInt(val.substr(5,2),16)) + “)”;
} else {
var r = (255 – parseInt(val.substr(1,2),16)).toString(16);
if (r.length < 2) r = “0” + r;
var g = (255 – parseInt(val.substr(3,2),16)).toString(16);
if (g.length < 2) g = “0” + g;
var b = (255 – parseInt(val.substr(5,2),16)).toString(16);
if (b.length < 2) b = “0” + b;
res = “#” + r + g + b;
}
return res;
}

The variable type determines whether the returned color is a hex or rgb color.
To return an rgb color the following is used:
res = “rgb(” + (255 – parseInt(val.substr(1,2),16)) + “,” + (255 – parseInt(val.substr(3,2),16)) + “,” + (255 – parseInt(val.substr(5,2),16)) + “)”;

For a hex color the hex output of the color input is converted to red green and blue components:
var r = (255 – parseInt(val.substr(1,2),16)).toString(16);
if (r.length < 2) r = “0” + r;

It is similar for blue and green.

The three values are then combined to a hex value:
res = “#” + r + g + b;

In this case it makes no difference which color value is used, but if it does, either one can be calculated.