Skip to content

Instantly share code, notes, and snippets.

View lqt0223's full-sized avatar

lqt0223 lqt0223

  • Shanghai, China
View GitHub Profile
@lqt0223
lqt0223 / index.js
Last active May 27, 2017 02:33
10 Concatenation of buffers in HTTP request and codes before displaying in front-end
// 前端收到一个服务器传来的Uint8Array数组后,
//(Uint8Array是JavaScript中的一种带类型的数组(元素为无符号8位整数,取值范围为0-255)
// Uint8Array也是Node.js中Buffer类所采用的数组)
var arr = new Uint8Array(response);
var blob = new Blob(arr.buffer);
img.src = URL.createObjectURL(blob);
@lqt0223
lqt0223 / quicksort.js
Last active April 6, 2017 02:23
11 Quick sort (with 2 partition schemes) and merge sort in JavaScript
// 实现基于两种不同原地分区方式的快排
function quicksort(arr){
qs(arr, 0, arr.length - 1);
}
function qs(arr, start, end){
if(start < end){
var k = partition2(arr, start, end);
qs(arr, start, k);
qs(arr, k + 1, end);
@lqt0223
lqt0223 / heap.js
Last active April 5, 2017 09:36
12 Implementation of Heap in JavaScript
class Heap {
constructor() {
/* A heap is a tree-based data structure with the following features
- Child nodes for a node in the heap is always greater(or smaller) than
the parent node(which can be categorized as min-heap or nax-heap)
- A heap is a complete tree. Because of this feature, we can keep records
of a heap by array and refer to all nodes by index manipulation
- For example, in the case of a binary heap where a node has an index of
i, then its children indexes should be ((i + 1) << 1) - 1 and
(i + 1) << 1
@lqt0223
lqt0223 / index.html
Last active April 6, 2017 02:22
13 Backbone tiny demo - using the purest MVC framework
<html>
<head>
<title></title>
</head>
<body>
<input id="input">
<!-- The DOM that will be binded later -->
<div id="app"></div>
@lqt0223
lqt0223 / g.md
Last active January 10, 2023 00:08
14 Simple algorithm for chromatic aberration effect in JavaScript

Chromatic aberration effect - a simple implementation in JavaScript

A chromatic aberration is an optical effect caused by one or more color channels being displaced. Although it is an optical failure and should be avoided for displaying or image capturing devices, chromatic aberration can be used to make graphics be more realistic in some other applications like 3D games.

The following codes is an example of implementing the effect using ImageData API of HTML5 canvas. By fetching and manipulating pixel data of the image, the chromatic aberration effect is easy to achieve.

<html>
<body>
<canvas id="canvas"></canvas>
@lqt0223
lqt0223 / graph.js
Last active April 6, 2017 02:22
15 Basic graph implementation, DFS & BFS traversal in JavaScript
// unweighted undirectional graph implementation
// TODO directional and weighted graph implementation; travelling salesman's problem
class Graph {
constructor() {
this.vertices = [];
this.edges = [];
}
add(){
@lqt0223
lqt0223 / p&c.js
Last active December 21, 2017 02:46
16 Permutation and Combination in JavaScript
/*
Permutation in mathmatics stands for all possible order for a set.
Combination in mathmatics stands for all possible ways of selecting items from a set.
Find more at
https://en.wikipedia.org/wiki/Permutation
https://en.wikipedia.org/wiki/Combination
*/
@lqt0223
lqt0223 / deepcopy.js
Last active April 7, 2017 03:49
17 Deep copy in JavaScript
// You are welcome to have a test on this function to check if it supports deep copy for very complex nested objects.
function deepCopy(o){
if(typeof o === "object" && o !== null){
var _o = new o.constructor();
for(var k in o){
_o[k] = deepCopy(o[k]);
}
return _o;
}else{
return o;
@lqt0223
lqt0223 / graph.js
Last active April 8, 2017 16:06
18 Weighted & Directional Graph and Dijkstra's Algorithm in JavaScript
// Weighted directional graph implementation and Dijkstra's algorithm
class Graph {
constructor() {
this.vertices = [];
this.edges = [];
}
// add a sequence of vertices to the graph
@lqt0223
lqt0223 / bitwise.js
Created April 9, 2017 12:02
19 Bitwise operation and utilities
// bitwise operation and utilities
// Left shift by 1, will multiply the number by 2
console.log(2 << 1); // return 4
// Right shift by 1, will divide the number by 2.
// In JavaScript, the decimal part will be floored
console.log(2 >> 1); // return 1
console.log(3 >> 1); // return 1