Skip to content

Instantly share code, notes, and snippets.

@soggybag
soggybag / gist:458dc218bf62622b9d990a69ef24eb41
Last active November 8, 2017 19:04
Display time as 00:00:00 or 09:26:34
padWithZeros(n) {
return n > 9 ? `${n}` : `0${n}` // Adds a 0 if the value is less than 10
}
// Pass in the time in ms
formatTime(time) {
const secs = Math.floor(time / 1000) // divide by 1000 to get secs
const mins = Math.floor(secs / 60) // divide secs by 60 for mins
const hrs = Math.floor(mins / 60) // divide mins by 60 for hrs
// Mod (%) 60 transforms values to 0 to 59 range
@soggybag
soggybag / setInterval() with delta time
Created November 8, 2017 18:25
Delta time to keep more accurate time.
// Save the current time in ms
let lastUpdateTime = Date.now()
setInterval(() => {
// get the current time in ms
const now = Date.now()
// calculate the delta time as difference between now and the last update
const deltaTime = now - lastUpdateTime
// Set the last update to now.
lastUpdateTime = now
@soggybag
soggybag / makeBars
Created November 8, 2017 18:20
Creates a graph from an array of objects. Colors bars rainbow of colors.
makeBars() {
// Get an array of timers tiemr.time is a utc
const timers = this.props.timers
// use reduce to get the max value to normalize times
const max = timers.reduce((acc, timer) => {
return Math.max(acc, timer.time)
}, 0)
// generate an array of bars
return this.props.timers.map((timer, index) => {
const h = timer.time / max * 100 // normalize the times to 0 to 100
// Local storage
// Create an object
var obj = {name:"Joe", age:33, shoesize:9.5, favoriteFoods:["Apples", "Bananas"]};
// Convert object to JSON string
var objJSON = JSON.stringify(obj);
// save JSON string to local storage with the key obj.
localStorage.setItem("obj", objJSON);
// List all of the letters of the alphabet lowercase
// get the starting code
var start = "a".charCodeAt(0);
var count = 26; // How many letters
var end = start + count;
for (var i = start; i < end; i++) {
var letter = String.fromCharCode(i)
console.log("Char:"+letter);
$("body").append("<div>"+letter+"</div>");
@soggybag
soggybag / Extensions.swift
Created May 6, 2016 14:21
UIView Rotaton
extension UIView {
func rotateRad(r: CGFloat) {
self.transform = CGAffineTransformMakeRotation(r)
}
func rotateDeg(r: CGFloat) {
self.transform = CGAffineTransformMakeRotation(r.degreesToRadians)
}
@soggybag
soggybag / CustomTextField.swift
Created April 30, 2016 06:14
Adds border, corner radius, and padding to the UITextField. These properties can be set via the attribute inspector in storyboard.
import UIKit
@IBDesignable
class CustomTextField: UITextField {
var padding: UIEdgeInsets {
get {
return UIEdgeInsets(top: 0, left: paddingValue, bottom: 0, right: paddingValue)
}
}
@soggybag
soggybag / ViewController.swift
Created February 23, 2016 19:27
Animate drawing sine wave. CABasicAnimation and UIBezierPath
// Draw a sine curve with a fill
let centerY = frame.height / 2 // find the vertical center
let steps = 200 // Divide the curve into steps
let stepX = frame.width / CGFloat(steps) // find the horizontal step distance
// Make a path
let path = UIBezierPath()
path.moveToPoint(CGPoint(x: 0, y: centerY))
// Loop and draw steps in straingt line segments
@soggybag
soggybag / ViewController.swift
Created February 23, 2016 19:04
Sine Wave with fill using UIBezierPath
// Draw a sine curve with a fill
let centerY = frame.height / 2 // find the vertical center
let steps = 200 // Divide the curve into steps
let stepX = frame.width / CGFloat(steps) // find the horizontal step distance
// Make a path
let path = UIBezierPath()
// Start in the lower left corner
path.moveToPoint(CGPoint(x: 0, y: frame.height))
@soggybag
soggybag / ViewController.swift
Created February 23, 2016 18:56
Short snippet for drawing a sine curve with UIBezierPath
// Draw a sine curve
let centerY = frame.height / 2 // find the vertical center
let steps = 200 // Divide the curve into steps
let stepX = frame.width / CGFloat(steps) // find the horizontal step distance
// Make a path
let path = UIBezierPath()
// Move the starting point to the left center
path.moveToPoint(CGPoint(x: 0, y: centerY))
// Loop and draw steps in straingt line segments