-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add createDOM and modifyCSS * chore: auto fix lint error * feat: memoize with lru cache, and add cache on toRGB api * chore: fix lint
- Loading branch information
Showing
21 changed files
with
188 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { createDOM, modifyCSS } from '../../../src'; | ||
|
||
describe('dom', function () { | ||
it('createDOM', () => { | ||
const dom = createDOM(`<div id="test">createDOM by string</div>`); | ||
expect(dom).not.toBeUndefined(); | ||
expect(dom.innerHTML).toBe('createDOM by string'); | ||
}); | ||
|
||
it('modifyCSS', () => { | ||
const dom = createDOM(`<div id="test">modifyCSS dom</div>`); | ||
|
||
expect(dom.style.color).toBe(''); | ||
expect(dom.style.fontSize).toBe(''); | ||
|
||
modifyCSS(dom, { | ||
color: 'red', | ||
fontSize: '20px', | ||
}); | ||
|
||
expect(dom.style.color).toBe('red'); | ||
expect(dom.style.fontSize).toBe('20px'); | ||
|
||
expect(modifyCSS(null, { color: 'red' })).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
export { rgb2arr } from './rgb2arr'; | ||
export { gradient } from './gradient'; | ||
// todo 没有 memoize | ||
export { toRGB } from './torgb'; | ||
export { toCSSGradient } from './tocssgradient'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Create DOM from a html string. | ||
* @param str | ||
* @returns | ||
*/ | ||
export function createDOM(str: string): HTMLElement { | ||
const container = document.createElement('div'); | ||
container.innerHTML = str; | ||
|
||
const dom = container.childNodes[0]; | ||
if (dom && container.contains(dom)) { | ||
container.removeChild(dom); | ||
} | ||
return dom as HTMLElement; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { createDOM } from './create-dom'; | ||
export { modifyCSS } from './modify-css'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Modify the CSS of a DOM. | ||
* @param dom | ||
* @param css | ||
* @returns | ||
*/ | ||
export function modifyCSS(dom: HTMLElement | null | undefined, css: { [key: string]: any }): HTMLElement { | ||
if (!dom) return; | ||
|
||
Object.keys(css).forEach((key) => { | ||
dom.style[key] = css[key]; | ||
}); | ||
return dom; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { vec2 } from 'gl-matrix'; | ||
import { direction } from './direction'; | ||
|
||
/** | ||
* 二维向量 v1 到 v2 的夹角 | ||
* @param v1 | ||
* @param v2 | ||
* @param direct | ||
*/ | ||
export function angleTo(v1: [number, number], v2: [number, number], direct?: boolean): number { | ||
const ang = vec2.angle(v1, v2); | ||
const angleLargeThanPI = direction(v1, v2) >= 0; | ||
if (direct) { | ||
if (angleLargeThanPI) { | ||
return Math.PI * 2 - ang; | ||
} | ||
return ang; | ||
} | ||
|
||
if (angleLargeThanPI) { | ||
return ang; | ||
} | ||
return Math.PI * 2 - ang; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* 向量 v1 到 向量 v2 夹角的方向 | ||
* @param {Array} v1 向量 | ||
* @param {Array} v2 向量 | ||
* @return {Boolean} >= 0 顺时针 < 0 逆时针 | ||
*/ | ||
export function direction(v1: number[], v2: number[]): number { | ||
return v1[0] * v2[1] - v2[0] * v1[1]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,7 @@ | ||
/** | ||
* @description 扩展方法,提供 gl-matrix 为提供的方法 | ||
* */ | ||
import { mat3, vec2 } from 'gl-matrix'; | ||
|
||
type mat3Type = [number, number, number, number, number, number, number, number, number]; | ||
|
||
function leftTranslate(out, a, v) { | ||
const transMat: mat3Type = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
mat3.fromTranslation(transMat, v); | ||
return mat3.multiply(out, transMat, a); | ||
} | ||
|
||
function leftRotate(out, a, rad) { | ||
const rotateMat: mat3Type = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
mat3.fromRotation(rotateMat, rad); | ||
return mat3.multiply(out, rotateMat, a); | ||
} | ||
|
||
function leftScale(out, a, v) { | ||
const scaleMat: mat3Type = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
mat3.fromScaling(scaleMat, v); | ||
return mat3.multiply(out, scaleMat, a); | ||
} | ||
|
||
function leftMultiply(out, a, a1) { | ||
return mat3.multiply(out, a1, a); | ||
} | ||
/** | ||
* 根据 actions 来做 transform | ||
* @param m | ||
* @param actions | ||
*/ | ||
export function transform(m: number[], actions: any[][]) { | ||
const matrix = m ? [].concat(m) : [1, 0, 0, 0, 1, 0, 0, 0, 1]; | ||
|
||
for (let i = 0, len = actions.length; i < len; i++) { | ||
const action = actions[i]; | ||
switch (action[0]) { | ||
case 't': | ||
leftTranslate(matrix, matrix, [action[1], action[2]]); | ||
break; | ||
case 's': | ||
leftScale(matrix, matrix, [action[1], action[2]]); | ||
break; | ||
case 'r': | ||
leftRotate(matrix, matrix, action[1]); | ||
break; | ||
case 'm': | ||
leftMultiply(matrix, matrix, action[1]); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
return matrix; | ||
} | ||
|
||
/** | ||
* 向量 v1 到 向量 v2 夹角的方向 | ||
* @param {Array} v1 向量 | ||
* @param {Array} v2 向量 | ||
* @return {Boolean} >= 0 顺时针 < 0 逆时针 | ||
*/ | ||
export function direction(v1: number[], v2: number[]): number { | ||
return v1[0] * v2[1] - v2[0] * v1[1]; | ||
} | ||
|
||
/** | ||
* 二维向量 v1 到 v2 的夹角 | ||
* @param v1 | ||
* @param v2 | ||
* @param direct | ||
*/ | ||
export function angleTo(v1: [number, number], v2: [number, number], direct?: boolean): number { | ||
const ang = vec2.angle(v1, v2); | ||
const angleLargeThanPI = direction(v1, v2) >= 0; | ||
if (direct) { | ||
if (angleLargeThanPI) { | ||
return Math.PI * 2 - ang; | ||
} | ||
return ang; | ||
} | ||
|
||
if (angleLargeThanPI) { | ||
return ang; | ||
} | ||
return Math.PI * 2 - ang; | ||
} | ||
|
||
/** | ||
* 计算二维向量的垂直向量 | ||
* @param out | ||
* @param v | ||
* @param flag | ||
*/ | ||
export function vertical(out: number[], v: number[], flag: boolean): number[] { | ||
if (flag) { | ||
out[0] = v[1]; | ||
out[1] = -1 * v[0]; | ||
} else { | ||
out[0] = -1 * v[1]; | ||
out[1] = v[0]; | ||
} | ||
|
||
return out; | ||
} | ||
**/ | ||
export { transform } from './transform'; | ||
export { angleTo } from './angle-to'; | ||
export { direction } from './direction'; | ||
export { vertical } from './vertical'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { mat3 } from 'gl-matrix'; | ||
|
||
type mat3Type = [number, number, number, number, number, number, number, number, number]; | ||
|
||
function leftTranslate(out, a, v) { | ||
const transMat: mat3Type = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
mat3.fromTranslation(transMat, v); | ||
return mat3.multiply(out, transMat, a); | ||
} | ||
|
||
function leftRotate(out, a, rad) { | ||
const rotateMat: mat3Type = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
mat3.fromRotation(rotateMat, rad); | ||
return mat3.multiply(out, rotateMat, a); | ||
} | ||
|
||
function leftScale(out, a, v) { | ||
const scaleMat: mat3Type = [0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
mat3.fromScaling(scaleMat, v); | ||
return mat3.multiply(out, scaleMat, a); | ||
} | ||
|
||
function leftMultiply(out, a, a1) { | ||
return mat3.multiply(out, a1, a); | ||
} | ||
/** | ||
* 根据 actions 来做 transform | ||
* @param m | ||
* @param actions | ||
*/ | ||
export function transform(m: number[], actions: any[][]) { | ||
const matrix = m ? [].concat(m) : [1, 0, 0, 0, 1, 0, 0, 0, 1]; | ||
|
||
for (let i = 0, len = actions.length; i < len; i++) { | ||
const action = actions[i]; | ||
switch (action[0]) { | ||
case 't': | ||
leftTranslate(matrix, matrix, [action[1], action[2]]); | ||
break; | ||
case 's': | ||
leftScale(matrix, matrix, [action[1], action[2]]); | ||
break; | ||
case 'r': | ||
leftRotate(matrix, matrix, action[1]); | ||
break; | ||
case 'm': | ||
leftMultiply(matrix, matrix, action[1]); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
return matrix; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* 计算二维向量的垂直向量 | ||
* @param out | ||
* @param v | ||
* @param flag | ||
*/ | ||
export function vertical(out: number[], v: number[], flag: boolean): number[] { | ||
if (flag) { | ||
out[0] = v[1]; | ||
out[1] = -1 * v[0]; | ||
} else { | ||
out[0] = -1 * v[1]; | ||
out[1] = v[0]; | ||
} | ||
|
||
return out; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.