Skip to content

Instantly share code, notes, and snippets.

View williamsiuhang's full-sized avatar

William Siuhang williamsiuhang

View GitHub Profile
class Shaders{
static basic_vertex() {
return `
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}
`;
}
static basic_fragment() {
@williamsiuhang
williamsiuhang / async-fetch
Last active April 1, 2019 00:24
async-fetch
// Basic GET request using fetch API
async function GET() {
try{
var url = "http://domain.com/route/api/endpoint";
var get = await fetch(url);
var val = await get.text();
console.log(val);
}catch(e){
// Fetches SQL table through ajax POST
// console.logs rows as JSON
// =============================== PHP (functions.php)
<?php
add_action( 'wp_ajax_clients', 'sql_get_clients' );
add_action( 'wp_ajax_nopriv_clients', 'sql_get_clients' ); // postman (if user not logged in)
function sql_get_clients() {
@williamsiuhang
williamsiuhang / mongo-queries.js
Created January 7, 2019 18:40
Basic mongo queries using Node.js / Express
// Connect
function connect() {
var mongoose = require('mongoose');
mongoose
.connect('mongodb://user:pass@ds123456.mlab.com:1234/project', {
useNewUrlParser: true
})
.then(() => {
console.log('MongoDB Connected');
@williamsiuhang
williamsiuhang / shaders-uniform-types
Created February 12, 2019 20:54
Uniform data types for Three js <-> shaders
switch (uniform.type)
{
case 'b':
case 'bool':
case 'boolean':
// single int value
case 'i':
case '1i'
@williamsiuhang
williamsiuhang / react-state-props-onupdate.js
Created April 28, 2019 00:31
Method to determine if state or prop is updated
componentDidUpdate(prevProps, prevState) {
if (this.state !== prevState) {
// update redux state when Information.js state updates
this.props.updateNewEventInfo(this.state);
}
if (prevProps.eventcreation !== this.props.eventcreation) {
// redux prop is updated in component prop (slight delay between redux / prop updates)
}
}
@williamsiuhang
williamsiuhang / gcloud-storage-upload.js
Last active May 10, 2019 17:43
Uploading an image/jpeg into Google Cloud Storage w/ POST method
// Upload image using http POST & Google Cloud Storage API
var uploadUrl = 'https://www.googleapis.com/upload/storage/v1/b/wkndevents/o?uploadType=media&name=loremipsum.jpg'; // upload with direct HTTP call
var tmp_auth = 'Bearer tokenhere123'; // Get Bearer Access token here - https://developers.google.com/oauthplayground
// Enable access for Cloud Storage v1 & Cloud Storage JSON API
var fileinput = $('.file-upload-input')[0]; // <input type='file'>
var file = fileinput.files[0]; // just support 1st file for now
// post image into google cloud storage
@williamsiuhang
williamsiuhang / exclude-darktheme
Created May 23, 2019 14:46
Terminal commands to exclude Mac apps from Dark Theme
// get app bundle id
osascript -e 'id of app "Calendar"'
// update theme | com.apple.iCal is App's bundle identifier
defaults write com.apple.iCal NSRequiresAquaSystemAppearance -bool yes
@williamsiuhang
williamsiuhang / .react-router-htaccess
Created October 15, 2019 18:11
Apache .htaccess to fix 404 issue with React Router client routing
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
@williamsiuhang
williamsiuhang / react-three-scene.js
Created December 26, 2019 07:33
Simple React Three js scene component
import React from 'react';
import * as THREE from 'three';
import { TrackballControls } from 'three/examples/jsm/controls/TrackballControls.js';
class Scene extends React.Component {
constructor(props) {
super(props)
this.animate = this.animate.bind(this);