Live Note

Remain optimistic

一个简陋的 VDOM

希望以后会慢慢完善吧…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
let data = {
tag: "div",
children: [
{
tag: "span",
children: [
{
tag: "#text",
content: "hello world",
},
],
},
{
tag: "p",
children: [
{
tag: "#text",
content: "This is a paragraph!",
},
{
tag: "h1",
children: [
{
tag: "#text",
content: "This is a H1 tag",
},
],
},
{
tag: "h6",
children: [
{
tag: "#text",
content: "and I'm h6",
},
],
},
],
},
],
}

class VDOM {
constructor(data) {
this.tag = data.tag
this.children = data.children
this.content = data.content
}
static render(data) {
let _this = new VDOM(data)
if (_this.tag === "#text") {
return document.createTextNode(_this.content)
}
let node = document.createElement(_this.tag)
_this.children.map((child) => node.appendChild(VDOM.render(child)))
return node
}
}

let diff = (root, oldV, newV, current = 0) => {
if (!oldV) root.appendChild(newV)
else if (!newV) root.removeChild(root.childNodes[current])
else if (newV.tag !== oldV.tag || newV.content !== oldV.content)
root.replaceChild(new VDOM(newV).render(), root.childNodes[current])
// 递归判断
else
for (let i = 0; i < newV.children.length || i < oldV.children.length; i++)
diff(root.childNodes[i], oldV.children[i], newV.children[i], i)
}
document.querySelector("#root").appendChild(VDOM.render(data))

结构为:

1
2
3
4
5
6
7
8
9
10
<div id="root">
<div>
<span>hello world</span>
<p>
This is a paragraph!
<h1>This is a H1 tag</h1>
<h6>and I'm h6</h6>
</p>
</div>
</div>

Shader

WebGL 使用两种 Shader:

  • Vertex Shader:用于描述 point 的特性。
  • Fragment Shader:用于逐片处理。

Shader Program 使用的是 GLSL ES 语言,在 JS 中需要使用字符串编写,再通过函数加载进去。

WebGL 的执行流程大致为:

  1. Get canvas.
  2. Get WebGL context.
  3. Initialize vertex shader and fragment shader.
  4. Set clear color.
  5. Clear canvas.
  6. Draw.

vec4

在 GLSL ES 中,vec4类型用四维适量描述一个点的三维空间投影,(x, y, z, w)等价与三维空间的(x / w, y / w, z / w)
WebGL 坐标系统水平向右为 x 正轴,竖直向上为 y 正轴,垂直屏幕向外为 z 正轴。范围都在[-1, 1]之间。

Read more »

子类的原型对象-类式继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 类式继承
// parent
function SuperClass() {
this.superValue = true
}
SuperClass.prototype.getSuperValue = function () {
return this.superValue
}

// child
function SubClass() {
this.subValue = false
}
SubClass.prototype = new SuperClass()
SubClass.prototype.getSubValue = function () {
return this.subValue
}

let child = new SubClass()
console.log(child.getSuperValue()) // true
console.log(child.getSubValue()) // false
Read more »

《纽约时报》美国当地时间 1 月 2 日报道说,美国官员确认,在美军对巴格达的一次空袭中,伊朗伊斯兰革命卫队特种部队“圣城旅”部队指挥官卡西姆·苏莱曼尼将军死亡。

《纽约时报》报道,伊朗官方媒体确认了,伊拉克当地时间 3 日,伊朗将军卡西姆·苏莱曼尼在美军对巴格达国际机场一处目标的空袭中死亡。苏莱曼尼是伊朗革命卫队中的重要官员。美国国防部声明表示,美国发动了空袭。

“苏莱曼尼将军积极筹划了对美国在伊拉克及地区的外交官和服务人员的攻击。苏莱曼尼将军和‘圣城旅’需要为美军和联军数百人死亡、数千人受伤负起责任。”

伊朗誓言为苏莱曼尼将军之死报复美国

伊朗伊斯兰革命卫队(伊朗武装力量组成部分)前总司令、伊朗确定国家利益委员会秘书长穆赫辛·雷扎伊称,伊朗将为在巴格达死于火箭弹袭击的伊斯兰革命卫队圣城旅指挥官卡西姆·苏莱曼尼将军复仇。

雷扎伊在推特上写道:

“殉难的卡西姆·苏莱曼尼去找他的兄弟们了。我们会狠狠报复美国。”

伊斯兰革命卫队圣城旅指挥官卡西姆·苏莱曼尼将军 2 日晚上在巴格达的火箭弹袭击中被炸死,伊拉克什叶派民兵武装“人民动员组织”(Al-Hashd al-Shaabi)的几位高级别成员也一同丧生,12 名伊拉克士兵受伤。

五角大楼称系按照美国总统川普的命令开展对苏莱曼尼的行动。五角大楼称,此次打击旨在预防伊朗方面的袭击,而苏莱曼尼本人涉嫌参与袭击联盟基地和美国驻伊拉克大使馆。