This shows you the differences between two versions of the page.
Previous revisionLast revision | |||
— | fuss:javascript [2022/04/19 08:28] – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Wizardry and Steamworks JavaScript Libraries ====== | ||
+ | |||
+ | ^ Name ^ Link ^ Description ^ | ||
+ | | '' | ||
+ | |||
+ | ====== Compare Arrays ====== | ||
+ | |||
+ | The following code: | ||
+ | |||
+ | <code javascript> | ||
+ | // attach the .equals method to Array' | ||
+ | Array.prototype.equals = function (array) { | ||
+ | // if the other array is a falsy value, return | ||
+ | if (!array) | ||
+ | return false; | ||
+ | |||
+ | // compare lengths - can save a lot of time | ||
+ | if (this.length != array.length) | ||
+ | return false; | ||
+ | |||
+ | for (var i = 0, l=this.length; | ||
+ | // Check if we have nested arrays | ||
+ | if (this[i] instanceof Array && array[i] instanceof Array) { | ||
+ | // recurse into the nested arrays | ||
+ | if (!this[i].equals(array[i])) | ||
+ | return false; | ||
+ | } | ||
+ | else if (this[i] != array[i]) { | ||
+ | // Warning - two different object instances will never be equal: {x:20} != {x:20} | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | return true; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | will add an '' | ||
+ | <code javascript> | ||
+ | var a = [ " | ||
+ | var b = [ " | ||
+ | alert(a.equals(b)); | ||
+ | </ | ||
+ | |||
+ | ====== Linearly Map a Value in a Range into another Range ====== | ||
+ | |||
+ | Following the formula to [[fuss/ | ||
+ | |||
+ | <code javascript> | ||
+ | / | ||
+ | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ | ||
+ | / | ||
+ | function wasMapValueToRange(value, | ||
+ | return yMin + ( | ||
+ | ( yMax - yMin ) * ( value - xMin ) / ( xMax - xMin ) | ||
+ | ); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== RGB Color to Hexadecimal Color ====== | ||
+ | |||
+ | <code javascript> | ||
+ | function wasRGBToHex(r, | ||
+ | return "#" | ||
+ | (1 << 24) + | ||
+ | (r << 16) + | ||
+ | (g << 8) + | ||
+ | b | ||
+ | ).toString(16).slice(1); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | where '' | ||
+ | |||
+ | ====== Hexadecimal Color to RGB Color ====== | ||
+ | |||
+ | <code javascript> | ||
+ | function wasHexToRGB(hex) { | ||
+ | var shortRegEx = / | ||
+ | hex = hex.replace( | ||
+ | shortRegEx, | ||
+ | function(m, r, g, b) { | ||
+ | return r + r + g + g + b + b; | ||
+ | } | ||
+ | ); | ||
+ | |||
+ | var result = / | ||
+ | return result ? { | ||
+ | r: parseInt(result[1], | ||
+ | g: parseInt(result[2], | ||
+ | b: parseInt(result[3], | ||
+ | } : null; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | where '' | ||
+ | |||