code
stringlengths 24
2.07M
| docstring
stringlengths 25
85.3k
| func_name
stringlengths 1
92
| language
stringclasses 1
value | repo
stringlengths 5
64
| path
stringlengths 4
172
| url
stringlengths 44
218
| license
stringclasses 7
values |
---|---|---|---|---|---|---|---|
formatMilliseconds (millisec) {
const seconds = (millisec / 1000).toFixed(1)
const minutes = (millisec / (1000 * 60)).toFixed(1)
const hours = (millisec / (1000 * 60 * 60)).toFixed(1)
const days = (millisec / (1000 * 60 * 60 * 24)).toFixed(1)
if (seconds < 60) {
return seconds + ' seconds'
} else if (minutes < 60) {
return minutes + ' minutes'
} else if (hours < 24) {
return hours + ' hours'
} else {
return days + ' days'
}
}
|
inspired from https://stackoverflow.com/questions/19700283/how-to-convert-time-milliseconds-to-hours-min-sec-format-in-javascript
@param millisec
@returns {string}
|
formatMilliseconds
|
javascript
|
evansiroky/timezone-boundary-builder
|
util/progressStats.js
|
https://github.com/evansiroky/timezone-boundary-builder/blob/master/util/progressStats.js
|
MIT
|
ObjectProperty(path) {
if (
DATA_FETCH_FNS.includes(path.node.value.name) &&
path.findParent(
(path) =>
path.isVariableDeclarator() &&
path.node.id.name === 'layoutProps',
)
) {
path.remove()
}
}
|
Currently it's not possible to export data fetching functions from MDX pages
because MDX includes them in `layoutProps`, and Next.js removes them at some
point, causing a `ReferenceError`.
https://github.com/mdx-js/mdx/issues/742#issuecomment-612652071
This plugin can be removed once MDX removes `layoutProps`, at least that
seems to be the current plan.
|
ObjectProperty
|
javascript
|
jaredpalmer/tsdx
|
website/.nextra/babel-plugin-nextjs-mdx-patch.js
|
https://github.com/jaredpalmer/tsdx/blob/master/website/.nextra/babel-plugin-nextjs-mdx-patch.js
|
MIT
|
function _createCanvas(){
var canvasses = document.getElementById("canvasses");
var canvas = document.createElement("canvas");
// Dimensions
var _onResize = function(){
var width = canvasses.clientWidth;
var height = canvasses.clientHeight;
canvas.width = width*2; // retina
canvas.style.width = width+"px";
canvas.height = height*2; // retina
canvas.style.height = height+"px";
};
_onResize();
// Add to body!
canvasses.appendChild(canvas);
// subscribe to RESIZE
subscribe("resize",function(){
_onResize();
});
// Gimme
return canvas;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createCanvas
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
_onResize = function(){
var width = canvasses.clientWidth;
var height = canvasses.clientHeight;
canvas.width = width*2; // retina
canvas.style.width = width+"px";
canvas.height = height*2; // retina
canvas.style.height = height+"px";
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_onResize
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _createLabel(message){
var label = document.createElement("div");
label.innerHTML = message;
label.setAttribute("class","component_label");
return label;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createLabel
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _createButton(label, onclick){
var button = document.createElement("div");
button.innerHTML = label;
button.onclick = onclick;
button.setAttribute("class","component_button");
return button;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createButton
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _createInput(className, textarea){
var input = textarea ? document.createElement("textarea") : document.createElement("input");
input.setAttribute("class",className);
input.addEventListener("keydown",function(event){
event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true);
},false); // STOP IT FROM TRIGGERING KEY.js
return input;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createInput
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _createNumberInput(onUpdate){
var self = {};
// dom!
self.dom = document.createElement("input");
self.dom.style.border = "none";
self.dom.style.width = "40px";
self.dom.style.padding = "5px";
self.dom.addEventListener("keydown",function(event){
event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true);
},false); // STOP IT FROM TRIGGERING KEY.js
// on update
self.dom.onchange = function(){
var value = parseInt(self.getValue());
if(isNaN(value)) value=0;
self.setValue(value);
onUpdate(value);
};
// select on click, yo
self.dom.onclick = function(){
self.dom.select();
};
// set & get value
self.getValue = function(){
return self.dom.value;
};
self.setValue = function(num){
self.dom.value = num;
};
// return an OBJECT.
return self;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createNumberInput
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _blank(){
// just a blank function to toss in.
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_blank
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _getTotalOffset(target){
var bounds = target.getBoundingClientRect();
return {
left: bounds.left,
top: bounds.top
};
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_getTotalOffset
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _addMouseEvents(target, onmousedown, onmousemove, onmouseup){
// WRAP THEM CALLBACKS
var _onmousedown = function(event){
var _fakeEvent = _onmousemove(event);
onmousedown(_fakeEvent);
};
var _onmousemove = function(event){
// Mouse position
var _fakeEvent = {};
if(event.changedTouches){
// Touch
var offset = _getTotalOffset(target);
_fakeEvent.x = event.changedTouches[0].clientX - offset.left;
_fakeEvent.y = event.changedTouches[0].clientY - offset.top;
event.preventDefault();
}else{
// Not Touch
_fakeEvent.x = event.offsetX;
_fakeEvent.y = event.offsetY;
}
// Mousemove callback
onmousemove(_fakeEvent);
return _fakeEvent;
};
var _onmouseup = function(event){
var _fakeEvent = {};
onmouseup(_fakeEvent);
};
// Add events!
target.addEventListener("mousedown", _onmousedown);
target.addEventListener("mousemove", _onmousemove);
document.body.addEventListener("mouseup", _onmouseup);
// TOUCH.
target.addEventListener("touchstart",_onmousedown,false);
target.addEventListener("touchmove",_onmousemove,false);
document.body.addEventListener("touchend",_onmouseup,false);
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_addMouseEvents
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
_onmousedown = function(event){
var _fakeEvent = _onmousemove(event);
onmousedown(_fakeEvent);
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_onmousedown
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
_onmousemove = function(event){
// Mouse position
var _fakeEvent = {};
if(event.changedTouches){
// Touch
var offset = _getTotalOffset(target);
_fakeEvent.x = event.changedTouches[0].clientX - offset.left;
_fakeEvent.y = event.changedTouches[0].clientY - offset.top;
event.preventDefault();
}else{
// Not Touch
_fakeEvent.x = event.offsetX;
_fakeEvent.y = event.offsetY;
}
// Mousemove callback
onmousemove(_fakeEvent);
return _fakeEvent;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_onmousemove
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
_onmouseup = function(event){
var _fakeEvent = {};
onmouseup(_fakeEvent);
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_onmouseup
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _getBounds(points){
// Bounds
var left=Infinity, top=Infinity, right=-Infinity, bottom=-Infinity;
for(var i=0;i<points.length;i++){
var point = points[i];
if(point[0]<left) left=point[0];
if(right<point[0]) right=point[0];
if(point[1]<top) top=point[1];
if(bottom<point[1]) bottom=point[1];
}
// Dimensions
var width = (right-left);
var height = (bottom-top);
// Gimme
return {
left:left, right:right, top:top, bottom:bottom,
width:width, height:height
};
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_getBounds
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _translatePoints(points, dx, dy){
points = JSON.parse(JSON.stringify(points));
for(var i=0;i<points.length;i++){
var p = points[i];
p[0] += dx;
p[1] += dy;
}
return points;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_translatePoints
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _rotatePoints(points, angle){
points = JSON.parse(JSON.stringify(points));
for(var i=0;i<points.length;i++){
var p = points[i];
var x = p[0];
var y = p[1];
p[0] = x*Math.cos(angle) - y*Math.sin(angle);
p[1] = y*Math.cos(angle) + x*Math.sin(angle);
}
return points;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_rotatePoints
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _configureProperties(self, config, properties){
for(var propName in properties){
// Default values!
if(config[propName]===undefined){
var value = properties[propName];
if(typeof value=="function") value=value();
config[propName] = value;
}
// Transfer to "self".
self[propName] = config[propName];
}
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_configureProperties
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _isPointInCircle(x, y, cx, cy, radius){
// Point distance
var dx = cx-x;
var dy = cy-y;
var dist2 = dx*dx + dy*dy;
// My radius
var r2 = radius*radius;
// Inside?
return dist2<=r2;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_isPointInCircle
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _isPointInBox(x, y, box){
if(x<box.x) return false;
if(x>box.x+box.width) return false;
if(y<box.y) return false;
if(y>box.y+box.height) return false;
return true;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_isPointInBox
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _makeErrorFunc(msg){
return function(){
throw Error(msg);
};
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_makeErrorFunc
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _getParameterByName(name){
var url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_getParameterByName
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _blendColors(hex1, hex2, blend){
var color = "#";
for(var i=0; i<3; i++) {
// Into numbers...
var sub1 = hex1.substring(1+2*i, 3+2*i);
var sub2 = hex2.substring(1+2*i, 3+2*i);
var num1 = parseInt(sub1, 16);
var num2 = parseInt(sub2, 16);
// Blended number & sub
var num = Math.floor( num1*(1-blend) + num2*blend );
var sub = num.toString(16).toUpperCase();
var paddedSub = ('0'+sub).slice(-2); // in case it's only one digit long
// Add that babe
color += paddedSub;
}
return color;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_blendColors
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function _shiftArray(array, shiftIndex){
var moveThisAround = array.splice(-shiftIndex);
var shifted = moveThisAround.concat(array);
return shifted;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_shiftArray
|
javascript
|
ncase/loopy
|
splash/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/splash/js/helpers.js
|
CC0-1.0
|
function Ink(loopy){
var self = this;
self.loopy = loopy;
// Create canvas & context
var canvas = _createCanvas();
var ctx = canvas.getContext("2d");
self.canvas = canvas;
self.context = ctx;
// Stroke data!
self.strokeData = [];
// Drawing!
self.drawInk = function(){
if(!Mouse.pressed) return;
// Last point
var lastPoint = self.strokeData[self.strokeData.length-1];
// Style
ctx.strokeStyle = "#ccc";
ctx.lineWidth = 5;
ctx.lineCap = "round";
// Draw line from last to current
ctx.beginPath();
ctx.moveTo(lastPoint[0]*2, lastPoint[1]*2);
ctx.lineTo(Mouse.x*2, Mouse.y*2);
ctx.stroke();
// Update last point
self.strokeData.push([Mouse.x,Mouse.y]);
};
self.reset = function(){
ctx.clearRect(0,0,canvas.width,canvas.height); // Clear canvas
self.strokeData = []; // Reset stroke data
};
subscribe("mousedown",function(){
// ONLY WHEN EDITING w INK
if(self.loopy.mode!=Loopy.MODE_EDIT) return;
if(self.loopy.tool!=Loopy.TOOL_INK) return;
// New stroke data
self.strokeData = [];
self.strokeData.push([Mouse.x,Mouse.y]);
// Draw to canvas!
self.drawInk();
});
subscribe("mousemove",function(){
// ONLY WHEN EDITING w INK
if(self.loopy.mode!=Loopy.MODE_EDIT) return;
if(self.loopy.tool!=Loopy.TOOL_INK) return;
// Draw ink!
self.drawInk();
});
subscribe("mouseup",function(){
// ONLY WHEN EDITING w INK
if(self.loopy.mode!=Loopy.MODE_EDIT) return;
if(self.loopy.tool!=Loopy.TOOL_INK) return;
if(self.strokeData.length<2) return;
if(!Mouse.moved) return;
/*************************
Detect what you drew!
1. Started in a node?
1a. If ended near/in a node, it's an EDGE.
2. If not, it's a NODE. // TODO: actual circle detection?
*************************/
// Started in a node?
var startPoint = self.strokeData[0];
var startNode = loopy.model.getNodeByPoint(startPoint[0], startPoint[1]);
if(!startNode) startNode=loopy.model.getNodeByPoint(startPoint[0], startPoint[1], 20); // try again with buffer
// Ended in a node?
var endPoint = self.strokeData[self.strokeData.length-1];
var endNode = loopy.model.getNodeByPoint(endPoint[0], endPoint[1]);
if(!endNode) endNode=loopy.model.getNodeByPoint(endPoint[0], endPoint[1], 40); // try again with buffer
// EDGE: started AND ended in nodes
if(startNode && endNode){
// Config!
var edgeConfig = {
from: startNode.id,
to: endNode.id
};
// If it's the same node...
if(startNode==endNode){
// TODO: clockwise or counterclockwise???
// TODO: if the arc DOES NOT go beyond radius, don't make self-connecting edge. also min distance.
// Find rotation first by getting average point
var bounds = _getBounds(self.strokeData);
var x = (bounds.left+bounds.right)/2;
var y = (bounds.top+bounds.bottom)/2;
var dx = x-startNode.x;
var dy = y-startNode.y;
var angle = Math.atan2(dy,dx);
// Then, find arc height.
var translated = _translatePoints(self.strokeData, -startNode.x, -startNode.y);
var rotated = _rotatePoints(translated, -angle);
bounds = _getBounds(rotated);
// Arc & Rotation!
edgeConfig.rotation = angle*(360/Math.TAU) + 90;
edgeConfig.arc = bounds.right;
// ACTUALLY, IF THE ARC IS *NOT* GREATER THAN THE RADIUS, DON'T DO IT.
// (and otherwise, make sure minimum distance of radius+25)
if(edgeConfig.arc < startNode.radius){
edgeConfig=null;
loopy.sidebar.edit(startNode); // you were probably trying to edit the node
}else{
var minimum = startNode.radius+25;
if(edgeConfig.arc<minimum) edgeConfig.arc=minimum;
}
}else{
// Otherwise, find the arc by translating & rotating
var dx = endNode.x-startNode.x;
var dy = endNode.y-startNode.y;
var angle = Math.atan2(dy,dx);
var translated = _translatePoints(self.strokeData, -startNode.x, -startNode.y);
var rotated = _rotatePoints(translated, -angle);
var bounds = _getBounds(rotated);
// Arc!
if(Math.abs(bounds.top)>Math.abs(bounds.bottom)) edgeConfig.arc = -bounds.top;
else edgeConfig.arc = -bounds.bottom;
}
// Add the edge!
if(edgeConfig){
var newEdge = loopy.model.addEdge(edgeConfig);
loopy.sidebar.edit(newEdge);
}
}
// NODE: did NOT start in a node.
if(!startNode){
// Just roughly make a circle the size of the bounds of the circle
var bounds = _getBounds(self.strokeData);
var x = (bounds.left+bounds.right)/2;
var y = (bounds.top+bounds.bottom)/2;
var r = ((bounds.width/2)+(bounds.height/2))/2;
// Circle can't be TOO smol
if(r>15){
// Snap to radius
/*r = Math.round(r/Ink.SNAP_TO_RADIUS)*Ink.SNAP_TO_RADIUS;
if(r<Ink.MINIMUM_RADIUS) r=Ink.MINIMUM_RADIUS;*/
// LOCK TO JUST SMALLEST CIRCLE.
r = Ink.MINIMUM_RADIUS;
// Make that node!
var newNode = loopy.model.addNode({
x:x,
y:y,
radius:r
});
// Edit it immediately
loopy.sidebar.edit(newNode);
}
}
// Reset.
self.reset();
});
subscribe("mouseclick",function(){
// ONLY WHEN EDITING w INK
if(self.loopy.mode!=Loopy.MODE_EDIT) return;
if(self.loopy.tool!=Loopy.TOOL_INK) return;
// Reset
self.reset();
});
}
|
*******************************
LOOPY!
- with edit & play mode
TODO: smoother bezier curve?
TODO: when switch away tool, clear the Ink canvas
********************************
|
Ink
|
javascript
|
ncase/loopy
|
splash/js/Ink.js
|
https://github.com/ncase/loopy/blob/master/splash/js/Ink.js
|
CC0-1.0
|
_onUpdate = function(){
var embedCode = '<iframe width="'+width.getValue()+'" height="'+height.getValue()+'" frameborder="0" src="'+iframeSRC+'"></iframe>';
output.output(embedCode);
}
|
********************
Use the same PAGE UI thing
**********************
|
_onUpdate
|
javascript
|
ncase/loopy
|
splash/js/Modal.js
|
https://github.com/ncase/loopy/blob/master/splash/js/Modal.js
|
CC0-1.0
|
function ModalIframe(config){
var self = this;
// IFRAME
var iframe = document.createElement("iframe");
self.dom = iframe;
iframe.width = config.width;
iframe.height = config.height;
// Show & Hide
if(!config.manual){
config.page.onshow = function(){
iframe.src = config.src;
};
config.page.onhide = function(){
iframe.removeAttribute("src");
};
}
}
|
********************
Use the same PAGE UI thing
**********************
|
ModalIframe
|
javascript
|
ncase/loopy
|
splash/js/Modal.js
|
https://github.com/ncase/loopy/blob/master/splash/js/Modal.js
|
CC0-1.0
|
function PageUI(dom){
var self = this;
self.dom = dom;
self.pages = [];
self.addPage = function(id, page){
page.id = id;
self.dom.appendChild(page.dom);
self.pages.push(page);
};
self.currentPage = null;
self.showPage = function(id){
var shownPage = null;
for(var i=0; i<self.pages.length; i++){
var page = self.pages[i];
if(page.id==id){
page.show();
shownPage = page;
}else{
page.hide();
}
}
self.currentPage = shownPage;
return shownPage;
};
}
|
*******************************
PAGE UI: to extend to Sidebar, Play Controls, Modal.
********************************
|
PageUI
|
javascript
|
ncase/loopy
|
splash/js/PageUI.js
|
https://github.com/ncase/loopy/blob/master/splash/js/PageUI.js
|
CC0-1.0
|
function Page(){
var self = this;
// DOM
self.dom = document.createElement("div");
self.show = function(){ self.dom.style.display="block"; };
self.hide = function(){ self.dom.style.display="none"; };
// Add Component
self.addComponent = function(component){
self.dom.appendChild(component.dom); // add to DOM
return component;
};
}
|
*******************************
PAGE UI: to extend to Sidebar, Play Controls, Modal.
********************************
|
Page
|
javascript
|
ncase/loopy
|
splash/js/PageUI.js
|
https://github.com/ncase/loopy/blob/master/splash/js/PageUI.js
|
CC0-1.0
|
function PlayButton(config){
var self = this;
var label = "<div class='play_button_icon' icon='"+config.icon+"'></div> "
+ "<div class='play_button_label'>"+config.label+"</div>";
self.dom = _createButton(label, function(){
config.onclick();
});
// Tooltip!
if(config.tooltip){
self.dom.setAttribute("data-balloon", config.tooltip);
self.dom.setAttribute("data-balloon-pos", "top");
}
}
|
*******************************
PLAY CONTROLS CODE:
- play
- pause/reset/speed
********************************
|
PlayButton
|
javascript
|
ncase/loopy
|
splash/js/PlayControls.js
|
https://github.com/ncase/loopy/blob/master/splash/js/PlayControls.js
|
CC0-1.0
|
function PlaySlider(config){
var self = this;
self.dom = document.createElement("div");
self.dom.style.bottom = "0px";
self.dom.style.position = "absolute";
self.dom.style.width = "100%";
self.dom.style.height = "20px";
// Input
var input = document.createElement("input");
input.setAttribute("class","play_slider");
self.dom.appendChild(input);
// Slow & Fast Icons
var img = new Image();
img.src = "css/icons/speed_slow.png";
img.width = 20;
img.height = 15;
img.style.position = "absolute";
img.style.left = "5px";
img.style.top = "-2px";
self.dom.appendChild(img);
var img = new Image();
img.src = "css/icons/speed_fast.png";
img.width = 20;
img.height = 15;
img.style.position = "absolute";
img.style.right = "5px";
img.style.top = "-2px";
self.dom.appendChild(img);
// Properties
input.type = "range";
input.value = config.value;
input.step = config.step;
input.min = config.min;
input.max = config.max;
input.oninput = function(event){
config.oninput(input.value);
};
}
|
*******************************
PLAY CONTROLS CODE:
- play
- pause/reset/speed
********************************
|
PlaySlider
|
javascript
|
ncase/loopy
|
splash/js/PlayControls.js
|
https://github.com/ncase/loopy/blob/master/splash/js/PlayControls.js
|
CC0-1.0
|
function _createCanvas(){
var canvasses = document.getElementById("canvasses");
var canvas = document.createElement("canvas");
// Dimensions
var _onResize = function(){
var width = canvasses.clientWidth;
var height = canvasses.clientHeight;
canvas.width = width*2; // retina
canvas.style.width = width+"px";
canvas.height = height*2; // retina
canvas.style.height = height+"px";
};
_onResize();
// Add to body!
canvasses.appendChild(canvas);
// subscribe to RESIZE
subscribe("resize",function(){
_onResize();
});
// Gimme
return canvas;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createCanvas
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
_onResize = function(){
var width = canvasses.clientWidth;
var height = canvasses.clientHeight;
canvas.width = width*2; // retina
canvas.style.width = width+"px";
canvas.height = height*2; // retina
canvas.style.height = height+"px";
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_onResize
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _createLabel(message){
var label = document.createElement("div");
label.innerHTML = message;
label.setAttribute("class","component_label");
return label;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createLabel
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _createButton(label, onclick){
var button = document.createElement("div");
button.innerHTML = label;
button.onclick = onclick;
button.setAttribute("class","component_button");
return button;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createButton
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _createInput(className, textarea){
var input = textarea ? document.createElement("textarea") : document.createElement("input");
input.setAttribute("class",className);
input.addEventListener("keydown",function(event){
event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true);
},false); // STOP IT FROM TRIGGERING KEY.js
return input;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createInput
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _createNumberInput(onUpdate){
var self = {};
// dom!
self.dom = document.createElement("input");
self.dom.style.border = "none";
self.dom.style.width = "40px";
self.dom.style.padding = "5px";
self.dom.addEventListener("keydown",function(event){
event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true);
},false); // STOP IT FROM TRIGGERING KEY.js
// on update
self.dom.onchange = function(){
var value = parseInt(self.getValue());
if(isNaN(value)) value=0;
self.setValue(value);
onUpdate(value);
};
// select on click, yo
self.dom.onclick = function(){
self.dom.select();
};
// set & get value
self.getValue = function(){
return self.dom.value;
};
self.setValue = function(num){
self.dom.value = num;
};
// return an OBJECT.
return self;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createNumberInput
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _blank(){
// just a blank function to toss in.
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_blank
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _getTotalOffset(target){
var bounds = target.getBoundingClientRect();
return {
left: bounds.left,
top: bounds.top
};
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_getTotalOffset
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _addMouseEvents(target, onmousedown, onmousemove, onmouseup){
// WRAP THEM CALLBACKS
var _onmousedown = function(event){
var _fakeEvent = _onmousemove(event);
onmousedown(_fakeEvent);
};
var _onmousemove = function(event){
// Mouse position
var _fakeEvent = {};
if(event.changedTouches){
// Touch
var offset = _getTotalOffset(target);
_fakeEvent.x = event.changedTouches[0].clientX - offset.left;
_fakeEvent.y = event.changedTouches[0].clientY - offset.top;
event.preventDefault();
}else{
// Not Touch
_fakeEvent.x = event.offsetX;
_fakeEvent.y = event.offsetY;
}
// Mousemove callback
onmousemove(_fakeEvent);
return _fakeEvent;
};
var _onmouseup = function(event){
var _fakeEvent = {};
onmouseup(_fakeEvent);
};
// Add events!
target.addEventListener("mousedown", _onmousedown);
target.addEventListener("mousemove", _onmousemove);
document.body.addEventListener("mouseup", _onmouseup);
// TOUCH.
target.addEventListener("touchstart",_onmousedown,false);
target.addEventListener("touchmove",_onmousemove,false);
document.body.addEventListener("touchend",_onmouseup,false);
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_addMouseEvents
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
_onmousedown = function(event){
var _fakeEvent = _onmousemove(event);
onmousedown(_fakeEvent);
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_onmousedown
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
_onmousemove = function(event){
// Mouse position
var _fakeEvent = {};
if(event.changedTouches){
// Touch
var offset = _getTotalOffset(target);
_fakeEvent.x = event.changedTouches[0].clientX - offset.left;
_fakeEvent.y = event.changedTouches[0].clientY - offset.top;
event.preventDefault();
}else{
// Not Touch
_fakeEvent.x = event.offsetX;
_fakeEvent.y = event.offsetY;
}
// Mousemove callback
onmousemove(_fakeEvent);
return _fakeEvent;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_onmousemove
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
_onmouseup = function(event){
var _fakeEvent = {};
onmouseup(_fakeEvent);
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_onmouseup
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _getBounds(points){
// Bounds
var left=Infinity, top=Infinity, right=-Infinity, bottom=-Infinity;
for(var i=0;i<points.length;i++){
var point = points[i];
if(point[0]<left) left=point[0];
if(right<point[0]) right=point[0];
if(point[1]<top) top=point[1];
if(bottom<point[1]) bottom=point[1];
}
// Dimensions
var width = (right-left);
var height = (bottom-top);
// Gimme
return {
left:left, right:right, top:top, bottom:bottom,
width:width, height:height
};
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_getBounds
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _translatePoints(points, dx, dy){
points = JSON.parse(JSON.stringify(points));
for(var i=0;i<points.length;i++){
var p = points[i];
p[0] += dx;
p[1] += dy;
}
return points;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_translatePoints
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _rotatePoints(points, angle){
points = JSON.parse(JSON.stringify(points));
for(var i=0;i<points.length;i++){
var p = points[i];
var x = p[0];
var y = p[1];
p[0] = x*Math.cos(angle) - y*Math.sin(angle);
p[1] = y*Math.cos(angle) + x*Math.sin(angle);
}
return points;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_rotatePoints
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _configureProperties(self, config, properties){
for(var propName in properties){
// Default values!
if(config[propName]===undefined){
var value = properties[propName];
if(typeof value=="function") value=value();
config[propName] = value;
}
// Transfer to "self".
self[propName] = config[propName];
}
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_configureProperties
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _isPointInCircle(x, y, cx, cy, radius){
// Point distance
var dx = cx-x;
var dy = cy-y;
var dist2 = dx*dx + dy*dy;
// My radius
var r2 = radius*radius;
// Inside?
return dist2<=r2;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_isPointInCircle
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _isPointInBox(x, y, box){
if(x<box.x) return false;
if(x>box.x+box.width) return false;
if(y<box.y) return false;
if(y>box.y+box.height) return false;
return true;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_isPointInBox
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _makeErrorFunc(msg){
return function(){
throw Error(msg);
};
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_makeErrorFunc
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _getParameterByName(name){
var url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_getParameterByName
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _blendColors(hex1, hex2, blend){
var color = "#";
for(var i=0; i<3; i++) {
// Into numbers...
var sub1 = hex1.substring(1+2*i, 3+2*i);
var sub2 = hex2.substring(1+2*i, 3+2*i);
var num1 = parseInt(sub1, 16);
var num2 = parseInt(sub2, 16);
// Blended number & sub
var num = Math.floor( num1*(1-blend) + num2*blend );
var sub = num.toString(16).toUpperCase();
var paddedSub = ('0'+sub).slice(-2); // in case it's only one digit long
// Add that babe
color += paddedSub;
}
return color;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_blendColors
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function _shiftArray(array, shiftIndex){
var moveThisAround = array.splice(-shiftIndex);
var shifted = moveThisAround.concat(array);
return shifted;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_shiftArray
|
javascript
|
ncase/loopy
|
v1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1/js/helpers.js
|
CC0-1.0
|
function Ink(loopy){
var self = this;
self.loopy = loopy;
// Create canvas & context
var canvas = _createCanvas();
var ctx = canvas.getContext("2d");
self.canvas = canvas;
self.context = ctx;
// Stroke data!
self.strokeData = [];
// Drawing!
self.drawInk = function(){
if(!Mouse.pressed) return;
// Last point
var lastPoint = self.strokeData[self.strokeData.length-1];
// Style
ctx.strokeStyle = "#ccc";
ctx.lineWidth = 5;
ctx.lineCap = "round";
// Draw line from last to current
ctx.beginPath();
ctx.moveTo(lastPoint[0]*2, lastPoint[1]*2);
ctx.lineTo(Mouse.x*2, Mouse.y*2);
ctx.stroke();
// Update last point
self.strokeData.push([Mouse.x,Mouse.y]);
};
self.reset = function(){
ctx.clearRect(0,0,canvas.width,canvas.height); // Clear canvas
self.strokeData = []; // Reset stroke data
};
subscribe("mousedown",function(){
// ONLY WHEN EDITING w INK
if(self.loopy.mode!=Loopy.MODE_EDIT) return;
if(self.loopy.tool!=Loopy.TOOL_INK) return;
// New stroke data
self.strokeData = [];
self.strokeData.push([Mouse.x,Mouse.y]);
// Draw to canvas!
self.drawInk();
});
subscribe("mousemove",function(){
// ONLY WHEN EDITING w INK
if(self.loopy.mode!=Loopy.MODE_EDIT) return;
if(self.loopy.tool!=Loopy.TOOL_INK) return;
// Draw ink!
self.drawInk();
});
subscribe("mouseup",function(){
// ONLY WHEN EDITING w INK
if(self.loopy.mode!=Loopy.MODE_EDIT) return;
if(self.loopy.tool!=Loopy.TOOL_INK) return;
if(self.strokeData.length<2) return;
if(!Mouse.moved) return;
/*************************
Detect what you drew!
1. Started in a node?
1a. If ended near/in a node, it's an EDGE.
2. If not, it's a NODE. // TODO: actual circle detection?
*************************/
// Started in a node?
var startPoint = self.strokeData[0];
var startNode = loopy.model.getNodeByPoint(startPoint[0], startPoint[1]);
if(!startNode) startNode=loopy.model.getNodeByPoint(startPoint[0], startPoint[1], 20); // try again with buffer
// Ended in a node?
var endPoint = self.strokeData[self.strokeData.length-1];
var endNode = loopy.model.getNodeByPoint(endPoint[0], endPoint[1]);
if(!endNode) endNode=loopy.model.getNodeByPoint(endPoint[0], endPoint[1], 40); // try again with buffer
// EDGE: started AND ended in nodes
if(startNode && endNode){
// Config!
var edgeConfig = {
from: startNode.id,
to: endNode.id
};
// If it's the same node...
if(startNode==endNode){
// TODO: clockwise or counterclockwise???
// TODO: if the arc DOES NOT go beyond radius, don't make self-connecting edge. also min distance.
// Find rotation first by getting average point
var bounds = _getBounds(self.strokeData);
var x = (bounds.left+bounds.right)/2;
var y = (bounds.top+bounds.bottom)/2;
var dx = x-startNode.x;
var dy = y-startNode.y;
var angle = Math.atan2(dy,dx);
// Then, find arc height.
var translated = _translatePoints(self.strokeData, -startNode.x, -startNode.y);
var rotated = _rotatePoints(translated, -angle);
bounds = _getBounds(rotated);
// Arc & Rotation!
edgeConfig.rotation = angle*(360/Math.TAU) + 90;
edgeConfig.arc = bounds.right;
// ACTUALLY, IF THE ARC IS *NOT* GREATER THAN THE RADIUS, DON'T DO IT.
// (and otherwise, make sure minimum distance of radius+25)
if(edgeConfig.arc < startNode.radius){
edgeConfig=null;
loopy.sidebar.edit(startNode); // you were probably trying to edit the node
}else{
var minimum = startNode.radius+25;
if(edgeConfig.arc<minimum) edgeConfig.arc=minimum;
}
}else{
// Otherwise, find the arc by translating & rotating
var dx = endNode.x-startNode.x;
var dy = endNode.y-startNode.y;
var angle = Math.atan2(dy,dx);
var translated = _translatePoints(self.strokeData, -startNode.x, -startNode.y);
var rotated = _rotatePoints(translated, -angle);
var bounds = _getBounds(rotated);
// Arc!
if(Math.abs(bounds.top)>Math.abs(bounds.bottom)) edgeConfig.arc = -bounds.top;
else edgeConfig.arc = -bounds.bottom;
}
// Add the edge!
if(edgeConfig){
var newEdge = loopy.model.addEdge(edgeConfig);
loopy.sidebar.edit(newEdge);
}
}
// NODE: did NOT start in a node.
if(!startNode){
// Just roughly make a circle the size of the bounds of the circle
var bounds = _getBounds(self.strokeData);
var x = (bounds.left+bounds.right)/2;
var y = (bounds.top+bounds.bottom)/2;
var r = ((bounds.width/2)+(bounds.height/2))/2;
// Circle can't be TOO smol
if(r>15){
// Snap to radius
/*r = Math.round(r/Ink.SNAP_TO_RADIUS)*Ink.SNAP_TO_RADIUS;
if(r<Ink.MINIMUM_RADIUS) r=Ink.MINIMUM_RADIUS;*/
// LOCK TO JUST SMALLEST CIRCLE.
r = Ink.MINIMUM_RADIUS;
// Make that node!
var newNode = loopy.model.addNode({
x:x,
y:y,
radius:r
});
// Edit it immediately
loopy.sidebar.edit(newNode);
}
}
// Reset.
self.reset();
});
subscribe("mouseclick",function(){
// ONLY WHEN EDITING w INK
if(self.loopy.mode!=Loopy.MODE_EDIT) return;
if(self.loopy.tool!=Loopy.TOOL_INK) return;
// Reset
self.reset();
});
}
|
*******************************
LOOPY!
- with edit & play mode
TODO: smoother bezier curve?
TODO: when switch away tool, clear the Ink canvas
********************************
|
Ink
|
javascript
|
ncase/loopy
|
v1/js/Ink.js
|
https://github.com/ncase/loopy/blob/master/v1/js/Ink.js
|
CC0-1.0
|
_onUpdate = function(){
var embedCode = '<iframe width="'+width.getValue()+'" height="'+height.getValue()+'" frameborder="0" src="'+iframeSRC+'"></iframe>';
output.output(embedCode);
}
|
********************
Use the same PAGE UI thing
**********************
|
_onUpdate
|
javascript
|
ncase/loopy
|
v1/js/Modal.js
|
https://github.com/ncase/loopy/blob/master/v1/js/Modal.js
|
CC0-1.0
|
function ModalIframe(config){
var self = this;
// IFRAME
var iframe = document.createElement("iframe");
self.dom = iframe;
iframe.width = config.width;
iframe.height = config.height;
// Show & Hide
if(!config.manual){
config.page.onshow = function(){
iframe.src = config.src;
};
config.page.onhide = function(){
iframe.removeAttribute("src");
};
}
}
|
********************
Use the same PAGE UI thing
**********************
|
ModalIframe
|
javascript
|
ncase/loopy
|
v1/js/Modal.js
|
https://github.com/ncase/loopy/blob/master/v1/js/Modal.js
|
CC0-1.0
|
function PageUI(dom){
var self = this;
self.dom = dom;
self.pages = [];
self.addPage = function(id, page){
page.id = id;
self.dom.appendChild(page.dom);
self.pages.push(page);
};
self.currentPage = null;
self.showPage = function(id){
var shownPage = null;
for(var i=0; i<self.pages.length; i++){
var page = self.pages[i];
if(page.id==id){
page.show();
shownPage = page;
}else{
page.hide();
}
}
self.currentPage = shownPage;
return shownPage;
};
}
|
*******************************
PAGE UI: to extend to Sidebar, Play Controls, Modal.
********************************
|
PageUI
|
javascript
|
ncase/loopy
|
v1/js/PageUI.js
|
https://github.com/ncase/loopy/blob/master/v1/js/PageUI.js
|
CC0-1.0
|
function Page(){
var self = this;
// DOM
self.dom = document.createElement("div");
self.show = function(){ self.dom.style.display="block"; };
self.hide = function(){ self.dom.style.display="none"; };
// Add Component
self.addComponent = function(component){
self.dom.appendChild(component.dom); // add to DOM
return component;
};
}
|
*******************************
PAGE UI: to extend to Sidebar, Play Controls, Modal.
********************************
|
Page
|
javascript
|
ncase/loopy
|
v1/js/PageUI.js
|
https://github.com/ncase/loopy/blob/master/v1/js/PageUI.js
|
CC0-1.0
|
function PlayButton(config){
var self = this;
var label = "<div class='play_button_icon' icon='"+config.icon+"'></div> "
+ "<div class='play_button_label'>"+config.label+"</div>";
self.dom = _createButton(label, function(){
config.onclick();
});
// Tooltip!
if(config.tooltip){
self.dom.setAttribute("data-balloon", config.tooltip);
self.dom.setAttribute("data-balloon-pos", "top");
}
}
|
*******************************
PLAY CONTROLS CODE:
- play
- pause/reset/speed
********************************
|
PlayButton
|
javascript
|
ncase/loopy
|
v1/js/PlayControls.js
|
https://github.com/ncase/loopy/blob/master/v1/js/PlayControls.js
|
CC0-1.0
|
function PlaySlider(config){
var self = this;
self.dom = document.createElement("div");
self.dom.style.bottom = "0px";
self.dom.style.position = "absolute";
self.dom.style.width = "100%";
self.dom.style.height = "20px";
// Input
var input = document.createElement("input");
input.setAttribute("class","play_slider");
self.dom.appendChild(input);
// Slow & Fast Icons
var img = new Image();
img.src = "css/icons/speed_slow.png";
img.width = 20;
img.height = 15;
img.style.position = "absolute";
img.style.left = "5px";
img.style.top = "-2px";
self.dom.appendChild(img);
var img = new Image();
img.src = "css/icons/speed_fast.png";
img.width = 20;
img.height = 15;
img.style.position = "absolute";
img.style.right = "5px";
img.style.top = "-2px";
self.dom.appendChild(img);
// Properties
input.type = "range";
input.value = config.value;
input.step = config.step;
input.min = config.min;
input.max = config.max;
input.oninput = function(event){
config.oninput(input.value);
};
}
|
*******************************
PLAY CONTROLS CODE:
- play
- pause/reset/speed
********************************
|
PlaySlider
|
javascript
|
ncase/loopy
|
v1/js/PlayControls.js
|
https://github.com/ncase/loopy/blob/master/v1/js/PlayControls.js
|
CC0-1.0
|
function _createCanvas(){
var canvasses = document.getElementById("canvasses");
var canvas = document.createElement("canvas");
// Dimensions
var _onResize = function(){
var width = canvasses.clientWidth;
var height = canvasses.clientHeight;
canvas.width = width*2; // retina
canvas.style.width = width+"px";
canvas.height = height*2; // retina
canvas.style.height = height+"px";
};
_onResize();
// Add to body!
canvasses.appendChild(canvas);
// subscribe to RESIZE
subscribe("resize",function(){
_onResize();
});
// Gimme
return canvas;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createCanvas
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
_onResize = function(){
var width = canvasses.clientWidth;
var height = canvasses.clientHeight;
canvas.width = width*2; // retina
canvas.style.width = width+"px";
canvas.height = height*2; // retina
canvas.style.height = height+"px";
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_onResize
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _createLabel(message){
var label = document.createElement("div");
label.innerHTML = message;
label.setAttribute("class","component_label");
return label;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createLabel
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _createButton(label, onclick){
var button = document.createElement("div");
button.innerHTML = label;
button.onclick = onclick;
button.setAttribute("class","component_button");
return button;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createButton
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _createInput(className, textarea){
var input = textarea ? document.createElement("textarea") : document.createElement("input");
input.setAttribute("class",className);
input.addEventListener("keydown",function(event){
event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true);
},false); // STOP IT FROM TRIGGERING KEY.js
return input;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createInput
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _createNumberInput(onUpdate){
var self = {};
// dom!
self.dom = document.createElement("input");
self.dom.style.border = "none";
self.dom.style.width = "40px";
self.dom.style.padding = "5px";
self.dom.addEventListener("keydown",function(event){
event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true);
},false); // STOP IT FROM TRIGGERING KEY.js
// on update
self.dom.onchange = function(){
var value = parseInt(self.getValue());
if(isNaN(value)) value=0;
self.setValue(value);
onUpdate(value);
};
// select on click, yo
self.dom.onclick = function(){
self.dom.select();
};
// set & get value
self.getValue = function(){
return self.dom.value;
};
self.setValue = function(num){
self.dom.value = num;
};
// return an OBJECT.
return self;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_createNumberInput
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _blank(){
// just a blank function to toss in.
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_blank
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _getTotalOffset(target){
var bounds = target.getBoundingClientRect();
return {
left: bounds.left,
top: bounds.top
};
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_getTotalOffset
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _addMouseEvents(target, onmousedown, onmousemove, onmouseup){
// WRAP THEM CALLBACKS
var _onmousedown = function(event){
var _fakeEvent = _onmousemove(event);
onmousedown(_fakeEvent);
};
var _onmousemove = function(event){
// Mouse position
var _fakeEvent = {};
if(event.changedTouches){
// Touch
var offset = _getTotalOffset(target);
_fakeEvent.x = event.changedTouches[0].clientX - offset.left;
_fakeEvent.y = event.changedTouches[0].clientY - offset.top;
event.preventDefault();
}else{
// Not Touch
_fakeEvent.x = event.offsetX;
_fakeEvent.y = event.offsetY;
}
// Mousemove callback
onmousemove(_fakeEvent);
return _fakeEvent;
};
var _onmouseup = function(event){
var _fakeEvent = {};
onmouseup(_fakeEvent);
};
// Add events!
target.addEventListener("mousedown", _onmousedown);
target.addEventListener("mousemove", _onmousemove);
document.body.addEventListener("mouseup", _onmouseup);
// TOUCH.
target.addEventListener("touchstart",_onmousedown,false);
target.addEventListener("touchmove",_onmousemove,false);
document.body.addEventListener("touchend",_onmouseup,false);
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_addMouseEvents
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
_onmousedown = function(event){
var _fakeEvent = _onmousemove(event);
onmousedown(_fakeEvent);
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_onmousedown
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
_onmousemove = function(event){
// Mouse position
var _fakeEvent = {};
if(event.changedTouches){
// Touch
var offset = _getTotalOffset(target);
_fakeEvent.x = event.changedTouches[0].clientX - offset.left;
_fakeEvent.y = event.changedTouches[0].clientY - offset.top;
event.preventDefault();
}else{
// Not Touch
_fakeEvent.x = event.offsetX;
_fakeEvent.y = event.offsetY;
}
// Mousemove callback
onmousemove(_fakeEvent);
return _fakeEvent;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_onmousemove
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
_onmouseup = function(event){
var _fakeEvent = {};
onmouseup(_fakeEvent);
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_onmouseup
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _getBounds(points){
// Bounds
var left=Infinity, top=Infinity, right=-Infinity, bottom=-Infinity;
for(var i=0;i<points.length;i++){
var point = points[i];
if(point[0]<left) left=point[0];
if(right<point[0]) right=point[0];
if(point[1]<top) top=point[1];
if(bottom<point[1]) bottom=point[1];
}
// Dimensions
var width = (right-left);
var height = (bottom-top);
// Gimme
return {
left:left, right:right, top:top, bottom:bottom,
width:width, height:height
};
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_getBounds
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _translatePoints(points, dx, dy){
points = JSON.parse(JSON.stringify(points));
for(var i=0;i<points.length;i++){
var p = points[i];
p[0] += dx;
p[1] += dy;
}
return points;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_translatePoints
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _rotatePoints(points, angle){
points = JSON.parse(JSON.stringify(points));
for(var i=0;i<points.length;i++){
var p = points[i];
var x = p[0];
var y = p[1];
p[0] = x*Math.cos(angle) - y*Math.sin(angle);
p[1] = y*Math.cos(angle) + x*Math.sin(angle);
}
return points;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_rotatePoints
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _configureProperties(self, config, properties){
for(var propName in properties){
// Default values!
if(config[propName]===undefined){
var value = properties[propName];
if(typeof value=="function") value=value();
config[propName] = value;
}
// Transfer to "self".
self[propName] = config[propName];
}
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_configureProperties
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _isPointInCircle(x, y, cx, cy, radius){
// Point distance
var dx = cx-x;
var dy = cy-y;
var dist2 = dx*dx + dy*dy;
// My radius
var r2 = radius*radius;
// Inside?
return dist2<=r2;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_isPointInCircle
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _isPointInBox(x, y, box){
if(x<box.x) return false;
if(x>box.x+box.width) return false;
if(y<box.y) return false;
if(y>box.y+box.height) return false;
return true;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_isPointInBox
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _makeErrorFunc(msg){
return function(){
throw Error(msg);
};
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_makeErrorFunc
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _getParameterByName(name){
var url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_getParameterByName
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _blendColors(hex1, hex2, blend){
var color = "#";
for(var i=0; i<3; i++) {
// Into numbers...
var sub1 = hex1.substring(1+2*i, 3+2*i);
var sub2 = hex2.substring(1+2*i, 3+2*i);
var num1 = parseInt(sub1, 16);
var num2 = parseInt(sub2, 16);
// Blended number & sub
var num = Math.floor( num1*(1-blend) + num2*blend );
var sub = num.toString(16).toUpperCase();
var paddedSub = ('0'+sub).slice(-2); // in case it's only one digit long
// Add that babe
color += paddedSub;
}
return color;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_blendColors
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function _shiftArray(array, shiftIndex){
var moveThisAround = array.splice(-shiftIndex);
var shifted = moveThisAround.concat(array);
return shifted;
}
|
**************************
A miscellaneous collection of reuseable helper methods
that I couldn't be arsed to put into separate classes
***************************
|
_shiftArray
|
javascript
|
ncase/loopy
|
v1.1/js/helpers.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/helpers.js
|
CC0-1.0
|
function Ink(loopy){
var self = this;
self.loopy = loopy;
// Create canvas & context
var canvas = _createCanvas();
var ctx = canvas.getContext("2d");
self.canvas = canvas;
self.context = ctx;
// Stroke data!
self.strokeData = [];
// Drawing!
self.drawInk = function(){
if(!Mouse.pressed) return;
// Last point
var lastPoint = self.strokeData[self.strokeData.length-1];
// Style
ctx.strokeStyle = "#ccc";
ctx.lineWidth = 5;
ctx.lineCap = "round";
// Draw line from last to current
ctx.beginPath();
ctx.moveTo(lastPoint[0]*2, lastPoint[1]*2);
ctx.lineTo(Mouse.x*2, Mouse.y*2);
ctx.stroke();
// Update last point
self.strokeData.push([Mouse.x,Mouse.y]);
};
self.reset = function(){
ctx.clearRect(0,0,canvas.width,canvas.height); // Clear canvas
self.strokeData = []; // Reset stroke data
};
subscribe("mousedown",function(){
// ONLY WHEN EDITING w INK
if(self.loopy.mode!=Loopy.MODE_EDIT) return;
if(self.loopy.tool!=Loopy.TOOL_INK) return;
// New stroke data
self.strokeData = [];
self.strokeData.push([Mouse.x,Mouse.y]);
// Draw to canvas!
self.drawInk();
});
subscribe("mousemove",function(){
// ONLY WHEN EDITING w INK
if(self.loopy.mode!=Loopy.MODE_EDIT) return;
if(self.loopy.tool!=Loopy.TOOL_INK) return;
// Draw ink!
self.drawInk();
});
subscribe("mouseup",function(){
// ONLY WHEN EDITING w INK
if(self.loopy.mode!=Loopy.MODE_EDIT) return;
if(self.loopy.tool!=Loopy.TOOL_INK) return;
if(self.strokeData.length<2) return;
if(!Mouse.moved) return;
/*************************
Detect what you drew!
1. Started in a node?
1a. If ended near/in a node, it's an EDGE.
2. If not, it's a NODE. // TODO: actual circle detection?
*************************/
// Started in a node?
var startPoint = self.strokeData[0];
var startNode = loopy.model.getNodeByPoint(startPoint[0], startPoint[1]);
if(!startNode) startNode=loopy.model.getNodeByPoint(startPoint[0], startPoint[1], 20); // try again with buffer
// Ended in a node?
var endPoint = self.strokeData[self.strokeData.length-1];
var endNode = loopy.model.getNodeByPoint(endPoint[0], endPoint[1]);
if(!endNode) endNode=loopy.model.getNodeByPoint(endPoint[0], endPoint[1], 40); // try again with buffer
// EDGE: started AND ended in nodes
if(startNode && endNode){
// Config!
var edgeConfig = {
from: startNode.id,
to: endNode.id
};
// If it's the same node...
if(startNode==endNode){
// TODO: clockwise or counterclockwise???
// TODO: if the arc DOES NOT go beyond radius, don't make self-connecting edge. also min distance.
// Find rotation first by getting average point
var bounds = _getBounds(self.strokeData);
var x = (bounds.left+bounds.right)/2;
var y = (bounds.top+bounds.bottom)/2;
var dx = x-startNode.x;
var dy = y-startNode.y;
var angle = Math.atan2(dy,dx);
// Then, find arc height.
var translated = _translatePoints(self.strokeData, -startNode.x, -startNode.y);
var rotated = _rotatePoints(translated, -angle);
bounds = _getBounds(rotated);
// Arc & Rotation!
edgeConfig.rotation = angle*(360/Math.TAU) + 90;
edgeConfig.arc = bounds.right;
// ACTUALLY, IF THE ARC IS *NOT* GREATER THAN THE RADIUS, DON'T DO IT.
// (and otherwise, make sure minimum distance of radius+25)
if(edgeConfig.arc < startNode.radius){
edgeConfig=null;
loopy.sidebar.edit(startNode); // you were probably trying to edit the node
}else{
var minimum = startNode.radius+25;
if(edgeConfig.arc<minimum) edgeConfig.arc=minimum;
}
}else{
// Otherwise, find the arc by translating & rotating
var dx = endNode.x-startNode.x;
var dy = endNode.y-startNode.y;
var angle = Math.atan2(dy,dx);
var translated = _translatePoints(self.strokeData, -startNode.x, -startNode.y);
var rotated = _rotatePoints(translated, -angle);
var bounds = _getBounds(rotated);
// Arc!
if(Math.abs(bounds.top)>Math.abs(bounds.bottom)) edgeConfig.arc = -bounds.top;
else edgeConfig.arc = -bounds.bottom;
}
// Add the edge!
if(edgeConfig){
var newEdge = loopy.model.addEdge(edgeConfig);
loopy.sidebar.edit(newEdge);
}
}
// NODE: did NOT start in a node.
if(!startNode){
// Just roughly make a circle the size of the bounds of the circle
var bounds = _getBounds(self.strokeData);
var x = (bounds.left+bounds.right)/2;
var y = (bounds.top+bounds.bottom)/2;
var r = ((bounds.width/2)+(bounds.height/2))/2;
// Circle can't be TOO smol
if(r>15){
// Snap to radius
/*r = Math.round(r/Ink.SNAP_TO_RADIUS)*Ink.SNAP_TO_RADIUS;
if(r<Ink.MINIMUM_RADIUS) r=Ink.MINIMUM_RADIUS;*/
// LOCK TO JUST SMALLEST CIRCLE.
r = Ink.MINIMUM_RADIUS;
// Make that node!
var newNode = loopy.model.addNode({
x:x,
y:y,
radius:r
});
// Edit it immediately
loopy.sidebar.edit(newNode);
}
}
// Reset.
self.reset();
});
subscribe("mouseclick",function(){
// ONLY WHEN EDITING w INK
if(self.loopy.mode!=Loopy.MODE_EDIT) return;
if(self.loopy.tool!=Loopy.TOOL_INK) return;
// Reset
self.reset();
});
}
|
*******************************
LOOPY!
- with edit & play mode
TODO: smoother bezier curve?
TODO: when switch away tool, clear the Ink canvas
********************************
|
Ink
|
javascript
|
ncase/loopy
|
v1.1/js/Ink.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/Ink.js
|
CC0-1.0
|
_onUpdate = function(){
var embedCode = '<iframe width="'+width.getValue()+'" height="'+height.getValue()+'" frameborder="0" src="'+iframeSRC+'"></iframe>';
output.output(embedCode);
}
|
********************
Use the same PAGE UI thing
**********************
|
_onUpdate
|
javascript
|
ncase/loopy
|
v1.1/js/Modal.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/Modal.js
|
CC0-1.0
|
function ModalIframe(config){
var self = this;
// IFRAME
var iframe = document.createElement("iframe");
self.dom = iframe;
iframe.width = config.width;
iframe.height = config.height;
// Show & Hide
if(!config.manual){
config.page.onshow = function(){
iframe.src = config.src;
};
config.page.onhide = function(){
iframe.removeAttribute("src");
};
}
}
|
********************
Use the same PAGE UI thing
**********************
|
ModalIframe
|
javascript
|
ncase/loopy
|
v1.1/js/Modal.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/Modal.js
|
CC0-1.0
|
function PageUI(dom){
var self = this;
self.dom = dom;
self.pages = [];
self.addPage = function(id, page){
page.id = id;
self.dom.appendChild(page.dom);
self.pages.push(page);
};
self.currentPage = null;
self.showPage = function(id){
var shownPage = null;
for(var i=0; i<self.pages.length; i++){
var page = self.pages[i];
if(page.id==id){
page.show();
shownPage = page;
}else{
page.hide();
}
}
self.currentPage = shownPage;
return shownPage;
};
}
|
*******************************
PAGE UI: to extend to Sidebar, Play Controls, Modal.
********************************
|
PageUI
|
javascript
|
ncase/loopy
|
v1.1/js/PageUI.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/PageUI.js
|
CC0-1.0
|
function Page(){
var self = this;
// DOM
self.dom = document.createElement("div");
self.show = function(){ self.dom.style.display="block"; };
self.hide = function(){ self.dom.style.display="none"; };
// Add Component
self.addComponent = function(component){
self.dom.appendChild(component.dom); // add to DOM
return component;
};
}
|
*******************************
PAGE UI: to extend to Sidebar, Play Controls, Modal.
********************************
|
Page
|
javascript
|
ncase/loopy
|
v1.1/js/PageUI.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/PageUI.js
|
CC0-1.0
|
function PlayButton(config){
var self = this;
var label = "<div class='play_button_icon' icon='"+config.icon+"'></div> "
+ "<div class='play_button_label'>"+config.label+"</div>";
self.dom = _createButton(label, function(){
config.onclick();
});
// Tooltip!
if(config.tooltip){
self.dom.setAttribute("data-balloon", config.tooltip);
self.dom.setAttribute("data-balloon-pos", "top");
}
}
|
*******************************
PLAY CONTROLS CODE:
- play
- pause/reset/speed
********************************
|
PlayButton
|
javascript
|
ncase/loopy
|
v1.1/js/PlayControls.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/PlayControls.js
|
CC0-1.0
|
function PlaySlider(config){
var self = this;
self.dom = document.createElement("div");
self.dom.style.bottom = "0px";
self.dom.style.position = "absolute";
self.dom.style.width = "100%";
self.dom.style.height = "20px";
// Input
var input = document.createElement("input");
input.setAttribute("class","play_slider");
self.dom.appendChild(input);
// Slow & Fast Icons
var img = new Image();
img.src = "css/icons/speed_slow.png";
img.width = 20;
img.height = 15;
img.style.position = "absolute";
img.style.left = "5px";
img.style.top = "-2px";
self.dom.appendChild(img);
var img = new Image();
img.src = "css/icons/speed_fast.png";
img.width = 20;
img.height = 15;
img.style.position = "absolute";
img.style.right = "5px";
img.style.top = "-2px";
self.dom.appendChild(img);
// Properties
input.type = "range";
input.value = config.value;
input.step = config.step;
input.min = config.min;
input.max = config.max;
input.oninput = function(event){
config.oninput(input.value);
};
}
|
*******************************
PLAY CONTROLS CODE:
- play
- pause/reset/speed
********************************
|
PlaySlider
|
javascript
|
ncase/loopy
|
v1.1/js/PlayControls.js
|
https://github.com/ncase/loopy/blob/master/v1.1/js/PlayControls.js
|
CC0-1.0
|
constructor (opts = {
apiKey: '',
apiSecret: '',
authToken: '',
company: '',
transform: false,
ws: {},
rest: {}
}) {
if (opts.constructor.name !== 'Object') {
throw new Error([
'constructor takes an object since version 2.0.0, see:',
'https://github.com/bitfinexcom/bitfinex-api-node#version-200-breaking-changes\n'
].join('\n'))
}
this._apiKey = opts.apiKey || ''
this._apiSecret = opts.apiSecret || ''
this._authToken = opts.authToken || ''
this._company = opts.company || ''
this._transform = opts.transform === true
this._wsArgs = opts.ws || {}
this._restArgs = opts.rest || {}
this._transportCache = {
rest: {},
ws: {}
}
}
|
@param {object} [opts] - options
@param {string} [opts.apiKey] - API key
@param {string} [opts.apiSecret] - API secret
@param {string} [opts.authToken] - optional auth option
@param {string} [opts.company] - optional auth option
@param {boolean} [opts.transform] - if true, packets are converted to models
@param {object} [opts.ws] - ws transport options
@param {object} [opts.rest] - rest transport options
|
constructor
|
javascript
|
bitfinexcom/bitfinex-api-node
|
index.js
|
https://github.com/bitfinexcom/bitfinex-api-node/blob/master/index.js
|
MIT
|
_getTransportPayload (extraOpts) {
return {
apiKey: this._apiKey,
apiSecret: this._apiSecret,
authToken: this._authToken,
company: this._company,
transform: this._transform,
...extraOpts
}
}
|
Returns an arguments map ready to pass to a transport constructor
@param {object} extraOpts - options to pass to transport
@returns {object} payload
|
_getTransportPayload
|
javascript
|
bitfinexcom/bitfinex-api-node
|
index.js
|
https://github.com/bitfinexcom/bitfinex-api-node/blob/master/index.js
|
MIT
|
rest (version = 2, extraOpts = {}) {
if (version !== 1 && version !== 2) {
throw new Error(`invalid http API version: ${version}`)
}
const key = `${version}|${JSON.stringify(extraOpts)}`
if (!this._transportCache.rest[key]) {
Object.assign(extraOpts, this._restArgs)
const payload = this._getTransportPayload(extraOpts)
this._transportCache.rest[key] = version === 2
? new RESTv2(payload)
: new RESTv1(payload)
}
return this._transportCache.rest[key]
}
|
Returns a new REST API class instance (cached by version)
@param {number} [version] - 1 or 2 (default)
@param {object} [extraOpts] - passed to transport constructor
@returns {RESTv1|RESTv2} transport
|
rest
|
javascript
|
bitfinexcom/bitfinex-api-node
|
index.js
|
https://github.com/bitfinexcom/bitfinex-api-node/blob/master/index.js
|
MIT
|
ws (version = 2, extraOpts = {}) {
if (version !== 1 && version !== 2) {
throw new Error(`invalid websocket API version: ${version}`)
}
const key = `${version}|${JSON.stringify(extraOpts)}`
if (!this._transportCache.ws[key]) {
Object.assign(extraOpts, this._wsArgs)
const payload = this._getTransportPayload(extraOpts)
this._transportCache.ws[key] = version === 2
? new WSv2(payload)
: new WSv1(payload)
}
return this._transportCache.ws[key]
}
|
Returns a new WebSocket API class instance (cached by version)
@param {number} [version] - 1 or 2 (default)
@param {object} [extraOpts] - passed to transport constructor
@returns {WSv1|WSv2} transport
|
ws
|
javascript
|
bitfinexcom/bitfinex-api-node
|
index.js
|
https://github.com/bitfinexcom/bitfinex-api-node/blob/master/index.js
|
MIT
|
debugTable = ({ rows = [], headers, widths }, extraRows = []) => {
debug('')
debugTableUtil({
rows: [...rows, ...extraRows],
headers,
widths,
debug
})
debug('')
}
|
Log a table to the console
@param {object} args - arguments
@param {object[]} args.rows - data, can be specified as 2nd param
@param {string[]} args.headers - column labels
@param {number[]} args.widths - column widths
@param {object[]} extraRows - optional row spec as 2nd param
|
debugTable
|
javascript
|
bitfinexcom/bitfinex-api-node
|
examples/util/setup.js
|
https://github.com/bitfinexcom/bitfinex-api-node/blob/master/examples/util/setup.js
|
MIT
|
debugTable = ({ rows = [], headers, widths }, extraRows = []) => {
debug('')
debugTableUtil({
rows: [...rows, ...extraRows],
headers,
widths,
debug
})
debug('')
}
|
Log a table to the console
@param {object} args - arguments
@param {object[]} args.rows - data, can be specified as 2nd param
@param {string[]} args.headers - column labels
@param {number[]} args.widths - column widths
@param {object[]} extraRows - optional row spec as 2nd param
|
debugTable
|
javascript
|
bitfinexcom/bitfinex-api-node
|
examples/util/setup.js
|
https://github.com/bitfinexcom/bitfinex-api-node/blob/master/examples/util/setup.js
|
MIT
|
get args () {
return argsFromEnv()
}
|
Log a table to the console
@param {object} args - arguments
@param {object[]} args.rows - data, can be specified as 2nd param
@param {string[]} args.headers - column labels
@param {number[]} args.widths - column widths
@param {object[]} extraRows - optional row spec as 2nd param
|
args
|
javascript
|
bitfinexcom/bitfinex-api-node
|
examples/util/setup.js
|
https://github.com/bitfinexcom/bitfinex-api-node/blob/master/examples/util/setup.js
|
MIT
|
get readline () {
return Readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
})
}
|
Log a table to the console
@param {object} args - arguments
@param {object[]} args.rows - data, can be specified as 2nd param
@param {string[]} args.headers - column labels
@param {number[]} args.widths - column widths
@param {object[]} extraRows - optional row spec as 2nd param
|
readline
|
javascript
|
bitfinexcom/bitfinex-api-node
|
examples/util/setup.js
|
https://github.com/bitfinexcom/bitfinex-api-node/blob/master/examples/util/setup.js
|
MIT
|
constructor (socketArgs, authArgs = { calc: 0, dms: 0 }) {
super()
this.setMaxListeners(1000)
this._authArgs = authArgs
this._sockets = []
this._socketArgs = {
...(socketArgs || {}),
reconnectThrottler
}
}
|
@param {object} socketArgs - passed to WSv2 constructors
@param {object} [authArgs] - cached for all internal socket auth() calls
@param {number} [authArgs.calc] - default 0
@param {number} [authArgs.dms] - default 0
|
constructor
|
javascript
|
bitfinexcom/bitfinex-api-node
|
lib/ws2_manager.js
|
https://github.com/bitfinexcom/bitfinex-api-node/blob/master/lib/ws2_manager.js
|
MIT
|
setAuthArgs (args = {}) {
this._authArgs = {
...this._authArgs,
...args
}
this._sockets.forEach(socket => socket.ws.updateAuthArgs(this._authArgs))
}
|
Update authentication arguments on all sockets
@param {object} args - arguments
@param {number} [args.calc] - calc value
@param {number} [args.dms] - active 4
|
setAuthArgs
|
javascript
|
bitfinexcom/bitfinex-api-node
|
lib/ws2_manager.js
|
https://github.com/bitfinexcom/bitfinex-api-node/blob/master/lib/ws2_manager.js
|
MIT
|
getAuthArgs () {
return this._authArgs
}
|
Retrieve internal authentication arguments
@returns {object} args
|
getAuthArgs
|
javascript
|
bitfinexcom/bitfinex-api-node
|
lib/ws2_manager.js
|
https://github.com/bitfinexcom/bitfinex-api-node/blob/master/lib/ws2_manager.js
|
MIT
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.