Skip to content

Instantly share code, notes, and snippets.

@jrainlau
jrainlau / index.html
Created October 9, 2022 06:53 — forked from oaluna/index.html
Stripe Website Gradient Animation
<html>
<head>
<title>Stripe Gradient</title>
</head>
<body>
<canvas id="gradient-canvas" data-js-darken-top data-transition-in>
<!--
Remove data-js-darken-top to keep the same brightness in the upper part of the canvas
-->
</canvas>
@jrainlau
jrainlau / baidu_style.css
Last active July 18, 2019 05:14
baidu restyle
div {
background: lightblue;
}
@jrainlau
jrainlau / vuexLastingPlugin.js
Last active April 17, 2019 03:00
vuex state lasting in localstorage
/**
* VuexLastingPlugin
*
* @desc Store vuex states in storage
* @author JrainLau [email protected]
*
* @param {Object} options options
* @param {String|Array} options.watch the state to store
* @param {Boolean} options.debug debug mode
* @param {Boolean} options.autoInit init state from storage automatically
@jrainlau
jrainlau / socketio.js
Created January 30, 2019 04:00
socketio发送消息
// sending to sender-client only
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.emit('message', "this is a test");
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
@jrainlau
jrainlau / spider.js
Created January 22, 2019 09:42
wallpapers爬虫
const puppeteer = require('puppeteer')
const fs = require('fs')
const cateNames = ['aero', 'auroras', 'black', 'bokeh', 'colorful', 'creative', 'fresh', 'macro', 'patterns', 'rainbow', 'vector_art', 'white', 'animals', 'birds', 'horses', 'insects', 'others', 'pets', 'reptiles_&_frogs', 'sea', 'wild', 'architecture', 'army', 'artistic', '3d', 'abstract', 'anime', 'drawings', 'fantasy', 'graffiti', 'grunge', 'sculpture', 'typography', 'urban', 'black_and_white', 'cartoons', 'bee_movie', 'bolt', 'brave', 'cars', 'coraline', 'futurama', 'gnomeo_&_juliet', 'ice_age', 'incredibles', 'kung_fu_panda', 'madagascar', 'monsters_inc', 'ninja_turtles', 'old_disney', 'open_season', 'others', 'planet_51', 'ratatouille', 'shrek', 'south_park', 'tangled', 'the_princess_and_the_frog', 'the_simpsons', 'tinker_bell', 'toy_story', 'up', 'walle', 'celebrities', 'models', 'movies', 'music', 'charity', 'city', 'computers', 'android', 'firefox', 'hardware', 'linux', 'mac', 'nvidia', 'others', 'vaio', 'web', 'windows', 'cute', 'eleme
@jrainlau
jrainlau / cartesian2polar.js
Last active January 11, 2019 08:22
笛卡尔坐标转极坐标算法
// v2
function angle (x, y) {
let angleRate = 360 * Math.atan(y / x) / (2 * Math.PI) |> parseInt |> Math.abs
if (y >= 0 && x < 0) {
angleRate = 180 - angleRate
} else if (y < 0 && x <= 0) {
angleRate += 180
} else if (y < 0 && x > 0) {
angleRate = 360 - angleRate
@jrainlau
jrainlau / cursorPosition.js
Created September 7, 2018 05:39
get and set cursor position
export const getCursorPosition = (element) => {
let caretOffset = 0
const doc = element.ownerDocument || element.document
const win = doc.defaultView || doc.parentWindow
const sel = win.getSelection()
if (sel.rangeCount > 0) {
const range = win.getSelection().getRangeAt(0)
const preCaretRange = range.cloneRange()
preCaretRange.selectNodeContents(element)
preCaretRange.setEnd(range.endContainer, range.endOffset)
@jrainlau
jrainlau / paste.js
Created September 6, 2018 13:28
custom paste event
import chooseImg from 'chooseImg'
const onPaste = (e) => {
if (!(e.clipboardData && e.clipboardData.items)) {
return
}
return new Promise((resolve, reject) => {
for (let i = 0, len = e.clipboardData.items.length; i < len; i++) {
const item = e.clipboardData.items[i]
if (item.kind === 'string') {
@jrainlau
jrainlau / chooseImg.js
Created September 6, 2018 13:27
choose and compress image
function toPreviewer (dataUrl, cb) {
cb && cb(dataUrl)
}
function compress (img, fileType, maxWidth) {
let canvas = document.createElement('canvas')
let ctx = canvas.getContext('2d')
const proportion = img.width / img.height
const width = maxWidth