Skip to content

Instantly share code, notes, and snippets.

View ashblue's full-sized avatar

Ash Blue ashblue

View GitHub Profile
@ashblue
ashblue / circle-click.js
Created September 28, 2012 16:04
Here's the circular radius test that we're doing upon mouse click. hitTest() is called for every entity upon click to determine a positive hit.
/**
* @author Kevin Moot
*/
Marker.prototype.hitTest = function(point) {
return Util.pointInCircle(point, this.positionCenter, this.radiusSquared);
};
Util.pointInCircle = function(point, circleCenter, circleRadiusSquared) {
// determine vector between center of circle and mouse position
@ashblue
ashblue / html5-canvas-pre-render.js
Created October 5, 2012 16:44
HTML5 Canvas Pre-Rendering
/**
* A simple object for creating pre-rendered Canvas elements on the fly without
* creating a mess in your files.
* @author Ash Blue [email protected]
* @link http://blueashes.com
*
* @example
* // Create a new pre-renderd Canvas and draw on it (width, height, drawLogic)
* var myPreRender = new PreRender(50, 50, function (ctx) {
* ctx.fillStyle = '#000';
@ashblue
ashblue / distance-between-points.js
Created October 5, 2012 19:36
Distance between points using two vertices
/**
* Calculates the distqance between two vertices and returns the value
* @param {array} startVert Start x and y point
* @param {array} endVert End x and y point
* @returns {number}
*/
function distanceBetweenPoints (startVert, endVert) {
return Math.sqrt(Math.pow(endVert[0] - startVert[0], 2) + Math.pow(endVert[1] - startVert[1], 2));
}
@ashblue
ashblue / angle-between-points.js
Created October 7, 2012 02:14
Angle between points
/**
* Calculates the angle between two separate points. 0 degrees points up and 180 degrees points down
* @link https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Math/atan2
* @link http://stackoverflow.com/questions/3309617/calculating-degrees-between-2-points-with-inverse-y-axis
* @param {object} start Beginning vertex formatted as {x, y}
* @param {object} end End vertex formatted as {x, y}
* @returns {number} Returns a counterclockwise angle, measured in radians
*/
function angleBetweenPoints (start, end) {
// find angle
@ashblue
ashblue / move-point-at-angle.js
Created October 9, 2012 17:12
Move point at an angle
/**
* Moves a vertex at an angle for a specific distance, 0 degrees points up and 180 degrees points down
* @param {array} point Location on a cartesian graph formatted as [x, y]
* @param {number} angle Angle at which a point should move in radians
* @param {number} distance How far should the point move at the given angle in pixels?
* @returns {array} Newly moved point formatted as [x, y]
*/
function movePointAtAngle (point, angle, distance) {
return [
point[0] + (Math.sin(angle) * distance),
@ashblue
ashblue / nodejs-recursive-directory.js
Created October 19, 2012 05:06
NodeJS recursive directory listing without a module package
/**
* Goes through the given directory to return all files and folders recursively
* @author Ash Blue [email protected]
* @example getFilesRecursive('./folder/sub-folder');
* @requires Must include the file system module native to NodeJS, ex. var fs = require('fs');
* @param {string} folder Folder location to search through
* @returns {object} Nested tree of the found files
*/
// var fs = require('fs');
function getFilesRecursive (folder) {
@ashblue
ashblue / point-in-circle-test.js
Created October 24, 2012 20:42
Detects if the vertex point's x and y are inside of a circle
/**
* Detects if the vertex point's x and y are inside of a circle. Only works with
* plain symetrical circles, not ovals
* @link Adapted from http://stackoverflow.com/questions/2212604/javascript-check-mouse-clicked-inside-the-circle-or-polygon#answer-2212678
* @param {array|number} point Point to test, formatted as [x, y]
* @param {array|number} circleCenter Center of the circle formatted as [x, y]
* @param {number} radius Radius of the circle, which calculates as half its with or height
* @returns {boolean} True if the point is contained in the circle
*/
function pointInCircle (point, circleCenter, radius) {
@ashblue
ashblue / number-to-string-comma.js
Created December 12, 2012 00:09
Converts numbers into a string with commas
/**
* Turns a number into a more readable format with commas
* @param {number} num Number for conversion
* @returns {string} Readable number such as 700,345,384
*/
function setCommas (num) {
var letters = (num).toString().split(''),
count = 0,
readableNum = '';
@ashblue
ashblue / helloworld.rake
Last active December 14, 2015 02:59
Rails 3 "Hello World" rake job
# Rails 3
# Place in lib/tasks/helloworld.rake
# Call via "rake hello:world"
# Primary name of the rake job you'll call
namespace :hello do
# Task of the rake job, can support multiple tasks
task :world do
print 'hello world'
end
@ashblue
ashblue / custom-date.js
Created April 1, 2013 20:02
Custom JavaScript prototype class for generating dates
var wk = wk || {};
$(document).ready(function () {
var _monthNames = [
'January',
'February',
'March',
'April',
'May',
'June',