UNPKG

@vatsa/simple.js

Version:
454 lines (378 loc) 10.4 kB
//starting message console.log( "%c Enabled, to use please launch a local or live server.", "background: #222; color: #bada55" ); //program //normal exports.think = (statement) => { console.log(statement); } exports.message = (content) => { alert(content); } exports.question = (content) => { prompt(content); } exports.includeHTML = () => { var z, i, elmnt, file, xhttp; /*loop through a collection of all HTML elements:*/ z = document.getElementsByTagName("*"); for (i = 0; i < z.length; i++) { elmnt = z[i]; /*search for elements with a certain atrribute:*/ file = elmnt.getAttribute("include"); if (file) { /*make an HTTP request using the attribute value as the file name:*/ xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { if (this.readyState == 4) { if (this.status == 200) { elmnt.innerHTML = this.responseText; } if (this.status == 404) { elmnt.innerHTML = "Page not found."; } /*remove the attribute, and call this function once more:*/ elmnt.removeAttribute("include"); includeHTML(); } }; xhttp.open("GET", file, true); xhttp.send(); /*exit the function:*/ return; } } } exports.dragHtml = (id) => { dragElement(document.getElementById(id)); document.getElementById(id).style.position = "absolute"; function dragElement(elmnt) { var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0; if (document.getElementById(elmnt.id + "header")) { // if present, the header is where you move the DIV from: document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown; } else { // otherwise, move the DIV from anywhere inside the DIV: elmnt.onmousedown = dragMouseDown; } function dragMouseDown(e) { e = e || window.event; e.preventDefault(); // get the mouse cursor position at startup: pos3 = e.clientX; pos4 = e.clientY; document.onmouseup = closeDragElement; // call a function whenever the cursor moves: document.onmousemove = elementDrag; } function elementDrag(e) { e = e || window.event; e.preventDefault(); // calculate the new cursor position: pos1 = pos3 - e.clientX; pos2 = pos4 - e.clientY; pos3 = e.clientX; pos4 = e.clientY; // set the element's new position: elmnt.style.top = (elmnt.offsetTop - pos2) + "px"; elmnt.style.left = (elmnt.offsetLeft - pos1) + "px"; } function closeDragElement() { // stop moving when mouse button is released: document.onmouseup = null; document.onmousemove = null; } } } //arrays exports.joinArray = (array1, array2) => { return array1.concat(array2); } exports.removeLastElement = (array) => { array.pop(); } exports.addElement = (array, content) => { array.push(content); } exports.reverse = (array) => { array.reverse(); } exports.removeFirstElement = (array) => { array.shift(); } exports.addNewFirstElement = (array) => { array.unshift(); } //geolocation exports.postion = () => { getCurrentPosition(); } exports.getPositionLatitude = () => { return position.coords.latitude; } exports.getPositionLongitude = () => { return position.coords.longitude; } //date exports.getYear = () => { var d = new Date(); return d.getFullYear(); } exports.getMonth = () => { var d = new Date(); return d.getMonth(); } exports.getWeekday = () => { var d = new Date(); return d.getDay(); } exports.getMonthDate = () => { var d = new Date(); return d.getDate(); } //math exports.minNum = (x, y, z) => { return min(x, y, z); } exports.maxNum = (x, y, z) => { return max(x, y, z); } exports.cubeRoot = (num) => { return cbrt(num); } exports.absolute = (num) => { return abs(num); } exports.EulerNum = () => { return Math.E; } exports.PiNum = () => { return Math.PI; } exports.root = (num) => { return Math.sqrt(num); } exports.add = (x, y) => { return x + y; } exports.sub = (x, y) => { return x - y; } exports.mul = (x, y) => { return x * y; } exports.div = (x, y) => { return x / y; } exports.mod = (x, y) => { return x % y; } exports.round = (num) => { return Math.floor(num); } exports.randomfrom = (min, max) => { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } exports.power = (level, num) => { return Math.pow(num, level); } exports.equal = (num1, num2) => { if (num1 == num2) { return true; } else { return false; } } //strings exports.stringLength = (str) => { return str.length; } exports.stringJoin = (str1, str2) => { return str1 + str2; } exports.stringLetter = (str, num) => { return str[num]; } exports.stringSplice = (str, l1, l2) => { return str.splice(l1, l2); } //jquery exports.say = (id, content) => { $(id).text(content); } exports.addpage = (content) => { $("body").append(content); } exports.fadeout = (id, sec) => { var time = sec * 1000; $(id).fadeOut(time); } exports.fadein = (id, sec) => { var time = sec * 1000; $(id).fadeIn(time); } exports.wait = (sec, func) => { var time = sec * 1000; setTimeout(func, time); } exports.clickHandler = (event) => { console.log("Click! " + event.pageX + " " + event.pageY); } exports.clickPos = (id) => { $(id).click(clickHandler); } exports.mousefollow = (id) => { $("html").mousemove(function (event) { $(id).offset({ left: event.pageX, top: event.pageY }); }); } exports.clickfollow = (id) => { $("html").click(function (event) { $(id).offset({ left: event.pageX, top: event.pageY }); }); } exports.keycode = () => { $("body").keydown(function (event) { console.log(event.keyCode); }); } //lodash exports.repeat = (times, func) => { _.times(times, func); } //canvas exports.canvasRectStroke = (x, y, width, height, color, thickness) => { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.strokeStyle = color; ctx.lineWidth = thickness; ctx.strokeRect(x, y, width, height); } exports.canvasRect = (x, y, width, height, color) => { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.fillStyle = color; ctx.fillRect(x, y, width, height); } exports.canvasBeginPath = (color, thickness) => { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.strokeStyle = color; ctx.lineWidth = thickness; ctx.beginPath(); } exports.canvasMove = (x, y) => { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.moveTo(x, y); } exports.canvasLine = (x, y) => { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.lineTo(x, y); } exports.canvasClosePath = () => { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.stroke(); } exports.canvasFill = (color) => { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.fillStyle = color; ctx.fill(); } exports.canvasGradientFillLinear = (color1, color2) => { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var grd = ctx.createLinearGradient(0, 0, 200, 0); grd.addColorStop(0, color1); grd.addColorStop(1, color2); ctx.fillStyle = grd; ctx.fill(); } exports.canvasGradientFillRadial = (color1, color2) => { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var grd = ctx.createRadialGradient(75, 50, 5, 90, 60, 100); grd.addColorStop(0, color1); grd.addColorStop(1, color2); ctx.fillStyle = grd; ctx.fill(); } exports.canvasArc = ( x, y, radius, startAngle, endAngle, clockwise, color, thickness ) => { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.lineWidth = thickness; ctx.strokeStyle = color; ctx.fillStyle = color; ctx.arc(x, y, radius, startAngle, endAngle, clockwise); } exports.canvasText = (x, y, font, text) => { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.font = font; ctx.fillText(text, x, y); } exports.canvasStrokeText = (x, y, font, text) => { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.font = font; ctx.strokeText(text, x, y); } //audio exports.audioSoundRepeat = (url) => { var audio = new Audio(url); audio.oncanplaythrough = function () { audio.play(); }; audio.loop = loop; audio.onended = function () { audio.play(); }; } exports.audioSound = (url) => { var audio = new Audio(url); audio.oncanplaythrough = function () { audio.play(); }; } //two.js exports.twoRect = (x, y, width, height, colorstroke, colorfill) => { var elem = document.getElementById("draw-area"); var two = new Two({ fullscreen: false }).appendTo(elem); var rect = two.makeRectangle(x, y, width, height); rect.stroke = colorstroke; rect.fill = colorfill; two.update(); } exports.twoCircle = (x, y, radius, colorstroke, colorfill, thickness) => { var elem = document.getElementById("draw-area"); var two = new Two({ fullscreen: false }).appendTo(elem); var circle = two.makeCircle(x, y, radius); circle.stroke = colorstroke; circle.fill = colorfill; circle.linewidth = thickness; two.update(); } exports.twoRoundRect = (x, y, width, height, radius, colorstroke, colorfill) => { var elem = document.getElementById("draw-area"); var two = new Two({ fullscreen: false }).appendTo(elem); var roundedRect = new Two.RoundedRectangle(x, y, width, height, radius); roundedRect.stroke = colorstroke; roundedRect.fill = colorfill; two.update(); }