Skip to content

Instantly share code, notes, and snippets.

@wewindy
wewindy / showPrimitiveShader.js
Last active December 14, 2020 10:12
showPrimitiveShader
/**
* Need Cesium.js 1.7x environment.
*/
var viewer = new Cesium.Viewer("cesiumContainer");
viewer.scene.globe.depthTestAgainstTerrain = true;
viewer.camera.setView({
destination : new Cesium.Cartesian3(-2644963.9889313546, 5763731.142118295, 2199400.7089496767), //世界坐标系下的一个坐标点
orientation : {//旋转角度
heading :6.075,
@wewindy
wewindy / cesiumClickShowLngLat.js
Last active January 5, 2021 01:51
cesiumClickShowLngLat.js
/**
* @description 需要 Cesium.js 环境
*/
const viewer = new Cesium.Viewer("cesiumContainer");
viewer.scene.globe.depthTestAgainstTerrain = true;
const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function(click){
const cart = viewer.scene.pickPosition(click.position);
@wewindy
wewindy / czm_computePosition.glsl
Created December 14, 2020 10:17
czm_computerPosition.glsl from Primitive.js
vec4 czm_computePosition()
{
vec4 p;
if (czm_morphTime == 1.0)
{
p = czm_translateRelativeToEye(position3DHigh, position3DLow);
}
else if (czm_morphTime == 0.0)
{
p = czm_translateRelativeToEye(position2DHigh.zxy, position2DLow.zxy);
@wewindy
wewindy / bilinear-interpolation.js
Last active March 7, 2022 02:48
bilinear-interpolation
/**
* 计算双线性内插点的值
* @param {Point} pt 目标点位
* @param {Point} p1 左上
* @param {Point} p2 右上
* @param {Point} p3 左下
* @param {Point} p4 右下
*
* @returns {Number} pt的插值结果
*/
@wewindy
wewindy / transpose.js
Created February 25, 2021 11:02
array[][] transpose
/**
* 将矩阵(二维数组形式)转置的高效方法
* @param {Array[][]} arr 二维数组,子数组中元素个数应一致
*/
export function transpose(arr) {
return arr[0].map((_, i) => arr.map(row => row[i]))
}
@wewindy
wewindy / jacobi.js
Last active March 18, 2021 18:43
jacobi_alg
import Matrix2 from '../core/mat2.js'
import Matrix3, { transpose, multiply, } from '../core/mat3.js'
/**
* 雅可比迭代法求解实对称矩阵的特征向量、特征值
* @param {Matrix2 | Matrix3} matrix 二维数组,当前支持二维、三维迭代求解
* @returns {Object} 返回一个对象 obj, obj.eigenVectors 是特征向量(二维数组),obj.eigenValues 是特征值(一维数组)
*/
function jacobi(matrix) {
const length = matrix.length === undefined ? matrix.dimensions : matrix.length
@wewindy
wewindy / localForage-demo.js
Last active May 24, 2021 01:28
localForage-demo
import localForage from 'localforage'
// 创建仓库对象,并创建名为 “test” 的 indexeddb 以及 名为 “testidb” 的表
const store = localForage.createInstance({
driver: localForage.INDEXEDDB,
name: "test",
storeName: "testidb"
})
// 设置 key-value
@wewindy
wewindy / friendly-filesize.js
Created March 29, 2021 02:00
return a string that friendly print file size.
function friendlyFileSize(sizeInBytes) {
if(sizeInBytes < 1024) {
return `${sizeInBytes} Bytes`;
} else if(sizeInBytes >= 1024 && sizeInBytes < 1048576) {
return `${(sizeInBytes/1024).toFixed(2)} KB`;
} else if(sizeInBytes >= 1048576) {
return `${(sizeInBytes/1048576).toFixed(2)} MB`;
}
}
@wewindy
wewindy / condition-rAF.js
Created March 29, 2021 02:16
能用时间和条件来控制 requestAnimationFrame 的渲染
function main() {
let last = 0
// 上次渲染的值(此处初始化为一个整数)
let lastState = Math.floor(Math.random() * 10)
const canWeRender = function() {
// 这次渲染的值
const currentState = Math.floor(Math.random() * 10)
// 比较二者是否一致,如果一致,则false,否则true
if (currentState === lastState) {
console.log("lastState eq. currentState. Will not render.")
@wewindy
wewindy / save.md
Created May 24, 2021 01:49
纯前端保存文件

使用a标签的 click 事件以及 URL.createObjectURL 方法 适合小文件的下载(无后端)

保存二进制文件

const buffer = new Float32Array([1, 2, 3, 4, 5]) // 创建一个类型数组,大小为 20 bytes
// 创建 blob 对象,第一个参数可以放置多个变量,这里放一个就好了
const blob = new Blob([buffer], {
 type: "application/octet-stream"