Last active
May 29, 2017 17:03
-
-
Save hightemp/a7c4e3d8372a2085d60ca8f6c877088c to your computer and use it in GitHub Desktop.
[JavaScript] Library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| (function() { | |
| window.objLibrary = { | |
| "TBitArray": function() { | |
| this.prototype = { | |
| "aBits": [], | |
| "fnConvert": function(in_objObject) { | |
| if (in_objObject["aBits"]) { | |
| return in_objObject["aBits"]; | |
| } else { | |
| if (typeof in_objObject == "number") { | |
| var iValue = in_objObject; | |
| var iResult = []; | |
| while (iValue>1) { | |
| var iDiv = iValue % 2; | |
| iValue = Math.floor(iValue/2); | |
| iResult.splice(0, 0, iDiv); | |
| } | |
| if (iValue==1) | |
| iResult.splice(0, 0, iValue); | |
| return iResult; | |
| } else | |
| throw "unknown type" | |
| } | |
| }, | |
| "fnSet": function(in_objObject) { | |
| this.aBits = this.fnConvert(in_objObject); | |
| }, | |
| "fnShiftRight": function(in_objObject) { | |
| aBits = this.fnConvert(in_objObject); | |
| }, | |
| "fnShiftLeft": function(in_objObject) { | |
| aBits = this.fnConvert(in_objObject); | |
| }, | |
| "fnAdd": function(in_objObject) { | |
| aBits = this.fnConvert(in_objObject); | |
| }, | |
| "fnAdd": function(in_objObject) { | |
| aBits = this.fnConvert(in_objObject); | |
| }, | |
| "fnSub": function(in_objObject) { | |
| aBits = this.fnConvert(in_objObject); | |
| }, | |
| "fnMul": function(in_objObject) { | |
| aBits = this.fnConvert(in_objObject); | |
| }, | |
| "fnDiv": function(in_objObject) { | |
| aBits = this.fnConvert(in_objObject); | |
| } | |
| } | |
| }, | |
| "TTable": function() { | |
| this.prototype = { | |
| "aColumns": [], | |
| "aColumnsTypes": [], | |
| "aColumnsToColumnsTypes": [], | |
| "aData": [], | |
| "dDataToColumns": [], | |
| "aRecords": [], | |
| "fnAddColumn": function(in_sName, in_sType) { | |
| if (this.aColumns.indexOf(sName)>-1) | |
| return false; | |
| // NAME to INT | |
| return iColumnID; | |
| }, | |
| "fnGetColumns": function() { | |
| aResult = 0; | |
| for (var iIndex=0; iIndex<aColumns.length; iIndex++) { | |
| } | |
| }, | |
| "fnAddRecord": function(in_dRecord) { | |
| }, | |
| "fnSize": function() { | |
| return this.aRecords.length; | |
| } | |
| } | |
| }, | |
| "fnColorRGBToInteger": function(in_iRed, in_iGreen, in_iBlue) { | |
| return in_iRed<<0 | in_iGreen<<8 | in_iBlue<<16; | |
| }, | |
| "fnIntegerToColorRGB": function(in_iColor) { | |
| return [ | |
| in_iColor>>0 & 0x0000FF, | |
| in_iColor>>8 & 0x0000FF, | |
| in_iColor>>16 & 0x0000FF, | |
| ]; | |
| }, | |
| "fnTimestamp": function() { | |
| if (!Date.now) { | |
| Date.now = function() { return new Date().getTime(); } | |
| } | |
| return Date.now(); | |
| }, | |
| "fnMap": function(in_Value, in_Range1Min, in_Range1Max, in_Range2Min, in_Range2Max) { | |
| return (in_Range1Min+in_Value)*(in_Range2Max-in_Range2Min)/(in_Range1Max-in_Range1Min) | |
| }, | |
| "TGraphSurface": function(in_ID, in_iWidth, in_iHeight, in_objArguments, in_fnOnCreate) { | |
| this.prototype = { | |
| "bDrawOnceTrigger": false, | |
| "bDrawOnce": false, | |
| "iClearColor": 0, | |
| "iMouseX": 0, | |
| "iMouseY": 0, | |
| "iFillColor": 0, | |
| "iStrokeColor": 0, | |
| "iAxisX": 0, | |
| "iAxisY": 0, | |
| "fnCreate": function() { | |
| this.fnCreateCanvas(in_ID, in_iWidth, in_iHeight, in_objArguments, in_fnOnCreate); | |
| this.fnUpdate(); | |
| }, | |
| "fnCreateCanvas": function(in_ID, in_iWidth, in_iHeight, in_objArguments, in_fnOnCreate) { | |
| if (this.objCanvas) { | |
| this.objCanvas.parent.removeChild(this.objCanvas); | |
| } | |
| for (var iIndex in in_objArguments) { | |
| this[iIndex] = in_objArguments[iIndex]; | |
| } | |
| var objParentElement = document.getElementById(in_ID); | |
| this.objCanvas = document.createElement('canvas'); | |
| objParentElement.appendChild(this.objCanvas); | |
| this.objContext = this.objCanvas.getContext('2d'); | |
| this.iOldWidth = in_iWidth; | |
| this.iOldHeight = in_iHeight; | |
| this.iWidth = in_iWidth; | |
| this.iHeight = in_iHeight; | |
| this.objCanvas.width = in_iWidth; | |
| this.objCanvas.height = in_iHeight; | |
| this.objCanvas.addEventListener("click", this.fnClickEvent.bind(this)); | |
| this.objCanvas.addEventListener("mousedown", this.fnMouseDownEvent.bind(this)); | |
| this.objCanvas.addEventListener("mouseup", this.fnMouseUpEvent.bind(this)); | |
| this.objCanvas.addEventListener("mousemove", this.fnBeforeMouseMoveEvent.bind(this)); | |
| if (typeof in_fnOnCreate == "function") | |
| in_fnOnCreate.call(this); | |
| }, | |
| "fnResizeEvent": function() { | |
| this.bDrawOnceTrigger = false; | |
| }, | |
| "fnClickEvent": function() { | |
| }, | |
| "fnMouseDownEvent": function() { | |
| }, | |
| "fnMouseUpEvent": function() { | |
| }, | |
| "fnBeforeMouseMoveEvent": function(in_objEvent) { | |
| this.iMouseX = in_objEvent.clientX; | |
| this.iMouseY = in_objEvent.clientY; | |
| this.fnMouseMoveEvent(in_objEvent); | |
| }, | |
| "fnMouseMoveEvent": function(in_objEvent) { | |
| }, | |
| "fnUpdate": function() { | |
| clearTimeout(this.iTimerID); | |
| var iXResizeDelta = Math.abs(this.iOldWidth - this.iWidth); | |
| var iYResizeDelta = Math.abs(this.iOldHeight - this.iHeight); | |
| if (iXResizeDelta>0 || iYResizeDelta>0) | |
| this.fnResizeEvent({ | |
| iXResizeDelta: iXResizeDelta, | |
| iYResizeDelta: iYResizeDelta, | |
| iOldWidth: this.iOldWidth, | |
| iOldHeight: this.iOldHeight, | |
| iWidth: this.iWidth, | |
| iHeight: this.iHeight | |
| }); | |
| this.iOldWidth = this.iWidth; | |
| this.iOldHeight = this.iHeight; | |
| if (this.bDrawOnce && !this.bDrawOnceTrigger) { | |
| this.bDrawOnceTrigger = true; | |
| this.fnClear(this.iClearColor); | |
| this.fnDraw(this, this.objContext, objLibrary); | |
| } | |
| if (!this.bDrawOnce) { | |
| this.fnClear(this.iClearColor); | |
| this.fnDraw(this, this.objContext, objLibrary); | |
| } | |
| var self = this; | |
| this.iTimerID = setTimeout(function() { | |
| self.fnUpdate(); | |
| }, 10); | |
| }, | |
| "fnDraw": function(in_objSelf, in_objContext, in_objLibrary) { | |
| }, | |
| "fnSetPixel": function(in_iX, in_iY) { | |
| var aImageData = this.objContext.getImageData(in_iX+this.iAxisX, in_iY+this.iAxisY, 1, 1); | |
| var aRGB = objLibrary.fnIntegerToColorRGB(this.iFillColor); | |
| aImageData.data[0] = aRGB[0]; | |
| aImageData.data[1] = aRGB[1]; | |
| aImageData.data[2] = aRGB[2]; | |
| this.objContext.putImageData(aImageData, in_iX+this.iAxisX, in_iY+this.iAxisY); | |
| }, | |
| "fnSetPixelColor": function(in_iX, in_iY, in_iColor) { | |
| var aImageData = this.objContext.getImageData(in_iX+this.iAxisX, in_iY+this.iAxisY, 1, 1); | |
| aImageData.data = objLibrary.fnIntegerToColorRGB(in_iColor); | |
| this.objContext.putImageData(aImageData, in_iX+this.iAxisX, in_iY+this.iAxisY); | |
| }, | |
| "fnGetPixelRGB": function(in_iX, in_iY) { | |
| var aImageData = this.objContext.getImageData(in_iX+this.iAxisX, in_iY+this.iAxisY, 1, 1); | |
| return aImageData.data; | |
| }, | |
| "fnGetPixelColor": function(in_iX, in_iY) { | |
| var aImageData = this.objContext.getImageData(in_iX+this.iAxisX, in_iY+this.iAxisY, 1, 1); | |
| return objLibrary.fnColorRGBToInteger(aImageData.data[0], aImageData.data[1], aImageData.data[2]); | |
| }, | |
| "fnLine": function(in_iX1, in_iY1, in_iX2, in_iY2) { | |
| console.log(); | |
| this.objContext.beginPath(); | |
| this.objContext.moveTo(in_iX1+this.iAxisX, in_iY1+this.iAxisY); | |
| this.objContext.lineTo(in_iX2+this.iAxisX, in_iY2+this.iAxisY); | |
| this.objContext.stroke(); | |
| }, | |
| "fnFillRectangle": function(in_iX, in_iY, in_iWidth, in_iHeight) { | |
| this.objContext.fillRect(in_iX+this.iAxisX, in_iY+this.iAxisY, in_iWidth, in_iHeight); | |
| }, | |
| "fnStrokeRectangle": function(in_iX, in_iY, in_iWidth, in_iHeight) { | |
| this.objContext.strokeRect(in_iX+this.iAxisX, in_iY+this.iAxisY, in_iWidth, in_iHeight); | |
| }, | |
| "fnFillRectangleCoordinates": function(in_iX1, in_iY1, in_iX2, in_iY2) { | |
| var iX = in_iX1>in_iX2 ? in_iX2 : in_iX1; | |
| var iY = in_iY1>in_iY2 ? in_iY2 : in_iY1; | |
| var iWidth = Math.abs(in_iX2-in_iX1); | |
| var iHeight = Math.abs(in_iY2-in_iY1); | |
| this.objContext.fillRect(iX+this.iAxisX, iY+this.iAxisY, in_iWidth, in_iHeight); | |
| }, | |
| "fnStrokeRectangleCoordinates": function(in_iX1, in_iY1, in_iX2, in_iY2) { | |
| var iX = in_iX1>in_iX2 ? in_iX2 : in_iX1; | |
| var iY = in_iY1>in_iY2 ? in_iY2 : in_iY1; | |
| var iWidth = Math.abs(in_iX2-in_iX1); | |
| var iHeight = Math.abs(in_iY2-in_iY1); | |
| this.objContext.strokeRect(iX+this.iAxisX, iY+this.iAxisY, in_iWidth, in_iHeight); | |
| }, | |
| "fnColor": function(in_iColor) { | |
| this.fnFillColor(in_iColor); | |
| this.fnStrokeColor(in_iColor); | |
| }, | |
| "fnFillColor": function(in_iColor) { | |
| var aColor = objLibrary.fnIntegerToColorRGB(in_iColor); | |
| this.objContext.fillStyle = 'rgb(' + aColor.join(',') + ')'; | |
| this.iFillColor = in_iColor; | |
| }, | |
| "fnStrokeColor": function(in_iColor) { | |
| var aColor = objLibrary.fnIntegerToColorRGB(in_iColor); | |
| this.objContext.strokeStyle = 'rgb(' + aColor.join(',') + ')'; | |
| this.iStrokeColor = in_iColor; | |
| }, | |
| "fnClear": function(in_iColor) { | |
| this.fnFillColor(in_iColor); | |
| this.objContext.fillRect(0, 0, this.iWidth, this.iHeight); | |
| } | |
| } | |
| this.prototype.fnCreate(); | |
| return this; | |
| } | |
| }; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment