-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.json
244 lines (244 loc) · 939 KB
/
feed.json
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
{
"version": "https://jsonfeed.org/version/1",
"title": "UniStart's Blog",
"subtitle": "心在哪里,家在哪里",
"icon": "https://unistart2.github.io/images/favicon.ico",
"description": "个人博客",
"home_page_url": "https://unistart2.github.io",
"items": [
{
"id": "https://unistart2.github.io/posts/dbb457de/",
"url": "https://unistart2.github.io/posts/dbb457de/",
"title": "Rails学习-使用turbo-rails时的一些问题",
"date_published": "2024-03-29T04:53:50.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><div class=\"note primary\">\n<p><code>Rails7</code> 开始默认使用 <code>importmap</code> 作为 <code>javascript</code> 依赖的管理工具(类似于 npm)</p>\n</div>\n<details class=\"danger\"><summary>遇到的问题</summary><div>\n<ol>\n<li>生成的项目工程目录下的 bin 目录中没有 importmap 命令</li>\n<li>turbo-rails 貌似没有被引入项目中(直接创建项目工程后没有进行相关配置)</li>\n</ol>\n</div></details>\n<h2 id=\"安装turbo-rails\"><a class=\"anchor\" href=\"#安装turbo-rails\">#</a> 安装 turbo-rails</h2>\n<ol>\n<li>安装 importmap</li>\n</ol>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># step1. 安装 importmap</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>bin/rails·importmap:install</pre></td></tr></table></figure><p>上述命令,主要会生成 <code>bin/importmap</code> 、 <code>config/importmap.rb</code> 、 <code>app/javascript/application.js</code> 文件</p>\n<ol start=\"2\">\n<li>通过 importmap 引入 turbo</li>\n</ol>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># step2. 使用 importmap 管理 turbo 依赖</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>bin/importmap·pin·@hotwired/turbo</pre></td></tr></table></figure><p>执行上述命令后, <code>Rails</code> 会自动往 <code>config/importmap.rb</code> 文件中加入依赖名称,并在 <code>app/javascript/application.js</code> 文件中导入 <code>turbo-rails</code></p>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>config/importmap.rb</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># Pin npm packages by running ./bin/importmap</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>pin <span class=\"token string-literal\"><span class=\"token string\">\"application\"</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre>pin <span class=\"token string-literal\"><span class=\"token string\">\"@hotwired/turbo-rails\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token symbol\">to</span><span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">\"turbo.min.js\"</span></span></pre></td></tr></table></figure><figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"><span>app/javascript/application.js</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\">// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token string\">\"@hotwired/turbo-rails\"</span></pre></td></tr></table></figure><ol start=\"3\">\n<li>安装 turbo</li>\n</ol>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># step3 </span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>bin/rails·turbo:install</pre></td></tr></table></figure><h2 id=\"使用turbo-rails\"><a class=\"anchor\" href=\"#使用turbo-rails\">#</a> 使用 turbo-rails</h2>\n<blockquote>\n<p>使用 <code>turbo_confirm</code> 代替原先的 <code>confirm</code> ,例如:<br />\n<%= button_to "Empty cart", cart, method: :delete, form: { data: { turbo_confirm: "Are you sure?" } } %></p>\n</blockquote>\n<p>生成的 HTML 代码如下</p>\n<figure class=\"highlight html\"><figcaption data-lang=\"HTML\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>form</span> <span class=\"token attr-name\">data-turbo-confirm</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>Are you sure?<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">class</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>button_to<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">method</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>post<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">action</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>/carts/20<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>input</span> <span class=\"token attr-name\">type</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>hidden<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">name</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>_method<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">value</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>delete<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">autocomplete</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>off<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>button</span> <span class=\"token attr-name\">type</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>submit<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>Empty cart<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>button</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>input</span> <span class=\"token attr-name\">type</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>hidden<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">name</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>authenticity_token<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">value</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>cxmrMyiULbRE9a3PrKRdSaFz90lhI7BnuQ45asjQjITJMsx-5186VueizTPDYklStOYWCtwSy_tRuc9mttDKOQ<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">autocomplete</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>off<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>form</span><span class=\"token punctuation\">></span></span></pre></td></tr></table></figure><details class=\"info\"><summary>效果</summary><div>\n<p><img data-src=\"https://img2.imgtp.com/2024/03/29/k141zI4Z.png\" alt=\"效果\" /></p>\n</div></details>\n<div class=\"note primary\">\n<p>关于 <code>Rails7+</code> 版本中使用 <code>importmap</code> ,可以参考<span class=\"exturl\" data-url=\"aHR0cHM6Ly9ibG9nLmFwcHNpZ25hbC5jb20vMjAyMi8wMy8wMi9pbXBvcnQtbWFwcy11bmRlci10aGUtaG9vZC1pbi1yYWlscy03Lmh0bWw=\"> Import Maps Under the Hood in Rails 7</span> 这篇文章</p>\n</div>\n",
"tags": [
"Rails",
"Ruby",
"Rails"
]
},
{
"id": "https://unistart2.github.io/posts/2fb1a3c3/",
"url": "https://unistart2.github.io/posts/2fb1a3c3/",
"title": "Rails学习-迁移方法",
"date_published": "2024-03-25T08:32:28.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><h2 id=\"change方法\"><a class=\"anchor\" href=\"#change方法\">#</a> change 方法</h2>\n<blockquote>\n<p>Rails 用于匹配迁移名称的两个模式分别是: <code>add_xxx_to_xxxTable</code> 和 <code>remove_xxx_from_xxxTable</code> ,<br />\n其中 <code>xxx</code> 的值会被忽略掉,<ins class=\"dot\">迁移名称之后的字段名和类型列表才是 Rails 获取字段信息的地方</ins></p>\n</blockquote>\n<ol>\n<li>添加字段</li>\n</ol>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># 创建添加 quantity 字段到 line_items 表的迁移</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\"># add_quantity_to_line_items 是迁移名称,并告诉 Rails 需要添加字段的表名,</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token comment\"># quantity:integer 告诉 Rails 添加的字段名称和类型,这样就会自动生成 add_column 那一行</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre>bin/rails generate migration add_quantity_to_line_items quantity:integer</pre></td></tr></table></figure><p>生成的迁移文件如下:</p>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">AddQuantityToLineItems</span> <span class=\"token operator\"><</span> ActiveRecord<span class=\"token double-colon punctuation\">::</span>Migration<span class=\"token punctuation\">[</span><span class=\"token number\">7.0</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">change</span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> add_column <span class=\"token symbol\">:line_items</span><span class=\"token punctuation\">,</span> <span class=\"token symbol\">:quantity</span><span class=\"token punctuation\">,</span> <span class=\"token symbol\">:integer</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><ol start=\"2\">\n<li>移除字段(和添加字段类似)</li>\n</ol>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre>bin/rails g migration remove_quantity_from_line_items quantity:integer</pre></td></tr></table></figure><p>生成的迁移文件如下:</p>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">RemoveQuantityFromLineItems</span> <span class=\"token operator\"><</span> ActiveRecord<span class=\"token double-colon punctuation\">::</span>Migration<span class=\"token punctuation\">[</span><span class=\"token number\">7.0</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">change</span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> remove_column <span class=\"token symbol\">:line_items</span><span class=\"token punctuation\">,</span> <span class=\"token symbol\">:quantity</span><span class=\"token punctuation\">,</span> <span class=\"token symbol\">:integer</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><div class=\"note info\">\n<p>可以使用 <code>up</code> 和 <code>down</code> 方法以传统风格编写迁移而不使用 <code>change</code> 方法。<br />\n <code>up</code> 方法用于描述对数据库模式所做的改变, <code>down</code> 方法用于撤销 <code>up</code> 方法所做的改变。<br />\n换句话说,如果调用 <code>up</code> 方法之后紧接着调用 <code>down</code> 方法,数据库模式不会发生任何改变。<br />\n例如用 <code>up</code> 方法创建数据表,就应该用 <code>down</code> 方法删除这个数据表。<br />\n在 <code>down</code> 方法中撤销迁移时,明智的做法是按照和 <code>up</code> 方法中操作相反的顺序执行操作。</p>\n</div>\n<h2 id=\"up方法\"><a class=\"anchor\" href=\"#up方法\">#</a> up 方法</h2>\n<blockquote>\n<p>用于对数据库模式做修改<br />\n执行 <code>bin/rails db:migrate</code> 命令时会被调用执行</p>\n</blockquote>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># 生成一个迁移文件用于测试</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>bin/rails g migration something</pre></td></tr></table></figure><p>添加 up 方法</p>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">Something</span> <span class=\"token operator\"><</span> ActiveRecord<span class=\"token double-colon punctuation\">::</span>Migration<span class=\"token punctuation\">[</span><span class=\"token number\">7.0</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token comment\"># 记得删除默认生成的空的 change 方法</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token comment\"># 创建一个表</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">up</span></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> create_table <span class=\"token symbol\">:people</span> <span class=\"token keyword\">do</span> <span class=\"token operator\">|</span>t<span class=\"token operator\">|</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> t<span class=\"token punctuation\">.</span>string <span class=\"token symbol\">:name</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> t<span class=\"token punctuation\">.</span>integer <span class=\"token symbol\">:age</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> t<span class=\"token punctuation\">.</span>timestamps</pre></td></tr><tr><td data-num=\"11\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># 查看迁移状态</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>bin/rails db:migrate:status</pre></td></tr><tr><td data-num=\"3\"></td><td><pre></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token comment\"># 输出如下:</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre>database: db/development.sqlite3</pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> Status Migration ID Migration Name</pre></td></tr><tr><td data-num=\"8\"></td><td><pre>--------------------------------------------------</pre></td></tr><tr><td data-num=\"9\"></td><td><pre> up <span class=\"token number\">20240316032753</span> Create products</pre></td></tr><tr><td data-num=\"10\"></td><td><pre> up <span class=\"token number\">20240320055343</span> Create carts</pre></td></tr><tr><td data-num=\"11\"></td><td><pre> up <span class=\"token number\">20240320060656</span> Create line items</pre></td></tr><tr><td data-num=\"12\"></td><td><pre> up <span class=\"token number\">20240323132057</span> Add quantity to line items</pre></td></tr><tr><td data-num=\"13\"></td><td><pre> up <span class=\"token number\">20240323133840</span> Combine items <span class=\"token keyword\">in</span> cart</pre></td></tr><tr><td data-num=\"14\"></td><td><pre> down <span class=\"token number\">20240325092635</span> Something</pre></td></tr></table></figure><p>此时数据库中还没有 people 这张表,执行迁移后,就会生成</p>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre>bin/rails db:migrate</pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\"># 输出如下:</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token operator\">==</span> <span class=\"token number\">20240325092635</span> Something: migrating <span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre>-- create_table<span class=\"token punctuation\">(</span>:people<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> -<span class=\"token operator\">></span> <span class=\"token number\">0</span>.0022s</pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token operator\">==</span> <span class=\"token number\">20240325092635</span> Something: migrated <span class=\"token punctuation\">(</span><span class=\"token number\">0</span>.0023s<span class=\"token punctuation\">)</span> <span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">=</span></pre></td></tr></table></figure><p>执行迁移后,查看迁移状态,发现变为 up 状态了</p>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre>bin/rails db:migrate:status</pre></td></tr><tr><td data-num=\"2\"></td><td><pre></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token comment\"># 输出如下:</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre>database: db/development.sqlite3</pre></td></tr><tr><td data-num=\"5\"></td><td><pre></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> Status Migration ID Migration Name</pre></td></tr><tr><td data-num=\"7\"></td><td><pre>--------------------------------------------------</pre></td></tr><tr><td data-num=\"8\"></td><td><pre> up <span class=\"token number\">20240316032753</span> Create products</pre></td></tr><tr><td data-num=\"9\"></td><td><pre> up <span class=\"token number\">20240320055343</span> Create carts</pre></td></tr><tr><td data-num=\"10\"></td><td><pre> up <span class=\"token number\">20240320060656</span> Create line items</pre></td></tr><tr><td data-num=\"11\"></td><td><pre> up <span class=\"token number\">20240323132057</span> Add quantity to line items</pre></td></tr><tr><td data-num=\"12\"></td><td><pre> up <span class=\"token number\">20240323133840</span> Combine items <span class=\"token keyword\">in</span> cart</pre></td></tr><tr><td data-num=\"13\"></td><td><pre> up <span class=\"token number\">20240325092635</span> Something</pre></td></tr></table></figure><h2 id=\"down方法\"><a class=\"anchor\" href=\"#down方法\">#</a> down 方法</h2>\n<blockquote>\n<p>用于撤销之前的 up 操作对数据库模式的修改<br />\n执行 <code>bin/rails db:rollback</code> 命令时会被调用执行</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">Something</span> <span class=\"token operator\"><</span> ActiveRecord<span class=\"token double-colon punctuation\">::</span>Migration<span class=\"token punctuation\">[</span><span class=\"token number\">7.0</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token comment\"># 省略之前的 up 方法</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token operator\">...</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token comment\"># 删除 up 创建的表</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">down</span></span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> drop_table <span class=\"token symbol\">:people</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><p>撤销之前的建表操作</p>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre>bin/rails db:rollback</pre></td></tr><tr><td data-num=\"2\"></td><td><pre></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token comment\"># 输出如下:</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token operator\">==</span> <span class=\"token number\">20240325092635</span> Something: reverting <span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre>-- drop_table<span class=\"token punctuation\">(</span>:people<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> -<span class=\"token operator\">></span> <span class=\"token number\">0</span>.0017s</pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token operator\">==</span> <span class=\"token number\">20240325092635</span> Something: reverted <span class=\"token punctuation\">(</span><span class=\"token number\">0</span>.0017s<span class=\"token punctuation\">)</span> <span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">==</span><span class=\"token operator\">=</span></pre></td></tr></table></figure><p>再次查看迁移状态,发现变为 down 状态了</p>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre>bin/rails db:migrate:status</pre></td></tr><tr><td data-num=\"2\"></td><td><pre></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>database: db/development.sqlite3</pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> Status Migration ID Migration Name</pre></td></tr><tr><td data-num=\"6\"></td><td><pre>--------------------------------------------------</pre></td></tr><tr><td data-num=\"7\"></td><td><pre> up <span class=\"token number\">20240316032753</span> Create products</pre></td></tr><tr><td data-num=\"8\"></td><td><pre> up <span class=\"token number\">20240320055343</span> Create carts</pre></td></tr><tr><td data-num=\"9\"></td><td><pre> up <span class=\"token number\">20240320060656</span> Create line items</pre></td></tr><tr><td data-num=\"10\"></td><td><pre> up <span class=\"token number\">20240323132057</span> Add quantity to line items</pre></td></tr><tr><td data-num=\"11\"></td><td><pre> up <span class=\"token number\">20240323133840</span> Combine items <span class=\"token keyword\">in</span> cart</pre></td></tr><tr><td data-num=\"12\"></td><td><pre> down <span class=\"token number\">20240325092635</span> Something</pre></td></tr></table></figure>",
"tags": [
"Rails",
"Ruby",
"Rails"
]
},
{
"id": "https://unistart2.github.io/posts/e552de99/",
"url": "https://unistart2.github.io/posts/e552de99/",
"title": "Rails学习-cache缓存",
"date_published": "2024-03-20T02:24:51.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><h2 id=\"启用或禁用cache\"><a class=\"anchor\" href=\"#启用或禁用cache\">#</a> 启用或禁用 cache</h2>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre>bin/rails dev:cache <span class=\"token comment\">#=> Development mode is now being cached.</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\"># 再次执行上述命令会禁用缓存</span></pre></td></tr></table></figure><h2 id=\"在模版中使用cache方法\"><a class=\"anchor\" href=\"#在模版中使用cache方法\">#</a> 在模版中使用 cache 方法</h2>\n<ol>\n<li>将原先的代码逻辑包裹在 cache 方法的代码块中。</li>\n<li>当数据发生变化的时候,Rails 会自动进行处理(包括缓存的存储、失效等问题)。</li>\n<li>当 Rails 使用缓存数据的时候,服务器的输出信息中会看到 <code>Read fragment</code> 字样。</li>\n<li>如果数据发生了变化,服务器输出信息中会看到多行 <code>Read fragment</code> 字样,同时有一行或多行 <code>Write fragment</code> 字样。</li>\n<li>缓存数据触发更新的条件:待渲染数据对应的数据库表中的 <code>updated_at</code> 字段的值发生了变化,也就是说在更新数据时,如果该记录的 updated_at 字段没有更新,那么之后渲染的数据仍然是之前缓存的旧数据。</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token operator\"><</span><span class=\"token string-literal\"><span class=\"token string\">%# 缓存@products实例变量中的数据,这是一个产品列表 %></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><% cache @products do %></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <% @products.each do |product| %></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <%#</span></span> 缓存<span class=\"token variable\">@product</span>实例变量中的数据 <span class=\"token string-literal\"><span class=\"token string\">%></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <% cache product do %></span></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token operator\"><</span>div <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string-literal\"><span class=\"token string\">\"entry\"</span></span><span class=\"token operator\">></span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token operator\"><</span><span class=\"token operator\">%=</span> image_tag<span class=\"token punctuation\">(</span>product<span class=\"token punctuation\">.</span>image_url<span class=\"token punctuation\">)</span> <span class=\"token string-literal\"><span class=\"token string\">%></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <h3></span></span><span class=\"token operator\"><</span><span class=\"token operator\">%=</span> product<span class=\"token punctuation\">.</span>title <span class=\"token string-literal\"><span class=\"token string\">%></h3></span></span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token operator\"><</span><span class=\"token operator\">%=</span> sanitize<span class=\"token punctuation\">(</span>product<span class=\"token punctuation\">.</span>description<span class=\"token punctuation\">)</span> <span class=\"token string-literal\"><span class=\"token string\">%></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <div class=\"price_line\"></span></span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> <span class=\"token operator\"><</span><span class=\"token operator\">%</span><span class=\"token comment\"># 利用 Rails 提供的 number_to_currency 对价格进行国际化显示 %></span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token operator\"><</span>span <span class=\"token keyword\">class</span><span class=\"token operator\">=</span><span class=\"token string-literal\"><span class=\"token string\">\"price\"</span></span><span class=\"token operator\">></span><span class=\"token operator\"><</span><span class=\"token operator\">%=</span> number_to_currency<span class=\"token punctuation\">(</span>product<span class=\"token punctuation\">.</span>price<span class=\"token punctuation\">,</span> <span class=\"token symbol\">unit</span><span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">\"$\"</span></span><span class=\"token punctuation\">)</span> <span class=\"token string-literal\"><span class=\"token string\">%></span></span></span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre> <span class=\"token operator\"><</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token operator\"><</span><span class=\"token operator\">/</span>div<span class=\"token operator\">></span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token operator\"><</span><span class=\"token operator\">%</span> <span class=\"token keyword\">end</span> <span class=\"token string-literal\"><span class=\"token string\">%></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <% end %></span></span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre><span class=\"token operator\"><</span><span class=\"token operator\">%</span> <span class=\"token keyword\">end</span> <span class=\"token operator\">%</span><span class=\"token operator\">></span></pre></td></tr></table></figure>",
"tags": [
"Rails",
"Ruby",
"Rails"
]
},
{
"id": "https://unistart2.github.io/posts/f68966e2/",
"url": "https://unistart2.github.io/posts/f68966e2/",
"title": "Ruby元编程(四)",
"date_published": "2023-10-14T03:34:19.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><div class=\"note info\">\n<p>参考书籍《Ruby 元编程(第二版)》<br />\nRuby 版本:书上使用的是 2.x,自己使用的 3.1.2</p>\n</div>\n<h2 id=\"类定义的本质\"><a class=\"anchor\" href=\"#类定义的本质\">#</a> 类定义的本质</h2>\n<div class=\"note primary\">\n<p>在 ruby 中类不过是增强的模块,因此对类的相关内容也适用于模块</p>\n</div>\n<h3 id=\"当前类\"><a class=\"anchor\" href=\"#当前类\">#</a> 当前类</h3>\n<blockquote>\n<p>不管处在 ruby 程序的那个位置,总存在一个当前对象:self。同样,也总是存在一个当前类(或模块)存在。</p>\n<ul>\n<li>在程序的顶层,当前类是 Object,也就是 main 对象所属的类(也是为什么在顶层定义的方法会成为 Object 的实例方法的原因)</li>\n<li>在一个方法中,当前类就是当前对象的类。(试着在一个方法中用 def 关键字定义另外一个方法,会发现新方法定义在 self 所属的类中。)</li>\n</ul>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">C</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">m1</span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">m2</span></span><span class=\"token punctuation\">;</span> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">D</span> <span class=\"token operator\"><</span> <span class=\"token constant\">C</span><span class=\"token punctuation\">;</span> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre></pre></td></tr><tr><td data-num=\"9\"></td><td><pre>obj <span class=\"token operator\">=</span> <span class=\"token class-name\">D</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre>obj<span class=\"token punctuation\">.</span>m1</pre></td></tr><tr><td data-num=\"11\"></td><td><pre></pre></td></tr><tr><td data-num=\"12\"></td><td><pre><span class=\"token constant\">C</span><span class=\"token punctuation\">.</span>instance_methods<span class=\"token punctuation\">(</span><span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> [:m1, :m2]</span></pre></td></tr></table></figure><div class=\"note info\">\n<p>用 class 关键字打开类时(或用 module 关键字打开模块时),这个类成为当前类</p>\n</div>\n<h3 id=\"class_eval方法\"><a class=\"anchor\" href=\"#class_eval方法\">#</a> class_eval 方法</h3>\n<blockquote>\n<p>如何在不知道类名字的情况下打开一个类?<br />\n比如:想要设计一个以类为参数的方法,给这个类添加一个新的实例方法</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">add_method_to</span></span><span class=\"token punctuation\">(</span>a_class<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token comment\"># TODO:在 a_class 上定义方法 m ()</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><blockquote>\n<p>可以使用 class_eval 方法:它会在一个已存在类的上下文中执行一个块</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">add_method_to</span></span><span class=\"token punctuation\">(</span>a_class<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> a_class<span class=\"token punctuation\">.</span>class_eval <span class=\"token keyword\">do</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">m</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token string-literal\"><span class=\"token string\">'Hello class_eval method'</span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre></pre></td></tr><tr><td data-num=\"9\"></td><td><pre>add_method_to<span class=\"token punctuation\">(</span><span class=\"token builtin\">String</span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token string-literal\"><span class=\"token string\">'abc'</span></span><span class=\"token punctuation\">.</span>m <span class=\"token comment\">#=> \"Hello class_eval method\"</span></pre></td></tr></table></figure><h2 id=\"类实例变量\"><a class=\"anchor\" href=\"#类实例变量\">#</a> 类实例变量</h2>\n<blockquote>\n<p>ruby 解释器假定所有实例变量都属于当前对象 self,在类定义时也是如此:</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">MyClass</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token variable\">@my_var</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><blockquote>\n<p>在类定义的时候,self 角色由类本身担任,因此实例变量 @my_var 属于这个类。注意:类的实例变量 和 类的对象的实例变量是不同的。<br />\n注意区分:类变量 和 类的实例变量<br />\n @@开头的是类变量,可以被子类或者类的实例使用;@开头的是类的实例变量,只能被类本身使用(类本身也是一个对象 ——Class 类的实例对象)</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">MyClass</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token variable\">@my_var</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token comment\"># MyClass 类的实例变量</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token keyword\">self</span><span class=\"token punctuation\">.</span><span class=\"token function\">read</span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token variable\">@my_var</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">write</span></span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token variable\">@my_var</span> <span class=\"token operator\">=</span> <span class=\"token number\">2</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">read</span></span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre> <span class=\"token variable\">@my_var</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre></pre></td></tr><tr><td data-num=\"17\"></td><td><pre>obj <span class=\"token operator\">=</span> <span class=\"token class-name\">MyClass</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre>obj<span class=\"token punctuation\">.</span>read <span class=\"token comment\">#=> nil 因为 obj 对象上此时还没有 @my_var 实例变量</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre>obj<span class=\"token punctuation\">.</span>write</pre></td></tr><tr><td data-num=\"20\"></td><td><pre>obj<span class=\"token punctuation\">.</span>read <span class=\"token comment\">#=> 2</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre>MyClass<span class=\"token punctuation\">.</span>read <span class=\"token comment\">#=> 1 这里返回的是 MyClass 类本身的实例变量</span></pre></td></tr></table></figure><h2 id=\"单件方法和单件类\"><a class=\"anchor\" href=\"#单件方法和单件类\">#</a> 单件方法和单件类</h2>\n<h3 id=\"单件方法\"><a class=\"anchor\" href=\"#单件方法\">#</a> 单件方法</h3>\n<blockquote>\n<p>ruby 允许给单个对象增加一个方法:例如:</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre>str <span class=\"token operator\">=</span> <span class=\"token string-literal\"><span class=\"token string\">'just a regular string'</span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token comment\"># 只在 str 这个变量上添加了 title? 方法</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token class-name\">str</span><span class=\"token punctuation\">.</span><span class=\"token function\">title</span></span><span class=\"token operator\">?</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> upcase <span class=\"token operator\">==</span> <span class=\"token keyword\">self</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre></pre></td></tr><tr><td data-num=\"8\"></td><td><pre>str<span class=\"token punctuation\">.</span>title<span class=\"token operator\">?</span> <span class=\"token comment\">#=> false</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre>str<span class=\"token punctuation\">.</span>methods<span class=\"token punctuation\">.</span>grep<span class=\"token punctuation\">(</span><span class=\"token regex-literal\"><span class=\"token regex\">/title?/</span></span><span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> [:title?]</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre>str<span class=\"token punctuation\">.</span>singleton_methods <span class=\"token comment\">#=> [:title?]</span></pre></td></tr></table></figure><h3 id=\"类方法的真相\"><a class=\"anchor\" href=\"#类方法的真相\">#</a> 类方法的真相</h3>\n<blockquote>\n<p>类本身也是对象(Class 类的实例),而类名只是一个常量。因此,类方法的实质就是:它是类的一个单件方法。</p>\n</blockquote>\n<p>用 def 定义单件方法多的语法总是如下:</p>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token class-name\">object</span><span class=\"token punctuation\">.</span><span class=\"token function\">method</span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token comment\"># 方法主体</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr></table></figure><blockquote>\n<p>上面定义中,object 可以是对象的引用、常量类名或者 self。在这三种形式下,定义的语法看起来有些不同,但实际上,底层机制是一样的。</p>\n</blockquote>\n<h3 id=\"单件类\"><a class=\"anchor\" href=\"#单件类\">#</a> 单件类</h3>\n<div class=\"note info\">\n<p>单件类也成为元类、本征类</p>\n</div>\n<h4 id=\"如何获取单件类\"><a class=\"anchor\" href=\"#如何获取单件类\">#</a> 如何获取单件类</h4>\n<ol>\n<li>方式一:通过 <code>class << object</code> 语法</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token operator\"><<</span> an_object</pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token comment\"># 自定义代码</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token comment\"># 如果像获得这个单件类的引用,可以在离开作用域的时候返回 self:</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre>obj <span class=\"token operator\">=</span> <span class=\"token class-name\">Object</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr><td data-num=\"7\"></td><td><pre>singleton_class <span class=\"token operator\">=</span> <span class=\"token keyword\">class</span> <span class=\"token operator\"><<</span> obj</pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token keyword\">self</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre></pre></td></tr><tr><td data-num=\"11\"></td><td><pre>singleton_class<span class=\"token punctuation\">.</span><span class=\"token keyword\">class</span> <span class=\"token comment\">#=> Class</span></pre></td></tr></table></figure><ol start=\"2\">\n<li>方式二:直接用 <code>obj.singleton_class</code> 方法</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre>obj<span class=\"token punctuation\">.</span>singleton_class <span class=\"token comment\">#=> #<Class:#<Object:0x0000000001d0d760>></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token comment\">## 上述的例子说明单件类也是类(一种特殊的类)。同时每个单件类只有一个实例,而且不能被继承。最重要的是单件类是对象的单件方法存活之所(类方法实际就是一个单件方法):</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token class-name\">obj</span><span class=\"token punctuation\">.</span><span class=\"token function\">my_singleton_method</span></span><span class=\"token punctuation\">;</span> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre>singleton_class<span class=\"token punctuation\">.</span>instance_methods<span class=\"token punctuation\">.</span>grep<span class=\"token punctuation\">(</span><span class=\"token regex-literal\"><span class=\"token regex\">/my_singleton_method/</span></span><span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> [:my_singleton_method]</span></pre></td></tr></table></figure><h2 id=\"方法查找补充\"><a class=\"anchor\" href=\"#方法查找补充\">#</a> 方法查找补充</h2>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">C</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">a_method</span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token string-literal\"><span class=\"token string\">'C#a_method'</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">D</span> <span class=\"token operator\"><</span> <span class=\"token constant\">C</span><span class=\"token punctuation\">;</span> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre></pre></td></tr><tr><td data-num=\"9\"></td><td><pre>obj <span class=\"token operator\">=</span> <span class=\"token class-name\">D</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre>obj<span class=\"token punctuation\">.</span>a_method <span class=\"token comment\">#=> \"C#a_method\"</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre></pre></td></tr><tr><td data-num=\"12\"></td><td><pre><span class=\"token comment\">## 在 obj 上定义一个单件方法</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token operator\"><<</span> obj</pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">a_singleton_method</span></span> </pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token string-literal\"><span class=\"token string\">'obj#a_singleton_method'</span></span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre></pre></td></tr><tr><td data-num=\"19\"></td><td><pre><span class=\"token comment\"># obj 单件类的父类是 D</span></pre></td></tr><tr><td data-num=\"20\"></td><td><pre>obj<span class=\"token punctuation\">.</span>singleton_class<span class=\"token punctuation\">.</span>superclass <span class=\"token comment\">#=> D</span></pre></td></tr></table></figure><blockquote>\n<p>因此如果对象有单件类,ruby 不是从对象所在的类开始查找,而是从对象的单件类开始查找方法。如果在单件类中找不到这个方法,那么它会沿着祖先链向上查找。</p>\n</blockquote>\n<h2 id=\"类扩展和对象扩展\"><a class=\"anchor\" href=\"#类扩展和对象扩展\">#</a> 类扩展和对象扩展</h2>\n<h3 id=\"类扩展\"><a class=\"anchor\" href=\"#类扩展\">#</a> 类扩展</h3>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">module</span> <span class=\"token class-name\">MyModule</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">my_method</span></span><span class=\"token punctuation\">;</span> <span class=\"token string-literal\"><span class=\"token string\">'hello'</span></span> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">MyClass</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">class</span> <span class=\"token operator\"><<</span> <span class=\"token keyword\">self</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token keyword\">include</span> MyModule</pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre></pre></td></tr><tr><td data-num=\"11\"></td><td><pre>MyClass<span class=\"token punctuation\">.</span>my_method <span class=\"token comment\">#=> \"hello\"</span></pre></td></tr></table></figure><h3 id=\"对象扩展\"><a class=\"anchor\" href=\"#对象扩展\">#</a> 对象扩展</h3>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">module</span> <span class=\"token class-name\">MyModule</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">my_method</span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token string-literal\"><span class=\"token string\">'MyModule#my_method'</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr><td data-num=\"7\"></td><td><pre>obj <span class=\"token operator\">=</span> <span class=\"token class-name\">Object</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token operator\"><<</span> obj</pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token keyword\">include</span> MyModule</pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre></pre></td></tr><tr><td data-num=\"12\"></td><td><pre>obj<span class=\"token punctuation\">.</span>my_method <span class=\"token comment\">#=> \"MyModule#my_method\"</span></pre></td></tr></table></figure><h3 id=\"扩展的简化形式\"><a class=\"anchor\" href=\"#扩展的简化形式\">#</a> 扩展的简化形式</h3>\n<p>Object#extend 方法</p>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">module</span> <span class=\"token class-name\">MyModule</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">my_method</span></span><span class=\"token punctuation\">;</span> <span class=\"token string-literal\"><span class=\"token string\">'hello'</span></span> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre>obj <span class=\"token operator\">=</span> <span class=\"token class-name\">Object</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre>obj<span class=\"token punctuation\">.</span><span class=\"token keyword\">extend</span> MyModule</pre></td></tr><tr><td data-num=\"7\"></td><td><pre>obj<span class=\"token punctuation\">.</span>my_method <span class=\"token comment\">#=> \"hello\"</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">MyClass</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token keyword\">extend</span> MyModule</pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre></pre></td></tr><tr><td data-num=\"13\"></td><td><pre>MyClass<span class=\"token punctuation\">.</span>my_method <span class=\"token comment\">#=> \"hello\"</span></pre></td></tr></table></figure><h2 id=\"方法包装器\"><a class=\"anchor\" href=\"#方法包装器\">#</a> 方法包装器</h2>\n<h3 id=\"方法别名\"><a class=\"anchor\" href=\"#方法别名\">#</a> 方法别名</h3>\n<blockquote>\n<p>ruby 中可以 alias_method 给一个方法取一个别名,一个参数是新名称,第二个参数是原始名称</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">MyClass</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">my_method</span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token string-literal\"><span class=\"token string\">'my_method'</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">alias</span> m my_method</pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre></pre></td></tr><tr><td data-num=\"9\"></td><td><pre>obj <span class=\"token operator\">=</span> <span class=\"token class-name\">MyClass</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre>obj<span class=\"token punctuation\">.</span>my_method <span class=\"token comment\"># => \"my_method\"</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre>obj<span class=\"token punctuation\">.</span>m <span class=\"token comment\"># => \"my_method\"</span></pre></td></tr></table></figure><div class=\"note primary\">\n<p>除此之外 ruby 还提供了 alias 关键字,可以代替 Module#alias_method 方法(当你需要在顶级作用域进行修改时使用,因为此时 Module#alias_method 不可用)</p>\n</div>\n<blockquote>\n<p>如果给一个方法起个别名,然后又重新定义它,会怎么样?</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">String</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">alias</span> real_length length</pre></td></tr><tr><td data-num=\"3\"></td><td><pre></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">length</span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> real_length <span class=\"token operator\">></span> <span class=\"token number\">5</span> <span class=\"token operator\">?</span> <span class=\"token string-literal\"><span class=\"token string\">'long'</span></span> <span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">'short'</span></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token string-literal\"><span class=\"token string\">'War and Peace'</span></span><span class=\"token punctuation\">.</span>length <span class=\"token comment\"># => \"long\"</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token string-literal\"><span class=\"token string\">'War and Peace'</span></span><span class=\"token punctuation\">.</span>real_length <span class=\"token comment\"># => 13</span></pre></td></tr></table></figure><blockquote>\n<p>上述的代码重新定义了 String#length 方法,但是别名方法引用的还是原始方法。这说明重定义方法的工作方式:重定义方法时,并不真正修改这个方法。相反,你定义了一个新方法并吧当前存在的这个方法名字跟它绑定。只要老方法还存在一个绑定的名字,仍旧可以调用它(通过老方法的别名调用,就比如上面的 real_length 方法)</p>\n</blockquote>\n<h3 id=\"环绕别名\"><a class=\"anchor\" href=\"#环绕别名\">#</a> 环绕别名</h3>\n<blockquote>\n<p>通过环绕别名可以给已用方法包装新的功能</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">module</span> <span class=\"token class-name\">Kernel</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">alias</span> old_puts puts</pre></td></tr><tr><td data-num=\"3\"></td><td><pre></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">puts</span></span><span class=\"token punctuation\">(</span><span class=\"token operator\">*</span>args<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token comment\"># 自定义的一些代码</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> old_puts<span class=\"token punctuation\">(</span><span class=\"token operator\">*</span>args<span class=\"token punctuation\">)</span> <span class=\"token comment\"># 调用原始的 puts 方法</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token comment\"># 自定义的一些代码</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><blockquote>\n<p>环境别名定义的三个步骤:</p>\n<ol>\n<li>给原始方法起一个别名</li>\n<li>重定义原始方法</li>\n<li>在新的方法中调用老的方法(通过之前定义的别名来进行)</li>\n</ol>\n</blockquote>\n<h3 id=\"细化包装器\"><a class=\"anchor\" href=\"#细化包装器\">#</a> 细化包装器</h3>\n<blockquote>\n<p>细化除了可以把一段代码直接加入一个类中,还可以用来替换环绕别名。<br />\n如果在细化的方法中,调用了 super 方法,则会调用没有细化的原始方法。</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">module</span> <span class=\"token class-name\">StringRefinement</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> refine <span class=\"token builtin\">String</span> <span class=\"token keyword\">do</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">length</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">super</span> <span class=\"token operator\">></span> <span class=\"token number\">5</span> <span class=\"token operator\">?</span> <span class=\"token string-literal\"><span class=\"token string\">'long'</span></span> <span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">'short'</span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre></pre></td></tr><tr><td data-num=\"9\"></td><td><pre>using StringRefinement</pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token string-literal\"><span class=\"token string\">'War and Peace'</span></span><span class=\"token punctuation\">.</span>length <span class=\"token comment\">#=> \"long\"</span></pre></td></tr></table></figure><p>上述代码技术成为细化封装器,其作用范围和之前的细化一样,作用范围只到文件末尾,在 ruby2.1 中是在模块的定义范围之内(这样要比环绕别名方法更加安全,因为环绕别名是全局性的)</p>\n<h3 id=\"moduleprepend方法\"><a class=\"anchor\" href=\"#moduleprepend方法\">#</a> Module#prepend 方法</h3>\n<p>Module#prepend 方法和 include 类似,但是它会把包含的模块插入到祖先链中该类下方。这意味着被 prepend 方法包含的模块可以覆写该类的同名方法,同时可以通过 super 调用该类中的原始方法:</p>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">module</span> <span class=\"token class-name\">ExplicitString</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">length</span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">super</span> <span class=\"token operator\">></span> <span class=\"token number\">5</span> <span class=\"token operator\">?</span> <span class=\"token string-literal\"><span class=\"token string\">\"long\"</span></span> <span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">\"short\"</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token builtin\">String</span><span class=\"token punctuation\">.</span>class_eval <span class=\"token keyword\">do</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token keyword\">prepend</span> ExplicitString</pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token string-literal\"><span class=\"token string\">\"War and Peace\"</span></span><span class=\"token punctuation\">.</span>length <span class=\"token comment\">#=> \"long\"</span></pre></td></tr></table></figure><p>这种技术也被称为下包含包装器。和细化包装器相比,它不是一种局部化方法,但是一般认为其比细化包装器和环绕别名都更清晰。</p>\n",
"tags": [
"Ruby",
"Ruby",
"Metaprogramming"
]
},
{
"id": "https://unistart2.github.io/posts/22b3ad13/",
"url": "https://unistart2.github.io/posts/22b3ad13/",
"title": "nest.js学习 - 全局模块和生命周期",
"date_published": "2023-05-31T08:50:49.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><h2 id=\"全局模块\"><a class=\"anchor\" href=\"#全局模块\">#</a> 全局模块</h2>\n<blockquote>\n<p>nest.js 项目一般由各个模块组成,如果一个模块 A 需要使用到模块 B 中的 provider,那么需要在模块 B 中导出对应的 provider,<br />\n同时还需要在模块 A 中导入模块 B,这样才能使用模块 B 的 provider</p>\n</blockquote>\n<figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"><span>AaaModule导出provider</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token imports\"><span class=\"token punctuation\">{</span> <span class=\"token maybe-class-name\">Global</span><span class=\"token punctuation\">,</span> <span class=\"token maybe-class-name\">Module</span> <span class=\"token punctuation\">}</span></span> <span class=\"token keyword module\">from</span> <span class=\"token string\">'@nestjs/common'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token imports\"><span class=\"token punctuation\">{</span> <span class=\"token maybe-class-name\">AaaService</span> <span class=\"token punctuation\">}</span></span> <span class=\"token keyword module\">from</span> <span class=\"token string\">'./aaa.service'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token imports\"><span class=\"token punctuation\">{</span> <span class=\"token maybe-class-name\">AaaController</span> <span class=\"token punctuation\">}</span></span> <span class=\"token keyword module\">from</span> <span class=\"token string\">'./aaa.controller'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre>@<span class=\"token function\"><span class=\"token maybe-class-name\">Module</span></span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token literal-property property\">controllers</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token maybe-class-name\">AaaController</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token literal-property property\">providers</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token maybe-class-name\">AaaService</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span></pre></td></tr><tr class=\"marked\"><td data-num=\"8\"></td><td><pre> <span class=\"token literal-property property\">exports</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token maybe-class-name\">AaaService</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// 导出的 provider</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token keyword module\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">AaaModule</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"><span>BbbModule导入AaaModule</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token imports\"><span class=\"token punctuation\">{</span> <span class=\"token maybe-class-name\">Module</span> <span class=\"token punctuation\">}</span></span> <span class=\"token keyword module\">from</span> <span class=\"token string\">'@nestjs/common'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token imports\"><span class=\"token punctuation\">{</span> <span class=\"token maybe-class-name\">BbbService</span> <span class=\"token punctuation\">}</span></span> <span class=\"token keyword module\">from</span> <span class=\"token string\">'./bbb.service'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token imports\"><span class=\"token punctuation\">{</span> <span class=\"token maybe-class-name\">BbbController</span> <span class=\"token punctuation\">}</span></span> <span class=\"token keyword module\">from</span> <span class=\"token string\">'./bbb.controller'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token imports\"><span class=\"token punctuation\">{</span> <span class=\"token maybe-class-name\">AaaModule</span> <span class=\"token punctuation\">}</span></span> <span class=\"token keyword module\">from</span> <span class=\"token string\">'src/aaa/aaa.module'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre></pre></td></tr><tr><td data-num=\"6\"></td><td><pre>@<span class=\"token function\"><span class=\"token maybe-class-name\">Module</span></span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span></pre></td></tr><tr class=\"marked\"><td data-num=\"7\"></td><td><pre> <span class=\"token literal-property property\">imports</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token maybe-class-name\">AaaModule</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token literal-property property\">controllers</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token maybe-class-name\">BbbController</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token literal-property property\">providers</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token maybe-class-name\">BbbService</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token keyword module\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">BbbModule</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><blockquote>\n<p>但是如果一个模块被多个其他模块所依赖,这样一个一个导入会显得很麻烦,<br />\n因此需要使用 @Global 装饰器将其作为全局模块,这样全局模块对所有其他模块均可见,不再需要导入模块了</p>\n</blockquote>\n<figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"><span>@Global全局模块</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token imports\"><span class=\"token punctuation\">{</span> <span class=\"token maybe-class-name\">Global</span><span class=\"token punctuation\">,</span> <span class=\"token maybe-class-name\">Module</span> <span class=\"token punctuation\">}</span></span> <span class=\"token keyword module\">from</span> <span class=\"token string\">'@nestjs/common'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token imports\"><span class=\"token punctuation\">{</span> <span class=\"token maybe-class-name\">AaaService</span> <span class=\"token punctuation\">}</span></span> <span class=\"token keyword module\">from</span> <span class=\"token string\">'./aaa.service'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token imports\"><span class=\"token punctuation\">{</span> <span class=\"token maybe-class-name\">AaaController</span> <span class=\"token punctuation\">}</span></span> <span class=\"token keyword module\">from</span> <span class=\"token string\">'./aaa.controller'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr class=\"marked\"><td data-num=\"5\"></td><td><pre>@<span class=\"token function\"><span class=\"token maybe-class-name\">Global</span></span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// </span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre>@<span class=\"token function\"><span class=\"token maybe-class-name\">Module</span></span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token literal-property property\">controllers</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token maybe-class-name\">AaaController</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token literal-property property\">providers</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token maybe-class-name\">AaaService</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token literal-property property\">exports</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token maybe-class-name\">AaaService</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token comment\">// 导出的 provider</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token keyword module\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">AaaModule</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"><span>其他模块不再需要imports导入了</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token imports\"><span class=\"token punctuation\">{</span> <span class=\"token maybe-class-name\">Module</span> <span class=\"token punctuation\">}</span></span> <span class=\"token keyword module\">from</span> <span class=\"token string\">'@nestjs/common'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token imports\"><span class=\"token punctuation\">{</span> <span class=\"token maybe-class-name\">BbbService</span> <span class=\"token punctuation\">}</span></span> <span class=\"token keyword module\">from</span> <span class=\"token string\">'./bbb.service'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword module\">import</span> <span class=\"token imports\"><span class=\"token punctuation\">{</span> <span class=\"token maybe-class-name\">BbbController</span> <span class=\"token punctuation\">}</span></span> <span class=\"token keyword module\">from</span> <span class=\"token string\">'./bbb.controller'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre>@<span class=\"token function\"><span class=\"token maybe-class-name\">Module</span></span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span></pre></td></tr><tr class=\"marked\"><td data-num=\"6\"></td><td><pre> <span class=\"token literal-property property\">imports</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token literal-property property\">controllers</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token maybe-class-name\">BbbController</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token literal-property property\">providers</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span><span class=\"token maybe-class-name\">BbbService</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token keyword module\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">BbbModule</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><h2 id=\"启动时的生命周期\"><a class=\"anchor\" href=\"#启动时的生命周期\">#</a> 启动时的生命周期</h2>\n<p><img data-src=\"https://cdn.unistart.top/blog/nest.js-bootstrap-lifecycle.png\" alt=\"nest.js启动时生命周期流程\" /></p>\n<blockquote>\n<p>在启动过程中,onModuleInit 和 onApplicationBootstrap 都是我们可以实现的生命周期方法<br />\n nest.js 提供了对应的接口 OnModuleInit 和 OnApplicationBootstrap</p>\n</blockquote>\n<figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token spread operator\">...</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>@<span class=\"token function\"><span class=\"token maybe-class-name\">Controller</span></span><span class=\"token punctuation\">(</span><span class=\"token string\">'ccc'</span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token keyword module\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">CccController</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">implements</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token class-name\">OnModuleInit</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token maybe-class-name\">OnApplicationBootstrap</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token keyword\">private</span> readonly cccService<span class=\"token operator\">:</span> <span class=\"token maybe-class-name\">CccService</span></span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> <span class=\"token function\">onModuleInit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'CccController onModuleInit'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token function\">onApplicationBootstrap</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'CccController onApplicationBootstrap'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre></pre></td></tr><tr><td data-num=\"18\"></td><td><pre> <span class=\"token spread operator\">...</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><h2 id=\"结束时的生命周期\"><a class=\"anchor\" href=\"#结束时的生命周期\">#</a> 结束时的生命周期</h2>\n<p><img data-src=\"https://cdn.unistart.top/blog/nestjs-destroy-lifecycle.png\" alt=\"nest.js结束时生命周期流程\" /></p>\n<blockquote>\n<p>在结束过程中,onModuleDestroy 和 beforeApplicationShutdown 以及 onApplicationShutdown 都是我们可以实现的生命周期方法<br />\n nest.js 提供了对应的接口 OnModuleDestroy 和 BeforeApplicationShutdown 以及 OnApplicationShutdown</p>\n</blockquote>\n<figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre>@<span class=\"token function\"><span class=\"token maybe-class-name\">Controller</span></span><span class=\"token punctuation\">(</span><span class=\"token string\">'ccc'</span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword module\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">CccController</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">implements</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token class-name\">OnModuleDestroy</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token maybe-class-name\">BeforeApplicationShutdown</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token maybe-class-name\">OnApplicationShutdown</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token function\">constructor</span><span class=\"token punctuation\">(</span><span class=\"token parameter\"><span class=\"token keyword\">private</span> readonly cccService<span class=\"token operator\">:</span> <span class=\"token maybe-class-name\">CccService</span></span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token function\">onModuleDestroy</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'CccController onModuleDestroy'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token function\">beforeApplicationShutdown</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">signal<span class=\"token operator\">?</span><span class=\"token operator\">:</span> string</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'CccController beforeApplicationShutdown'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre></pre></td></tr><tr><td data-num=\"18\"></td><td><pre> <span class=\"token function\">onApplicationShutdown</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">signal<span class=\"token operator\">?</span><span class=\"token operator\">:</span> string</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'CccController onApplicationShutdown'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"20\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre></pre></td></tr><tr><td data-num=\"22\"></td><td><pre> <span class=\"token spread operator\">...</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure>",
"tags": [
"nest.js",
"nest.js"
]
},
{
"id": "https://unistart2.github.io/posts/2688dc52/",
"url": "https://unistart2.github.io/posts/2688dc52/",
"title": "Rails学习 - 固件",
"date_published": "2023-05-04T10:47:15.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><blockquote>\n<p>参考书籍:<br />\nRuby on Rails 教程(第四版)</p>\n</blockquote>\n<div class=\"note primary\">\n<p>固件(fixture)是<ins class=\"red dot\"> Rails</ins> 中组织<ins class=\"red dot\">测试数据</ins>的一种方式,这些数据会载入测试数据库</p>\n</div>\n<h2 id=\"简单的使用示例\"><a class=\"anchor\" href=\"#简单的使用示例\">#</a> 简单的使用示例</h2>\n<p>例如,我们有一个 <code>User</code> 模型,它有 <code>name</code> , <code>email</code> , <code>password_digest</code> 三个字段,那么我们可以通过如下操作来进行测试</p>\n<ol>\n<li>在模型对应的固件文件中编写测试数据</li>\n</ol>\n<figure class=\"highlight yaml\"><figcaption data-lang=\"YAML\"><span>test/fixture/users.yml</span></figcaption><table><tr class=\"marked\"><td data-num=\"1\"></td><td><pre><span class=\"token key atrule\">unistart</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> unistart</pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token key atrule\">email</span><span class=\"token punctuation\">:</span> [email protected]</pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token key atrule\">password_digest</span><span class=\"token punctuation\">:</span> <%= User.digest('password') %<span class=\"token punctuation\">></span></pre></td></tr></table></figure><ol start=\"2\">\n<li>编写测试文件,并使用固件中的数据</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>test/integration/users_login_test.rb</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">require</span> <span class=\"token string-literal\"><span class=\"token string\">\"test_helper\"</span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">UsersLoginTest</span> <span class=\"token operator\"><</span> ActionDispatch<span class=\"token double-colon punctuation\">::</span>IntegrationTest</pre></td></tr><tr class=\"marked\"><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">setup</span></span></pre></td></tr><tr class=\"marked\"><td data-num=\"5\"></td><td><pre> <span class=\"token variable\">@user</span> <span class=\"token operator\">=</span> users<span class=\"token punctuation\">(</span><span class=\"token symbol\">:unistart</span><span class=\"token punctuation\">)</span></pre></td></tr><tr class=\"marked\"><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token operator\">...</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> test <span class=\"token string-literal\"><span class=\"token string\">\"login with valid information\"</span></span> <span class=\"token keyword\">do</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> get login_path</pre></td></tr><tr><td data-num=\"12\"></td><td><pre> post login_path<span class=\"token punctuation\">,</span> <span class=\"token symbol\">params</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token symbol\">session</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token symbol\">email</span><span class=\"token operator\">:</span> <span class=\"token variable\">@user</span><span class=\"token punctuation\">.</span>email<span class=\"token punctuation\">,</span> <span class=\"token symbol\">password</span><span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">\"password\"</span></span> <span class=\"token punctuation\">}</span> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre> assert_redirected_to <span class=\"token variable\">@user</span> <span class=\"token comment\"># 检查重定向地址是否正确</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> follow_redirect<span class=\"token operator\">!</span> <span class=\"token comment\"># 访问重定向地址</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> assert_select <span class=\"token string-literal\"><span class=\"token string\">\"a[href=?]\"</span></span><span class=\"token punctuation\">,</span> login_path<span class=\"token punctuation\">,</span> <span class=\"token symbol\">count</span><span class=\"token operator\">:</span> <span class=\"token number\">0</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> assert_select <span class=\"token string-literal\"><span class=\"token string\">\"a[href=?]\"</span></span><span class=\"token punctuation\">,</span> logout_path</pre></td></tr><tr><td data-num=\"17\"></td><td><pre> assert_select <span class=\"token string-literal\"><span class=\"token string\">\"a[href=?]\"</span></span><span class=\"token punctuation\">,</span> user_path<span class=\"token punctuation\">(</span><span class=\"token variable\">@user</span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre></pre></td></tr><tr><td data-num=\"19\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"20\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><details class=\"info\"><summary>users(:unistart)的说明</summary><div>\n<p>在上面的测试文件中,我们使用 <code>setup</code> 方法,在每个测试用例执行前生成一个名为 <code>@user</code> 的实例变量,同时该变量的值由对应固件文件 <code>users.yml</code> 中定义的 <code>unistart</code> 提供,也即 <code>users</code> 方法对应 <code>users.yml</code> 固件文件, <code>:unistart</code> 对应固件文件中定义的用户 <code>unistart</code></p>\n</div></details>\n",
"tags": [
"Rails",
"Ruby",
"Rails"
]
},
{
"id": "https://unistart2.github.io/posts/99006ab8/",
"url": "https://unistart2.github.io/posts/99006ab8/",
"title": "Railway部署Rails应用",
"date_published": "2023-05-01T22:18:27.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><h2 id=\"基本步骤\"><a class=\"anchor\" href=\"#基本步骤\">#</a> 基本步骤</h2>\n<ol>\n<li>\n<p>在 Railway 上新建一个项目,选择 <code>Deploy from github repo</code> ,然后选择自己需要部署的项目仓库(如果是第一次使用需要进行授权)<br />\n<img data-src=\"https://cdn.unistart.top/blog/msedge_wruSnnSJYq.png\" alt=\""Deploy from github repo"\" /></p>\n</li>\n<li>\n<p>接下来的选项卡,选择 <code>Deploy now</code> 或 <code>Add variables</code> 都可以,之后 Railway 会创建项目并自动进行构建部署,记得在 <code>Deployments</code> 面板中先中止本次的部署操作<br />\n<img data-src=\"https://cdn.unistart.top/blog/msedge_jYFkmcyJR2.png\" alt=\""Deploy project"\" /></p>\n</li>\n<li>\n<p>在当前项目中点击 <code>New</code> 按钮,创建一个 <code>Postgresql</code> 数据库</p>\n</li>\n<li>\n<p>配置环境变量(可以根据需要修改)</p>\n</li>\n</ol>\n<table>\n<thead>\n<tr>\n<th>环境变量名</th>\n<th>值</th>\n<th>备注</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>PORT</td>\n<td><code>3000</code></td>\n<td>可以根据需要修改</td>\n</tr>\n<tr>\n<td>RAILS_MAX_THREADS</td>\n<td><code>5</code></td>\n<td>可以根据需要修改</td>\n</tr>\n<tr>\n<td>RACK_ENV</td>\n<td><code>production</code></td>\n<td>不要修改</td>\n</tr>\n<tr>\n<td>RAILS_ENV</td>\n<td><code>production</code></td>\n<td>不要修改</td>\n</tr>\n<tr>\n<td>RAILS_MASTER_KEY</td>\n<td>[your master key]</td>\n<td>项目中 <code>master.key</code> 文件中的值</td>\n</tr>\n<tr>\n<td>SECRET_KEY_BASE</td>\n<td>[your secret_key_base]</td>\n<td>可以使用 <code>RAILS_ENV=production rake secret</code> 生成</td>\n</tr>\n<tr>\n<td>DATABASE_URL</td>\n<td><code>${{Postgres.DATABASE_URL}}</code></td>\n<td>直接引入之前创建好的数据库的连接信息</td>\n</tr>\n<tr>\n<td>PGHOST</td>\n<td><code>${{Postgres.PGHOST}}</code></td>\n<td>直接引入之前创建好的数据库的连接信息</td>\n</tr>\n<tr>\n<td>PGPORT</td>\n<td><code>${{Postgres.PGPORT}}</code></td>\n<td>直接引入之前创建好的数据库的连接信息</td>\n</tr>\n<tr>\n<td>PGUSER</td>\n<td><code>${{Postgres.PGUSER}}</code></td>\n<td>直接引入之前创建好的数据库的连接信息</td>\n</tr>\n<tr>\n<td>PGPASSWORD</td>\n<td><code>${{Postgres.PGPASSWORD}}</code></td>\n<td>直接引入之前创建好的数据库的连接信息</td>\n</tr>\n<tr>\n<td>PGDATABASE</td>\n<td><code>${{Postgres.PGDATABASE}}</code></td>\n<td>直接引入之前创建好的数据库的连接信息</td>\n</tr>\n</tbody>\n</table>\n<ol start=\"5\">\n<li>\n<p>接下来在 Rails 项目的 <code>Settings</code> 面板中配置域名和启动命令 <code>rails server</code> <br />\n<img data-src=\"https://cdn.unistart.top/blog/msedge_5V1JpFbkBZ.png\" alt=\""Setup Start command"\" /></p>\n</li>\n<li>\n<p>手动重新部署项目</p>\n</li>\n<li>\n<p>点击 <code>Set up your project locally</code> 选项卡,安装 <code>Railway CLI</code> ,连接到当前项目,在本地终端执行命令 <code>railway run rails db:migrate</code> 完成数据库迁移操作<br />\n<img data-src=\"https://cdn.unistart.top/blog/WindowsTerminal_Nt6o3fyxuW.png\" alt=\""migrate database"\" /></p>\n</li>\n</ol>\n<h2 id=\"踩坑\"><a class=\"anchor\" href=\"#踩坑\">#</a> 踩坑</h2>\n<ol>\n<li><code>Missing secret_key_base</code> 错误,这是生产环境部署时没有在环境变量中配置 <code>secret_key_base</code> 值导致的<br />\n<img data-src=\"https://cdn.unistart.top/blog/msedge_W8UMZ3A1Zh.png\" alt=\""Missing secret_key_base"\" /><br />\n 除了配置环境变量以外,还要在 <code>config/environments/production.rb</code> 文件进行配置</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>config/environments/production.rb</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">require</span> <span class=\"token string-literal\"><span class=\"token string\">\"active_support/core_ext/integer/time\"</span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>Rails<span class=\"token punctuation\">.</span>application<span class=\"token punctuation\">.</span>configure <span class=\"token keyword\">do</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token operator\">...</span> </pre></td></tr><tr><td data-num=\"5\"></td><td><pre></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token comment\"># Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> config<span class=\"token punctuation\">.</span>force_ssl <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token operator\">...</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> <span class=\"token comment\"># Production SECRET_KEY_BASE</span></pre></td></tr><tr class=\"marked\"><td data-num=\"12\"></td><td><pre> config<span class=\"token punctuation\">.</span>secret_key_base <span class=\"token operator\">=</span> <span class=\"token string-literal\"><span class=\"token string\">'<%= ENV[\"SECRET_KEY_BASE\"] %>'</span></span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><ol start=\"2\">\n<li><code>Database empty url</code> 错误,应该是环境变量值没有读到导致的<br />\n<img data-src=\"https://cdn.unistart.top/blog/msedge_XXMRvIZzB2.png\" alt=\""Database empty url"\" /></li>\n</ol>\n<ul>\n<li>出现错误的配置如下:</li>\n</ul>\n<figure class=\"highlight yml\"><figcaption data-lang=\"YAML\"><span>config/database.yml</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token punctuation\">...</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token key atrule\">production</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token key atrule\">adapter</span><span class=\"token punctuation\">:</span> postgresql</pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token key atrule\">url</span><span class=\"token punctuation\">:</span> <span class=\"token string\">'<%= ENV[\"DATABASE_URL\"] %>'</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token key atrule\">user</span><span class=\"token punctuation\">:</span> <span class=\"token string\">'<%= ENV[\"PGUSER\"] %>'</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token key atrule\">password</span><span class=\"token punctuation\">:</span> <span class=\"token string\">'<%= ENV[\"PGPASSWORD\"] %>'</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token key atrule\">pool</span><span class=\"token punctuation\">:</span> <span class=\"token number\">5</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token key atrule\">timeout</span><span class=\"token punctuation\">:</span> <span class=\"token number\">5000</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token key atrule\">encoding</span><span class=\"token punctuation\">:</span> unicode</pre></td></tr></table></figure><ul>\n<li>解决方案是将 url 配置项中的单引号对删掉,然后重新部署:</li>\n</ul>\n<figure class=\"highlight yml\"><figcaption data-lang=\"YAML\"><span>config/database.yml</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token punctuation\">...</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token key atrule\">production</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token key atrule\">adapter</span><span class=\"token punctuation\">:</span> postgresql</pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token key atrule\">url</span><span class=\"token punctuation\">:</span> <%= ENV<span class=\"token punctuation\">[</span><span class=\"token string\">\"DATABASE_URL\"</span><span class=\"token punctuation\">]</span> %<span class=\"token punctuation\">></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token key atrule\">user</span><span class=\"token punctuation\">:</span> <%= ENV<span class=\"token punctuation\">[</span><span class=\"token string\">\"PGUSER\"</span><span class=\"token punctuation\">]</span> %<span class=\"token punctuation\">></span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token key atrule\">password</span><span class=\"token punctuation\">:</span> <%= ENV<span class=\"token punctuation\">[</span><span class=\"token string\">\"PGPASSWORD\"</span><span class=\"token punctuation\">]</span> %<span class=\"token punctuation\">></span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token key atrule\">pool</span><span class=\"token punctuation\">:</span> <span class=\"token number\">5</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token key atrule\">timeout</span><span class=\"token punctuation\">:</span> <span class=\"token number\">5000</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token key atrule\">encoding</span><span class=\"token punctuation\">:</span> unicode</pre></td></tr></table></figure><p>当然也可以直接复制 <code>数据库连接url</code> 到 <code>config/database.yml</code> 配置文件中,不过不推荐使用,因为这样既不安全也不灵活</p>\n",
"tags": [
"云服务",
"Rails",
"云服务",
"Railway"
]
},
{
"id": "https://unistart2.github.io/posts/b702c88f/",
"url": "https://unistart2.github.io/posts/b702c88f/",
"title": "Linux学习 - 快速上手几个Linux命令",
"date_published": "2023-04-28T08:43:57.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><div class=\"note info\">\n<p>参考教程:<a href=\"%E2%80%9Chttps://time.geekbang.org/column/intro/100024701?tab=catalog%E2%80%9D\">趣谈 Linux 操作系统</a></p>\n</div>\n<h2 id=\"用户名和密码\"><a class=\"anchor\" href=\"#用户名和密码\">#</a> 用户名和密码</h2>\n<ol>\n<li><code>passwd</code> 命令</li>\n</ol>\n<blockquote>\n<p>用来修改用户的登录密码<br />\n在 <code>New password</code> 后面输入需要新设置的密码就好了,注意输入的密码不会显示在屏幕上,回车确认后会出现 <code>Retype new password</code> 需要你再次输入新设置的密码</p>\n</blockquote>\n<figure class=\"highlight bash\"><figcaption data-lang=\"bash\"></figcaption><table><tr><td data-num=\"1\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 改变当前用户的密码</span></pre></td></tr><tr><td data-num=\"2\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">passwd</span></pre></td></tr><tr><td data-num=\"3\"></td><td data-command=\"\"></td><td><pre>Changing password <span class=\"token keyword\">for</span> user root.</pre></td></tr><tr><td data-num=\"4\"></td><td data-command=\"\"></td><td><pre>New password:</pre></td></tr><tr><td data-num=\"5\"></td><td data-command=\"\"></td><td><pre></pre></td></tr><tr><td data-num=\"6\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 改变指定用户的密码</span></pre></td></tr><tr><td data-num=\"7\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">passwd</span> <span class=\"token operator\"><</span>username<span class=\"token operator\">></span></pre></td></tr><tr><td data-num=\"8\"></td><td data-command=\"\"></td><td><pre>Changing password <span class=\"token keyword\">for</span> user <span class=\"token operator\"><</span>username<span class=\"token operator\">></span>.</pre></td></tr><tr><td data-num=\"9\"></td><td data-command=\"\"></td><td><pre>New password:</pre></td></tr></table></figure><ol start=\"2\">\n<li><code>useradd</code> 命令</li>\n</ol>\n<blockquote>\n<p>用来新建一个用户<br />\n如果不使用 -g 参数指定新建用户的属组的话,默认会创建一个当前用户同名的属组</p>\n</blockquote>\n<figure class=\"highlight bash\"><figcaption data-lang=\"bash\"></figcaption><table><tr><td data-num=\"1\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 不指定新建用户属组</span></pre></td></tr><tr><td data-num=\"2\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">useradd</span> lighthouse</pre></td></tr><tr><td data-num=\"3\"></td><td data-command=\"\"></td><td><pre></pre></td></tr><tr><td data-num=\"4\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 指定新建用户属组</span></pre></td></tr><tr><td data-num=\"5\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">useradd</span> <span class=\"token parameter variable\">-g</span> root lighthouse</pre></td></tr></table></figure><details class=\"info\"><summary>用户信息存放的位置</summary><div>\n<blockquote>\n<p>在 Linux 中和用户相关的信息一般存放在 <code>/etc/passwd</code> 和 <code>/etc/group</code> 里面</p>\n</blockquote>\n<figure class=\"highlight bash\"><figcaption data-lang=\"bash\"><span>/etc/passwd</span></figcaption><table><tr><td data-num=\"1\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">cat</span> /etc/passwd</pre></td></tr><tr><td data-num=\"2\"></td><td data-command=\"\"></td><td><pre>root:x:0:0:root:/root:/bin/bash</pre></td></tr><tr><td data-num=\"3\"></td><td data-command=\"\"></td><td><pre><span class=\"token punctuation\">..</span>.</pre></td></tr><tr><td data-num=\"4\"></td><td data-command=\"\"></td><td><pre>lighthouse:x:1000:1000::/home/lighthouse:/bin/bash</pre></td></tr><tr><td data-num=\"5\"></td><td data-command=\"\"></td><td><pre><span class=\"token punctuation\">..</span>.</pre></td></tr></table></figure><blockquote>\n<p>在 <code>/etc/passwd</code> 中第一位对应的是当前 Linux 系统中存在的用户名,如 <code>root</code> 和 <code>lighthouse</code> ,第二位 <code>x</code> 代表用户的密码(密码不会存储在这里),第三位和第四位分别代表 <code>用户ID</code> 和 <code>组ID</code> ,接下来的 <code>/root</code> 或 <code>/home/lighthouse</code> 代表的是它们对应 <code>用户的主目录</code> ,最后的 <code>/bin/bash</code> 代表用户登录后使用 <code>bash进行命令行交互</code></p>\n</blockquote>\n</div></details>\n<h2 id=\"浏览文件\"><a class=\"anchor\" href=\"#浏览文件\">#</a> 浏览文件</h2>\n<ol>\n<li><code>cd</code> 命令</li>\n</ol>\n<blockquote>\n<p>用户切换目录</p>\n</blockquote>\n<figure class=\"highlight bash\"><figcaption data-lang=\"bash\"></figcaption><table><tr><td data-num=\"1\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 切换到当前目录</span></pre></td></tr><tr><td data-num=\"2\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token builtin class-name\">cd</span> <span class=\"token builtin class-name\">.</span></pre></td></tr><tr><td data-num=\"3\"></td><td data-command=\"\"></td><td><pre></pre></td></tr><tr><td data-num=\"4\"></td><td data-command=\"[root@localhost /]#\"></td><td><pre><span class=\"token comment\"># 切换到上级目录</span></pre></td></tr><tr><td data-num=\"5\"></td><td data-command=\"\"></td><td><pre><span class=\"token builtin class-name\">cd</span> <span class=\"token punctuation\">..</span>/</pre></td></tr></table></figure><ol start=\"2\">\n<li><code>pwd</code> 命令</li>\n</ol>\n<blockquote>\n<p>输出当前工作目录的路径</p>\n</blockquote>\n<figure class=\"highlight bash\"><figcaption data-lang=\"bash\"></figcaption><table><tr><td data-num=\"1\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token comment\"># 输出当前工作目录</span></pre></td></tr><tr><td data-num=\"2\"></td><td data-command=\"\"></td><td><pre><span class=\"token builtin class-name\">pwd</span></pre></td></tr><tr><td data-num=\"3\"></td><td data-command=\"\"></td><td><pre>/root</pre></td></tr></table></figure><ol start=\"3\">\n<li><code>ls</code> 命令</li>\n</ol>\n<blockquote>\n<p>列出当前工作目录下的文件</p>\n</blockquote>\n<figure class=\"highlight bash\"><figcaption data-lang=\"bash\"></figcaption><table><tr><td data-num=\"1\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 列出文件</span></pre></td></tr><tr><td data-num=\"2\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">ls</span> </pre></td></tr><tr><td data-num=\"3\"></td><td data-command=\"\"></td><td><pre>bot.log github-project</pre></td></tr><tr><td data-num=\"4\"></td><td data-command=\"\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 以列表的形式列出 (可以使用 ls -al 列出隐藏文件)</span></pre></td></tr><tr><td data-num=\"6\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">ls</span> <span class=\"token parameter variable\">-l</span></pre></td></tr><tr><td data-num=\"7\"></td><td data-command=\"\"></td><td><pre>total <span class=\"token number\">196</span></pre></td></tr><tr><td data-num=\"8\"></td><td data-command=\"\"></td><td><pre>-rw-r--r-- <span class=\"token number\">1</span> root root <span class=\"token number\">188890</span> Apr <span class=\"token number\">28</span> <span class=\"token number\">12</span>:39 bot.log</pre></td></tr><tr><td data-num=\"9\"></td><td data-command=\"\"></td><td><pre>drwxr-xr-x <span class=\"token number\">6</span> root root <span class=\"token number\">4096</span> Apr <span class=\"token number\">14</span> <span class=\"token number\">11</span>:20 github-project</pre></td></tr></table></figure><details class=\"info\"><summary>文件列表信息说明</summary><div>\n<ol>\n<li><code>第一个字段的第一个字符是文件类型</code> :如果是 “-”,表示普通文件;如果是 d,就表示目录(当然除此之外,还有很多种文件类型)</li>\n<li><code>第一个字段剩下的9个字符是模式</code> :其实是权限位,3 个一组,每一组 <code>rwx</code> 表示 <code>读(read)写(write)执行(execute)</code> 。如果是字母,就说明有这个权限;如果是横线,就是没有这个权限。这三组分别表示 <code>文件所属的用户权限</code> 、 <code>文件所属的组权限</code> 以及 <code>其他用户的权限</code> 。例如,上面的例子中, <code>-rw-r–r--</code> 代表这是一个普通文件,对于所属用户,可读可写不能执行;对于所属的组,仅仅可读;对于其他用户,也是仅仅可读</li>\n<li>第二个字段是 <code>硬链接数目</code></li>\n<li>第三个字段是 <code>所属用户</code> ,第四个字段是 <code>所属组</code></li>\n<li>第五个字段是 <code>文件大小</code> ,第六个字段是 <code>文件被修改的日期</code> ,最后是 <code>文件名</code></li>\n</ol>\n<hr />\n<p>修改文件信息</p>\n<ol>\n<li>通过 <code>chmod</code> 命令修改文件权限信息</li>\n</ol>\n<figure class=\"highlight bash\"><figcaption data-lang=\"bash\"></figcaption><table><tr><td data-num=\"1\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 创建一个演示文件</span></pre></td></tr><tr><td data-num=\"2\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">touch</span> demo.txt</pre></td></tr><tr><td data-num=\"3\"></td><td data-command=\"\"></td><td><pre></pre></td></tr><tr><td data-num=\"4\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 列出当前目录下的文件</span></pre></td></tr><tr><td data-num=\"5\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">ls</span> <span class=\"token parameter variable\">-l</span></pre></td></tr><tr><td data-num=\"6\"></td><td data-command=\"\"></td><td><pre>-rw-r--r-- <span class=\"token number\">1</span> root root <span class=\"token number\">188890</span> Apr <span class=\"token number\">28</span> <span class=\"token number\">12</span>:39 bot.log</pre></td></tr><tr><td data-num=\"7\"></td><td data-command=\"\"></td><td><pre>-rw-r--r-- <span class=\"token number\">1</span> root root <span class=\"token number\">0</span> Apr <span class=\"token number\">28</span> <span class=\"token number\">17</span>:44 demo.txt</pre></td></tr><tr><td data-num=\"8\"></td><td data-command=\"\"></td><td><pre>drwxr-xr-x <span class=\"token number\">6</span> root root <span class=\"token number\">4096</span> Apr <span class=\"token number\">14</span> <span class=\"token number\">11</span>:20 github-project</pre></td></tr><tr><td data-num=\"9\"></td><td data-command=\"\"></td><td><pre></pre></td></tr><tr><td data-num=\"10\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 修改 demo.txt 文件的权限</span></pre></td></tr><tr><td data-num=\"11\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">chmod</span> <span class=\"token number\">711</span> demot.txt </pre></td></tr><tr><td data-num=\"12\"></td><td data-command=\"\"></td><td><pre></pre></td></tr><tr><td data-num=\"13\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 重新列出当前目录下的文件</span></pre></td></tr><tr><td data-num=\"14\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">ls</span> <span class=\"token parameter variable\">-l</span></pre></td></tr><tr><td data-num=\"15\"></td><td data-command=\"\"></td><td><pre>-rw-r--r-- <span class=\"token number\">1</span> root root <span class=\"token number\">188890</span> Apr <span class=\"token number\">28</span> <span class=\"token number\">12</span>:39 bot.log</pre></td></tr><tr><td data-num=\"16\"></td><td data-command=\"\"></td><td><pre>-rwx--x--x <span class=\"token number\">1</span> root root <span class=\"token number\">0</span> Apr <span class=\"token number\">28</span> <span class=\"token number\">17</span>:44 demo.txt</pre></td></tr><tr><td data-num=\"17\"></td><td data-command=\"\"></td><td><pre>drwxr-xr-x <span class=\"token number\">6</span> root root <span class=\"token number\">4096</span> Apr <span class=\"token number\">14</span> <span class=\"token number\">11</span>:20 github-project</pre></td></tr></table></figure><div class=\"note primary\">\n<p><code>chmod 711</code> 的含义:我们知道<ins class=\"dot\">文件权限位是 3 个一组,一共 3 组,每组中的 r、w、x 对应的值分别是 4、2、1(每组权值累加即代表该组权限)</ins>,因此 <code>711</code> 就相当于 <code>rwx--x--x</code> ,即赋予文件所属用户可读、可写、可执行权限;文件所属组可执行权限;其他用户可执行权限</p>\n</div>\n<ol start=\"2\">\n<li>通过 <code>chown</code> 命令修改文件所属用户</li>\n</ol>\n<figure class=\"highlight bash\"><figcaption data-lang=\"bash\"></figcaption><table><tr><td data-num=\"1\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 列出当前目录下的文件</span></pre></td></tr><tr><td data-num=\"2\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">ls</span> <span class=\"token parameter variable\">-l</span></pre></td></tr><tr><td data-num=\"3\"></td><td data-command=\"\"></td><td><pre>total <span class=\"token number\">196</span></pre></td></tr><tr><td data-num=\"4\"></td><td data-command=\"\"></td><td><pre>-rw-r--r-- <span class=\"token number\">1</span> root root <span class=\"token number\">188890</span> Apr <span class=\"token number\">28</span> <span class=\"token number\">12</span>:39 bot.log</pre></td></tr><tr><td data-num=\"5\"></td><td data-command=\"\"></td><td><pre>-rwx--x--x <span class=\"token number\">1</span> root root <span class=\"token number\">0</span> Apr <span class=\"token number\">28</span> <span class=\"token number\">17</span>:44 demo.txt</pre></td></tr><tr><td data-num=\"6\"></td><td data-command=\"\"></td><td><pre>drwxr-xr-x <span class=\"token number\">6</span> root root <span class=\"token number\">4096</span> Apr <span class=\"token number\">14</span> <span class=\"token number\">11</span>:20 github-project</pre></td></tr><tr><td data-num=\"7\"></td><td data-command=\"\"></td><td><pre></pre></td></tr><tr><td data-num=\"8\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 修改 demo.txt 文件所属用户</span></pre></td></tr><tr><td data-num=\"9\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">chown</span> lighthouse demo.txt</pre></td></tr><tr><td data-num=\"10\"></td><td data-command=\"\"></td><td><pre></pre></td></tr><tr><td data-num=\"11\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 重新列出当前目录下的文件(demo.txt 所属用户已经改为 lighthouse)</span></pre></td></tr><tr><td data-num=\"12\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">ls</span> <span class=\"token parameter variable\">-l</span></pre></td></tr><tr><td data-num=\"13\"></td><td data-command=\"\"></td><td><pre>total <span class=\"token number\">196</span></pre></td></tr><tr><td data-num=\"14\"></td><td data-command=\"\"></td><td><pre>-rw-r--r-- <span class=\"token number\">1</span> root root <span class=\"token number\">188890</span> Apr <span class=\"token number\">28</span> <span class=\"token number\">12</span>:39 bot.log</pre></td></tr><tr><td data-num=\"15\"></td><td data-command=\"\"></td><td><pre>-rwx--x--x <span class=\"token number\">1</span> lighthouse root <span class=\"token number\">0</span> Apr <span class=\"token number\">28</span> <span class=\"token number\">17</span>:44 demo.txt</pre></td></tr><tr><td data-num=\"16\"></td><td data-command=\"\"></td><td><pre>drwxr-xr-x <span class=\"token number\">6</span> root root <span class=\"token number\">4096</span> Apr <span class=\"token number\">14</span> <span class=\"token number\">11</span>:20 github-project</pre></td></tr></table></figure><ol start=\"3\">\n<li>通过 <code>chgrp</code> 命令修改文件所属组</li>\n</ol>\n<figure class=\"highlight bash\"><figcaption data-lang=\"bash\"></figcaption><table><tr><td data-num=\"1\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 列出当前目录下的文件</span></pre></td></tr><tr><td data-num=\"2\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">ls</span> <span class=\"token parameter variable\">-l</span></pre></td></tr><tr><td data-num=\"3\"></td><td data-command=\"\"></td><td><pre>total <span class=\"token number\">196</span></pre></td></tr><tr><td data-num=\"4\"></td><td data-command=\"\"></td><td><pre>-rw-r--r-- <span class=\"token number\">1</span> root root <span class=\"token number\">188890</span> Apr <span class=\"token number\">28</span> <span class=\"token number\">12</span>:39 bot.log</pre></td></tr><tr><td data-num=\"5\"></td><td data-command=\"\"></td><td><pre>-rwx--x--x <span class=\"token number\">1</span> lighthouse root <span class=\"token number\">0</span> Apr <span class=\"token number\">28</span> <span class=\"token number\">17</span>:44 demo.txt</pre></td></tr><tr><td data-num=\"6\"></td><td data-command=\"\"></td><td><pre>drwxr-xr-x <span class=\"token number\">6</span> root root <span class=\"token number\">4096</span> Apr <span class=\"token number\">14</span> <span class=\"token number\">11</span>:20 github-project</pre></td></tr><tr><td data-num=\"7\"></td><td data-command=\"\"></td><td><pre></pre></td></tr><tr><td data-num=\"8\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 修改 demo.txt 文件所属组</span></pre></td></tr><tr><td data-num=\"9\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">chgrp</span> lighthouse demo.txt</pre></td></tr><tr><td data-num=\"10\"></td><td data-command=\"\"></td><td><pre></pre></td></tr><tr><td data-num=\"11\"></td><td data-command=\"\"></td><td><pre><span class=\"token comment\"># 重新列出当前目录下的文件(demo.txt 所属组已经改为 lighthouse)</span></pre></td></tr><tr><td data-num=\"12\"></td><td data-command=\"[root@localhost ~]#\"></td><td><pre><span class=\"token function\">ls</span> <span class=\"token parameter variable\">-l</span></pre></td></tr><tr><td data-num=\"13\"></td><td data-command=\"\"></td><td><pre>total <span class=\"token number\">196</span></pre></td></tr><tr><td data-num=\"14\"></td><td data-command=\"\"></td><td><pre>-rw-r--r-- <span class=\"token number\">1</span> root root <span class=\"token number\">188890</span> Apr <span class=\"token number\">28</span> <span class=\"token number\">12</span>:39 bot.log</pre></td></tr><tr><td data-num=\"15\"></td><td data-command=\"\"></td><td><pre>-rwx--x--x <span class=\"token number\">1</span> lighthouse lighthouse <span class=\"token number\">0</span> Apr <span class=\"token number\">28</span> <span class=\"token number\">17</span>:44 demo.txt</pre></td></tr><tr><td data-num=\"16\"></td><td data-command=\"\"></td><td><pre>drwxr-xr-x <span class=\"token number\">6</span> root root <span class=\"token number\">4096</span> Apr <span class=\"token number\">14</span> <span class=\"token number\">11</span>:20 github-project</pre></td></tr></table></figure><div class=\"note info\">\n<p>以可以通过 <code>chown user:group filename</code> 一次性修改文件的所属用户和所属组</p>\n</div>\n</div></details>\n<h2 id=\"安装软件\"><a class=\"anchor\" href=\"#安装软件\">#</a> 安装软件</h2>\n<ul>\n<li>CentOS 体系\n<ol>\n<li>使用 <code>rpm包</code> ,通过命令 <code>rpm -i xxx.rpm</code> 进行安装, <code>rpm -qa</code> 可以查看安装的软件列表, <code>rpm -e xxx</code> 来删除已经安装的软件</li>\n<li>通过 <code>yum</code> 包管理器安装,执行命令 <code>yum install xxx</code> 即可。比如, <code>yum install zip.x86_64 unzip.x86_64</code></li>\n</ol>\n</li>\n<li>Ubuntu 体系\n<ol>\n<li>使用 <code>deb</code> 包,通过命令 <code>dpkg -i xxx.deb</code> 进行安装, <code>dpkg -l</code> 查看安装的软件列表, <code>dpkg -r xxx</code> 来删除已经安装的软件</li>\n<li>通过 <code>apt</code> 包管理器安装,执行命令 <code>apt install xxx</code> 即可。比如, <code>apt install zip unzip</code></li>\n</ol>\n</li>\n</ul>\n<blockquote>\n<p>其实无论是先下载再安装,还是通过软件管家进行安装,都是下载一些文件,然后将这些文件放在某个路径下,然后在相应的配置文件中配置一下。例如,在 <code>Windows</code> 里面,最终会变成 <code>C:\\Program Files</code> 下面的一个文件夹以及注册表里面的一些配置。对应 <code>Linux</code> 里面会放的更散一点。例如,主执行文件会放在 <code>/usr/bin</code> 或者 <code>/usr/sbin</code> 下面,其他的库文件会放在 <code>/var</code> 下面,配置文件会放在 <code>/etc</code> 下面</p>\n</blockquote>\n<ul>\n<li>通用方案\n<blockquote>\n<p>通过将安装好的路径直接下载下来,然后解压缩成为一个整的路径<br />\n如果是 <code>tar.gz</code> 这种格式的,通过 <code>tar xvzf xxx.tar.gz</code> 就可以解压缩了<br />\n通过压缩包进行安装的方式,需要自行设置环境变量。比如,安装 JDK 需要将 <code>export JAVA_HOME=/root/jdk-XXX_linux-x64</code> <br />\n 和 <code>export PATH=$JAVA_HOME/bin:$PATH</code> 环境变量加入到 <code>当前用户主目录</code> 下的 <code>.bashrc</code> 文件中(如果使用的是 bash 进行进行命令行交互)</p>\n</blockquote>\n</li>\n</ul>\n",
"tags": [
"Linux",
"Linux"
]
},
{
"id": "https://unistart2.github.io/posts/6909bdb9/",
"url": "https://unistart2.github.io/posts/6909bdb9/",
"title": "微信个人订阅号连接ChatGPT",
"date_published": "2023-04-19T09:45:53.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><div class=\"note info\">\n<p>需要使用到的 GitHub 仓库:</p>\n<ol>\n<li><span class=\"exturl\" data-url=\"aHR0cHM6Ly9naXRodWIuY29tL3dhbmhlYmluL2NsYXNoLWZvci1saW51eA==\">clash-for-linux</span></li>\n<li><span class=\"exturl\" data-url=\"aHR0cHM6Ly9naXRodWIuY29tL3poYXl1amllL2JvdC1vbi1hbnl0aGluZw==\">bot-on-anything</span></li>\n</ol>\n<hr />\n<p>应用运行在 <code>腾讯云服务器</code> 上,使用的操作系统是<span class=\"red\"> OpenCloudOS</span> (兼容 CentOS 8)</p>\n</div>\n<h2 id=\"在linux服务器上启动代理\"><a class=\"anchor\" href=\"#在linux服务器上启动代理\">#</a> 在 Linux 服务器上启动代理</h2>\n<ol>\n<li>基本安装步骤参考<span class=\"exturl\" data-url=\"aHR0cHM6Ly9naXRodWIuY29tL3dhbmhlYmluL2NsYXNoLWZvci1saW51eA==\"> clash-for-linux</span> 项目的使用说明</li>\n<li>编辑 <code>.env</code> 文件时, <code>CLASH_URL</code> 的值应为自己 <code>Clash订阅链接的地址</code></li>\n</ol>\n<details class=\"info\"><summary>查看Clash日志</summary><div>\n<p><img data-src=\"https://cdn.unistart.top/blog/chrome_WGoTlQjM4F.png\" alt=\"查看Clash错误日志\" /></p>\n</div></details>\n<ol start=\"3\">\n<li>如果启动后出现 <code>unsupported rule type RULE-SET</code> 错误,可以参考如下 issue:</li>\n</ol>\n<ul>\n<li><span class=\"exturl\" data-url=\"aHR0cHM6Ly9naXRodWIuY29tL3dhbmhlYmluL2NsYXNoLWZvci1saW51eC9pc3N1ZXMvNTM=\">关于启动后 clash 日志报 unsupported rule type RULE-SET 错误,检查服务端口发现 9090 端口没有应用 </span></li>\n<li><span class=\"exturl\" data-url=\"aHR0cHM6Ly9naXRodWIuY29tL3dhbmhlYmluL2NsYXNoLWZvci1saW51eC9pc3N1ZXMvMjUjaXNzdWVjb21tZW50LTE1MDk5ODM5ODA=\">显示成功开启代理,后台查询不到</span></li>\n</ul>\n<ol start=\"4\">\n<li>进入 <code>Clash Dashboard</code> ,配置不同选择的节点</li>\n</ol>\n<details class=\"info\"><summary>进入Clash Dashboard</summary><div>\n<p><img data-src=\"https://cdn.unistart.top/blog/msedge_sdafeLHPAG.png\" alt=\"Clash Dashboard界面\" /></p>\n</div></details>\n<h2 id=\"部署bot-on-anything程序\"><a class=\"anchor\" href=\"#部署bot-on-anything程序\">#</a> 部署 bot-on-anything 程序</h2>\n<div class=\"note primary\">\n<p>注意:下面仅以个人订阅号的使用为例</p>\n</div>\n<ol>\n<li>基本安装步骤参考<span class=\"exturl\" data-url=\"aHR0cHM6Ly9naXRodWIuY29tL3poYXl1amllL2JvdC1vbi1hbnl0aGluZw==\"> bot-on-anything</span> 项目的使用说明</li>\n<li>以下是关于在 <code>个人订阅号启用服务器配置</code> 的一些说明</li>\n</ol>\n<ul>\n<li><span class=\"red\">服务器地址 (URL)</span> 格式为 <code>http://<IP>:<port>/wx</code> , 由于<span class=\"red\">服务器地址 (URL)</span> 只能配置 80/443 端口,但我们启动的 python 程序默认监听 8088 端口,因此需要通过 <code>nginx</code> 做转发</li>\n</ul>\n<details class=\"primary\"><summary>nginx配置如下</summary><div>\n<figure class=\"highlight nginx\"><figcaption data-lang=\"nginx\"><span>/etc/nginx/nginx.conf</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># For more information on configuration, see:</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\"># * Official English Documentation: http://nginx.org/en/docs/</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token comment\"># * Official Russian Documentation: http://nginx.org/ru/docs/</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token directive\"><span class=\"token keyword\">user</span> nginx</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token directive\"><span class=\"token keyword\">worker_processes</span> auto</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token directive\"><span class=\"token keyword\">error_log</span> /var/log/nginx/error.log</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token directive\"><span class=\"token keyword\">pid</span> /run/nginx.pid</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token comment\"># Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token directive\"><span class=\"token keyword\">include</span> /usr/share/nginx/modules/*.conf</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre></pre></td></tr><tr><td data-num=\"13\"></td><td><pre><span class=\"token directive\"><span class=\"token keyword\">events</span></span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">worker_connections</span> <span class=\"token number\">1024</span></span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre></pre></td></tr><tr><td data-num=\"17\"></td><td><pre><span class=\"token directive\"><span class=\"token keyword\">http</span></span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">log_format</span> main <span class=\"token string\">'<span class=\"token variable\">$remote_addr</span> - <span class=\"token variable\">$remote_user</span> [<span class=\"token variable\">$time_local]</span> \"<span class=\"token variable\">$request</span>\" '</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre> <span class=\"token string\">'<span class=\"token variable\">$status</span> <span class=\"token variable\">$body_bytes_sent</span> \"<span class=\"token variable\">$http_referer</span>\" '</span></pre></td></tr><tr><td data-num=\"20\"></td><td><pre> <span class=\"token string\">'\"<span class=\"token variable\">$http_user_agent</span>\" \"<span class=\"token variable\">$http_x_forwarded_for</span>\"'</span></span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre></pre></td></tr><tr><td data-num=\"22\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">access_log</span> /var/log/nginx/access.log main</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre></pre></td></tr><tr><td data-num=\"24\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">sendfile</span> <span class=\"token boolean\">on</span></span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"25\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">tcp_nopush</span> <span class=\"token boolean\">on</span></span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"26\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">tcp_nodelay</span> <span class=\"token boolean\">on</span></span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"27\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">keepalive_timeout</span> <span class=\"token number\">65</span></span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"28\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">types_hash_max_size</span> <span class=\"token number\">2048</span></span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"29\"></td><td><pre></pre></td></tr><tr><td data-num=\"30\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">include</span> /etc/nginx/mime.types</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"31\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">default_type</span> application/octet-stream</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"32\"></td><td><pre></pre></td></tr><tr><td data-num=\"33\"></td><td><pre> <span class=\"token comment\"># Load modular configuration files from the /etc/nginx/conf.d directory.</span></pre></td></tr><tr><td data-num=\"34\"></td><td><pre> <span class=\"token comment\"># See http://nginx.org/en/docs/ngx_core_module.html#include</span></pre></td></tr><tr><td data-num=\"35\"></td><td><pre> <span class=\"token comment\"># for more information.</span></pre></td></tr><tr class=\"marked\"><td data-num=\"36\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">include</span> /etc/nginx/conf.d/*.conf</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"37\"></td><td><pre></pre></td></tr><tr><td data-num=\"38\"></td><td><pre></pre></td></tr><tr><td data-num=\"39\"></td><td><pre><span class=\"token comment\"># Settings for a TLS enabled server.</span></pre></td></tr><tr><td data-num=\"40\"></td><td><pre><span class=\"token comment\">#</span></pre></td></tr><tr><td data-num=\"41\"></td><td><pre><span class=\"token comment\"># server {</span></pre></td></tr><tr><td data-num=\"42\"></td><td><pre><span class=\"token comment\"># listen 443 ssl http2 default_server;</span></pre></td></tr><tr><td data-num=\"43\"></td><td><pre><span class=\"token comment\"># listen [::]:443 ssl http2 default_server;</span></pre></td></tr><tr><td data-num=\"44\"></td><td><pre><span class=\"token comment\"># server_name _;</span></pre></td></tr><tr><td data-num=\"45\"></td><td><pre><span class=\"token comment\"># root /usr/share/nginx/html;</span></pre></td></tr><tr><td data-num=\"46\"></td><td><pre><span class=\"token comment\">#</span></pre></td></tr><tr><td data-num=\"47\"></td><td><pre><span class=\"token comment\"># ssl_certificate \"/etc/pki/nginx/server.crt\";</span></pre></td></tr><tr><td data-num=\"48\"></td><td><pre><span class=\"token comment\"># ssl_certificate_key \"/etc/pki/nginx/private/server.key\";</span></pre></td></tr><tr><td data-num=\"49\"></td><td><pre><span class=\"token comment\"># ssl_session_cache shared:SSL:1m;</span></pre></td></tr><tr><td data-num=\"50\"></td><td><pre><span class=\"token comment\"># ssl_session_timeout 10m;</span></pre></td></tr><tr><td data-num=\"51\"></td><td><pre><span class=\"token comment\"># ssl_ciphers PROFILE=SYSTEM;</span></pre></td></tr><tr><td data-num=\"52\"></td><td><pre><span class=\"token comment\"># ssl_prefer_server_ciphers on;</span></pre></td></tr><tr><td data-num=\"53\"></td><td><pre><span class=\"token comment\">#</span></pre></td></tr><tr><td data-num=\"54\"></td><td><pre><span class=\"token comment\"># # Load configuration files for the default server block.</span></pre></td></tr><tr><td data-num=\"55\"></td><td><pre><span class=\"token comment\"># include /etc/nginx/default.d/*.conf;</span></pre></td></tr><tr><td data-num=\"56\"></td><td><pre><span class=\"token comment\">#</span></pre></td></tr><tr><td data-num=\"57\"></td><td><pre><span class=\"token comment\"># location / {</span></pre></td></tr><tr><td data-num=\"58\"></td><td><pre><span class=\"token comment\"># }</span></pre></td></tr><tr><td data-num=\"59\"></td><td><pre><span class=\"token comment\">#</span></pre></td></tr><tr><td data-num=\"60\"></td><td><pre><span class=\"token comment\"># error_page 404 /404.html;</span></pre></td></tr><tr><td data-num=\"61\"></td><td><pre><span class=\"token comment\"># location = /40x.html {</span></pre></td></tr><tr><td data-num=\"62\"></td><td><pre><span class=\"token comment\"># }</span></pre></td></tr><tr><td data-num=\"63\"></td><td><pre><span class=\"token comment\">#</span></pre></td></tr><tr><td data-num=\"64\"></td><td><pre><span class=\"token comment\"># error_page 500 502 503 504 /50x.html;</span></pre></td></tr><tr><td data-num=\"65\"></td><td><pre><span class=\"token comment\"># location = /50x.html {</span></pre></td></tr><tr><td data-num=\"66\"></td><td><pre><span class=\"token comment\"># }</span></pre></td></tr><tr><td data-num=\"67\"></td><td><pre><span class=\"token comment\"># }</span></pre></td></tr><tr><td data-num=\"68\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><figure class=\"highlight nginx\"><figcaption data-lang=\"nginx\"><span>/etc/nginx/conf.d/bot-on-anything.conf</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token directive\"><span class=\"token keyword\">server</span></span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">listen</span> <span class=\"token number\">80</span> default_server</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">listen</span> [::]:80 default_server</span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"4\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">server_name</span> <xxx.xxx.xxx.xxx></span><span class=\"token punctuation\">;</span> <span class=\"token comment\"># 填写自己主机的 IP 地址 </span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre></pre></td></tr><tr class=\"marked\"><td data-num=\"6\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">location</span> /wx</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr class=\"marked\"><td data-num=\"7\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">proxy_pass</span> http://127.0.0.1:8088</span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"8\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">proxy_read_timeout</span> <span class=\"token number\">300</span></span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"9\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">proxy_connect_timeout</span> <span class=\"token number\">300</span></span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"10\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">proxy_redirect</span> <span class=\"token boolean\">off</span></span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"11\"></td><td><pre></pre></td></tr><tr class=\"marked\"><td data-num=\"12\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">proxy_set_header</span> X-Forwarded-Proto <span class=\"token variable\">$scheme</span></span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"13\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">proxy_set_header</span> Host <span class=\"token variable\">$http_host</span></span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"14\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">proxy_set_header</span> X-Real-IP <span class=\"token variable\">$remote_addr</span></span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"15\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre></pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">error_page</span> <span class=\"token number\">404</span> /404.html</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">location</span> = /40x.html</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"20\"></td><td><pre></pre></td></tr><tr><td data-num=\"21\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">error_page</span> <span class=\"token number\">500</span> <span class=\"token number\">502</span> <span class=\"token number\">503</span> <span class=\"token number\">504</span> /50x.html</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"22\"></td><td><pre> <span class=\"token directive\"><span class=\"token keyword\">location</span> = /50x.html</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"24\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure></div></details>\n<ul>\n<li><ins class=\"dot\"><span class=\"red\">令牌 (Token)</span> 为自己手动输入,并满足格式要求即可</ins></li>\n<li><span class=\"red\">EncodingAESKey</span> 点击随机生成即可</li>\n<li><span class=\"red\">消息加解密方式</span>处于简单的考虑选择 <code>明文模式</code> 即可,如有需要可以自行根据需求修改</li>\n</ul>\n<ol start=\"3\">\n<li>将应用程序挂到后台运行<br />\n进入项目目录执行如下命令</li>\n</ol>\n<figure class=\"highlight bash\"><figcaption data-lang=\"bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># 通过 nohup 命令将 python 应用程序挂到后台运行</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\"># 并将输出的日志重定向到当前用户目录下的 bot.log 文件中</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token comment\"># 关于 2>&1 的说明: </span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token comment\"># 将标准错误 2 重定向到标准输出 &1 ,标准输出 &1 再被重定向输入到 bot.log 文件中</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token comment\"># 0 – stdin (standard input,标准输入)</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token comment\"># 1 – stdout (standard output,标准输出)</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token comment\"># 2 – stderr (standard error,标准错误输出)</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token function\">nohup</span> python3 app.py <span class=\"token operator\">></span> ~/bot.log <span class=\"token operator\"><span class=\"token file-descriptor important\">2</span>></span><span class=\"token file-descriptor important\">&1</span> <span class=\"token operator\">&</span></pre></td></tr></table></figure>",
"tags": [
"WeChat",
"WeChat",
"ChatGPT"
]
},
{
"id": "https://unistart2.github.io/posts/a93d6153/",
"url": "https://unistart2.github.io/posts/a93d6153/",
"title": "通过Github Action跨仓库部署Hexo",
"date_published": "2023-04-06T08:01:22.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><h2 id=\"前言\"><a class=\"anchor\" href=\"#前言\">#</a> 前言</h2>\n<blockquote>\n<p>一直想给 <code>Hexo</code> 博客找个在线管理面板,能够满足在线修改和发布文章的功能,这样就可以在多台不同电脑上进行写作。<br />\n刚好在 B 站上看到有人用 <code>Qexo</code> 对 <code>Hexo</code> 博客进行管理,想试试好不好用。</p>\n</blockquote>\n<h2 id=\"前置条件\"><a class=\"anchor\" href=\"#前置条件\">#</a> 前置条件</h2>\n<ol>\n<li>完成<span class=\"exturl\" data-url=\"aHR0cHM6Ly9naXRodWIuY29tL1FleG8vUWV4bw==\"> Qexo</span> 的相关安装和部署,具体安装和部署可以看<span class=\"exturl\" data-url=\"aHR0cHM6Ly93d3cub3Bsb2cuY24vcWV4by9zdGFydC9idWlsZC5odG1s\">部署程序</span></li>\n<li>一个已经通过 Github Pages 部署好的个人博客仓库(仓库名称是:username/username.github.io 的形式)</li>\n<li>将 Hexo 博客源码整体作为一个 <code>git仓库</code> 上传到 <code>Github</code> ,例如叫做 <code>hexo-shoka</code></li>\n</ol>\n<details class=\"info\"><summary>流程图</summary><div>\n<p><img data-src=\"https://cdn.unistart.top/blog/excalidraw/qexo-deploy-process.png\" alt=\"Qexo发布博客整个流程\" /></p>\n</div></details>\n<h2 id=\"使用github-action\"><a class=\"anchor\" href=\"#使用github-action\">#</a> 使用 Github Action</h2>\n<ol>\n<li>\n<p>创建 Github Token<br />\n 在 <code>Github</code> 的用户页面左侧边栏找到 <code>Developer settings</code> ,点击后找到 <code>Personal access tokens</code> ,选择两种 <code>Token</code> 类型的其中一种进行创建,要求创建的 <code>Token</code> 至少对 <code>相关仓库</code> 具有 <code>读写权限</code> 。(<ins class=\"dot\">之后的工作流中要用到,请确保权限足够</ins>)</p>\n<details class=\"info\"><summary>创建示例</summary><div>\n<p>例如:我选择创建 <code>Personal access tokens (classic)</code> 类型的 <code>Token</code> ,点击 <code>Gernerate new token</code> 按钮,选择 <code>Generate new token (classic)</code> 选项。进入页面后, <code>Note</code> 选项框中填入待创建 <code>Token</code> 的名称,比如 <code>HEXO_AUTO_REDEPLOY</code> 。 <code>Expiration</code> 用于设定待创建 <code>Token</code> 的过期时间,比如选择 <code>No Expiration</code> (一般不推荐这样)。 <code>Scope</code> 用于设置 <code>Token</code> 的相关权限,比如勾选 <code>repo</code> 和 <code>workflow</code> 。最后在底部点击 <code>Gernerate token</code> 按钮完成当前 <code>Token</code> 的创建。</p>\n<p><img data-src=\"https://cdn.unistart.top/blog/qexo/msedge_ap12qlk6se.png\" alt=\"创建好的HEXO_AUTO_REDEPLOY\" /></p>\n</div></details>\n</li>\n<li>\n<p>创建 Repository secret<br />\n 在 <code>Hexo博客源码仓库</code> 点击 <code>Settings</code> ,在左侧栏找到 <code>Secrets and variables</code> ,选择 <code>Actions</code> ,进入 <code>Actions secrets and variables</code> 页面,点击 <code>New repository secret</code> 创建一个仓库 secret,名称随意,例如 <code>TARGET_REPO_TOKEN</code> ,值为 <code>刚才生成的Token</code> 。同样的方式再创建一个仓库 secret,名称随意,例如 <code>TARGET_REPO_NAME</code> , 值以 <code>username/username.github.io</code> 形式填入自己的 Hexo 博客部署仓库的名称</p>\n</li>\n<li>\n<p>创建工作流文件<br />\n在存储 Hexo 博客源码的仓库 ( <code>hexo-shoka</code> ) 中创建 <code>.github/workflows/</code> 文件夹路径,并在其下面创建一个 <code>yaml</code> 格式的文件,名称随意,例如叫 <code>pages.yml</code></p>\n</li>\n<li>\n<p>编写工作流步骤<br />\n在 <code>pages.yml</code> 文件中写入如下内容:</p>\n</li>\n</ol>\n<figure class=\"highlight yml\"><figcaption data-lang=\"YAML\"><span>.github/workflows/page.yml</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Deploy Hexo Blog</pre></td></tr><tr><td data-num=\"2\"></td><td><pre></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token comment\"># Github Action 触发条件</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token key atrule\">on</span><span class=\"token punctuation\">:</span> </pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token key atrule\">push</span><span class=\"token punctuation\">:</span> </pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token key atrule\">branches</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token punctuation\">-</span> main <span class=\"token comment\"># 这里设置的是当前仓库的 main 分支发生 push 时触发</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token comment\"># 作业设置</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token key atrule\">jobs</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> <span class=\"token key atrule\">deploy</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token key atrule\">runs-on</span><span class=\"token punctuation\">:</span> ubuntu<span class=\"token punctuation\">-</span>latest <span class=\"token comment\"># 设置运行的宿主机系统</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre> <span class=\"token key atrule\">steps</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token comment\"># 检出当前仓库(存放 Hexo 博客源码的仓库)</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Checkout source code</pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token key atrule\">uses</span><span class=\"token punctuation\">:</span> actions/checkout@v2</pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token key atrule\">with</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre> <span class=\"token comment\"># 如果你的主题文件夹使用的是 git submodule 进行管理的话这个操作是必须的</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre> <span class=\"token comment\"># 不然检出当前仓库后,themes/your_theme/ 文件下内容是空的</span></pre></td></tr><tr class=\"marked\"><td data-num=\"20\"></td><td><pre> <span class=\"token key atrule\">submodules</span><span class=\"token punctuation\">:</span> <span class=\"token string\">'recursive'</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre></pre></td></tr><tr><td data-num=\"22\"></td><td><pre> <span class=\"token comment\"># git config 设置</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre> <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Configure Git</pre></td></tr><tr><td data-num=\"24\"></td><td><pre> <span class=\"token key atrule\">env</span><span class=\"token punctuation\">:</span> <span class=\"token comment\"># 如果不想通过环境变量来设置,也可以抽取成 Repository secret</span></pre></td></tr><tr class=\"marked\"><td data-num=\"25\"></td><td><pre> <span class=\"token key atrule\">USERNAME</span><span class=\"token punctuation\">:</span> unistart <span class=\"token comment\"># 改成自己的</span></pre></td></tr><tr class=\"marked\"><td data-num=\"26\"></td><td><pre> <span class=\"token key atrule\">USER_EMAIL</span><span class=\"token punctuation\">:</span> [email protected] <span class=\"token comment\"># 改成自己的</span></pre></td></tr><tr><td data-num=\"27\"></td><td><pre> <span class=\"token key atrule\">run</span><span class=\"token punctuation\">:</span> <span class=\"token punctuation\">|</span><span class=\"token scalar string\"></pre></td></tr><tr><td data-num=\"28\"></td><td><pre> git config --global user.email $USER_EMAIL</pre></td></tr><tr><td data-num=\"29\"></td><td><pre> git config --global user.name $USERNAME</span></pre></td></tr><tr><td data-num=\"30\"></td><td><pre></pre></td></tr><tr><td data-num=\"31\"></td><td><pre> <span class=\"token comment\"># 配置 Node.js 环境</span></pre></td></tr><tr><td data-num=\"32\"></td><td><pre> <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Setup Node.js environment</pre></td></tr><tr><td data-num=\"33\"></td><td><pre> <span class=\"token key atrule\">uses</span><span class=\"token punctuation\">:</span> actions/setup<span class=\"token punctuation\">-</span>node@v2</pre></td></tr><tr><td data-num=\"34\"></td><td><pre> <span class=\"token key atrule\">with</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"35\"></td><td><pre> <span class=\"token key atrule\">node-version</span><span class=\"token punctuation\">:</span> <span class=\"token string\">'18.x'</span> <span class=\"token comment\"># 可以根据自己的需要修改 Node.js 版本</span></pre></td></tr><tr><td data-num=\"36\"></td><td><pre></pre></td></tr><tr><td data-num=\"37\"></td><td><pre> <span class=\"token comment\"># 安装 npm 依赖</span></pre></td></tr><tr><td data-num=\"38\"></td><td><pre> <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Install dependencies</pre></td></tr><tr><td data-num=\"39\"></td><td><pre> <span class=\"token key atrule\">run</span><span class=\"token punctuation\">:</span> <span class=\"token punctuation\">|</span><span class=\"token scalar string\"></pre></td></tr><tr><td data-num=\"40\"></td><td><pre> npm install hexo-cli -g</pre></td></tr><tr><td data-num=\"41\"></td><td><pre> npm install</span></pre></td></tr><tr><td data-num=\"42\"></td><td><pre></pre></td></tr><tr><td data-num=\"43\"></td><td><pre> <span class=\"token comment\"># 执行 hexo 命令并推送新生成的内容到指定的目标仓库(存放 Hexo 博客静态页面的仓库)</span></pre></td></tr><tr><td data-num=\"44\"></td><td><pre> <span class=\"token comment\"># 下面使用 git 进行远程推送时,可以根据自己的需要修改推送分支的名称</span></pre></td></tr><tr><td data-num=\"45\"></td><td><pre> <span class=\"token comment\"># 下面的操作是进行 CURRENT_REPO_NAME:main -> TARGET_REPO_NAME:main</span></pre></td></tr><tr><td data-num=\"46\"></td><td><pre> <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Build and deploy</pre></td></tr><tr><td data-num=\"47\"></td><td><pre> <span class=\"token key atrule\">env</span><span class=\"token punctuation\">:</span> <span class=\"token comment\"># 注意去掉插值表达式中多余的 '\\' 符号,因为 markdown 渲染的关系目前只能这样写了</span></pre></td></tr><tr class=\"marked\"><td data-num=\"48\"></td><td><pre> <span class=\"token key atrule\">TARGET_REPO_TOKEN</span><span class=\"token punctuation\">:</span> $\\<span class=\"token punctuation\">{</span>\\<span class=\"token punctuation\">{</span> secrets.TARGET_REPO_TOKEN \\<span class=\"token punctuation\">}</span>\\<span class=\"token punctuation\">}</span></pre></td></tr><tr class=\"marked\"><td data-num=\"49\"></td><td><pre> <span class=\"token key atrule\">TARGET_REPO_NAME</span><span class=\"token punctuation\">:</span> $\\<span class=\"token punctuation\">{</span>\\<span class=\"token punctuation\">{</span> secrets.TARGET_REPO_NAME \\<span class=\"token punctuation\">}</span>\\<span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"50\"></td><td><pre> <span class=\"token key atrule\">run</span><span class=\"token punctuation\">:</span> <span class=\"token punctuation\">|</span><span class=\"token scalar string\"></pre></td></tr><tr><td data-num=\"51\"></td><td><pre> hexo clean</pre></td></tr><tr><td data-num=\"52\"></td><td><pre> hexo generate</pre></td></tr><tr><td data-num=\"53\"></td><td><pre> cd public</pre></td></tr><tr><td data-num=\"54\"></td><td><pre> git init</pre></td></tr><tr><td data-num=\"55\"></td><td><pre> git remote add origin \"https://[email protected]/$TARGET_REPO_NAME\"</pre></td></tr><tr><td data-num=\"56\"></td><td><pre> git checkout -b main</pre></td></tr><tr><td data-num=\"57\"></td><td><pre> git add .</pre></td></tr><tr><td data-num=\"58\"></td><td><pre> git commit -m \"Redeploy Hexo Blog\"</pre></td></tr><tr><td data-num=\"59\"></td><td><pre> git push origin main --force</span></pre></td></tr></table></figure><ol start=\"5\">\n<li>在当前仓库( <code>hexo-shoka</code> )提交编写好的工作流,等待 Github Action 完成</li>\n</ol>\n",
"tags": [
"Github",
"Github",
"Github Action"
]
},
{
"id": "https://unistart2.github.io/posts/84819354/",
"url": "https://unistart2.github.io/posts/84819354/",
"title": "Rust学习-归并排序",
"date_published": "2023-04-04T09:50:01.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><div class=\"note info\">\n<p><span class=\"label primary\">分而治之(Divide and Conquer)</span>:将一个复杂的问题,分解成若干个规模相同或类似的子问题,然后再对这些子问题再进一步细分,直到最后的子问题变得很简单,很容易就能被求解出来,这样这个复杂的问题就求解出来了。</p>\n<p>归并排序中就运用到了<ins class=\"dot red\">分而治之</ins>的思想。</p>\n</div>\n<div class=\"note primary\">\n<p><span class=\"label\">实现步骤</span></p>\n<ol>\n<li>对数组进行拆分,后面具体实现中是对数组进行左右两等分(二路归并)</li>\n<li>分别对拆分后的左右数组进行排序</li>\n<li>当待排序数组只有一个元素时直接返回该数组</li>\n<li>将左右排序好的数组合并成一个数组,并返回</li>\n</ol>\n</div>\n<h2 id=\"归并排序并返回新数组\"><a class=\"anchor\" href=\"#归并排序并返回新数组\">#</a> 归并排序并返回新数组</h2>\n<figure class=\"highlight rust\"><figcaption data-lang=\"rust\"><span>归并排序</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">pub</span> <span class=\"token keyword\">mod</span> <span class=\"token module-declaration namespace\">merge_not_inplace</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">use</span> <span class=\"token namespace\">std<span class=\"token punctuation\">::</span>fmt<span class=\"token punctuation\">::</span></span><span class=\"token class-name\">Debug</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">pub</span> <span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">merge_sort</span><span class=\"token operator\"><</span><span class=\"token class-name\">T</span><span class=\"token operator\">></span><span class=\"token punctuation\">(</span>origin_arr<span class=\"token punctuation\">:</span> <span class=\"token class-name\">Vec</span><span class=\"token operator\"><</span><span class=\"token class-name\">T</span><span class=\"token operator\">></span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">-></span> <span class=\"token class-name\">Vec</span><span class=\"token operator\"><</span><span class=\"token class-name\">T</span><span class=\"token operator\">></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">where</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token class-name\">T</span><span class=\"token punctuation\">:</span> <span class=\"token class-name\">Debug</span> <span class=\"token operator\">+</span> <span class=\"token class-name\">Eq</span> <span class=\"token operator\">+</span> <span class=\"token class-name\">Ord</span> <span class=\"token operator\">+</span> <span class=\"token class-name\">Clone</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token keyword\">if</span> origin_arr<span class=\"token punctuation\">.</span><span class=\"token function\">is_empty</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token macro property\">panic!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"This array is empty!\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token comment\">// 如果分解到只剩一个数,返回该数</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre> <span class=\"token keyword\">if</span> origin_arr<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">==</span> <span class=\"token number\">1</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token keyword\">return</span> origin_arr<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre></pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token comment\">// 将待排序数组分解成两半</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre> <span class=\"token keyword\">let</span> mid <span class=\"token operator\">=</span> origin_arr<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">/</span> <span class=\"token number\">2</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre> <span class=\"token keyword\">let</span> <span class=\"token keyword\">mut</span> left_arr <span class=\"token operator\">=</span> origin_arr<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">..</span>mid<span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">to_vec</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"20\"></td><td><pre> <span class=\"token keyword\">let</span> <span class=\"token keyword\">mut</span> right_arr <span class=\"token operator\">=</span> origin_arr<span class=\"token punctuation\">[</span>mid<span class=\"token punctuation\">..</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">to_vec</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre></pre></td></tr><tr><td data-num=\"22\"></td><td><pre> <span class=\"token comment\">// 嵌套调用,对两半分别进行排序</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre> left_arr <span class=\"token operator\">=</span> <span class=\"token function\">merge_sort</span><span class=\"token punctuation\">(</span>left_arr<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"24\"></td><td><pre> right_arr <span class=\"token operator\">=</span> <span class=\"token function\">merge_sort</span><span class=\"token punctuation\">(</span>right_arr<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"25\"></td><td><pre></pre></td></tr><tr><td data-num=\"26\"></td><td><pre> <span class=\"token comment\">// 合并排序后的两半</span></pre></td></tr><tr><td data-num=\"27\"></td><td><pre> <span class=\"token keyword\">let</span> merged <span class=\"token operator\">=</span> <span class=\"token function\">merge</span><span class=\"token punctuation\">(</span>left_arr<span class=\"token punctuation\">,</span> right_arr<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"28\"></td><td><pre></pre></td></tr><tr><td data-num=\"29\"></td><td><pre> <span class=\"token keyword\">return</span> merged<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"30\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"31\"></td><td><pre></pre></td></tr><tr><td data-num=\"32\"></td><td><pre> <span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">merge</span><span class=\"token operator\"><</span><span class=\"token class-name\">T</span><span class=\"token operator\">></span><span class=\"token punctuation\">(</span>left_arr<span class=\"token punctuation\">:</span> <span class=\"token class-name\">Vec</span><span class=\"token operator\"><</span><span class=\"token class-name\">T</span><span class=\"token operator\">></span><span class=\"token punctuation\">,</span> right_arr<span class=\"token punctuation\">:</span> <span class=\"token class-name\">Vec</span><span class=\"token operator\"><</span><span class=\"token class-name\">T</span><span class=\"token operator\">></span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">-></span> <span class=\"token class-name\">Vec</span><span class=\"token operator\"><</span><span class=\"token class-name\">T</span><span class=\"token operator\">></span></pre></td></tr><tr><td data-num=\"33\"></td><td><pre> <span class=\"token keyword\">where</span></pre></td></tr><tr><td data-num=\"34\"></td><td><pre> <span class=\"token class-name\">T</span><span class=\"token punctuation\">:</span> <span class=\"token class-name\">Debug</span> <span class=\"token operator\">+</span> <span class=\"token class-name\">Eq</span> <span class=\"token operator\">+</span> <span class=\"token class-name\">Ord</span> <span class=\"token operator\">+</span> <span class=\"token class-name\">Clone</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"35\"></td><td><pre> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"36\"></td><td><pre> <span class=\"token keyword\">if</span> left_arr<span class=\"token punctuation\">.</span><span class=\"token function\">is_empty</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"37\"></td><td><pre> <span class=\"token macro property\">panic!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"left array is empty!\"</span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"38\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"39\"></td><td><pre> <span class=\"token keyword\">if</span> right_arr<span class=\"token punctuation\">.</span><span class=\"token function\">is_empty</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"40\"></td><td><pre> <span class=\"token macro property\">panic!</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"right array is empty!\"</span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"41\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"42\"></td><td><pre></pre></td></tr><tr><td data-num=\"43\"></td><td><pre> <span class=\"token keyword\">let</span> <span class=\"token keyword\">mut</span> li <span class=\"token operator\">=</span> <span class=\"token number\">0usize</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"44\"></td><td><pre> <span class=\"token keyword\">let</span> <span class=\"token keyword\">mut</span> ri <span class=\"token operator\">=</span> <span class=\"token number\">0usize</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"45\"></td><td><pre> <span class=\"token keyword\">let</span> <span class=\"token keyword\">mut</span> result <span class=\"token operator\">=</span> <span class=\"token class-name\">Vec</span><span class=\"token punctuation\">::</span><span class=\"token operator\"><</span><span class=\"token class-name\">T</span><span class=\"token operator\">></span><span class=\"token punctuation\">::</span><span class=\"token function\">with_capacity</span><span class=\"token punctuation\">(</span>left_arr<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">+</span> right_arr<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"46\"></td><td><pre></pre></td></tr><tr><td data-num=\"47\"></td><td><pre> <span class=\"token comment\">// 轮流从两个数组中取出较小的值,放入合并后的数组中</span></pre></td></tr><tr><td data-num=\"48\"></td><td><pre> <span class=\"token keyword\">while</span> li <span class=\"token operator\"><</span> left_arr<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">&&</span> ri <span class=\"token operator\"><</span> right_arr<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"49\"></td><td><pre> <span class=\"token keyword\">if</span> left_arr<span class=\"token punctuation\">[</span>li<span class=\"token punctuation\">]</span> <span class=\"token operator\"><=</span> right_arr<span class=\"token punctuation\">[</span>ri<span class=\"token punctuation\">]</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"50\"></td><td><pre> result<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span>left_arr<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span>li<span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">unwrap</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">clone</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"51\"></td><td><pre> li <span class=\"token operator\">+=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"52\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"53\"></td><td><pre> result<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span>right_arr<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span>ri<span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">unwrap</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">clone</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"54\"></td><td><pre> ri <span class=\"token operator\">+=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"55\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"56\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"57\"></td><td><pre></pre></td></tr><tr><td data-num=\"58\"></td><td><pre> <span class=\"token comment\">// 如果两个数组有一个先结束了,就将另一个数组的元素直接依次放入进来</span></pre></td></tr><tr><td data-num=\"59\"></td><td><pre> <span class=\"token keyword\">if</span> li <span class=\"token operator\"><</span> left_arr<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"60\"></td><td><pre> <span class=\"token keyword\">for</span> i <span class=\"token keyword\">in</span> li<span class=\"token punctuation\">..</span>left_arr<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"61\"></td><td><pre> result<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span>left_arr<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span>i<span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">unwrap</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">clone</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"62\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"63\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"64\"></td><td><pre> <span class=\"token keyword\">for</span> i <span class=\"token keyword\">in</span> ri<span class=\"token punctuation\">..</span>right_arr<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"65\"></td><td><pre> result<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span>right_arr<span class=\"token punctuation\">.</span><span class=\"token function\">get</span><span class=\"token punctuation\">(</span>i<span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">unwrap</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">clone</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"66\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"67\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"68\"></td><td><pre></pre></td></tr><tr><td data-num=\"69\"></td><td><pre> result</pre></td></tr><tr><td data-num=\"70\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"71\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><h2 id=\"在原数组上进行归并排序\"><a class=\"anchor\" href=\"#在原数组上进行归并排序\">#</a> 在原数组上进行归并排序</h2>\n<figure class=\"highlight rust\"><figcaption data-lang=\"rust\"><span>归并排序</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">pub</span> <span class=\"token keyword\">mod</span> <span class=\"token module-declaration namespace\">merge_inplace</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">pub</span> <span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">merge_sort</span><span class=\"token operator\"><</span><span class=\"token class-name\">T</span><span class=\"token punctuation\">:</span> <span class=\"token class-name\">Clone</span> <span class=\"token operator\">+</span> <span class=\"token class-name\">Eq</span> <span class=\"token operator\">+</span> <span class=\"token class-name\">Ord</span><span class=\"token operator\">></span><span class=\"token punctuation\">(</span>arr<span class=\"token punctuation\">:</span> <span class=\"token operator\">&</span><span class=\"token keyword\">mut</span> <span class=\"token punctuation\">[</span><span class=\"token class-name\">T</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">let</span> len <span class=\"token operator\">=</span> arr<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">if</span> len <span class=\"token operator\"><</span> <span class=\"token number\">2</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token keyword\">let</span> mid <span class=\"token operator\">=</span> len <span class=\"token operator\">/</span> <span class=\"token number\">2</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token function\">merge_sort</span><span class=\"token punctuation\">(</span><span class=\"token operator\">&</span><span class=\"token keyword\">mut</span> arr<span class=\"token punctuation\">[</span><span class=\"token punctuation\">..</span>mid<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token function\">merge_sort</span><span class=\"token punctuation\">(</span><span class=\"token operator\">&</span><span class=\"token keyword\">mut</span> arr<span class=\"token punctuation\">[</span>mid<span class=\"token punctuation\">..</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token function\">merge</span><span class=\"token punctuation\">(</span>arr<span class=\"token punctuation\">,</span> mid<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre></pre></td></tr><tr><td data-num=\"13\"></td><td><pre> <span class=\"token keyword\">fn</span> <span class=\"token function-definition function\">merge</span><span class=\"token operator\"><</span><span class=\"token class-name\">T</span><span class=\"token punctuation\">:</span> <span class=\"token class-name\">Clone</span> <span class=\"token operator\">+</span> <span class=\"token class-name\">Eq</span> <span class=\"token operator\">+</span> <span class=\"token class-name\">Ord</span><span class=\"token operator\">></span><span class=\"token punctuation\">(</span>arr<span class=\"token punctuation\">:</span> <span class=\"token operator\">&</span><span class=\"token keyword\">mut</span> <span class=\"token punctuation\">[</span><span class=\"token class-name\">T</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> mid<span class=\"token punctuation\">:</span> <span class=\"token keyword\">usize</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token keyword\">let</span> <span class=\"token keyword\">mut</span> left <span class=\"token operator\">=</span> arr<span class=\"token punctuation\">[</span><span class=\"token punctuation\">..</span>mid<span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">to_vec</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token keyword\">let</span> <span class=\"token keyword\">mut</span> right <span class=\"token operator\">=</span> arr<span class=\"token punctuation\">[</span>mid<span class=\"token punctuation\">..</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">to_vec</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token keyword\">let</span> <span class=\"token keyword\">mut</span> i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token keyword\">let</span> <span class=\"token keyword\">mut</span> j <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre> <span class=\"token keyword\">let</span> <span class=\"token keyword\">mut</span> k <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre> <span class=\"token keyword\">while</span> i <span class=\"token operator\"><</span> left<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">&&</span> j <span class=\"token operator\"><</span> right<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"20\"></td><td><pre> <span class=\"token keyword\">if</span> left<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\"><=</span> right<span class=\"token punctuation\">[</span>j<span class=\"token punctuation\">]</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre> arr<span class=\"token punctuation\">[</span>k<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> left<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">clone</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"22\"></td><td><pre> i <span class=\"token operator\">+=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"24\"></td><td><pre> arr<span class=\"token punctuation\">[</span>k<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> right<span class=\"token punctuation\">[</span>j<span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">clone</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"25\"></td><td><pre> j <span class=\"token operator\">+=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"26\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"27\"></td><td><pre> k <span class=\"token operator\">+=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"28\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"29\"></td><td><pre> <span class=\"token keyword\">while</span> i <span class=\"token operator\"><</span> left<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"30\"></td><td><pre> arr<span class=\"token punctuation\">[</span>k<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> left<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">clone</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"31\"></td><td><pre> i <span class=\"token operator\">+=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"32\"></td><td><pre> k <span class=\"token operator\">+=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"33\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"34\"></td><td><pre> <span class=\"token keyword\">while</span> j <span class=\"token operator\"><</span> right<span class=\"token punctuation\">.</span><span class=\"token function\">len</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"35\"></td><td><pre> arr<span class=\"token punctuation\">[</span>k<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> right<span class=\"token punctuation\">[</span>j<span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">clone</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"36\"></td><td><pre> j <span class=\"token operator\">+=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"37\"></td><td><pre> k <span class=\"token operator\">+=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"38\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"39\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"40\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><div class=\"note info\">\n<p>Rust 中的数组包括定长数组和可变数组,前者的类型为 <code>[T; Sized]</code> ,后者类型为 <code>Vec<T></code> ,注意从数组中获取值时,通常获得的是值的一个引用(或者说是一个借用),这是由于 Rust 所有权机制导致的。如果想要在合并数组时,取得待合并元素本身而不是其引用,就需要泛型 T 实现 <code>Clone Trait</code> 约束,这样才可以通过 <code>clone</code> 方法来获取到元素的值拷贝。</p>\n</div>\n",
"tags": [
"算法",
"Rust",
"算法"
]
},
{
"id": "https://unistart2.github.io/posts/cd788354/",
"url": "https://unistart2.github.io/posts/cd788354/",
"title": "WebGL学习(附录)",
"date_published": "2023-03-15T02:40:04.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><p><span class=\"label info\">参考书籍中使用到的辅助库代码</span></p>\n<figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"><span>cuon-matrix.js</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\">// cuon-matrix.js (c) 2012 kanda and matsuda</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token doc-comment comment\">/** </pre></td></tr><tr><td data-num=\"3\"></td><td><pre> * This is a class treating 4x4 matrix.</pre></td></tr><tr><td data-num=\"4\"></td><td><pre> * This class contains the function that is equivalent to OpenGL matrix stack.</pre></td></tr><tr><td data-num=\"5\"></td><td><pre> * The matrix after conversion is calculated by multiplying a conversion matrix from the right.</pre></td></tr><tr><td data-num=\"6\"></td><td><pre> * The matrix is replaced by the calculated result.</pre></td></tr><tr><td data-num=\"7\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"10\"></td><td><pre> * Constructor of Matrix4</pre></td></tr><tr><td data-num=\"11\"></td><td><pre> * If opt_src is specified, new matrix is initialized by opt_src.</pre></td></tr><tr><td data-num=\"12\"></td><td><pre> * Otherwise, new matrix is initialized by identity matrix.</pre></td></tr><tr><td data-num=\"13\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">opt_src</span> source matrix(option)</pre></td></tr><tr><td data-num=\"14\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token function-variable function\"><span class=\"token maybe-class-name\">Matrix4</span></span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">opt_src</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token keyword\">var</span> i<span class=\"token punctuation\">,</span> s<span class=\"token punctuation\">,</span> d<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>opt_src <span class=\"token operator\">&&</span> <span class=\"token keyword\">typeof</span> opt_src <span class=\"token operator\">===</span> <span class=\"token string\">'object'</span> <span class=\"token operator\">&&</span> opt_src<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">hasOwnProperty</span><span class=\"token punctuation\">(</span><span class=\"token string\">'elements'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre> s <span class=\"token operator\">=</span> opt_src<span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre> d <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Float32Array</span><span class=\"token punctuation\">(</span><span class=\"token number\">16</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"20\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span>i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\"><</span> <span class=\"token number\">16</span><span class=\"token punctuation\">;</span> <span class=\"token operator\">++</span>i<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre> d<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> s<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"22\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span> <span class=\"token operator\">=</span> d<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"24\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"25\"></td><td><pre> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Float32Array</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"26\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"27\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"28\"></td><td><pre></pre></td></tr><tr><td data-num=\"29\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"30\"></td><td><pre> * Set the identity matrix.</pre></td></tr><tr><td data-num=\"31\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"32\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"33\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">setIdentity</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"34\"></td><td><pre> <span class=\"token keyword\">var</span> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"35\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"36\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"37\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"38\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"39\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"40\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"41\"></td><td><pre></pre></td></tr><tr><td data-num=\"42\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"43\"></td><td><pre> * Copy matrix.</pre></td></tr><tr><td data-num=\"44\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">src</span> source matrix</pre></td></tr><tr><td data-num=\"45\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"46\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"47\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">set</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">src</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"48\"></td><td><pre> <span class=\"token keyword\">var</span> i<span class=\"token punctuation\">,</span> s<span class=\"token punctuation\">,</span> d<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"49\"></td><td><pre></pre></td></tr><tr><td data-num=\"50\"></td><td><pre> s <span class=\"token operator\">=</span> src<span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"51\"></td><td><pre> d <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"52\"></td><td><pre></pre></td></tr><tr><td data-num=\"53\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>s <span class=\"token operator\">===</span> d<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"54\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"55\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"56\"></td><td><pre> </pre></td></tr><tr><td data-num=\"57\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span>i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\"><</span> <span class=\"token number\">16</span><span class=\"token punctuation\">;</span> <span class=\"token operator\">++</span>i<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"58\"></td><td><pre> d<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> s<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"59\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"60\"></td><td><pre></pre></td></tr><tr><td data-num=\"61\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"62\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"63\"></td><td><pre></pre></td></tr><tr><td data-num=\"64\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"65\"></td><td><pre> * Multiply the matrix from the right.</pre></td></tr><tr><td data-num=\"66\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">other</span> The multiply matrix</pre></td></tr><tr><td data-num=\"67\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"68\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"69\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">concat</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">other</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"70\"></td><td><pre> <span class=\"token keyword\">var</span> i<span class=\"token punctuation\">,</span> e<span class=\"token punctuation\">,</span> a<span class=\"token punctuation\">,</span> b<span class=\"token punctuation\">,</span> ai0<span class=\"token punctuation\">,</span> ai1<span class=\"token punctuation\">,</span> ai2<span class=\"token punctuation\">,</span> ai3<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"71\"></td><td><pre> </pre></td></tr><tr><td data-num=\"72\"></td><td><pre> <span class=\"token comment\">// Calculate e = a * b</span></pre></td></tr><tr><td data-num=\"73\"></td><td><pre> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"74\"></td><td><pre> a <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"75\"></td><td><pre> b <span class=\"token operator\">=</span> other<span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"76\"></td><td><pre> </pre></td></tr><tr><td data-num=\"77\"></td><td><pre> <span class=\"token comment\">// If e equals b, copy b to temporary matrix.</span></pre></td></tr><tr><td data-num=\"78\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>e <span class=\"token operator\">===</span> b<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"79\"></td><td><pre> b <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Float32Array</span><span class=\"token punctuation\">(</span><span class=\"token number\">16</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"80\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span>i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\"><</span> <span class=\"token number\">16</span><span class=\"token punctuation\">;</span> <span class=\"token operator\">++</span>i<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"81\"></td><td><pre> b<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> e<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"82\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"83\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"84\"></td><td><pre> </pre></td></tr><tr><td data-num=\"85\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span>i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\"><</span> <span class=\"token number\">4</span><span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"86\"></td><td><pre> ai0<span class=\"token operator\">=</span>a<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> ai1<span class=\"token operator\">=</span>a<span class=\"token punctuation\">[</span>i<span class=\"token operator\">+</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> ai2<span class=\"token operator\">=</span>a<span class=\"token punctuation\">[</span>i<span class=\"token operator\">+</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> ai3<span class=\"token operator\">=</span>a<span class=\"token punctuation\">[</span>i<span class=\"token operator\">+</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"87\"></td><td><pre> e<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> ai0 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> ai1 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> ai2 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> ai3 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"88\"></td><td><pre> e<span class=\"token punctuation\">[</span>i<span class=\"token operator\">+</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> ai0 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> ai1 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> ai2 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> ai3 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"89\"></td><td><pre> e<span class=\"token punctuation\">[</span>i<span class=\"token operator\">+</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> ai0 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> ai1 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> ai2 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> ai3 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"90\"></td><td><pre> e<span class=\"token punctuation\">[</span>i<span class=\"token operator\">+</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> ai0 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> ai1 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> ai2 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> ai3 <span class=\"token operator\">*</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"91\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"92\"></td><td><pre> </pre></td></tr><tr><td data-num=\"93\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"94\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"95\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">multiply</span> <span class=\"token operator\">=</span> <span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">concat</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"96\"></td><td><pre></pre></td></tr><tr><td data-num=\"97\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"98\"></td><td><pre> * Multiply the three-dimensional vector.</pre></td></tr><tr><td data-num=\"99\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">pos</span> The multiply vector</pre></td></tr><tr><td data-num=\"100\"></td><td><pre> * <span class=\"token keyword\">@return</span> The result of multiplication(Float32Array)</pre></td></tr><tr><td data-num=\"101\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"102\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">multiplyVector3</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">pos</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"103\"></td><td><pre> <span class=\"token keyword\">var</span> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"104\"></td><td><pre> <span class=\"token keyword\">var</span> p <span class=\"token operator\">=</span> pos<span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"105\"></td><td><pre> <span class=\"token keyword\">var</span> v <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Vector3</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"106\"></td><td><pre> <span class=\"token keyword\">var</span> result <span class=\"token operator\">=</span> v<span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"107\"></td><td><pre></pre></td></tr><tr><td data-num=\"108\"></td><td><pre> result<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"109\"></td><td><pre> result<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"110\"></td><td><pre> result<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"111\"></td><td><pre></pre></td></tr><tr><td data-num=\"112\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> v<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"113\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"114\"></td><td><pre></pre></td></tr><tr><td data-num=\"115\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"116\"></td><td><pre> * Multiply the four-dimensional vector.</pre></td></tr><tr><td data-num=\"117\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">pos</span> The multiply vector</pre></td></tr><tr><td data-num=\"118\"></td><td><pre> * <span class=\"token keyword\">@return</span> The result of multiplication(Float32Array)</pre></td></tr><tr><td data-num=\"119\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"120\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">multiplyVector4</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">pos</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"121\"></td><td><pre> <span class=\"token keyword\">var</span> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"122\"></td><td><pre> <span class=\"token keyword\">var</span> p <span class=\"token operator\">=</span> pos<span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"123\"></td><td><pre> <span class=\"token keyword\">var</span> v <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Vector4</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"124\"></td><td><pre> <span class=\"token keyword\">var</span> result <span class=\"token operator\">=</span> v<span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"125\"></td><td><pre></pre></td></tr><tr><td data-num=\"126\"></td><td><pre> result<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"127\"></td><td><pre> result<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"128\"></td><td><pre> result<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"129\"></td><td><pre> result<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> p<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"130\"></td><td><pre></pre></td></tr><tr><td data-num=\"131\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> v<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"132\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"133\"></td><td><pre></pre></td></tr><tr><td data-num=\"134\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"135\"></td><td><pre> * Transpose the matrix.</pre></td></tr><tr><td data-num=\"136\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"137\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"138\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">transpose</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"139\"></td><td><pre> <span class=\"token keyword\">var</span> e<span class=\"token punctuation\">,</span> t<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"140\"></td><td><pre></pre></td></tr><tr><td data-num=\"141\"></td><td><pre> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"142\"></td><td><pre></pre></td></tr><tr><td data-num=\"143\"></td><td><pre> t <span class=\"token operator\">=</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> t<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"144\"></td><td><pre> t <span class=\"token operator\">=</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> t<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"145\"></td><td><pre> t <span class=\"token operator\">=</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> t<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"146\"></td><td><pre> t <span class=\"token operator\">=</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">9</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> t<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"147\"></td><td><pre> t <span class=\"token operator\">=</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> t<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"148\"></td><td><pre> t <span class=\"token operator\">=</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> t<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"149\"></td><td><pre></pre></td></tr><tr><td data-num=\"150\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"151\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"152\"></td><td><pre></pre></td></tr><tr><td data-num=\"153\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"154\"></td><td><pre> * Calculate the inverse matrix of specified matrix, and set to this.</pre></td></tr><tr><td data-num=\"155\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">other</span> The source matrix</pre></td></tr><tr><td data-num=\"156\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"157\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"158\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">setInverseOf</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">other</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"159\"></td><td><pre> <span class=\"token keyword\">var</span> i<span class=\"token punctuation\">,</span> s<span class=\"token punctuation\">,</span> d<span class=\"token punctuation\">,</span> inv<span class=\"token punctuation\">,</span> det<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"160\"></td><td><pre></pre></td></tr><tr><td data-num=\"161\"></td><td><pre> s <span class=\"token operator\">=</span> other<span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"162\"></td><td><pre> d <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"163\"></td><td><pre> inv <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Float32Array</span><span class=\"token punctuation\">(</span><span class=\"token number\">16</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"164\"></td><td><pre></pre></td></tr><tr><td data-num=\"165\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"166\"></td><td><pre> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"167\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"168\"></td><td><pre> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"169\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"170\"></td><td><pre> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"171\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"172\"></td><td><pre> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"173\"></td><td><pre></pre></td></tr><tr><td data-num=\"174\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"175\"></td><td><pre> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"176\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"177\"></td><td><pre> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"178\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"179\"></td><td><pre> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"180\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"181\"></td><td><pre> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"182\"></td><td><pre></pre></td></tr><tr><td data-num=\"183\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"184\"></td><td><pre> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"185\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"186\"></td><td><pre> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"187\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"188\"></td><td><pre> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"189\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"190\"></td><td><pre> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"191\"></td><td><pre></pre></td></tr><tr><td data-num=\"192\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"193\"></td><td><pre> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"194\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"195\"></td><td><pre> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"196\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"197\"></td><td><pre> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"198\"></td><td><pre> inv<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"199\"></td><td><pre> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>s<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"200\"></td><td><pre></pre></td></tr><tr><td data-num=\"201\"></td><td><pre> det <span class=\"token operator\">=</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>inv<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>inv<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>inv<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> s<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token operator\">*</span>inv<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"202\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>det <span class=\"token operator\">===</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"203\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"204\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"205\"></td><td><pre></pre></td></tr><tr><td data-num=\"206\"></td><td><pre> det <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token operator\">/</span> det<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"207\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span>i <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\"><</span> <span class=\"token number\">16</span><span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"208\"></td><td><pre> d<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> inv<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> det<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"209\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"210\"></td><td><pre></pre></td></tr><tr><td data-num=\"211\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"212\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"213\"></td><td><pre></pre></td></tr><tr><td data-num=\"214\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"215\"></td><td><pre> * Calculate the inverse matrix of this, and set to this.</pre></td></tr><tr><td data-num=\"216\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"217\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"218\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">invert</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"219\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">setInverseOf</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">this</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"220\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"221\"></td><td><pre></pre></td></tr><tr><td data-num=\"222\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"223\"></td><td><pre> * Set the orthographic projection matrix.</pre></td></tr><tr><td data-num=\"224\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">left</span> The coordinate of the left of clipping plane.</pre></td></tr><tr><td data-num=\"225\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">right</span> The coordinate of the right of clipping plane.</pre></td></tr><tr><td data-num=\"226\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">bottom</span> The coordinate of the bottom of clipping plane.</pre></td></tr><tr><td data-num=\"227\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">top</span> The coordinate of the top top clipping plane.</pre></td></tr><tr><td data-num=\"228\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">near</span> The distances to the nearer depth clipping plane. This value is minus if the plane is to be behind the viewer.</pre></td></tr><tr><td data-num=\"229\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">far</span> The distances to the farther depth clipping plane. This value is minus if the plane is to be behind the viewer.</pre></td></tr><tr><td data-num=\"230\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"231\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"232\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">setOrtho</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">left<span class=\"token punctuation\">,</span> right<span class=\"token punctuation\">,</span> bottom<span class=\"token punctuation\">,</span> top<span class=\"token punctuation\">,</span> near<span class=\"token punctuation\">,</span> far</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"233\"></td><td><pre> <span class=\"token keyword\">var</span> e<span class=\"token punctuation\">,</span> rw<span class=\"token punctuation\">,</span> rh<span class=\"token punctuation\">,</span> rd<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"234\"></td><td><pre></pre></td></tr><tr><td data-num=\"235\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>left <span class=\"token operator\">===</span> right <span class=\"token operator\">||</span> bottom <span class=\"token operator\">===</span> top <span class=\"token operator\">||</span> near <span class=\"token operator\">===</span> far<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"236\"></td><td><pre> <span class=\"token keyword control-flow\">throw</span> <span class=\"token string\">'null frustum'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"237\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"238\"></td><td><pre></pre></td></tr><tr><td data-num=\"239\"></td><td><pre> rw <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token operator\">/</span> <span class=\"token punctuation\">(</span>right <span class=\"token operator\">-</span> left<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"240\"></td><td><pre> rh <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token operator\">/</span> <span class=\"token punctuation\">(</span>top <span class=\"token operator\">-</span> bottom<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"241\"></td><td><pre> rd <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token operator\">/</span> <span class=\"token punctuation\">(</span>far <span class=\"token operator\">-</span> near<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"242\"></td><td><pre></pre></td></tr><tr><td data-num=\"243\"></td><td><pre> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"244\"></td><td><pre></pre></td></tr><tr><td data-num=\"245\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">2</span> <span class=\"token operator\">*</span> rw<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"246\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"247\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"248\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"249\"></td><td><pre></pre></td></tr><tr><td data-num=\"250\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"251\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">2</span> <span class=\"token operator\">*</span> rh<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"252\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"253\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"254\"></td><td><pre></pre></td></tr><tr><td data-num=\"255\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"256\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"257\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span><span class=\"token number\">2</span> <span class=\"token operator\">*</span> rd<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"258\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"259\"></td><td><pre></pre></td></tr><tr><td data-num=\"260\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span><span class=\"token punctuation\">(</span>right <span class=\"token operator\">+</span> left<span class=\"token punctuation\">)</span> <span class=\"token operator\">*</span> rw<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"261\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span><span class=\"token punctuation\">(</span>top <span class=\"token operator\">+</span> bottom<span class=\"token punctuation\">)</span> <span class=\"token operator\">*</span> rh<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"262\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span><span class=\"token punctuation\">(</span>far <span class=\"token operator\">+</span> near<span class=\"token punctuation\">)</span> <span class=\"token operator\">*</span> rd<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"263\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"264\"></td><td><pre></pre></td></tr><tr><td data-num=\"265\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"266\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"267\"></td><td><pre></pre></td></tr><tr><td data-num=\"268\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"269\"></td><td><pre> * Multiply the orthographic projection matrix from the right.</pre></td></tr><tr><td data-num=\"270\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">left</span> The coordinate of the left of clipping plane.</pre></td></tr><tr><td data-num=\"271\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">right</span> The coordinate of the right of clipping plane.</pre></td></tr><tr><td data-num=\"272\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">bottom</span> The coordinate of the bottom of clipping plane.</pre></td></tr><tr><td data-num=\"273\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">top</span> The coordinate of the top top clipping plane.</pre></td></tr><tr><td data-num=\"274\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">near</span> The distances to the nearer depth clipping plane. This value is minus if the plane is to be behind the viewer.</pre></td></tr><tr><td data-num=\"275\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">far</span> The distances to the farther depth clipping plane. This value is minus if the plane is to be behind the viewer.</pre></td></tr><tr><td data-num=\"276\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"277\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"278\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">ortho</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">left<span class=\"token punctuation\">,</span> right<span class=\"token punctuation\">,</span> bottom<span class=\"token punctuation\">,</span> top<span class=\"token punctuation\">,</span> near<span class=\"token punctuation\">,</span> far</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"279\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">concat</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">new</span> <span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">setOrtho</span><span class=\"token punctuation\">(</span>left<span class=\"token punctuation\">,</span> right<span class=\"token punctuation\">,</span> bottom<span class=\"token punctuation\">,</span> top<span class=\"token punctuation\">,</span> near<span class=\"token punctuation\">,</span> far<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"280\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"281\"></td><td><pre></pre></td></tr><tr><td data-num=\"282\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"283\"></td><td><pre> * Set the perspective projection matrix.</pre></td></tr><tr><td data-num=\"284\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">left</span> The coordinate of the left of clipping plane.</pre></td></tr><tr><td data-num=\"285\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">right</span> The coordinate of the right of clipping plane.</pre></td></tr><tr><td data-num=\"286\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">bottom</span> The coordinate of the bottom of clipping plane.</pre></td></tr><tr><td data-num=\"287\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">top</span> The coordinate of the top top clipping plane.</pre></td></tr><tr><td data-num=\"288\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">near</span> The distances to the nearer depth clipping plane. This value must be plus value.</pre></td></tr><tr><td data-num=\"289\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">far</span> The distances to the farther depth clipping plane. This value must be plus value.</pre></td></tr><tr><td data-num=\"290\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"291\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"292\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">setFrustum</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">left<span class=\"token punctuation\">,</span> right<span class=\"token punctuation\">,</span> bottom<span class=\"token punctuation\">,</span> top<span class=\"token punctuation\">,</span> near<span class=\"token punctuation\">,</span> far</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"293\"></td><td><pre> <span class=\"token keyword\">var</span> e<span class=\"token punctuation\">,</span> rw<span class=\"token punctuation\">,</span> rh<span class=\"token punctuation\">,</span> rd<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"294\"></td><td><pre></pre></td></tr><tr><td data-num=\"295\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>left <span class=\"token operator\">===</span> right <span class=\"token operator\">||</span> top <span class=\"token operator\">===</span> bottom <span class=\"token operator\">||</span> near <span class=\"token operator\">===</span> far<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"296\"></td><td><pre> <span class=\"token keyword control-flow\">throw</span> <span class=\"token string\">'null frustum'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"297\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"298\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>near <span class=\"token operator\"><=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"299\"></td><td><pre> <span class=\"token keyword control-flow\">throw</span> <span class=\"token string\">'near <= 0'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"300\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"301\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>far <span class=\"token operator\"><=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"302\"></td><td><pre> <span class=\"token keyword control-flow\">throw</span> <span class=\"token string\">'far <= 0'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"303\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"304\"></td><td><pre></pre></td></tr><tr><td data-num=\"305\"></td><td><pre> rw <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token operator\">/</span> <span class=\"token punctuation\">(</span>right <span class=\"token operator\">-</span> left<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"306\"></td><td><pre> rh <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token operator\">/</span> <span class=\"token punctuation\">(</span>top <span class=\"token operator\">-</span> bottom<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"307\"></td><td><pre> rd <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token operator\">/</span> <span class=\"token punctuation\">(</span>far <span class=\"token operator\">-</span> near<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"308\"></td><td><pre></pre></td></tr><tr><td data-num=\"309\"></td><td><pre> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"310\"></td><td><pre></pre></td></tr><tr><td data-num=\"311\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">2</span> <span class=\"token operator\">*</span> near <span class=\"token operator\">*</span> rw<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"312\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"313\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"314\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"315\"></td><td><pre></pre></td></tr><tr><td data-num=\"316\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"317\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">2</span> <span class=\"token operator\">*</span> near <span class=\"token operator\">*</span> rh<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"318\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"319\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"320\"></td><td><pre></pre></td></tr><tr><td data-num=\"321\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span>right <span class=\"token operator\">+</span> left<span class=\"token punctuation\">)</span> <span class=\"token operator\">*</span> rw<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"322\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span>top <span class=\"token operator\">+</span> bottom<span class=\"token punctuation\">)</span> <span class=\"token operator\">*</span> rh<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"323\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span><span class=\"token punctuation\">(</span>far <span class=\"token operator\">+</span> near<span class=\"token punctuation\">)</span> <span class=\"token operator\">*</span> rd<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"324\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span><span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"325\"></td><td><pre></pre></td></tr><tr><td data-num=\"326\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"327\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"328\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span><span class=\"token number\">2</span> <span class=\"token operator\">*</span> near <span class=\"token operator\">*</span> far <span class=\"token operator\">*</span> rd<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"329\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"330\"></td><td><pre></pre></td></tr><tr><td data-num=\"331\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"332\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"333\"></td><td><pre></pre></td></tr><tr><td data-num=\"334\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"335\"></td><td><pre> * Multiply the perspective projection matrix from the right.</pre></td></tr><tr><td data-num=\"336\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">left</span> The coordinate of the left of clipping plane.</pre></td></tr><tr><td data-num=\"337\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">right</span> The coordinate of the right of clipping plane.</pre></td></tr><tr><td data-num=\"338\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">bottom</span> The coordinate of the bottom of clipping plane.</pre></td></tr><tr><td data-num=\"339\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">top</span> The coordinate of the top top clipping plane.</pre></td></tr><tr><td data-num=\"340\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">near</span> The distances to the nearer depth clipping plane. This value must be plus value.</pre></td></tr><tr><td data-num=\"341\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">far</span> The distances to the farther depth clipping plane. This value must be plus value.</pre></td></tr><tr><td data-num=\"342\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"343\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"344\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">frustum</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">left<span class=\"token punctuation\">,</span> right<span class=\"token punctuation\">,</span> bottom<span class=\"token punctuation\">,</span> top<span class=\"token punctuation\">,</span> near<span class=\"token punctuation\">,</span> far</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"345\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">concat</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">new</span> <span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">setFrustum</span><span class=\"token punctuation\">(</span>left<span class=\"token punctuation\">,</span> right<span class=\"token punctuation\">,</span> bottom<span class=\"token punctuation\">,</span> top<span class=\"token punctuation\">,</span> near<span class=\"token punctuation\">,</span> far<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"346\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"347\"></td><td><pre></pre></td></tr><tr><td data-num=\"348\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"349\"></td><td><pre> * Set the perspective projection matrix by fovy and aspect.</pre></td></tr><tr><td data-num=\"350\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">fovy</span> The angle between the upper and lower sides of the frustum.</pre></td></tr><tr><td data-num=\"351\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">aspect</span> The aspect ratio of the frustum. (width/height)</pre></td></tr><tr><td data-num=\"352\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">near</span> The distances to the nearer depth clipping plane. This value must be plus value.</pre></td></tr><tr><td data-num=\"353\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">far</span> The distances to the farther depth clipping plane. This value must be plus value.</pre></td></tr><tr><td data-num=\"354\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"355\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"356\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">setPerspective</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">fovy<span class=\"token punctuation\">,</span> aspect<span class=\"token punctuation\">,</span> near<span class=\"token punctuation\">,</span> far</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"357\"></td><td><pre> <span class=\"token keyword\">var</span> e<span class=\"token punctuation\">,</span> rd<span class=\"token punctuation\">,</span> s<span class=\"token punctuation\">,</span> ct<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"358\"></td><td><pre></pre></td></tr><tr><td data-num=\"359\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>near <span class=\"token operator\">===</span> far <span class=\"token operator\">||</span> aspect <span class=\"token operator\">===</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"360\"></td><td><pre> <span class=\"token keyword control-flow\">throw</span> <span class=\"token string\">'null frustum'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"361\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"362\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>near <span class=\"token operator\"><=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"363\"></td><td><pre> <span class=\"token keyword control-flow\">throw</span> <span class=\"token string\">'near <= 0'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"364\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"365\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>far <span class=\"token operator\"><=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"366\"></td><td><pre> <span class=\"token keyword control-flow\">throw</span> <span class=\"token string\">'far <= 0'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"367\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"368\"></td><td><pre></pre></td></tr><tr><td data-num=\"369\"></td><td><pre> fovy <span class=\"token operator\">=</span> <span class=\"token known-class-name class-name\">Math</span><span class=\"token punctuation\">.</span><span class=\"token constant\">PI</span> <span class=\"token operator\">*</span> fovy <span class=\"token operator\">/</span> <span class=\"token number\">180</span> <span class=\"token operator\">/</span> <span class=\"token number\">2</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"370\"></td><td><pre> s <span class=\"token operator\">=</span> <span class=\"token known-class-name class-name\">Math</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">sin</span><span class=\"token punctuation\">(</span>fovy<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"371\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>s <span class=\"token operator\">===</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"372\"></td><td><pre> <span class=\"token keyword control-flow\">throw</span> <span class=\"token string\">'null frustum'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"373\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"374\"></td><td><pre></pre></td></tr><tr><td data-num=\"375\"></td><td><pre> rd <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token operator\">/</span> <span class=\"token punctuation\">(</span>far <span class=\"token operator\">-</span> near<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"376\"></td><td><pre> ct <span class=\"token operator\">=</span> <span class=\"token known-class-name class-name\">Math</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">cos</span><span class=\"token punctuation\">(</span>fovy<span class=\"token punctuation\">)</span> <span class=\"token operator\">/</span> s<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"377\"></td><td><pre></pre></td></tr><tr><td data-num=\"378\"></td><td><pre> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"379\"></td><td><pre></pre></td></tr><tr><td data-num=\"380\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> ct <span class=\"token operator\">/</span> aspect<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"381\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"382\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"383\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"384\"></td><td><pre></pre></td></tr><tr><td data-num=\"385\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"386\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> ct<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"387\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"388\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"389\"></td><td><pre></pre></td></tr><tr><td data-num=\"390\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"391\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"392\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span><span class=\"token punctuation\">(</span>far <span class=\"token operator\">+</span> near<span class=\"token punctuation\">)</span> <span class=\"token operator\">*</span> rd<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"393\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span><span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"394\"></td><td><pre></pre></td></tr><tr><td data-num=\"395\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"396\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"397\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span><span class=\"token number\">2</span> <span class=\"token operator\">*</span> near <span class=\"token operator\">*</span> far <span class=\"token operator\">*</span> rd<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"398\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"399\"></td><td><pre></pre></td></tr><tr><td data-num=\"400\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"401\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"402\"></td><td><pre></pre></td></tr><tr><td data-num=\"403\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"404\"></td><td><pre> * Multiply the perspective projection matrix from the right.</pre></td></tr><tr><td data-num=\"405\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">fovy</span> The angle between the upper and lower sides of the frustum.</pre></td></tr><tr><td data-num=\"406\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">aspect</span> The aspect ratio of the frustum. (width/height)</pre></td></tr><tr><td data-num=\"407\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">near</span> The distances to the nearer depth clipping plane. This value must be plus value.</pre></td></tr><tr><td data-num=\"408\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">far</span> The distances to the farther depth clipping plane. This value must be plus value.</pre></td></tr><tr><td data-num=\"409\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"410\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"411\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">perspective</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">fovy<span class=\"token punctuation\">,</span> aspect<span class=\"token punctuation\">,</span> near<span class=\"token punctuation\">,</span> far</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"412\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">concat</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">new</span> <span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">setPerspective</span><span class=\"token punctuation\">(</span>fovy<span class=\"token punctuation\">,</span> aspect<span class=\"token punctuation\">,</span> near<span class=\"token punctuation\">,</span> far<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"413\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"414\"></td><td><pre></pre></td></tr><tr><td data-num=\"415\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"416\"></td><td><pre> * Set the matrix for scaling.</pre></td></tr><tr><td data-num=\"417\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">x</span> The scale factor along the X axis</pre></td></tr><tr><td data-num=\"418\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">y</span> The scale factor along the Y axis</pre></td></tr><tr><td data-num=\"419\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">z</span> The scale factor along the Z axis</pre></td></tr><tr><td data-num=\"420\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"421\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"422\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">setScale</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">x<span class=\"token punctuation\">,</span> y<span class=\"token punctuation\">,</span> z</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"423\"></td><td><pre> <span class=\"token keyword\">var</span> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"424\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> x<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"425\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> y<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"426\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> z<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"427\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"428\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"429\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"430\"></td><td><pre></pre></td></tr><tr><td data-num=\"431\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"432\"></td><td><pre> * Multiply the matrix for scaling from the right.</pre></td></tr><tr><td data-num=\"433\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">x</span> The scale factor along the X axis</pre></td></tr><tr><td data-num=\"434\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">y</span> The scale factor along the Y axis</pre></td></tr><tr><td data-num=\"435\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">z</span> The scale factor along the Z axis</pre></td></tr><tr><td data-num=\"436\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"437\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"438\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">scale</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">x<span class=\"token punctuation\">,</span> y<span class=\"token punctuation\">,</span> z</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"439\"></td><td><pre> <span class=\"token keyword\">var</span> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"440\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*=</span> x<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*=</span> y<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*=</span> z<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"441\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*=</span> x<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*=</span> y<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*=</span> z<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"442\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*=</span> x<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*=</span> y<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*=</span> z<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"443\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*=</span> x<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*=</span> y<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*=</span> z<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"444\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"445\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"446\"></td><td><pre></pre></td></tr><tr><td data-num=\"447\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"448\"></td><td><pre> * Set the matrix for translation.</pre></td></tr><tr><td data-num=\"449\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">x</span> The X value of a translation.</pre></td></tr><tr><td data-num=\"450\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">y</span> The Y value of a translation.</pre></td></tr><tr><td data-num=\"451\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">z</span> The Z value of a translation.</pre></td></tr><tr><td data-num=\"452\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"453\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"454\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">setTranslate</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">x<span class=\"token punctuation\">,</span> y<span class=\"token punctuation\">,</span> z</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"455\"></td><td><pre> <span class=\"token keyword\">var</span> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"456\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> x<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"457\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> y<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"458\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> z<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"459\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"460\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"461\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"462\"></td><td><pre></pre></td></tr><tr><td data-num=\"463\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"464\"></td><td><pre> * Multiply the matrix for translation from the right.</pre></td></tr><tr><td data-num=\"465\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">x</span> The X value of a translation.</pre></td></tr><tr><td data-num=\"466\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">y</span> The Y value of a translation.</pre></td></tr><tr><td data-num=\"467\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">z</span> The Z value of a translation.</pre></td></tr><tr><td data-num=\"468\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"469\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"470\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">translate</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">x<span class=\"token punctuation\">,</span> y<span class=\"token punctuation\">,</span> z</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"471\"></td><td><pre> <span class=\"token keyword\">var</span> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"472\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+=</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> x <span class=\"token operator\">+</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> y <span class=\"token operator\">+</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> z<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"473\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+=</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> x <span class=\"token operator\">+</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> y <span class=\"token operator\">+</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> z<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"474\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+=</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> x <span class=\"token operator\">+</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> y <span class=\"token operator\">+</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> z<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"475\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+=</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> x <span class=\"token operator\">+</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> y <span class=\"token operator\">+</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> z<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"476\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"477\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"478\"></td><td><pre></pre></td></tr><tr><td data-num=\"479\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"480\"></td><td><pre> * Set the matrix for rotation.</pre></td></tr><tr><td data-num=\"481\"></td><td><pre> * The vector of rotation axis may not be normalized.</pre></td></tr><tr><td data-num=\"482\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">angle</span> The angle of rotation (degrees)</pre></td></tr><tr><td data-num=\"483\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">x</span> The X coordinate of vector of rotation axis.</pre></td></tr><tr><td data-num=\"484\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">y</span> The Y coordinate of vector of rotation axis.</pre></td></tr><tr><td data-num=\"485\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">z</span> The Z coordinate of vector of rotation axis.</pre></td></tr><tr><td data-num=\"486\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"487\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"488\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">setRotate</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">angle<span class=\"token punctuation\">,</span> x<span class=\"token punctuation\">,</span> y<span class=\"token punctuation\">,</span> z</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"489\"></td><td><pre> <span class=\"token keyword\">var</span> e<span class=\"token punctuation\">,</span> s<span class=\"token punctuation\">,</span> c<span class=\"token punctuation\">,</span> len<span class=\"token punctuation\">,</span> rlen<span class=\"token punctuation\">,</span> nc<span class=\"token punctuation\">,</span> xy<span class=\"token punctuation\">,</span> yz<span class=\"token punctuation\">,</span> zx<span class=\"token punctuation\">,</span> xs<span class=\"token punctuation\">,</span> ys<span class=\"token punctuation\">,</span> zs<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"490\"></td><td><pre></pre></td></tr><tr><td data-num=\"491\"></td><td><pre> angle <span class=\"token operator\">=</span> <span class=\"token known-class-name class-name\">Math</span><span class=\"token punctuation\">.</span><span class=\"token constant\">PI</span> <span class=\"token operator\">*</span> angle <span class=\"token operator\">/</span> <span class=\"token number\">180</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"492\"></td><td><pre> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"493\"></td><td><pre></pre></td></tr><tr><td data-num=\"494\"></td><td><pre> s <span class=\"token operator\">=</span> <span class=\"token known-class-name class-name\">Math</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">sin</span><span class=\"token punctuation\">(</span>angle<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"495\"></td><td><pre> c <span class=\"token operator\">=</span> <span class=\"token known-class-name class-name\">Math</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">cos</span><span class=\"token punctuation\">(</span>angle<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"496\"></td><td><pre></pre></td></tr><tr><td data-num=\"497\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token number\">0</span> <span class=\"token operator\">!==</span> x <span class=\"token operator\">&&</span> <span class=\"token number\">0</span> <span class=\"token operator\">===</span> y <span class=\"token operator\">&&</span> <span class=\"token number\">0</span> <span class=\"token operator\">===</span> z<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"498\"></td><td><pre> <span class=\"token comment\">// Rotation around X axis</span></pre></td></tr><tr><td data-num=\"499\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>x <span class=\"token operator\"><</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"500\"></td><td><pre> s <span class=\"token operator\">=</span> <span class=\"token operator\">-</span>s<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"501\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"502\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"503\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> c<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span><span class=\"token operator\">-</span>s<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"504\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> s<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> c<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"505\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"506\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token number\">0</span> <span class=\"token operator\">===</span> x <span class=\"token operator\">&&</span> <span class=\"token number\">0</span> <span class=\"token operator\">!==</span> y <span class=\"token operator\">&&</span> <span class=\"token number\">0</span> <span class=\"token operator\">===</span> z<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"507\"></td><td><pre> <span class=\"token comment\">// Rotation around Y axis</span></pre></td></tr><tr><td data-num=\"508\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>y <span class=\"token operator\"><</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"509\"></td><td><pre> s <span class=\"token operator\">=</span> <span class=\"token operator\">-</span>s<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"510\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"511\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> c<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> s<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"512\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"513\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span><span class=\"token operator\">-</span>s<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> c<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"514\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"515\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token number\">0</span> <span class=\"token operator\">===</span> x <span class=\"token operator\">&&</span> <span class=\"token number\">0</span> <span class=\"token operator\">===</span> y <span class=\"token operator\">&&</span> <span class=\"token number\">0</span> <span class=\"token operator\">!==</span> z<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"516\"></td><td><pre> <span class=\"token comment\">// Rotation around Z axis</span></pre></td></tr><tr><td data-num=\"517\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>z <span class=\"token operator\"><</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"518\"></td><td><pre> s <span class=\"token operator\">=</span> <span class=\"token operator\">-</span>s<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"519\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"520\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> c<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span><span class=\"token operator\">-</span>s<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"521\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> s<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> c<span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span> <span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"522\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"523\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"524\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"525\"></td><td><pre> <span class=\"token comment\">// Rotation around another axis</span></pre></td></tr><tr><td data-num=\"526\"></td><td><pre> len <span class=\"token operator\">=</span> <span class=\"token known-class-name class-name\">Math</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">sqrt</span><span class=\"token punctuation\">(</span>x<span class=\"token operator\">*</span>x <span class=\"token operator\">+</span> y<span class=\"token operator\">*</span>y <span class=\"token operator\">+</span> z<span class=\"token operator\">*</span>z<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"527\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>len <span class=\"token operator\">!==</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"528\"></td><td><pre> rlen <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token operator\">/</span> len<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"529\"></td><td><pre> x <span class=\"token operator\">*=</span> rlen<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"530\"></td><td><pre> y <span class=\"token operator\">*=</span> rlen<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"531\"></td><td><pre> z <span class=\"token operator\">*=</span> rlen<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"532\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"533\"></td><td><pre> nc <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token operator\">-</span> c<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"534\"></td><td><pre> xy <span class=\"token operator\">=</span> x <span class=\"token operator\">*</span> y<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"535\"></td><td><pre> yz <span class=\"token operator\">=</span> y <span class=\"token operator\">*</span> z<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"536\"></td><td><pre> zx <span class=\"token operator\">=</span> z <span class=\"token operator\">*</span> x<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"537\"></td><td><pre> xs <span class=\"token operator\">=</span> x <span class=\"token operator\">*</span> s<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"538\"></td><td><pre> ys <span class=\"token operator\">=</span> y <span class=\"token operator\">*</span> s<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"539\"></td><td><pre> zs <span class=\"token operator\">=</span> z <span class=\"token operator\">*</span> s<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"540\"></td><td><pre></pre></td></tr><tr><td data-num=\"541\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> x<span class=\"token operator\">*</span>x<span class=\"token operator\">*</span>nc <span class=\"token operator\">+</span> c<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"542\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> xy <span class=\"token operator\">*</span>nc <span class=\"token operator\">+</span> zs<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"543\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> zx <span class=\"token operator\">*</span>nc <span class=\"token operator\">-</span> ys<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"544\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"545\"></td><td><pre></pre></td></tr><tr><td data-num=\"546\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> xy <span class=\"token operator\">*</span>nc <span class=\"token operator\">-</span> zs<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"547\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> y<span class=\"token operator\">*</span>y<span class=\"token operator\">*</span>nc <span class=\"token operator\">+</span> c<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"548\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> yz <span class=\"token operator\">*</span>nc <span class=\"token operator\">+</span> xs<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"549\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"550\"></td><td><pre></pre></td></tr><tr><td data-num=\"551\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> zx <span class=\"token operator\">*</span>nc <span class=\"token operator\">+</span> ys<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"552\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> yz <span class=\"token operator\">*</span>nc <span class=\"token operator\">-</span> xs<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"553\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> z<span class=\"token operator\">*</span>z<span class=\"token operator\">*</span>nc <span class=\"token operator\">+</span> c<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"554\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"555\"></td><td><pre></pre></td></tr><tr><td data-num=\"556\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"557\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"558\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"559\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"560\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"561\"></td><td><pre></pre></td></tr><tr><td data-num=\"562\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"563\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"564\"></td><td><pre></pre></td></tr><tr><td data-num=\"565\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"566\"></td><td><pre> * Multiply the matrix for rotation from the right.</pre></td></tr><tr><td data-num=\"567\"></td><td><pre> * The vector of rotation axis may not be normalized.</pre></td></tr><tr><td data-num=\"568\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">angle</span> The angle of rotation (degrees)</pre></td></tr><tr><td data-num=\"569\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">x</span> The X coordinate of vector of rotation axis.</pre></td></tr><tr><td data-num=\"570\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">y</span> The Y coordinate of vector of rotation axis.</pre></td></tr><tr><td data-num=\"571\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">z</span> The Z coordinate of vector of rotation axis.</pre></td></tr><tr><td data-num=\"572\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"573\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"574\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">rotate</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">angle<span class=\"token punctuation\">,</span> x<span class=\"token punctuation\">,</span> y<span class=\"token punctuation\">,</span> z</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"575\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">concat</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">new</span> <span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">setRotate</span><span class=\"token punctuation\">(</span>angle<span class=\"token punctuation\">,</span> x<span class=\"token punctuation\">,</span> y<span class=\"token punctuation\">,</span> z<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"576\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"577\"></td><td><pre></pre></td></tr><tr><td data-num=\"578\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"579\"></td><td><pre> * Set the viewing matrix.</pre></td></tr><tr><td data-num=\"580\"></td><td><pre> * <span class=\"token keyword\">@param</span> eyeX, eyeY, eyeZ The position of the eye point.</pre></td></tr><tr><td data-num=\"581\"></td><td><pre> * <span class=\"token keyword\">@param</span> centerX, centerY, centerZ The position of the reference point.</pre></td></tr><tr><td data-num=\"582\"></td><td><pre> * <span class=\"token keyword\">@param</span> upX, upY, upZ The direction of the up vector.</pre></td></tr><tr><td data-num=\"583\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"584\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"585\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">setLookAt</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">eyeX<span class=\"token punctuation\">,</span> eyeY<span class=\"token punctuation\">,</span> eyeZ<span class=\"token punctuation\">,</span> centerX<span class=\"token punctuation\">,</span> centerY<span class=\"token punctuation\">,</span> centerZ<span class=\"token punctuation\">,</span> upX<span class=\"token punctuation\">,</span> upY<span class=\"token punctuation\">,</span> upZ</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"586\"></td><td><pre> <span class=\"token keyword\">var</span> e<span class=\"token punctuation\">,</span> fx<span class=\"token punctuation\">,</span> fy<span class=\"token punctuation\">,</span> fz<span class=\"token punctuation\">,</span> rlf<span class=\"token punctuation\">,</span> sx<span class=\"token punctuation\">,</span> sy<span class=\"token punctuation\">,</span> sz<span class=\"token punctuation\">,</span> rls<span class=\"token punctuation\">,</span> ux<span class=\"token punctuation\">,</span> uy<span class=\"token punctuation\">,</span> uz<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"587\"></td><td><pre></pre></td></tr><tr><td data-num=\"588\"></td><td><pre> fx <span class=\"token operator\">=</span> centerX <span class=\"token operator\">-</span> eyeX<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"589\"></td><td><pre> fy <span class=\"token operator\">=</span> centerY <span class=\"token operator\">-</span> eyeY<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"590\"></td><td><pre> fz <span class=\"token operator\">=</span> centerZ <span class=\"token operator\">-</span> eyeZ<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"591\"></td><td><pre></pre></td></tr><tr><td data-num=\"592\"></td><td><pre> <span class=\"token comment\">// Normalize f.</span></pre></td></tr><tr><td data-num=\"593\"></td><td><pre> rlf <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token operator\">/</span> <span class=\"token known-class-name class-name\">Math</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">sqrt</span><span class=\"token punctuation\">(</span>fx<span class=\"token operator\">*</span>fx <span class=\"token operator\">+</span> fy<span class=\"token operator\">*</span>fy <span class=\"token operator\">+</span> fz<span class=\"token operator\">*</span>fz<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"594\"></td><td><pre> fx <span class=\"token operator\">*=</span> rlf<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"595\"></td><td><pre> fy <span class=\"token operator\">*=</span> rlf<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"596\"></td><td><pre> fz <span class=\"token operator\">*=</span> rlf<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"597\"></td><td><pre></pre></td></tr><tr><td data-num=\"598\"></td><td><pre> <span class=\"token comment\">// Calculate cross product of f and up.</span></pre></td></tr><tr><td data-num=\"599\"></td><td><pre> sx <span class=\"token operator\">=</span> fy <span class=\"token operator\">*</span> upZ <span class=\"token operator\">-</span> fz <span class=\"token operator\">*</span> upY<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"600\"></td><td><pre> sy <span class=\"token operator\">=</span> fz <span class=\"token operator\">*</span> upX <span class=\"token operator\">-</span> fx <span class=\"token operator\">*</span> upZ<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"601\"></td><td><pre> sz <span class=\"token operator\">=</span> fx <span class=\"token operator\">*</span> upY <span class=\"token operator\">-</span> fy <span class=\"token operator\">*</span> upX<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"602\"></td><td><pre></pre></td></tr><tr><td data-num=\"603\"></td><td><pre> <span class=\"token comment\">// Normalize s.</span></pre></td></tr><tr><td data-num=\"604\"></td><td><pre> rls <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token operator\">/</span> <span class=\"token known-class-name class-name\">Math</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">sqrt</span><span class=\"token punctuation\">(</span>sx<span class=\"token operator\">*</span>sx <span class=\"token operator\">+</span> sy<span class=\"token operator\">*</span>sy <span class=\"token operator\">+</span> sz<span class=\"token operator\">*</span>sz<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"605\"></td><td><pre> sx <span class=\"token operator\">*=</span> rls<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"606\"></td><td><pre> sy <span class=\"token operator\">*=</span> rls<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"607\"></td><td><pre> sz <span class=\"token operator\">*=</span> rls<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"608\"></td><td><pre></pre></td></tr><tr><td data-num=\"609\"></td><td><pre> <span class=\"token comment\">// Calculate cross product of s and f.</span></pre></td></tr><tr><td data-num=\"610\"></td><td><pre> ux <span class=\"token operator\">=</span> sy <span class=\"token operator\">*</span> fz <span class=\"token operator\">-</span> sz <span class=\"token operator\">*</span> fy<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"611\"></td><td><pre> uy <span class=\"token operator\">=</span> sz <span class=\"token operator\">*</span> fx <span class=\"token operator\">-</span> sx <span class=\"token operator\">*</span> fz<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"612\"></td><td><pre> uz <span class=\"token operator\">=</span> sx <span class=\"token operator\">*</span> fy <span class=\"token operator\">-</span> sy <span class=\"token operator\">*</span> fx<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"613\"></td><td><pre></pre></td></tr><tr><td data-num=\"614\"></td><td><pre> <span class=\"token comment\">// Set to this.</span></pre></td></tr><tr><td data-num=\"615\"></td><td><pre> e <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"616\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> sx<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"617\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> ux<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"618\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span>fx<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"619\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"620\"></td><td><pre></pre></td></tr><tr><td data-num=\"621\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> sy<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"622\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> uy<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"623\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span>fy<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"624\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"625\"></td><td><pre></pre></td></tr><tr><td data-num=\"626\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> sz<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"627\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> uz<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"628\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span>fz<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"629\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"630\"></td><td><pre></pre></td></tr><tr><td data-num=\"631\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"632\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"633\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"634\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"635\"></td><td><pre></pre></td></tr><tr><td data-num=\"636\"></td><td><pre> <span class=\"token comment\">// Translate.</span></pre></td></tr><tr><td data-num=\"637\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">translate</span><span class=\"token punctuation\">(</span><span class=\"token operator\">-</span>eyeX<span class=\"token punctuation\">,</span> <span class=\"token operator\">-</span>eyeY<span class=\"token punctuation\">,</span> <span class=\"token operator\">-</span>eyeZ<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"638\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"639\"></td><td><pre></pre></td></tr><tr><td data-num=\"640\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"641\"></td><td><pre> * Multiply the viewing matrix from the right.</pre></td></tr><tr><td data-num=\"642\"></td><td><pre> * <span class=\"token keyword\">@param</span> eyeX, eyeY, eyeZ The position of the eye point.</pre></td></tr><tr><td data-num=\"643\"></td><td><pre> * <span class=\"token keyword\">@param</span> centerX, centerY, centerZ The position of the reference point.</pre></td></tr><tr><td data-num=\"644\"></td><td><pre> * <span class=\"token keyword\">@param</span> upX, upY, upZ The direction of the up vector.</pre></td></tr><tr><td data-num=\"645\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"646\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"647\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">lookAt</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">eyeX<span class=\"token punctuation\">,</span> eyeY<span class=\"token punctuation\">,</span> eyeZ<span class=\"token punctuation\">,</span> centerX<span class=\"token punctuation\">,</span> centerY<span class=\"token punctuation\">,</span> centerZ<span class=\"token punctuation\">,</span> upX<span class=\"token punctuation\">,</span> upY<span class=\"token punctuation\">,</span> upZ</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"648\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">concat</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">new</span> <span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">setLookAt</span><span class=\"token punctuation\">(</span>eyeX<span class=\"token punctuation\">,</span> eyeY<span class=\"token punctuation\">,</span> eyeZ<span class=\"token punctuation\">,</span> centerX<span class=\"token punctuation\">,</span> centerY<span class=\"token punctuation\">,</span> centerZ<span class=\"token punctuation\">,</span> upX<span class=\"token punctuation\">,</span> upY<span class=\"token punctuation\">,</span> upZ<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"649\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"650\"></td><td><pre></pre></td></tr><tr><td data-num=\"651\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"652\"></td><td><pre> * Multiply the matrix for project vertex to plane from the right.</pre></td></tr><tr><td data-num=\"653\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">plane</span> The array[A, B, C, D] of the equation of plane \"Ax + By + Cz + D = 0\".</pre></td></tr><tr><td data-num=\"654\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">light</span> The array which stored coordinates of the light. if light[3]=0, treated as parallel light.</pre></td></tr><tr><td data-num=\"655\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"656\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"657\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">dropShadow</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">plane<span class=\"token punctuation\">,</span> light</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"658\"></td><td><pre> <span class=\"token keyword\">var</span> mat <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"659\"></td><td><pre> <span class=\"token keyword\">var</span> e <span class=\"token operator\">=</span> mat<span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"660\"></td><td><pre></pre></td></tr><tr><td data-num=\"661\"></td><td><pre> <span class=\"token keyword\">var</span> dot <span class=\"token operator\">=</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">+</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"662\"></td><td><pre></pre></td></tr><tr><td data-num=\"663\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> dot <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"664\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"665\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"666\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"667\"></td><td><pre></pre></td></tr><tr><td data-num=\"668\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">4</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"669\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">5</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> dot <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"670\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">6</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"671\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">7</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"672\"></td><td><pre></pre></td></tr><tr><td data-num=\"673\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">8</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"674\"></td><td><pre> e<span class=\"token punctuation\">[</span> <span class=\"token number\">9</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"675\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> dot <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"676\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"677\"></td><td><pre></pre></td></tr><tr><td data-num=\"678\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">12</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"679\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">13</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"680\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">14</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"681\"></td><td><pre> e<span class=\"token punctuation\">[</span><span class=\"token number\">15</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> dot <span class=\"token operator\">-</span> light<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">*</span> plane<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"682\"></td><td><pre></pre></td></tr><tr><td data-num=\"683\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">concat</span><span class=\"token punctuation\">(</span>mat<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"684\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"685\"></td><td><pre></pre></td></tr><tr><td data-num=\"686\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"687\"></td><td><pre> * Multiply the matrix for project vertex to plane from the right.(Projected by parallel light.)</pre></td></tr><tr><td data-num=\"688\"></td><td><pre> * <span class=\"token keyword\">@param</span> normX, normY, normZ The normal vector of the plane.(Not necessary to be normalized.)</pre></td></tr><tr><td data-num=\"689\"></td><td><pre> * <span class=\"token keyword\">@param</span> planeX, planeY, planeZ The coordinate of arbitrary points on a plane.</pre></td></tr><tr><td data-num=\"690\"></td><td><pre> * <span class=\"token keyword\">@param</span> lightX, lightY, lightZ The vector of the direction of light.(Not necessary to be normalized.)</pre></td></tr><tr><td data-num=\"691\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"692\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"693\"></td><td><pre><span class=\"token class-name\">Matrix4</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">dropShadowDirectionally</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">normX<span class=\"token punctuation\">,</span> normY<span class=\"token punctuation\">,</span> normZ<span class=\"token punctuation\">,</span> planeX<span class=\"token punctuation\">,</span> planeY<span class=\"token punctuation\">,</span> planeZ<span class=\"token punctuation\">,</span> lightX<span class=\"token punctuation\">,</span> lightY<span class=\"token punctuation\">,</span> lightZ</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"694\"></td><td><pre> <span class=\"token keyword\">var</span> a <span class=\"token operator\">=</span> planeX <span class=\"token operator\">*</span> normX <span class=\"token operator\">+</span> planeY <span class=\"token operator\">*</span> normY <span class=\"token operator\">+</span> planeZ <span class=\"token operator\">*</span> normZ<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"695\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">dropShadow</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span>normX<span class=\"token punctuation\">,</span> normY<span class=\"token punctuation\">,</span> normZ<span class=\"token punctuation\">,</span> <span class=\"token operator\">-</span>a<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span>lightX<span class=\"token punctuation\">,</span> lightY<span class=\"token punctuation\">,</span> lightZ<span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"696\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"697\"></td><td><pre></pre></td></tr><tr><td data-num=\"698\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"699\"></td><td><pre> * Constructor of Vector3</pre></td></tr><tr><td data-num=\"700\"></td><td><pre> * If opt_src is specified, new vector is initialized by opt_src.</pre></td></tr><tr><td data-num=\"701\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">opt_src</span> source vector(option)</pre></td></tr><tr><td data-num=\"702\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"703\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token function-variable function\"><span class=\"token maybe-class-name\">Vector3</span></span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">opt_src</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"704\"></td><td><pre> <span class=\"token keyword\">var</span> v <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Float32Array</span><span class=\"token punctuation\">(</span><span class=\"token number\">3</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"705\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>opt_src <span class=\"token operator\">&&</span> <span class=\"token keyword\">typeof</span> opt_src <span class=\"token operator\">===</span> <span class=\"token string\">'object'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"706\"></td><td><pre> v<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> opt_src<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> v<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> opt_src<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> v<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> opt_src<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"707\"></td><td><pre> <span class=\"token punctuation\">}</span> </pre></td></tr><tr><td data-num=\"708\"></td><td><pre> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span> <span class=\"token operator\">=</span> v<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"709\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"710\"></td><td><pre></pre></td></tr><tr><td data-num=\"711\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"712\"></td><td><pre> * Normalize.</pre></td></tr><tr><td data-num=\"713\"></td><td><pre> * <span class=\"token keyword\">@return</span> this</pre></td></tr><tr><td data-num=\"714\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"715\"></td><td><pre><span class=\"token class-name\">Vector3</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">prototype</span><span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">normalize</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"716\"></td><td><pre> <span class=\"token keyword\">var</span> v <span class=\"token operator\">=</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"717\"></td><td><pre> <span class=\"token keyword\">var</span> c <span class=\"token operator\">=</span> v<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> d <span class=\"token operator\">=</span> v<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> e <span class=\"token operator\">=</span> v<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> g <span class=\"token operator\">=</span> <span class=\"token known-class-name class-name\">Math</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">sqrt</span><span class=\"token punctuation\">(</span>c<span class=\"token operator\">*</span>c<span class=\"token operator\">+</span>d<span class=\"token operator\">*</span>d<span class=\"token operator\">+</span>e<span class=\"token operator\">*</span>e<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"718\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span>g<span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"719\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span>g <span class=\"token operator\">==</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"720\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"721\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"722\"></td><td><pre> v<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> v<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> v<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"723\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"724\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"725\"></td><td><pre> g <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token operator\">/</span>g<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"726\"></td><td><pre> v<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> c<span class=\"token operator\">*</span>g<span class=\"token punctuation\">;</span> v<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> d<span class=\"token operator\">*</span>g<span class=\"token punctuation\">;</span> v<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> e<span class=\"token operator\">*</span>g<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"727\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">this</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"728\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"729\"></td><td><pre></pre></td></tr><tr><td data-num=\"730\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"731\"></td><td><pre> * Constructor of Vector4</pre></td></tr><tr><td data-num=\"732\"></td><td><pre> * If opt_src is specified, new vector is initialized by opt_src.</pre></td></tr><tr><td data-num=\"733\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">opt_src</span> source vector(option)</pre></td></tr><tr><td data-num=\"734\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"735\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token function-variable function\"><span class=\"token maybe-class-name\">Vector4</span></span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">opt_src</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"736\"></td><td><pre> <span class=\"token keyword\">var</span> v <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Float32Array</span><span class=\"token punctuation\">(</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"737\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>opt_src <span class=\"token operator\">&&</span> <span class=\"token keyword\">typeof</span> opt_src <span class=\"token operator\">===</span> <span class=\"token string\">'object'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"738\"></td><td><pre> v<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> opt_src<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> v<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> opt_src<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> v<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> opt_src<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> v<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> opt_src<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"739\"></td><td><pre> <span class=\"token punctuation\">}</span> </pre></td></tr><tr><td data-num=\"740\"></td><td><pre> <span class=\"token keyword\">this</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">elements</span> <span class=\"token operator\">=</span> v<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"741\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"><span>cuon-utils.js</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\">// cuon-utils.js (c) 2012 kanda and matsuda</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"3\"></td><td><pre> * Create a program object and make current</pre></td></tr><tr><td data-num=\"4\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">gl</span> GL context</pre></td></tr><tr><td data-num=\"5\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">vshader</span> a vertex shader program (string)</pre></td></tr><tr><td data-num=\"6\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">fshader</span> a fragment shader program (string)</pre></td></tr><tr><td data-num=\"7\"></td><td><pre> * <span class=\"token keyword\">@return</span> true, if the program object was created and successfully made current </pre></td></tr><tr><td data-num=\"8\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">initShaders</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">gl<span class=\"token punctuation\">,</span> vshader<span class=\"token punctuation\">,</span> fshader</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token keyword\">var</span> program <span class=\"token operator\">=</span> <span class=\"token function\">createProgram</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">,</span> vshader<span class=\"token punctuation\">,</span> fshader<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>program<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to create program'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">useProgram</span><span class=\"token punctuation\">(</span>program<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token property-access\">program</span> <span class=\"token operator\">=</span> program<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre></pre></td></tr><tr><td data-num=\"19\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"20\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre></pre></td></tr><tr><td data-num=\"22\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"23\"></td><td><pre> * Create the linked program object</pre></td></tr><tr><td data-num=\"24\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">gl</span> GL context</pre></td></tr><tr><td data-num=\"25\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">vshader</span> a vertex shader program (string)</pre></td></tr><tr><td data-num=\"26\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">fshader</span> a fragment shader program (string)</pre></td></tr><tr><td data-num=\"27\"></td><td><pre> * <span class=\"token keyword\">@return</span> created program object, or null if the creation has failed</pre></td></tr><tr><td data-num=\"28\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"29\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">createProgram</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">gl<span class=\"token punctuation\">,</span> vshader<span class=\"token punctuation\">,</span> fshader</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"30\"></td><td><pre> <span class=\"token comment\">// Create shader object</span></pre></td></tr><tr><td data-num=\"31\"></td><td><pre> <span class=\"token keyword\">var</span> vertexShader <span class=\"token operator\">=</span> <span class=\"token function\">loadShader</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">,</span> gl<span class=\"token punctuation\">.</span><span class=\"token constant\">VERTEX_SHADER</span><span class=\"token punctuation\">,</span> vshader<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"32\"></td><td><pre> <span class=\"token keyword\">var</span> fragmentShader <span class=\"token operator\">=</span> <span class=\"token function\">loadShader</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">,</span> gl<span class=\"token punctuation\">.</span><span class=\"token constant\">FRAGMENT_SHADER</span><span class=\"token punctuation\">,</span> fshader<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"33\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>vertexShader <span class=\"token operator\">||</span> <span class=\"token operator\">!</span>fragmentShader<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"34\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"35\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"36\"></td><td><pre></pre></td></tr><tr><td data-num=\"37\"></td><td><pre> <span class=\"token comment\">// Create a program object</span></pre></td></tr><tr><td data-num=\"38\"></td><td><pre> <span class=\"token keyword\">var</span> program <span class=\"token operator\">=</span> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">createProgram</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"39\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>program<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"40\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"41\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"42\"></td><td><pre></pre></td></tr><tr><td data-num=\"43\"></td><td><pre> <span class=\"token comment\">// Attach the shader objects</span></pre></td></tr><tr><td data-num=\"44\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">attachShader</span><span class=\"token punctuation\">(</span>program<span class=\"token punctuation\">,</span> vertexShader<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"45\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">attachShader</span><span class=\"token punctuation\">(</span>program<span class=\"token punctuation\">,</span> fragmentShader<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"46\"></td><td><pre></pre></td></tr><tr><td data-num=\"47\"></td><td><pre> <span class=\"token comment\">// Link the program object</span></pre></td></tr><tr><td data-num=\"48\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">linkProgram</span><span class=\"token punctuation\">(</span>program<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"49\"></td><td><pre></pre></td></tr><tr><td data-num=\"50\"></td><td><pre> <span class=\"token comment\">// Check the result of linking</span></pre></td></tr><tr><td data-num=\"51\"></td><td><pre> <span class=\"token keyword\">var</span> linked <span class=\"token operator\">=</span> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getProgramParameter</span><span class=\"token punctuation\">(</span>program<span class=\"token punctuation\">,</span> gl<span class=\"token punctuation\">.</span><span class=\"token constant\">LINK_STATUS</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"52\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>linked<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"53\"></td><td><pre> <span class=\"token keyword\">var</span> error <span class=\"token operator\">=</span> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getProgramInfoLog</span><span class=\"token punctuation\">(</span>program<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"54\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to link program: '</span> <span class=\"token operator\">+</span> error<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"55\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">deleteProgram</span><span class=\"token punctuation\">(</span>program<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"56\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">deleteShader</span><span class=\"token punctuation\">(</span>fragmentShader<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"57\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">deleteShader</span><span class=\"token punctuation\">(</span>vertexShader<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"58\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"59\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"60\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> program<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"61\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"62\"></td><td><pre></pre></td></tr><tr><td data-num=\"63\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"64\"></td><td><pre> * Create a shader object</pre></td></tr><tr><td data-num=\"65\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">gl</span> GL context</pre></td></tr><tr><td data-num=\"66\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">type</span> the type of the shader object to be created</pre></td></tr><tr><td data-num=\"67\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">source</span> shader program (string)</pre></td></tr><tr><td data-num=\"68\"></td><td><pre> * <span class=\"token keyword\">@return</span> created shader object, or null if the creation has failed.</pre></td></tr><tr><td data-num=\"69\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"70\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">loadShader</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">gl<span class=\"token punctuation\">,</span> type<span class=\"token punctuation\">,</span> source</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"71\"></td><td><pre> <span class=\"token comment\">// Create shader object</span></pre></td></tr><tr><td data-num=\"72\"></td><td><pre> <span class=\"token keyword\">var</span> shader <span class=\"token operator\">=</span> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">createShader</span><span class=\"token punctuation\">(</span>type<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"73\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>shader <span class=\"token operator\">==</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"74\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'unable to create shader'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"75\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"76\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"77\"></td><td><pre></pre></td></tr><tr><td data-num=\"78\"></td><td><pre> <span class=\"token comment\">// Set the shader program</span></pre></td></tr><tr><td data-num=\"79\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">shaderSource</span><span class=\"token punctuation\">(</span>shader<span class=\"token punctuation\">,</span> source<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"80\"></td><td><pre></pre></td></tr><tr><td data-num=\"81\"></td><td><pre> <span class=\"token comment\">// Compile the shader</span></pre></td></tr><tr><td data-num=\"82\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">compileShader</span><span class=\"token punctuation\">(</span>shader<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"83\"></td><td><pre></pre></td></tr><tr><td data-num=\"84\"></td><td><pre> <span class=\"token comment\">// Check the result of compilation</span></pre></td></tr><tr><td data-num=\"85\"></td><td><pre> <span class=\"token keyword\">var</span> compiled <span class=\"token operator\">=</span> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getShaderParameter</span><span class=\"token punctuation\">(</span>shader<span class=\"token punctuation\">,</span> gl<span class=\"token punctuation\">.</span><span class=\"token constant\">COMPILE_STATUS</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"86\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>compiled<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"87\"></td><td><pre> <span class=\"token keyword\">var</span> error <span class=\"token operator\">=</span> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getShaderInfoLog</span><span class=\"token punctuation\">(</span>shader<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"88\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to compile shader: '</span> <span class=\"token operator\">+</span> error<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"89\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">deleteShader</span><span class=\"token punctuation\">(</span>shader<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"90\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"91\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"92\"></td><td><pre></pre></td></tr><tr><td data-num=\"93\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> shader<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"94\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"95\"></td><td><pre></pre></td></tr><tr><td data-num=\"96\"></td><td><pre><span class=\"token doc-comment comment\">/** </pre></td></tr><tr><td data-num=\"97\"></td><td><pre> * Initialize and get the rendering for WebGL</pre></td></tr><tr><td data-num=\"98\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">canvas</span> <cavnas> element</pre></td></tr><tr><td data-num=\"99\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token parameter\">opt_debug</span> flag to initialize the context for debugging</pre></td></tr><tr><td data-num=\"100\"></td><td><pre> * <span class=\"token keyword\">@return</span> the rendering context for WebGL</pre></td></tr><tr><td data-num=\"101\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"102\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">getWebGLContext</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">canvas<span class=\"token punctuation\">,</span> opt_debug</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"103\"></td><td><pre> <span class=\"token comment\">// Get the rendering context for WebGL</span></pre></td></tr><tr><td data-num=\"104\"></td><td><pre> <span class=\"token keyword\">var</span> gl <span class=\"token operator\">=</span> <span class=\"token maybe-class-name\">WebGLUtils</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">setupWebGL</span><span class=\"token punctuation\">(</span>canvas<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"105\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>gl<span class=\"token punctuation\">)</span> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"106\"></td><td><pre></pre></td></tr><tr><td data-num=\"107\"></td><td><pre> <span class=\"token comment\">// if opt_debug is explicitly false, create the context for debugging</span></pre></td></tr><tr><td data-num=\"108\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>arguments<span class=\"token punctuation\">.</span><span class=\"token property-access\">length</span> <span class=\"token operator\"><</span> <span class=\"token number\">2</span> <span class=\"token operator\">||</span> opt_debug<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"109\"></td><td><pre> gl <span class=\"token operator\">=</span> <span class=\"token maybe-class-name\">WebGLDebugUtils</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">makeDebugContext</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"110\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"111\"></td><td><pre></pre></td></tr><tr><td data-num=\"112\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> gl<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"113\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"><span>webgl-debug.js</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\">//Copyright (c) 2009 The Chromium Authors. All rights reserved.</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\">//Use of this source code is governed by a BSD-style license that can be</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token comment\">//found in the LICENSE file.</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token comment\">// Various functions for helping debug WebGL apps.</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token function-variable function\"><span class=\"token maybe-class-name\">WebGLDebugUtils</span></span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"10\"></td><td><pre> * Wrapped logging function.</pre></td></tr><tr><td data-num=\"11\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>string<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">msg</span> Message to log.</pre></td></tr><tr><td data-num=\"12\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token function-variable function\">log</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">msg</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">console</span> <span class=\"token operator\">&&</span> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">console</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">log</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span>msg<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre></pre></td></tr><tr><td data-num=\"19\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"20\"></td><td><pre> * Which arguements are enums.</pre></td></tr><tr><td data-num=\"21\"></td><td><pre> * <span class=\"token keyword\">@type</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>!Object<span class=\"token punctuation\">.</span><span class=\"token punctuation\"><</span>number<span class=\"token punctuation\">,</span> string<span class=\"token punctuation\">></span><span class=\"token punctuation\">}</span></span></pre></td></tr><tr><td data-num=\"22\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre><span class=\"token keyword\">var</span> glValidEnumContexts <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"24\"></td><td><pre></pre></td></tr><tr><td data-num=\"25\"></td><td><pre> <span class=\"token comment\">// Generic setters and getters</span></pre></td></tr><tr><td data-num=\"26\"></td><td><pre></pre></td></tr><tr><td data-num=\"27\"></td><td><pre> <span class=\"token string-property property\">'enable'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"28\"></td><td><pre> <span class=\"token string-property property\">'disable'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"29\"></td><td><pre> <span class=\"token string-property property\">'getParameter'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"30\"></td><td><pre></pre></td></tr><tr><td data-num=\"31\"></td><td><pre> <span class=\"token comment\">// Rendering</span></pre></td></tr><tr><td data-num=\"32\"></td><td><pre></pre></td></tr><tr><td data-num=\"33\"></td><td><pre> <span class=\"token string-property property\">'drawArrays'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"34\"></td><td><pre> <span class=\"token string-property property\">'drawElements'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"35\"></td><td><pre></pre></td></tr><tr><td data-num=\"36\"></td><td><pre> <span class=\"token comment\">// Shaders</span></pre></td></tr><tr><td data-num=\"37\"></td><td><pre></pre></td></tr><tr><td data-num=\"38\"></td><td><pre> <span class=\"token string-property property\">'createShader'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"39\"></td><td><pre> <span class=\"token string-property property\">'getShaderParameter'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"40\"></td><td><pre> <span class=\"token string-property property\">'getProgramParameter'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"41\"></td><td><pre></pre></td></tr><tr><td data-num=\"42\"></td><td><pre> <span class=\"token comment\">// Vertex attributes</span></pre></td></tr><tr><td data-num=\"43\"></td><td><pre></pre></td></tr><tr><td data-num=\"44\"></td><td><pre> <span class=\"token string-property property\">'getVertexAttrib'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"45\"></td><td><pre> <span class=\"token string-property property\">'vertexAttribPointer'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">2</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"46\"></td><td><pre></pre></td></tr><tr><td data-num=\"47\"></td><td><pre> <span class=\"token comment\">// Textures</span></pre></td></tr><tr><td data-num=\"48\"></td><td><pre></pre></td></tr><tr><td data-num=\"49\"></td><td><pre> <span class=\"token string-property property\">'bindTexture'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"50\"></td><td><pre> <span class=\"token string-property property\">'activeTexture'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"51\"></td><td><pre> <span class=\"token string-property property\">'getTexParameter'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"52\"></td><td><pre> <span class=\"token string-property property\">'texParameterf'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"53\"></td><td><pre> <span class=\"token string-property property\">'texParameteri'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"54\"></td><td><pre> <span class=\"token string-property property\">'texImage2D'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">6</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">7</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"55\"></td><td><pre> <span class=\"token string-property property\">'texSubImage2D'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">6</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">7</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"56\"></td><td><pre> <span class=\"token string-property property\">'copyTexImage2D'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"57\"></td><td><pre> <span class=\"token string-property property\">'copyTexSubImage2D'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"58\"></td><td><pre> <span class=\"token string-property property\">'generateMipmap'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"59\"></td><td><pre></pre></td></tr><tr><td data-num=\"60\"></td><td><pre> <span class=\"token comment\">// Buffer objects</span></pre></td></tr><tr><td data-num=\"61\"></td><td><pre></pre></td></tr><tr><td data-num=\"62\"></td><td><pre> <span class=\"token string-property property\">'bindBuffer'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"63\"></td><td><pre> <span class=\"token string-property property\">'bufferData'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"64\"></td><td><pre> <span class=\"token string-property property\">'bufferSubData'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"65\"></td><td><pre> <span class=\"token string-property property\">'getBufferParameter'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"66\"></td><td><pre></pre></td></tr><tr><td data-num=\"67\"></td><td><pre> <span class=\"token comment\">// Renderbuffers and framebuffers</span></pre></td></tr><tr><td data-num=\"68\"></td><td><pre></pre></td></tr><tr><td data-num=\"69\"></td><td><pre> <span class=\"token string-property property\">'pixelStorei'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"70\"></td><td><pre> <span class=\"token string-property property\">'readPixels'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">4</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">5</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"71\"></td><td><pre> <span class=\"token string-property property\">'bindRenderbuffer'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"72\"></td><td><pre> <span class=\"token string-property property\">'bindFramebuffer'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"73\"></td><td><pre> <span class=\"token string-property property\">'checkFramebufferStatus'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"74\"></td><td><pre> <span class=\"token string-property property\">'framebufferRenderbuffer'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"75\"></td><td><pre> <span class=\"token string-property property\">'framebufferTexture2D'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"76\"></td><td><pre> <span class=\"token string-property property\">'getFramebufferAttachmentParameter'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"77\"></td><td><pre> <span class=\"token string-property property\">'getRenderbufferParameter'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"78\"></td><td><pre> <span class=\"token string-property property\">'renderbufferStorage'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"79\"></td><td><pre></pre></td></tr><tr><td data-num=\"80\"></td><td><pre> <span class=\"token comment\">// Frame buffer operations (clear, blend, depth test, stencil)</span></pre></td></tr><tr><td data-num=\"81\"></td><td><pre></pre></td></tr><tr><td data-num=\"82\"></td><td><pre> <span class=\"token string-property property\">'clear'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"83\"></td><td><pre> <span class=\"token string-property property\">'depthFunc'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"84\"></td><td><pre> <span class=\"token string-property property\">'blendFunc'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"85\"></td><td><pre> <span class=\"token string-property property\">'blendFuncSeparate'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"86\"></td><td><pre> <span class=\"token string-property property\">'blendEquation'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"87\"></td><td><pre> <span class=\"token string-property property\">'blendEquationSeparate'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"88\"></td><td><pre> <span class=\"token string-property property\">'stencilFunc'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"89\"></td><td><pre> <span class=\"token string-property property\">'stencilFuncSeparate'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"90\"></td><td><pre> <span class=\"token string-property property\">'stencilMaskSeparate'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"91\"></td><td><pre> <span class=\"token string-property property\">'stencilOp'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"92\"></td><td><pre> <span class=\"token string-property property\">'stencilOpSeparate'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"93\"></td><td><pre></pre></td></tr><tr><td data-num=\"94\"></td><td><pre> <span class=\"token comment\">// Culling</span></pre></td></tr><tr><td data-num=\"95\"></td><td><pre></pre></td></tr><tr><td data-num=\"96\"></td><td><pre> <span class=\"token string-property property\">'cullFace'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"97\"></td><td><pre> <span class=\"token string-property property\">'frontFace'</span><span class=\"token operator\">:</span> <span class=\"token punctuation\">{</span> <span class=\"token number\">0</span><span class=\"token operator\">:</span><span class=\"token boolean\">true</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"98\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"99\"></td><td><pre></pre></td></tr><tr><td data-num=\"100\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"101\"></td><td><pre> * Map of numbers to names.</pre></td></tr><tr><td data-num=\"102\"></td><td><pre> * <span class=\"token keyword\">@type</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>Object<span class=\"token punctuation\">}</span></span></pre></td></tr><tr><td data-num=\"103\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"104\"></td><td><pre><span class=\"token keyword\">var</span> glEnums <span class=\"token operator\">=</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"105\"></td><td><pre></pre></td></tr><tr><td data-num=\"106\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"107\"></td><td><pre> * Initializes this module. Safe to call more than once.</pre></td></tr><tr><td data-num=\"108\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>!WebGLRenderingContext<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">ctx</span> A WebGL context. If</pre></td></tr><tr><td data-num=\"109\"></td><td><pre> * you have more than one context it doesn't matter which one</pre></td></tr><tr><td data-num=\"110\"></td><td><pre> * you pass in, it is only used to pull out constants.</pre></td></tr><tr><td data-num=\"111\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"112\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">init</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">ctx</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"113\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>glEnums <span class=\"token operator\">==</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"114\"></td><td><pre> glEnums <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"115\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> propertyName <span class=\"token keyword\">in</span> ctx<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"116\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">typeof</span> ctx<span class=\"token punctuation\">[</span>propertyName<span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">'number'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"117\"></td><td><pre> glEnums<span class=\"token punctuation\">[</span>ctx<span class=\"token punctuation\">[</span>propertyName<span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> propertyName<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"118\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"119\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"120\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"121\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"122\"></td><td><pre></pre></td></tr><tr><td data-num=\"123\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"124\"></td><td><pre> * Checks the utils have been initialized.</pre></td></tr><tr><td data-num=\"125\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"126\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">checkInit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"127\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>glEnums <span class=\"token operator\">==</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"128\"></td><td><pre> <span class=\"token keyword control-flow\">throw</span> <span class=\"token string\">'WebGLDebugUtils.init(ctx) not called'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"129\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"130\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"131\"></td><td><pre></pre></td></tr><tr><td data-num=\"132\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"133\"></td><td><pre> * Returns true or false if value matches any WebGL enum</pre></td></tr><tr><td data-num=\"134\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span><span class=\"token operator\">*</span><span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">value</span> Value to check if it might be an enum.</pre></td></tr><tr><td data-num=\"135\"></td><td><pre> * <span class=\"token keyword\">@return</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>boolean<span class=\"token punctuation\">}</span></span> True if value matches one of the WebGL defined enums</pre></td></tr><tr><td data-num=\"136\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"137\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">mightBeEnum</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">value</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"138\"></td><td><pre> <span class=\"token function\">checkInit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"139\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token punctuation\">(</span>glEnums<span class=\"token punctuation\">[</span>value<span class=\"token punctuation\">]</span> <span class=\"token operator\">!==</span> <span class=\"token keyword nil\">undefined</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"140\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"141\"></td><td><pre></pre></td></tr><tr><td data-num=\"142\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"143\"></td><td><pre> * Gets an string version of an WebGL enum.</pre></td></tr><tr><td data-num=\"144\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"145\"></td><td><pre> * Example:</pre></td></tr><tr><td data-num=\"146\"></td><td><pre> * var str = WebGLDebugUtil.glEnumToString(ctx.getError());</pre></td></tr><tr><td data-num=\"147\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"148\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>number<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">value</span> Value to return an enum for</pre></td></tr><tr><td data-num=\"149\"></td><td><pre> * <span class=\"token keyword\">@return</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>string<span class=\"token punctuation\">}</span></span> The string version of the enum.</pre></td></tr><tr><td data-num=\"150\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"151\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">glEnumToString</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">value</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"152\"></td><td><pre> <span class=\"token function\">checkInit</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"153\"></td><td><pre> <span class=\"token keyword\">var</span> name <span class=\"token operator\">=</span> glEnums<span class=\"token punctuation\">[</span>value<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"154\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token punctuation\">(</span>name <span class=\"token operator\">!==</span> <span class=\"token keyword nil\">undefined</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">?</span> name <span class=\"token operator\">:</span></pre></td></tr><tr><td data-num=\"155\"></td><td><pre> <span class=\"token punctuation\">(</span><span class=\"token string\">\"*UNKNOWN WebGL ENUM (0x\"</span> <span class=\"token operator\">+</span> value<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">toString</span><span class=\"token punctuation\">(</span><span class=\"token number\">16</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">+</span> <span class=\"token string\">\")\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"156\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"157\"></td><td><pre></pre></td></tr><tr><td data-num=\"158\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"159\"></td><td><pre> * Returns the string version of a WebGL argument.</pre></td></tr><tr><td data-num=\"160\"></td><td><pre> * Attempts to convert enum arguments to strings.</pre></td></tr><tr><td data-num=\"161\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>string<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">functionName</span> the name of the WebGL function.</pre></td></tr><tr><td data-num=\"162\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>number<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">argumentIndx</span> the index of the argument.</pre></td></tr><tr><td data-num=\"163\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span><span class=\"token operator\">*</span><span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">value</span> The value of the argument.</pre></td></tr><tr><td data-num=\"164\"></td><td><pre> * <span class=\"token keyword\">@return</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>string<span class=\"token punctuation\">}</span></span> The value as a string.</pre></td></tr><tr><td data-num=\"165\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"166\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">glFunctionArgToString</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">functionName<span class=\"token punctuation\">,</span> argumentIndex<span class=\"token punctuation\">,</span> value</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"167\"></td><td><pre> <span class=\"token keyword\">var</span> funcInfo <span class=\"token operator\">=</span> glValidEnumContexts<span class=\"token punctuation\">[</span>functionName<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"168\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>funcInfo <span class=\"token operator\">!==</span> <span class=\"token keyword nil\">undefined</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"169\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>funcInfo<span class=\"token punctuation\">[</span>argumentIndex<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"170\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token function\">glEnumToString</span><span class=\"token punctuation\">(</span>value<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"171\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"172\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"173\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> value<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">toString</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"174\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"175\"></td><td><pre></pre></td></tr><tr><td data-num=\"176\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"177\"></td><td><pre> * Given a WebGL context returns a wrapped context that calls</pre></td></tr><tr><td data-num=\"178\"></td><td><pre> * gl.getError after every command and calls a function if the</pre></td></tr><tr><td data-num=\"179\"></td><td><pre> * result is not gl.NO_ERROR.</pre></td></tr><tr><td data-num=\"180\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"181\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>!WebGLRenderingContext<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">ctx</span> The webgl context to</pre></td></tr><tr><td data-num=\"182\"></td><td><pre> * wrap.</pre></td></tr><tr><td data-num=\"183\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>!<span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">,</span> funcName<span class=\"token punctuation\">,</span> args<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token keyword\">void</span><span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">opt_onErrorFunc</span></pre></td></tr><tr><td data-num=\"184\"></td><td><pre> * The function to call when gl.getError returns an</pre></td></tr><tr><td data-num=\"185\"></td><td><pre> * error. If not specified the default function calls</pre></td></tr><tr><td data-num=\"186\"></td><td><pre> * console.log with a message.</pre></td></tr><tr><td data-num=\"187\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"188\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">makeDebugContext</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">ctx<span class=\"token punctuation\">,</span> opt_onErrorFunc</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"189\"></td><td><pre> <span class=\"token function\">init</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"190\"></td><td><pre> opt_onErrorFunc <span class=\"token operator\">=</span> opt_onErrorFunc <span class=\"token operator\">||</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">err<span class=\"token punctuation\">,</span> functionName<span class=\"token punctuation\">,</span> args</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"191\"></td><td><pre> <span class=\"token comment\">// apparently we can't do args.join(\",\");</span></pre></td></tr><tr><td data-num=\"192\"></td><td><pre> <span class=\"token keyword\">var</span> argStr <span class=\"token operator\">=</span> <span class=\"token string\">\"\"</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"193\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> ii <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> ii <span class=\"token operator\"><</span> args<span class=\"token punctuation\">.</span><span class=\"token property-access\">length</span><span class=\"token punctuation\">;</span> <span class=\"token operator\">++</span>ii<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"194\"></td><td><pre> argStr <span class=\"token operator\">+=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span>ii <span class=\"token operator\">==</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">?</span> <span class=\"token string\">''</span> <span class=\"token operator\">:</span> <span class=\"token string\">', '</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"195\"></td><td><pre> <span class=\"token function\">glFunctionArgToString</span><span class=\"token punctuation\">(</span>functionName<span class=\"token punctuation\">,</span> ii<span class=\"token punctuation\">,</span> args<span class=\"token punctuation\">[</span>ii<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"196\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"197\"></td><td><pre> <span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"WebGL error \"</span><span class=\"token operator\">+</span> <span class=\"token function\">glEnumToString</span><span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span> <span class=\"token operator\">+</span> <span class=\"token string\">\" in \"</span><span class=\"token operator\">+</span> functionName <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"198\"></td><td><pre> <span class=\"token string\">\"(\"</span> <span class=\"token operator\">+</span> argStr <span class=\"token operator\">+</span> <span class=\"token string\">\")\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"199\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"200\"></td><td><pre></pre></td></tr><tr><td data-num=\"201\"></td><td><pre> <span class=\"token comment\">// Holds booleans for each GL error so after we get the error ourselves</span></pre></td></tr><tr><td data-num=\"202\"></td><td><pre> <span class=\"token comment\">// we can still return it to the client app.</span></pre></td></tr><tr><td data-num=\"203\"></td><td><pre> <span class=\"token keyword\">var</span> glErrorShadow <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"204\"></td><td><pre></pre></td></tr><tr><td data-num=\"205\"></td><td><pre> <span class=\"token comment\">// Makes a function that calls a WebGL function and then calls getError.</span></pre></td></tr><tr><td data-num=\"206\"></td><td><pre> <span class=\"token keyword\">function</span> <span class=\"token function\">makeErrorWrapper</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">ctx<span class=\"token punctuation\">,</span> functionName</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"207\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"208\"></td><td><pre> <span class=\"token keyword\">var</span> result <span class=\"token operator\">=</span> ctx<span class=\"token punctuation\">[</span>functionName<span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">apply</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">,</span> arguments<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"209\"></td><td><pre> <span class=\"token keyword\">var</span> err <span class=\"token operator\">=</span> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getError</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"210\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>err <span class=\"token operator\">!=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"211\"></td><td><pre> glErrorShadow<span class=\"token punctuation\">[</span>err<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"212\"></td><td><pre> <span class=\"token function\">opt_onErrorFunc</span><span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">,</span> functionName<span class=\"token punctuation\">,</span> arguments<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"213\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"214\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> result<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"215\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"216\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"217\"></td><td><pre></pre></td></tr><tr><td data-num=\"218\"></td><td><pre> <span class=\"token comment\">// Make a an object that has a copy of every property of the WebGL context</span></pre></td></tr><tr><td data-num=\"219\"></td><td><pre> <span class=\"token comment\">// but wraps all functions.</span></pre></td></tr><tr><td data-num=\"220\"></td><td><pre> <span class=\"token keyword\">var</span> wrapper <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"221\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> propertyName <span class=\"token keyword\">in</span> ctx<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"222\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">typeof</span> ctx<span class=\"token punctuation\">[</span>propertyName<span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">'function'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"223\"></td><td><pre> wrapper<span class=\"token punctuation\">[</span>propertyName<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">makeErrorWrapper</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">,</span> propertyName<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"224\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"225\"></td><td><pre> wrapper<span class=\"token punctuation\">[</span>propertyName<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> ctx<span class=\"token punctuation\">[</span>propertyName<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"226\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"227\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"228\"></td><td><pre></pre></td></tr><tr><td data-num=\"229\"></td><td><pre> <span class=\"token comment\">// Override the getError function with one that returns our saved results.</span></pre></td></tr><tr><td data-num=\"230\"></td><td><pre> wrapper<span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">getError</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"231\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> err <span class=\"token keyword\">in</span> glErrorShadow<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"232\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>glErrorShadow<span class=\"token punctuation\">[</span>err<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"233\"></td><td><pre> glErrorShadow<span class=\"token punctuation\">[</span>err<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"234\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> err<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"235\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"236\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"237\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">NO_ERROR</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"238\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"239\"></td><td><pre></pre></td></tr><tr><td data-num=\"240\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> wrapper<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"241\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"242\"></td><td><pre></pre></td></tr><tr><td data-num=\"243\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">resetToInitialState</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">ctx</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"244\"></td><td><pre> <span class=\"token keyword\">var</span> numAttribs <span class=\"token operator\">=</span> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getParameter</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">MAX_VERTEX_ATTRIBS</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"245\"></td><td><pre> <span class=\"token keyword\">var</span> tmp <span class=\"token operator\">=</span> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">createBuffer</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"246\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">bindBuffer</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">ARRAY_BUFFER</span><span class=\"token punctuation\">,</span> tmp<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"247\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> ii <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> ii <span class=\"token operator\"><</span> numAttribs<span class=\"token punctuation\">;</span> <span class=\"token operator\">++</span>ii<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"248\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">disableVertexAttribArray</span><span class=\"token punctuation\">(</span>ii<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"249\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">vertexAttribPointer</span><span class=\"token punctuation\">(</span>ii<span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">,</span> ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">FLOAT</span><span class=\"token punctuation\">,</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"250\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">vertexAttrib1f</span><span class=\"token punctuation\">(</span>ii<span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"251\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"252\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">deleteBuffer</span><span class=\"token punctuation\">(</span>tmp<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"253\"></td><td><pre></pre></td></tr><tr><td data-num=\"254\"></td><td><pre> <span class=\"token keyword\">var</span> numTextureUnits <span class=\"token operator\">=</span> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getParameter</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">MAX_TEXTURE_IMAGE_UNITS</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"255\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> ii <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> ii <span class=\"token operator\"><</span> numTextureUnits<span class=\"token punctuation\">;</span> <span class=\"token operator\">++</span>ii<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"256\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">activeTexture</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">TEXTURE0</span> <span class=\"token operator\">+</span> ii<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"257\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">bindTexture</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">TEXTURE_CUBE_MAP</span><span class=\"token punctuation\">,</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"258\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">bindTexture</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">TEXTURE_2D</span><span class=\"token punctuation\">,</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"259\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"260\"></td><td><pre></pre></td></tr><tr><td data-num=\"261\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">activeTexture</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">TEXTURE0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"262\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">useProgram</span><span class=\"token punctuation\">(</span><span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"263\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">bindBuffer</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">ARRAY_BUFFER</span><span class=\"token punctuation\">,</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"264\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">bindBuffer</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">ELEMENT_ARRAY_BUFFER</span><span class=\"token punctuation\">,</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"265\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">bindFramebuffer</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">FRAMEBUFFER</span><span class=\"token punctuation\">,</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"266\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">bindRenderbuffer</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">RENDERBUFFER</span><span class=\"token punctuation\">,</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"267\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">disable</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">BLEND</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"268\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">disable</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">CULL_FACE</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"269\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">disable</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">DEPTH_TEST</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"270\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">disable</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">DITHER</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"271\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">disable</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">SCISSOR_TEST</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"272\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">blendColor</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"273\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">blendEquation</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">FUNC_ADD</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"274\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">blendFunc</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">ONE</span><span class=\"token punctuation\">,</span> ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">ZERO</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"275\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">clearColor</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"276\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">clearDepth</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"277\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">clearStencil</span><span class=\"token punctuation\">(</span><span class=\"token operator\">-</span><span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"278\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">colorMask</span><span class=\"token punctuation\">(</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">,</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"279\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">cullFace</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">BACK</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"280\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">depthFunc</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">LESS</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"281\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">depthMask</span><span class=\"token punctuation\">(</span><span class=\"token boolean\">true</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"282\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">depthRange</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"283\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">frontFace</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">CCW</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"284\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">hint</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">GENERATE_MIPMAP_HINT</span><span class=\"token punctuation\">,</span> ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">DONT_CARE</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"285\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">lineWidth</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"286\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">pixelStorei</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">PACK_ALIGNMENT</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"287\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">pixelStorei</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">UNPACK_ALIGNMENT</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"288\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">pixelStorei</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">UNPACK_FLIP_Y_WEBGL</span><span class=\"token punctuation\">,</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"289\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">pixelStorei</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">UNPACK_PREMULTIPLY_ALPHA_WEBGL</span><span class=\"token punctuation\">,</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"290\"></td><td><pre> <span class=\"token comment\">// TODO: Delete this IF.</span></pre></td></tr><tr><td data-num=\"291\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">UNPACK_COLORSPACE_CONVERSION_WEBGL</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"292\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">pixelStorei</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">UNPACK_COLORSPACE_CONVERSION_WEBGL</span><span class=\"token punctuation\">,</span> ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">BROWSER_DEFAULT_WEBGL</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"293\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"294\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">polygonOffset</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"295\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">sampleCoverage</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"296\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">scissor</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> ctx<span class=\"token punctuation\">.</span><span class=\"token property-access\">canvas</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">width</span><span class=\"token punctuation\">,</span> ctx<span class=\"token punctuation\">.</span><span class=\"token property-access\">canvas</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">height</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"297\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">stencilFunc</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">ALWAYS</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0xFFFFFFFF</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"298\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">stencilMask</span><span class=\"token punctuation\">(</span><span class=\"token number\">0xFFFFFFFF</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"299\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">stencilOp</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">KEEP</span><span class=\"token punctuation\">,</span> ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">KEEP</span><span class=\"token punctuation\">,</span> ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">KEEP</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"300\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">viewport</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> ctx<span class=\"token punctuation\">.</span><span class=\"token property-access\">canvas</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">clientWidth</span><span class=\"token punctuation\">,</span> ctx<span class=\"token punctuation\">.</span><span class=\"token property-access\">canvas</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">clientHeight</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"301\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">clear</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">COLOR_BUFFER_BIT</span> <span class=\"token operator\">|</span> ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">DEPTH_BUFFER_BIT</span> <span class=\"token operator\">|</span> ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">STENCIL_BUFFER_BIT</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"302\"></td><td><pre></pre></td></tr><tr><td data-num=\"303\"></td><td><pre> <span class=\"token comment\">// TODO: This should NOT be needed but Firefox fails with 'hint'</span></pre></td></tr><tr><td data-num=\"304\"></td><td><pre> <span class=\"token keyword control-flow\">while</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getError</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"305\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"306\"></td><td><pre></pre></td></tr><tr><td data-num=\"307\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">makeLostContextSimulatingContext</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">ctx</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"308\"></td><td><pre> <span class=\"token keyword\">var</span> wrapper_ <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"309\"></td><td><pre> <span class=\"token keyword\">var</span> contextId_ <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"310\"></td><td><pre> <span class=\"token keyword\">var</span> contextLost_ <span class=\"token operator\">=</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"311\"></td><td><pre> <span class=\"token keyword\">var</span> resourceId_ <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"312\"></td><td><pre> <span class=\"token keyword\">var</span> resourceDb_ <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"313\"></td><td><pre> <span class=\"token keyword\">var</span> onLost_ <span class=\"token operator\">=</span> <span class=\"token keyword nil\">undefined</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"314\"></td><td><pre> <span class=\"token keyword\">var</span> onRestored_ <span class=\"token operator\">=</span> <span class=\"token keyword nil\">undefined</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"315\"></td><td><pre> <span class=\"token keyword\">var</span> nextOnRestored_ <span class=\"token operator\">=</span> <span class=\"token keyword nil\">undefined</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"316\"></td><td><pre></pre></td></tr><tr><td data-num=\"317\"></td><td><pre> <span class=\"token comment\">// Holds booleans for each GL error so can simulate errors.</span></pre></td></tr><tr><td data-num=\"318\"></td><td><pre> <span class=\"token keyword\">var</span> glErrorShadow_ <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"319\"></td><td><pre></pre></td></tr><tr><td data-num=\"320\"></td><td><pre> <span class=\"token keyword\">function</span> <span class=\"token function\">isWebGLObject</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">obj</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"321\"></td><td><pre> <span class=\"token comment\">//return false;</span></pre></td></tr><tr><td data-num=\"322\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token punctuation\">(</span>obj <span class=\"token keyword\">instanceof</span> <span class=\"token class-name\">WebGLBuffer</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"323\"></td><td><pre> obj <span class=\"token keyword\">instanceof</span> <span class=\"token class-name\">WebGLFramebuffer</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"324\"></td><td><pre> obj <span class=\"token keyword\">instanceof</span> <span class=\"token class-name\">WebGLProgram</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"325\"></td><td><pre> obj <span class=\"token keyword\">instanceof</span> <span class=\"token class-name\">WebGLRenderbuffer</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"326\"></td><td><pre> obj <span class=\"token keyword\">instanceof</span> <span class=\"token class-name\">WebGLShader</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"327\"></td><td><pre> obj <span class=\"token keyword\">instanceof</span> <span class=\"token class-name\">WebGLTexture</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"328\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"329\"></td><td><pre></pre></td></tr><tr><td data-num=\"330\"></td><td><pre> <span class=\"token keyword\">function</span> <span class=\"token function\">checkResources</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">args</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"331\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> ii <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> ii <span class=\"token operator\"><</span> args<span class=\"token punctuation\">.</span><span class=\"token property-access\">length</span><span class=\"token punctuation\">;</span> <span class=\"token operator\">++</span>ii<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"332\"></td><td><pre> <span class=\"token keyword\">var</span> arg <span class=\"token operator\">=</span> args<span class=\"token punctuation\">[</span>ii<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"333\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token function\">isWebGLObject</span><span class=\"token punctuation\">(</span>arg<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"334\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> arg<span class=\"token punctuation\">.</span><span class=\"token property-access\">__webglDebugContextLostId__</span> <span class=\"token operator\">==</span> contextId_<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"335\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"336\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"337\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"338\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"339\"></td><td><pre></pre></td></tr><tr><td data-num=\"340\"></td><td><pre> <span class=\"token keyword\">function</span> <span class=\"token function\">clearErrors</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"341\"></td><td><pre> <span class=\"token keyword\">var</span> k <span class=\"token operator\">=</span> <span class=\"token known-class-name class-name\">Object</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">keys</span><span class=\"token punctuation\">(</span>glErrorShadow_<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"342\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> ii <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> ii <span class=\"token operator\"><</span> k<span class=\"token punctuation\">.</span><span class=\"token property-access\">length</span><span class=\"token punctuation\">;</span> <span class=\"token operator\">++</span>ii<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"343\"></td><td><pre> <span class=\"token keyword\">delete</span> glErrorShdow_<span class=\"token punctuation\">[</span>k<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"344\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"345\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"346\"></td><td><pre></pre></td></tr><tr><td data-num=\"347\"></td><td><pre> <span class=\"token comment\">// Makes a function that simulates WebGL when out of context.</span></pre></td></tr><tr><td data-num=\"348\"></td><td><pre> <span class=\"token keyword\">function</span> <span class=\"token function\">makeLostContextWrapper</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">ctx<span class=\"token punctuation\">,</span> functionName</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"349\"></td><td><pre> <span class=\"token keyword\">var</span> f <span class=\"token operator\">=</span> ctx<span class=\"token punctuation\">[</span>functionName<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"350\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"351\"></td><td><pre> <span class=\"token comment\">// Only call the functions if the context is not lost.</span></pre></td></tr><tr><td data-num=\"352\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>contextLost_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"353\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span><span class=\"token function\">checkResources</span><span class=\"token punctuation\">(</span>arguments<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"354\"></td><td><pre> glErrorShadow_<span class=\"token punctuation\">[</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">INVALID_OPERATION</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"355\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"356\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"357\"></td><td><pre> <span class=\"token keyword\">var</span> result <span class=\"token operator\">=</span> f<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">apply</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">,</span> arguments<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"358\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> result<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"359\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"360\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"361\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"362\"></td><td><pre></pre></td></tr><tr><td data-num=\"363\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> propertyName <span class=\"token keyword\">in</span> ctx<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"364\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">typeof</span> ctx<span class=\"token punctuation\">[</span>propertyName<span class=\"token punctuation\">]</span> <span class=\"token operator\">==</span> <span class=\"token string\">'function'</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"365\"></td><td><pre> wrapper_<span class=\"token punctuation\">[</span>propertyName<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token function\">makeLostContextWrapper</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">,</span> propertyName<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"366\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"367\"></td><td><pre> wrapper_<span class=\"token punctuation\">[</span>propertyName<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> ctx<span class=\"token punctuation\">[</span>propertyName<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"368\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"369\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"370\"></td><td><pre></pre></td></tr><tr><td data-num=\"371\"></td><td><pre> <span class=\"token keyword\">function</span> <span class=\"token function\">makeWebGLContextEvent</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">statusMessage</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"372\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token punctuation\">{</span><span class=\"token literal-property property\">statusMessage</span><span class=\"token operator\">:</span> statusMessage<span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"373\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"374\"></td><td><pre></pre></td></tr><tr><td data-num=\"375\"></td><td><pre> <span class=\"token keyword\">function</span> <span class=\"token function\">freeResources</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"376\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> ii <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> ii <span class=\"token operator\"><</span> resourceDb_<span class=\"token punctuation\">.</span><span class=\"token property-access\">length</span><span class=\"token punctuation\">;</span> <span class=\"token operator\">++</span>ii<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"377\"></td><td><pre> <span class=\"token keyword\">var</span> resource <span class=\"token operator\">=</span> resourceDb_<span class=\"token punctuation\">[</span>ii<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"378\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>resource <span class=\"token keyword\">instanceof</span> <span class=\"token class-name\">WebGLBuffer</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"379\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">deleteBuffer</span><span class=\"token punctuation\">(</span>resource<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"380\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>resource <span class=\"token keyword\">instanceof</span> <span class=\"token class-name\">WebctxFramebuffer</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"381\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">deleteFramebuffer</span><span class=\"token punctuation\">(</span>resource<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"382\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>resource <span class=\"token keyword\">instanceof</span> <span class=\"token class-name\">WebctxProgram</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"383\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">deleteProgram</span><span class=\"token punctuation\">(</span>resource<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"384\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>resource <span class=\"token keyword\">instanceof</span> <span class=\"token class-name\">WebctxRenderbuffer</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"385\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">deleteRenderbuffer</span><span class=\"token punctuation\">(</span>resource<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"386\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>resource <span class=\"token keyword\">instanceof</span> <span class=\"token class-name\">WebctxShader</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"387\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">deleteShader</span><span class=\"token punctuation\">(</span>resource<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"388\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>resource <span class=\"token keyword\">instanceof</span> <span class=\"token class-name\">WebctxTexture</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"389\"></td><td><pre> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">deleteTexture</span><span class=\"token punctuation\">(</span>resource<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"390\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"391\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"392\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"393\"></td><td><pre></pre></td></tr><tr><td data-num=\"394\"></td><td><pre> wrapper_<span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">loseContext</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"395\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>contextLost_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"396\"></td><td><pre> contextLost_ <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"397\"></td><td><pre> <span class=\"token operator\">++</span>contextId_<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"398\"></td><td><pre> <span class=\"token keyword control-flow\">while</span> <span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getError</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"399\"></td><td><pre> <span class=\"token function\">clearErrors</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"400\"></td><td><pre> glErrorShadow_<span class=\"token punctuation\">[</span>ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">CONTEXT_LOST_WEBGL</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"401\"></td><td><pre> <span class=\"token function\">setTimeout</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"402\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>onLost_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"403\"></td><td><pre> <span class=\"token function\">onLost_</span><span class=\"token punctuation\">(</span><span class=\"token function\">makeWebGLContextEvent</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"context lost\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"404\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"405\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"406\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"407\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"408\"></td><td><pre></pre></td></tr><tr><td data-num=\"409\"></td><td><pre> wrapper_<span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">restoreContext</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"410\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>contextLost_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"411\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>onRestored_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"412\"></td><td><pre> <span class=\"token function\">setTimeout</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"413\"></td><td><pre> <span class=\"token function\">freeResources</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"414\"></td><td><pre> <span class=\"token function\">resetToInitialState</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"415\"></td><td><pre> contextLost_ <span class=\"token operator\">=</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"416\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>onRestored_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"417\"></td><td><pre> <span class=\"token keyword\">var</span> callback <span class=\"token operator\">=</span> onRestored_<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"418\"></td><td><pre> onRestored_ <span class=\"token operator\">=</span> nextOnRestored_<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"419\"></td><td><pre> nextOnRestored_ <span class=\"token operator\">=</span> <span class=\"token keyword nil\">undefined</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"420\"></td><td><pre> <span class=\"token function\">callback</span><span class=\"token punctuation\">(</span><span class=\"token function\">makeWebGLContextEvent</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"context restored\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"421\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"422\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"423\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"424\"></td><td><pre> <span class=\"token keyword control-flow\">throw</span> <span class=\"token string\">\"You can not restore the context without a listener\"</span></pre></td></tr><tr><td data-num=\"425\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"426\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"427\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"428\"></td><td><pre></pre></td></tr><tr><td data-num=\"429\"></td><td><pre> <span class=\"token comment\">// Wrap a few functions specially.</span></pre></td></tr><tr><td data-num=\"430\"></td><td><pre> wrapper_<span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">getError</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"431\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>contextLost_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"432\"></td><td><pre> <span class=\"token keyword\">var</span> err<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"433\"></td><td><pre> <span class=\"token keyword control-flow\">while</span> <span class=\"token punctuation\">(</span>err <span class=\"token operator\">=</span> ctx<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getError</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"434\"></td><td><pre> glErrorShadow_<span class=\"token punctuation\">[</span>err<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"435\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"436\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"437\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> err <span class=\"token keyword\">in</span> glErrorShadow_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"438\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>glErrorShadow_<span class=\"token punctuation\">[</span>err<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"439\"></td><td><pre> <span class=\"token keyword\">delete</span> glErrorShadow_<span class=\"token punctuation\">[</span>err<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"440\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> err<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"441\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"442\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"443\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">NO_ERROR</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"444\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"445\"></td><td><pre></pre></td></tr><tr><td data-num=\"446\"></td><td><pre> <span class=\"token keyword\">var</span> creationFunctions <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span></pre></td></tr><tr><td data-num=\"447\"></td><td><pre> <span class=\"token string\">\"createBuffer\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"448\"></td><td><pre> <span class=\"token string\">\"createFramebuffer\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"449\"></td><td><pre> <span class=\"token string\">\"createProgram\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"450\"></td><td><pre> <span class=\"token string\">\"createRenderbuffer\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"451\"></td><td><pre> <span class=\"token string\">\"createShader\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"452\"></td><td><pre> <span class=\"token string\">\"createTexture\"</span></pre></td></tr><tr><td data-num=\"453\"></td><td><pre> <span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"454\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> ii <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> ii <span class=\"token operator\"><</span> creationFunctions<span class=\"token punctuation\">.</span><span class=\"token property-access\">length</span><span class=\"token punctuation\">;</span> <span class=\"token operator\">++</span>ii<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"455\"></td><td><pre> <span class=\"token keyword\">var</span> functionName <span class=\"token operator\">=</span> creationFunctions<span class=\"token punctuation\">[</span>ii<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"456\"></td><td><pre> wrapper_<span class=\"token punctuation\">[</span>functionName<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">f</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"457\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"458\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>contextLost_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"459\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"460\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"461\"></td><td><pre> <span class=\"token keyword\">var</span> obj <span class=\"token operator\">=</span> f<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">apply</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">,</span> arguments<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"462\"></td><td><pre> obj<span class=\"token punctuation\">.</span><span class=\"token property-access\">__webglDebugContextLostId__</span> <span class=\"token operator\">=</span> contextId_<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"463\"></td><td><pre> resourceDb_<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">push</span><span class=\"token punctuation\">(</span>obj<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"464\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> obj<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"465\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"466\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">[</span>functionName<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"467\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"468\"></td><td><pre></pre></td></tr><tr><td data-num=\"469\"></td><td><pre> <span class=\"token keyword\">var</span> functionsThatShouldReturnNull <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span></pre></td></tr><tr><td data-num=\"470\"></td><td><pre> <span class=\"token string\">\"getActiveAttrib\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"471\"></td><td><pre> <span class=\"token string\">\"getActiveUniform\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"472\"></td><td><pre> <span class=\"token string\">\"getBufferParameter\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"473\"></td><td><pre> <span class=\"token string\">\"getContextAttributes\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"474\"></td><td><pre> <span class=\"token string\">\"getAttachedShaders\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"475\"></td><td><pre> <span class=\"token string\">\"getFramebufferAttachmentParameter\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"476\"></td><td><pre> <span class=\"token string\">\"getParameter\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"477\"></td><td><pre> <span class=\"token string\">\"getProgramParameter\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"478\"></td><td><pre> <span class=\"token string\">\"getProgramInfoLog\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"479\"></td><td><pre> <span class=\"token string\">\"getRenderbufferParameter\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"480\"></td><td><pre> <span class=\"token string\">\"getShaderParameter\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"481\"></td><td><pre> <span class=\"token string\">\"getShaderInfoLog\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"482\"></td><td><pre> <span class=\"token string\">\"getShaderSource\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"483\"></td><td><pre> <span class=\"token string\">\"getTexParameter\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"484\"></td><td><pre> <span class=\"token string\">\"getUniform\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"485\"></td><td><pre> <span class=\"token string\">\"getUniformLocation\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"486\"></td><td><pre> <span class=\"token string\">\"getVertexAttrib\"</span></pre></td></tr><tr><td data-num=\"487\"></td><td><pre> <span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"488\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> ii <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> ii <span class=\"token operator\"><</span> functionsThatShouldReturnNull<span class=\"token punctuation\">.</span><span class=\"token property-access\">length</span><span class=\"token punctuation\">;</span> <span class=\"token operator\">++</span>ii<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"489\"></td><td><pre> <span class=\"token keyword\">var</span> functionName <span class=\"token operator\">=</span> functionsThatShouldReturnNull<span class=\"token punctuation\">[</span>ii<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"490\"></td><td><pre> wrapper_<span class=\"token punctuation\">[</span>functionName<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">f</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"491\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"492\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>contextLost_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"493\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"494\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"495\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> f<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">apply</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">,</span> arguments<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"496\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"497\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">(</span>wrapper_<span class=\"token punctuation\">[</span>functionName<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"498\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"499\"></td><td><pre></pre></td></tr><tr><td data-num=\"500\"></td><td><pre> <span class=\"token keyword\">var</span> isFunctions <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span></pre></td></tr><tr><td data-num=\"501\"></td><td><pre> <span class=\"token string\">\"isBuffer\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"502\"></td><td><pre> <span class=\"token string\">\"isEnabled\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"503\"></td><td><pre> <span class=\"token string\">\"isFramebuffer\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"504\"></td><td><pre> <span class=\"token string\">\"isProgram\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"505\"></td><td><pre> <span class=\"token string\">\"isRenderbuffer\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"506\"></td><td><pre> <span class=\"token string\">\"isShader\"</span><span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"507\"></td><td><pre> <span class=\"token string\">\"isTexture\"</span></pre></td></tr><tr><td data-num=\"508\"></td><td><pre> <span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"509\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> ii <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> ii <span class=\"token operator\"><</span> isFunctions<span class=\"token punctuation\">.</span><span class=\"token property-access\">length</span><span class=\"token punctuation\">;</span> <span class=\"token operator\">++</span>ii<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"510\"></td><td><pre> <span class=\"token keyword\">var</span> functionName <span class=\"token operator\">=</span> isFunctions<span class=\"token punctuation\">[</span>ii<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"511\"></td><td><pre> wrapper_<span class=\"token punctuation\">[</span>functionName<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">f</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"512\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"513\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>contextLost_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"514\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"515\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"516\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> f<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">apply</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">,</span> arguments<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"517\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"518\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">(</span>wrapper_<span class=\"token punctuation\">[</span>functionName<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"519\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"520\"></td><td><pre></pre></td></tr><tr><td data-num=\"521\"></td><td><pre> wrapper_<span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">checkFramebufferStatus</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">f</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"522\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"523\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>contextLost_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"524\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> ctx<span class=\"token punctuation\">.</span><span class=\"token constant\">FRAMEBUFFER_UNSUPPORTED</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"525\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"526\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> f<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">apply</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">,</span> arguments<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"527\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"528\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">(</span>wrapper_<span class=\"token punctuation\">.</span><span class=\"token property-access\">checkFramebufferStatus</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"529\"></td><td><pre></pre></td></tr><tr><td data-num=\"530\"></td><td><pre> wrapper_<span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">getAttribLocation</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">f</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"531\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"532\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>contextLost_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"533\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token operator\">-</span><span class=\"token number\">1</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"534\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"535\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> f<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">apply</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">,</span> arguments<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"536\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"537\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">(</span>wrapper_<span class=\"token punctuation\">.</span><span class=\"token property-access\">getAttribLocation</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"538\"></td><td><pre></pre></td></tr><tr><td data-num=\"539\"></td><td><pre> wrapper_<span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">getVertexAttribOffset</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">f</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"540\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"541\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>contextLost_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"542\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"543\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"544\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> f<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">apply</span><span class=\"token punctuation\">(</span>ctx<span class=\"token punctuation\">,</span> arguments<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"545\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"546\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">(</span>wrapper_<span class=\"token punctuation\">.</span><span class=\"token property-access\">getVertexAttribOffset</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"547\"></td><td><pre></pre></td></tr><tr><td data-num=\"548\"></td><td><pre> wrapper_<span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">isContextLost</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"549\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> contextLost_<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"550\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"551\"></td><td><pre></pre></td></tr><tr><td data-num=\"552\"></td><td><pre> <span class=\"token keyword\">function</span> <span class=\"token function\">wrapEvent</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">listener</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"553\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">typeof</span><span class=\"token punctuation\">(</span>listener<span class=\"token punctuation\">)</span> <span class=\"token operator\">==</span> <span class=\"token string\">\"function\"</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"554\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> listener<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"555\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"556\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">info</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"557\"></td><td><pre> listener<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">handleEvent</span><span class=\"token punctuation\">(</span>info<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"558\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"559\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"560\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"561\"></td><td><pre></pre></td></tr><tr><td data-num=\"562\"></td><td><pre> wrapper_<span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">registerOnContextLostListener</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">listener</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"563\"></td><td><pre> onLost_ <span class=\"token operator\">=</span> <span class=\"token function\">wrapEvent</span><span class=\"token punctuation\">(</span>listener<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"564\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"565\"></td><td><pre></pre></td></tr><tr><td data-num=\"566\"></td><td><pre> wrapper_<span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">registerOnContextRestoredListener</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">listener</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"567\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>contextLost_<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"568\"></td><td><pre> nextOnRestored_ <span class=\"token operator\">=</span> <span class=\"token function\">wrapEvent</span><span class=\"token punctuation\">(</span>listener<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"569\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"570\"></td><td><pre> onRestored_ <span class=\"token operator\">=</span> <span class=\"token function\">wrapEvent</span><span class=\"token punctuation\">(</span>listener<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"571\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"572\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"573\"></td><td><pre></pre></td></tr><tr><td data-num=\"574\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> wrapper_<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"575\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"576\"></td><td><pre></pre></td></tr><tr><td data-num=\"577\"></td><td><pre><span class=\"token keyword control-flow\">return</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"578\"></td><td><pre> <span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"579\"></td><td><pre> * Initializes this module. Safe to call more than once.</pre></td></tr><tr><td data-num=\"580\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>!WebGLRenderingContext<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">ctx</span> A WebGL context. If</pre></td></tr><tr><td data-num=\"581\"></td><td><pre> * you have more than one context it doesn't matter which one</pre></td></tr><tr><td data-num=\"582\"></td><td><pre> * you pass in, it is only used to pull out constants.</pre></td></tr><tr><td data-num=\"583\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"584\"></td><td><pre> <span class=\"token string-property property\">'init'</span><span class=\"token operator\">:</span> init<span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"585\"></td><td><pre></pre></td></tr><tr><td data-num=\"586\"></td><td><pre> <span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"587\"></td><td><pre> * Returns true or false if value matches any WebGL enum</pre></td></tr><tr><td data-num=\"588\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span><span class=\"token operator\">*</span><span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">value</span> Value to check if it might be an enum.</pre></td></tr><tr><td data-num=\"589\"></td><td><pre> * <span class=\"token keyword\">@return</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>boolean<span class=\"token punctuation\">}</span></span> True if value matches one of the WebGL defined enums</pre></td></tr><tr><td data-num=\"590\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"591\"></td><td><pre> <span class=\"token string-property property\">'mightBeEnum'</span><span class=\"token operator\">:</span> mightBeEnum<span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"592\"></td><td><pre></pre></td></tr><tr><td data-num=\"593\"></td><td><pre> <span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"594\"></td><td><pre> * Gets an string version of an WebGL enum.</pre></td></tr><tr><td data-num=\"595\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"596\"></td><td><pre> * Example:</pre></td></tr><tr><td data-num=\"597\"></td><td><pre> * WebGLDebugUtil.init(ctx);</pre></td></tr><tr><td data-num=\"598\"></td><td><pre> * var str = WebGLDebugUtil.glEnumToString(ctx.getError());</pre></td></tr><tr><td data-num=\"599\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"600\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>number<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">value</span> Value to return an enum for</pre></td></tr><tr><td data-num=\"601\"></td><td><pre> * <span class=\"token keyword\">@return</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>string<span class=\"token punctuation\">}</span></span> The string version of the enum.</pre></td></tr><tr><td data-num=\"602\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"603\"></td><td><pre> <span class=\"token string-property property\">'glEnumToString'</span><span class=\"token operator\">:</span> glEnumToString<span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"604\"></td><td><pre></pre></td></tr><tr><td data-num=\"605\"></td><td><pre> <span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"606\"></td><td><pre> * Converts the argument of a WebGL function to a string.</pre></td></tr><tr><td data-num=\"607\"></td><td><pre> * Attempts to convert enum arguments to strings.</pre></td></tr><tr><td data-num=\"608\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"609\"></td><td><pre> * Example:</pre></td></tr><tr><td data-num=\"610\"></td><td><pre> * WebGLDebugUtil.init(ctx);</pre></td></tr><tr><td data-num=\"611\"></td><td><pre> * var str = WebGLDebugUtil.glFunctionArgToString('bindTexture', 0, gl.TEXTURE_2D);</pre></td></tr><tr><td data-num=\"612\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"613\"></td><td><pre> * would return 'TEXTURE_2D'</pre></td></tr><tr><td data-num=\"614\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"615\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>string<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">functionName</span> the name of the WebGL function.</pre></td></tr><tr><td data-num=\"616\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>number<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">argumentIndx</span> the index of the argument.</pre></td></tr><tr><td data-num=\"617\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span><span class=\"token operator\">*</span><span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">value</span> The value of the argument.</pre></td></tr><tr><td data-num=\"618\"></td><td><pre> * <span class=\"token keyword\">@return</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>string<span class=\"token punctuation\">}</span></span> The value as a string.</pre></td></tr><tr><td data-num=\"619\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"620\"></td><td><pre> <span class=\"token string-property property\">'glFunctionArgToString'</span><span class=\"token operator\">:</span> glFunctionArgToString<span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"621\"></td><td><pre></pre></td></tr><tr><td data-num=\"622\"></td><td><pre> <span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"623\"></td><td><pre> * Given a WebGL context returns a wrapped context that calls</pre></td></tr><tr><td data-num=\"624\"></td><td><pre> * gl.getError after every command and calls a function if the</pre></td></tr><tr><td data-num=\"625\"></td><td><pre> * result is not NO_ERROR.</pre></td></tr><tr><td data-num=\"626\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"627\"></td><td><pre> * You can supply your own function if you want. For example, if you'd like</pre></td></tr><tr><td data-num=\"628\"></td><td><pre> * an exception thrown on any GL error you could do this</pre></td></tr><tr><td data-num=\"629\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"630\"></td><td><pre> * function throwOnGLError(err, funcName, args) <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"631\"></td><td><pre> * throw WebGLDebugUtils.glEnumToString(err) + \" was caused by call to\" +</pre></td></tr><tr><td data-num=\"632\"></td><td><pre> * funcName;</pre></td></tr><tr><td data-num=\"633\"></td><td><pre> * <span class=\"token punctuation\">}</span>;</pre></td></tr><tr><td data-num=\"634\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"635\"></td><td><pre> * ctx = WebGLDebugUtils.makeDebugContext(</pre></td></tr><tr><td data-num=\"636\"></td><td><pre> * canvas.getContext(\"webgl\"), throwOnGLError);</pre></td></tr><tr><td data-num=\"637\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"638\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>!WebGLRenderingContext<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">ctx</span> The webgl context to wrap.</pre></td></tr><tr><td data-num=\"639\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>!<span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">,</span> funcName<span class=\"token punctuation\">,</span> args<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> <span class=\"token keyword\">void</span><span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">opt_onErrorFunc</span> The function</pre></td></tr><tr><td data-num=\"640\"></td><td><pre> * to call when gl.getError returns an error. If not specified the default</pre></td></tr><tr><td data-num=\"641\"></td><td><pre> * function calls console.log with a message.</pre></td></tr><tr><td data-num=\"642\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"643\"></td><td><pre> <span class=\"token string-property property\">'makeDebugContext'</span><span class=\"token operator\">:</span> makeDebugContext<span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"644\"></td><td><pre></pre></td></tr><tr><td data-num=\"645\"></td><td><pre> <span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"646\"></td><td><pre> * Given a WebGL context returns a wrapped context that adds 4</pre></td></tr><tr><td data-num=\"647\"></td><td><pre> * functions.</pre></td></tr><tr><td data-num=\"648\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"649\"></td><td><pre> * ctx.loseContext:</pre></td></tr><tr><td data-num=\"650\"></td><td><pre> * simulates a lost context event.</pre></td></tr><tr><td data-num=\"651\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"652\"></td><td><pre> * ctx.restoreContext:</pre></td></tr><tr><td data-num=\"653\"></td><td><pre> * simulates the context being restored.</pre></td></tr><tr><td data-num=\"654\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"655\"></td><td><pre> * ctx.registerOnContextLostListener(listener):</pre></td></tr><tr><td data-num=\"656\"></td><td><pre> * lets you register a listener for context lost. Use instead</pre></td></tr><tr><td data-num=\"657\"></td><td><pre> * of addEventListener('webglcontextlostevent', listener);</pre></td></tr><tr><td data-num=\"658\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"659\"></td><td><pre> * ctx.registerOnContextRestoredListener(listener):</pre></td></tr><tr><td data-num=\"660\"></td><td><pre> * lets you register a listener for context restored. Use</pre></td></tr><tr><td data-num=\"661\"></td><td><pre> * instead of addEventListener('webglcontextrestored',</pre></td></tr><tr><td data-num=\"662\"></td><td><pre> * listener);</pre></td></tr><tr><td data-num=\"663\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"664\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>!WebGLRenderingContext<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">ctx</span> The webgl context to wrap.</pre></td></tr><tr><td data-num=\"665\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"666\"></td><td><pre> <span class=\"token string-property property\">'makeLostContextSimulatingContext'</span><span class=\"token operator\">:</span> makeLostContextSimulatingContext<span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"667\"></td><td><pre></pre></td></tr><tr><td data-num=\"668\"></td><td><pre> <span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"669\"></td><td><pre> * Resets a context to the initial state.</pre></td></tr><tr><td data-num=\"670\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>!WebGLRenderingContext<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">ctx</span> The webgl context to</pre></td></tr><tr><td data-num=\"671\"></td><td><pre> * reset.</pre></td></tr><tr><td data-num=\"672\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"673\"></td><td><pre> <span class=\"token string-property property\">'resetToInitialState'</span><span class=\"token operator\">:</span> resetToInitialState</pre></td></tr><tr><td data-num=\"674\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"675\"></td><td><pre></pre></td></tr><tr><td data-num=\"676\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr></table></figure><figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"><span>webgl-utils.js</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\">/*</pre></td></tr><tr><td data-num=\"2\"></td><td><pre> * Copyright 2010, Google Inc.</pre></td></tr><tr><td data-num=\"3\"></td><td><pre> * All rights reserved.</pre></td></tr><tr><td data-num=\"4\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"5\"></td><td><pre> * Redistribution and use in source and binary forms, with or without</pre></td></tr><tr><td data-num=\"6\"></td><td><pre> * modification, are permitted provided that the following conditions are</pre></td></tr><tr><td data-num=\"7\"></td><td><pre> * met:</pre></td></tr><tr><td data-num=\"8\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"9\"></td><td><pre> * * Redistributions of source code must retain the above copyright</pre></td></tr><tr><td data-num=\"10\"></td><td><pre> * notice, this list of conditions and the following disclaimer.</pre></td></tr><tr><td data-num=\"11\"></td><td><pre> * * Redistributions in binary form must reproduce the above</pre></td></tr><tr><td data-num=\"12\"></td><td><pre> * copyright notice, this list of conditions and the following disclaimer</pre></td></tr><tr><td data-num=\"13\"></td><td><pre> * in the documentation and/or other materials provided with the</pre></td></tr><tr><td data-num=\"14\"></td><td><pre> * distribution.</pre></td></tr><tr><td data-num=\"15\"></td><td><pre> * * Neither the name of Google Inc. nor the names of its</pre></td></tr><tr><td data-num=\"16\"></td><td><pre> * contributors may be used to endorse or promote products derived from</pre></td></tr><tr><td data-num=\"17\"></td><td><pre> * this software without specific prior written permission.</pre></td></tr><tr><td data-num=\"18\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"19\"></td><td><pre> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</pre></td></tr><tr><td data-num=\"20\"></td><td><pre> * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</pre></td></tr><tr><td data-num=\"21\"></td><td><pre> * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR</pre></td></tr><tr><td data-num=\"22\"></td><td><pre> * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT</pre></td></tr><tr><td data-num=\"23\"></td><td><pre> * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</pre></td></tr><tr><td data-num=\"24\"></td><td><pre> * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT</pre></td></tr><tr><td data-num=\"25\"></td><td><pre> * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,</pre></td></tr><tr><td data-num=\"26\"></td><td><pre> * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY</pre></td></tr><tr><td data-num=\"27\"></td><td><pre> * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</pre></td></tr><tr><td data-num=\"28\"></td><td><pre> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE</pre></td></tr><tr><td data-num=\"29\"></td><td><pre> * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</pre></td></tr><tr><td data-num=\"30\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"31\"></td><td><pre></pre></td></tr><tr><td data-num=\"32\"></td><td><pre></pre></td></tr><tr><td data-num=\"33\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"34\"></td><td><pre> * <span class=\"token keyword\">@fileoverview</span> This file contains functions every webgl program will need</pre></td></tr><tr><td data-num=\"35\"></td><td><pre> * a version of one way or another.</pre></td></tr><tr><td data-num=\"36\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"37\"></td><td><pre> * Instead of setting up a context manually it is recommended to</pre></td></tr><tr><td data-num=\"38\"></td><td><pre> * use. This will check for success or failure. On failure it</pre></td></tr><tr><td data-num=\"39\"></td><td><pre> * will attempt to present an approriate message to the user.</pre></td></tr><tr><td data-num=\"40\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"41\"></td><td><pre> * gl = WebGLUtils.setupWebGL(canvas);</pre></td></tr><tr><td data-num=\"42\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"43\"></td><td><pre> * For animated WebGL apps use of setTimeout or setInterval are</pre></td></tr><tr><td data-num=\"44\"></td><td><pre> * discouraged. It is recommended you structure your rendering</pre></td></tr><tr><td data-num=\"45\"></td><td><pre> * loop like this.</pre></td></tr><tr><td data-num=\"46\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"47\"></td><td><pre> * function render() <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"48\"></td><td><pre> * window.requestAnimationFrame(render, canvas);</pre></td></tr><tr><td data-num=\"49\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"50\"></td><td><pre> * // do rendering</pre></td></tr><tr><td data-num=\"51\"></td><td><pre> * ...</pre></td></tr><tr><td data-num=\"52\"></td><td><pre> * <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"53\"></td><td><pre> * render();</pre></td></tr><tr><td data-num=\"54\"></td><td><pre> *</pre></td></tr><tr><td data-num=\"55\"></td><td><pre> * This will call your rendering function up to the refresh rate</pre></td></tr><tr><td data-num=\"56\"></td><td><pre> * of your display but will stop rendering if your app is not</pre></td></tr><tr><td data-num=\"57\"></td><td><pre> * visible.</pre></td></tr><tr><td data-num=\"58\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"59\"></td><td><pre></pre></td></tr><tr><td data-num=\"60\"></td><td><pre><span class=\"token function-variable function\"><span class=\"token maybe-class-name\">WebGLUtils</span></span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"61\"></td><td><pre></pre></td></tr><tr><td data-num=\"62\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"63\"></td><td><pre> * Creates the HTLM for a failure message</pre></td></tr><tr><td data-num=\"64\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>string<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">canvasContainerId</span> id of container of th</pre></td></tr><tr><td data-num=\"65\"></td><td><pre> * canvas.</pre></td></tr><tr><td data-num=\"66\"></td><td><pre> * <span class=\"token keyword\">@return</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>string<span class=\"token punctuation\">}</span></span> The html.</pre></td></tr><tr><td data-num=\"67\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"68\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token function-variable function\">makeFailHTML</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">msg</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"69\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token string\">''</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"70\"></td><td><pre> <span class=\"token string\">'<div style=\"margin: auto; width:500px;z-index:10000;margin-top:20em;text-align:center;\">'</span> <span class=\"token operator\">+</span> msg <span class=\"token operator\">+</span> <span class=\"token string\">'</div>'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"71\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token string\">''</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"72\"></td><td><pre> <span class=\"token string\">'<table style=\"background-color: #8CE; width: 100%; height: 100%;\"><tr>'</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"73\"></td><td><pre> <span class=\"token string\">'<td align=\"center\">'</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"74\"></td><td><pre> <span class=\"token string\">'<div style=\"display: table-cell; vertical-align: middle;\">'</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"75\"></td><td><pre> <span class=\"token string\">'<div style=\"\">'</span> <span class=\"token operator\">+</span> msg <span class=\"token operator\">+</span> <span class=\"token string\">'</div>'</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"76\"></td><td><pre> <span class=\"token string\">'</div>'</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"77\"></td><td><pre> <span class=\"token string\">'</td></tr></table>'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"78\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"79\"></td><td><pre></pre></td></tr><tr><td data-num=\"80\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"81\"></td><td><pre> * Mesasge for getting a webgl browser</pre></td></tr><tr><td data-num=\"82\"></td><td><pre> * <span class=\"token keyword\">@type</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>string<span class=\"token punctuation\">}</span></span></pre></td></tr><tr><td data-num=\"83\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"84\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token constant\">GET_A_WEBGL_BROWSER</span> <span class=\"token operator\">=</span> <span class=\"token string\">''</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"85\"></td><td><pre> <span class=\"token string\">'This page requires a browser that supports WebGL.<br/>'</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"86\"></td><td><pre> <span class=\"token string\">'<a href=\"http://get.webgl.org\">Click here to upgrade your browser.</a>'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"87\"></td><td><pre></pre></td></tr><tr><td data-num=\"88\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"89\"></td><td><pre> * Mesasge for need better hardware</pre></td></tr><tr><td data-num=\"90\"></td><td><pre> * <span class=\"token keyword\">@type</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>string<span class=\"token punctuation\">}</span></span></pre></td></tr><tr><td data-num=\"91\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"92\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token constant\">OTHER_PROBLEM</span> <span class=\"token operator\">=</span> <span class=\"token string\">''</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"93\"></td><td><pre> <span class=\"token string\">\"It doesn't appear your computer can support WebGL.<br/>\"</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"94\"></td><td><pre> <span class=\"token string\">'<a href=\"http://get.webgl.org\">Click here for more information.</a>'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"95\"></td><td><pre></pre></td></tr><tr><td data-num=\"96\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"97\"></td><td><pre> * Creates a webgl context. If creation fails it will</pre></td></tr><tr><td data-num=\"98\"></td><td><pre> * change the contents of the container of the <canvas></pre></td></tr><tr><td data-num=\"99\"></td><td><pre> * tag to an error message with the correct links for WebGL.</pre></td></tr><tr><td data-num=\"100\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>Element<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">canvas<span class=\"token punctuation\">.</span></span> The canvas element to create a</pre></td></tr><tr><td data-num=\"101\"></td><td><pre> * context from.</pre></td></tr><tr><td data-num=\"102\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>WebGLContextCreationAttirbutes<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">opt_attribs</span> Any</pre></td></tr><tr><td data-num=\"103\"></td><td><pre> * creation attributes you want to pass in.</pre></td></tr><tr><td data-num=\"104\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span><span class=\"token keyword\">function</span><span class=\"token operator\">:</span><span class=\"token punctuation\">(</span>msg<span class=\"token punctuation\">)</span><span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">opt_onError</span> An function to call</pre></td></tr><tr><td data-num=\"105\"></td><td><pre> * if there is an error during creation.</pre></td></tr><tr><td data-num=\"106\"></td><td><pre> * <span class=\"token keyword\">@return</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>WebGLRenderingContext<span class=\"token punctuation\">}</span></span> The created context.</pre></td></tr><tr><td data-num=\"107\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"108\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token function-variable function\">setupWebGL</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">canvas<span class=\"token punctuation\">,</span> opt_attribs<span class=\"token punctuation\">,</span> opt_onError</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"109\"></td><td><pre> <span class=\"token keyword\">function</span> <span class=\"token function\">handleCreationError</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">msg</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"110\"></td><td><pre> <span class=\"token keyword\">var</span> container <span class=\"token operator\">=</span> <span class=\"token dom variable\">document</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getElementsByTagName</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"body\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"111\"></td><td><pre> <span class=\"token comment\">//var container = canvas.parentNode;</span></pre></td></tr><tr><td data-num=\"112\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>container<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"113\"></td><td><pre> <span class=\"token keyword\">var</span> str <span class=\"token operator\">=</span> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\"><span class=\"token maybe-class-name\">WebGLRenderingContext</span></span> <span class=\"token operator\">?</span></pre></td></tr><tr><td data-num=\"114\"></td><td><pre> <span class=\"token constant\">OTHER_PROBLEM</span> <span class=\"token operator\">:</span></pre></td></tr><tr><td data-num=\"115\"></td><td><pre> <span class=\"token constant\">GET_A_WEBGL_BROWSER</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"116\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>msg<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"117\"></td><td><pre> str <span class=\"token operator\">+=</span> <span class=\"token string\">\"<br/><br/>Status: \"</span> <span class=\"token operator\">+</span> msg<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"118\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"119\"></td><td><pre> container<span class=\"token punctuation\">.</span><span class=\"token property-access\">innerHTML</span> <span class=\"token operator\">=</span> <span class=\"token function\">makeFailHTML</span><span class=\"token punctuation\">(</span>str<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"120\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"121\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"122\"></td><td><pre></pre></td></tr><tr><td data-num=\"123\"></td><td><pre> opt_onError <span class=\"token operator\">=</span> opt_onError <span class=\"token operator\">||</span> handleCreationError<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"124\"></td><td><pre></pre></td></tr><tr><td data-num=\"125\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>canvas<span class=\"token punctuation\">.</span><span class=\"token property-access\">addEventListener</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"126\"></td><td><pre> canvas<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">addEventListener</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"webglcontextcreationerror\"</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">event</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"127\"></td><td><pre> <span class=\"token function\">opt_onError</span><span class=\"token punctuation\">(</span>event<span class=\"token punctuation\">.</span><span class=\"token property-access\">statusMessage</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"128\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"129\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"130\"></td><td><pre> <span class=\"token keyword\">var</span> context <span class=\"token operator\">=</span> <span class=\"token function\">create3DContext</span><span class=\"token punctuation\">(</span>canvas<span class=\"token punctuation\">,</span> opt_attribs<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"131\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>context<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"132\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span><span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\"><span class=\"token maybe-class-name\">WebGLRenderingContext</span></span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"133\"></td><td><pre> <span class=\"token function\">opt_onError</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"134\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">else</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"135\"></td><td><pre> <span class=\"token function\">opt_onError</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"136\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"137\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"138\"></td><td><pre></pre></td></tr><tr><td data-num=\"139\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> context<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"140\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"141\"></td><td><pre></pre></td></tr><tr><td data-num=\"142\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"143\"></td><td><pre> * Creates a webgl context.</pre></td></tr><tr><td data-num=\"144\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>!Canvas<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">canvas</span> The canvas tag to get context</pre></td></tr><tr><td data-num=\"145\"></td><td><pre> * from. If one is not passed in one will be created.</pre></td></tr><tr><td data-num=\"146\"></td><td><pre> * <span class=\"token keyword\">@return</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>!WebGLContext<span class=\"token punctuation\">}</span></span> The created context.</pre></td></tr><tr><td data-num=\"147\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"148\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token function-variable function\">create3DContext</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">canvas<span class=\"token punctuation\">,</span> opt_attribs</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"149\"></td><td><pre> <span class=\"token keyword\">var</span> names <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">\"webgl\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"experimental-webgl\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"webkit-3d\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"moz-webgl\"</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"150\"></td><td><pre> <span class=\"token keyword\">var</span> context <span class=\"token operator\">=</span> <span class=\"token keyword null nil\">null</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"151\"></td><td><pre> <span class=\"token keyword control-flow\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> ii <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> ii <span class=\"token operator\"><</span> names<span class=\"token punctuation\">.</span><span class=\"token property-access\">length</span><span class=\"token punctuation\">;</span> <span class=\"token operator\">++</span>ii<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"152\"></td><td><pre> <span class=\"token keyword control-flow\">try</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"153\"></td><td><pre> context <span class=\"token operator\">=</span> canvas<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getContext</span><span class=\"token punctuation\">(</span>names<span class=\"token punctuation\">[</span>ii<span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> opt_attribs<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"154\"></td><td><pre> <span class=\"token punctuation\">}</span> <span class=\"token keyword control-flow\">catch</span><span class=\"token punctuation\">(</span>e<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"155\"></td><td><pre> <span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span>context<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"156\"></td><td><pre> <span class=\"token keyword control-flow\">break</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"157\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"158\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"159\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> context<span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"160\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"161\"></td><td><pre></pre></td></tr><tr><td data-num=\"162\"></td><td><pre><span class=\"token keyword control-flow\">return</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"163\"></td><td><pre> <span class=\"token literal-property property\">create3DContext</span><span class=\"token operator\">:</span> create3DContext<span class=\"token punctuation\">,</span></pre></td></tr><tr><td data-num=\"164\"></td><td><pre> <span class=\"token literal-property property\">setupWebGL</span><span class=\"token operator\">:</span> setupWebGL</pre></td></tr><tr><td data-num=\"165\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"166\"></td><td><pre><span class=\"token punctuation\">}</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"167\"></td><td><pre></pre></td></tr><tr><td data-num=\"168\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"169\"></td><td><pre> * Provides requestAnimationFrame in a cross browser</pre></td></tr><tr><td data-num=\"170\"></td><td><pre> * way.</pre></td></tr><tr><td data-num=\"171\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"172\"></td><td><pre><span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span><span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">requestAnimationFrame</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"173\"></td><td><pre> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">requestAnimationFrame</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"174\"></td><td><pre> <span class=\"token keyword control-flow\">return</span> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">requestAnimationFrame</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"175\"></td><td><pre> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">webkitRequestAnimationFrame</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"176\"></td><td><pre> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">mozRequestAnimationFrame</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"177\"></td><td><pre> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">oRequestAnimationFrame</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"178\"></td><td><pre> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">msRequestAnimationFrame</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"179\"></td><td><pre> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token comment\">/* function FrameRequestCallback */</span> callback<span class=\"token punctuation\">,</span> <span class=\"token comment\">/* DOMElement Element */</span> element<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"180\"></td><td><pre> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">setTimeout</span><span class=\"token punctuation\">(</span>callback<span class=\"token punctuation\">,</span> <span class=\"token number\">1000</span><span class=\"token operator\">/</span><span class=\"token number\">60</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"181\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"182\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"183\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"184\"></td><td><pre></pre></td></tr><tr><td data-num=\"185\"></td><td><pre><span class=\"token doc-comment comment\">/** * ERRATA: 'cancelRequestAnimationFrame' renamed to 'cancelAnimationFrame' to reflect an update to the W3C Animation-Timing Spec. </pre></td></tr><tr><td data-num=\"186\"></td><td><pre> * </pre></td></tr><tr><td data-num=\"187\"></td><td><pre> * Cancels an animation frame request. </pre></td></tr><tr><td data-num=\"188\"></td><td><pre> * Checks for cross-browser support, falls back to clearTimeout. </pre></td></tr><tr><td data-num=\"189\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span>number<span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">Animation</span> frame request. */</span></pre></td></tr><tr><td data-num=\"190\"></td><td><pre><span class=\"token keyword control-flow\">if</span> <span class=\"token punctuation\">(</span><span class=\"token operator\">!</span><span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">cancelAnimationFrame</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"191\"></td><td><pre> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">cancelAnimationFrame</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">cancelRequestAnimationFrame</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"192\"></td><td><pre> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">webkitCancelAnimationFrame</span> <span class=\"token operator\">||</span> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">webkitCancelRequestAnimationFrame</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"193\"></td><td><pre> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">mozCancelAnimationFrame</span> <span class=\"token operator\">||</span> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">mozCancelRequestAnimationFrame</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"194\"></td><td><pre> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">msCancelAnimationFrame</span> <span class=\"token operator\">||</span> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">msCancelRequestAnimationFrame</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"195\"></td><td><pre> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">oCancelAnimationFrame</span> <span class=\"token operator\">||</span> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">oCancelRequestAnimationFrame</span> <span class=\"token operator\">||</span></pre></td></tr><tr><td data-num=\"196\"></td><td><pre> <span class=\"token dom variable\">window</span><span class=\"token punctuation\">.</span><span class=\"token property-access\">clearTimeout</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"197\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure>",
"tags": [
"WebGL",
"WebGL"
]
},
{
"id": "https://unistart2.github.io/posts/2aaf59a8/",
"url": "https://unistart2.github.io/posts/2aaf59a8/",
"title": "WebGL学习(一)",
"date_published": "2023-03-15T01:47:20.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><div class=\"note info\">\n<p>参考书籍:《WebGL 编程指南》</p>\n</div>\n<h2 id=\"canvas\"><a class=\"anchor\" href=\"#canvas\">#</a> canvas</h2>\n<blockquote>\n<p><span class=\"red\">canvas</span> 是<span class=\"red\"> HTML5</span> 新增标签,用于定义网页上的绘图区域,可以通过<span class=\"red\"> JavaScript</span> 代码在上面绘制图形</p>\n</blockquote>\n<p><span class=\"label primary\">关于 Canvas 更多信息请参考 MDN 相关文档</span><br />\n<span class=\"label primary\"> Canvas 简单使用示例</span></p>\n<figure class=\"highlight html\"><figcaption data-lang=\"HTML\"><span>HelloCanvas.html</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token doctype\"><span class=\"token punctuation\"><!</span><span class=\"token doctype-tag\">DOCTYPE</span> <span class=\"token name\">html</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>html</span> <span class=\"token attr-name\">lang</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>en<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>head</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>meta</span> <span class=\"token attr-name\">charset</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>UTF-8<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>meta</span> <span class=\"token attr-name\">http-equiv</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>X-UA-Compatible<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">content</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>IE=edge<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>meta</span> <span class=\"token attr-name\">name</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>viewport<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">content</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>width=device-width, initial-scale=1.0<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>title</span><span class=\"token punctuation\">></span></span>Hello Canvas<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>title</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>head</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>body</span> <span class=\"token special-attr\"><span class=\"token attr-name\">onload</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token value javascript language-javascript\"><span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></span><span class=\"token punctuation\">\"</span></span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr class=\"marked\"><td data-num=\"10\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>canvas</span> <span class=\"token attr-name\">id</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>webgl<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">width</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>400<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">height</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>400<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr class=\"marked\"><td data-num=\"11\"></td><td><pre> Please use the browser supporting \"canvas\"</pre></td></tr><tr class=\"marked\"><td data-num=\"12\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>canvas</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>../lib/webgl-utils.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>../lib/webgl-debug.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>../lib/cuon-utils.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>HelloCanvas.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>body</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>html</span><span class=\"token punctuation\">></span></span></pre></td></tr></table></figure><figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token comment\">// 获取 canvas 元素</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">var</span> canvas <span class=\"token operator\">=</span> <span class=\"token dom variable\">document</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getElementById</span><span class=\"token punctuation\">(</span><span class=\"token string\">'webgl'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token comment\">// 获取 WebGL 绘图上下文(调用 cuon-util.js 中封装的方法)</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">var</span> gl <span class=\"token operator\">=</span> <span class=\"token function\">getWebGLContext</span><span class=\"token punctuation\">(</span>canvas<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>gl<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to get the rendering context for WebGL'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token comment\">// 指定清空 canvas 的颜色</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">clearColor</span><span class=\"token punctuation\">(</span><span class=\"token number\">0.2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.4</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.6</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.8</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token comment\">// 清空 canvas</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">clear</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token constant\">COLOR_BUFFER_BIT</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> </pre></td></tr><tr><td data-num=\"17\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><details class=\"info\"><summary>效果</summary><div>\n<p><img data-src=\"https://cdn.unistart.top/blog/webgl-study/cVnIs68.png\" alt=\"HelloCanvas\" /></p>\n</div></details>\n<p>上述代码在 HTML 中定义了一个<span class=\"red\"> 400x400 的 canvas</span>,并通过 js 代码获取到了<span class=\"red\"> canvas 元素对象</span>以及其<span class=\"red\"> WebGL 绘图上下文对象</span>,通过<span class=\"red\"> WebGL 绘图上下文对象</span>以<span class=\"red\"> RBGA</span> 的形式设置了<span class=\"red\"> canvas</span> 的背景颜色,最后用之前设置的背景色清空<span class=\"red\"> canvas</span> 绘图区域。</p>\n<div class=\"note warning\">\n<p>一旦通过 clearColor 方法指定背景色后,背景色就会驻存在 WebGL 系统中,在下一次重新调用 clearColor 方法设置背景色前不会改变。</p>\n</div>\n<details class=\"primary\"><summary>clear方法说明</summary><div>\n<p>clear 用于清空指定缓冲区数据,它接受一个枚举类型的参数 buffer,具体说明如下:</p>\n<ol>\n<li>gl.COLOR_BUFFER_BIT:指定颜色缓冲区</li>\n<li>gl.DEPTH_BUFFER_BIT: 指定深度缓冲区</li>\n<li>gl.STENCIL_BUFFER_BIT: 指定模板缓冲区<br />\n返回值:无<br />\n错误:INVALID_VALUE - 缓冲区不是以上三种类型</li>\n</ol>\n<hr />\n<p>各缓冲区默认行为:<br />\n颜色缓冲区:相关函数 gl.clearColor (r, g, b, a),默认值 (0.0, 0.0, 0.0, 0.0)<br />\n 深度缓冲区:相关函数 gl.clearDepth (depth),默认值 1.0<br />\n 模板缓冲区:相关函数 gl.clearStencil (s),默认值 0</p>\n</div></details>\n<h2 id=\"初识着色器程序\"><a class=\"anchor\" href=\"#初识着色器程序\">#</a> 初识着色器程序</h2>\n<blockquote>\n<p><span class=\"red\">WebGL</span> 依赖于<span class=\"red\">着色器 (shader)</span> 机制进行绘图,在<span class=\"red\"> WebGL</span> 中<span class=\"red\">着色器程序</span>是<span class=\"red\">以字符串的形式嵌入在 JavaScript 代码中</span>的</p>\n</blockquote>\n<figure class=\"highlight html\"><figcaption data-lang=\"HTML\"><span>HelloPoint1.html</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token doctype\"><span class=\"token punctuation\"><!</span><span class=\"token doctype-tag\">DOCTYPE</span> <span class=\"token name\">html</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>html</span> <span class=\"token attr-name\">lang</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>en<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>head</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>meta</span> <span class=\"token attr-name\">charset</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>UTF-8<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>meta</span> <span class=\"token attr-name\">http-equiv</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>X-UA-Compatible<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">content</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>IE=edge<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>meta</span> <span class=\"token attr-name\">name</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>viewport<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">content</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>width=device-width, initial-scale=1.0<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>title</span><span class=\"token punctuation\">></span></span>Draw a point (1)<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>title</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>head</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>body</span> <span class=\"token special-attr\"><span class=\"token attr-name\">onload</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token value javascript language-javascript\"><span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></span><span class=\"token punctuation\">\"</span></span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>canvas</span> <span class=\"token attr-name\">id</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>webgl<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">width</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>400<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">height</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>400<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> Please use the browser supporting \"canvas\"</pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>canvas</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>../lib/webgl-utils.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>../lib/webgl-debug.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>../lib/cuon-utils.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>HelloPoint1.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>body</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>html</span><span class=\"token punctuation\">></span></span></pre></td></tr></table></figure><figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"><span>HelloPoint1.js 28</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\">// 顶点着色器程序 (vertex shader program)</span></pre></td></tr><tr class=\"marked\"><td data-num=\"2\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token constant\">VSHADER_SOURCE</span> <span class=\"token operator\">=</span> </pre></td></tr><tr class=\"marked\"><td data-num=\"3\"></td><td><pre> <span class=\"token string\">'void main() {\\n'</span> <span class=\"token operator\">+</span> </pre></td></tr><tr class=\"marked\"><td data-num=\"4\"></td><td><pre> <span class=\"token string\">' gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\\n'</span> <span class=\"token operator\">+</span> <span class=\"token comment\">// 设置坐标</span></pre></td></tr><tr class=\"marked\"><td data-num=\"5\"></td><td><pre> <span class=\"token string\">' gl_PointSize = 10.0;\\n'</span> <span class=\"token operator\">+</span> <span class=\"token comment\">// 设置尺寸</span></pre></td></tr><tr class=\"marked\"><td data-num=\"6\"></td><td><pre> <span class=\"token string\">'}\\n'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"7\"></td><td><pre></pre></td></tr><tr class=\"marked\"><td data-num=\"8\"></td><td><pre></pre></td></tr><tr class=\"marked\"><td data-num=\"9\"></td><td><pre><span class=\"token comment\">// 片元着色器程序 (Fragment shader program)</span></pre></td></tr><tr class=\"marked\"><td data-num=\"10\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token constant\">FSHADER_SOURCE</span> <span class=\"token operator\">=</span> </pre></td></tr><tr class=\"marked\"><td data-num=\"11\"></td><td><pre> <span class=\"token string\">'void main() {\\n'</span> <span class=\"token operator\">+</span></pre></td></tr><tr class=\"marked\"><td data-num=\"12\"></td><td><pre> <span class=\"token string\">' gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\\n'</span> <span class=\"token operator\">+</span> <span class=\"token comment\">// 设置颜色</span></pre></td></tr><tr class=\"marked\"><td data-num=\"13\"></td><td><pre> <span class=\"token string\">'}\\n'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre></pre></td></tr><tr><td data-num=\"15\"></td><td><pre></pre></td></tr><tr><td data-num=\"16\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token comment\">// 获取 canvans 元素</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre> <span class=\"token keyword\">var</span> canvans <span class=\"token operator\">=</span> <span class=\"token dom variable\">document</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getElementById</span><span class=\"token punctuation\">(</span><span class=\"token string\">'webgl'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre></pre></td></tr><tr><td data-num=\"20\"></td><td><pre> <span class=\"token comment\">// 获取 WebGL 上下文</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre> <span class=\"token keyword\">var</span> gl <span class=\"token operator\">=</span> <span class=\"token function\">getWebGLContext</span><span class=\"token punctuation\">(</span>canvans<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"22\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>gl<span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to get the rendering context for WebGL'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"24\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"25\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"26\"></td><td><pre></pre></td></tr><tr><td data-num=\"27\"></td><td><pre> <span class=\"token comment\">// 初始化着色器</span></pre></td></tr><tr><td data-num=\"28\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span><span class=\"token operator\">!</span><span class=\"token function\">initShaders</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">,</span> <span class=\"token constant\">VSHADER_SOURCE</span><span class=\"token punctuation\">,</span> <span class=\"token constant\">FSHADER_SOURCE</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"29\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to initialize shaders.'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"30\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"31\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"32\"></td><td><pre></pre></td></tr><tr><td data-num=\"33\"></td><td><pre> <span class=\"token comment\">// 设置 canvas 颜色</span></pre></td></tr><tr><td data-num=\"34\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">clearColor</span><span class=\"token punctuation\">(</span><span class=\"token number\">0.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1.0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"35\"></td><td><pre></pre></td></tr><tr><td data-num=\"36\"></td><td><pre> <span class=\"token comment\">// 清空 canvas</span></pre></td></tr><tr><td data-num=\"37\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">clear</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token constant\">COLOR_BUFFER_BIT</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"38\"></td><td><pre></pre></td></tr><tr><td data-num=\"39\"></td><td><pre> <span class=\"token comment\">// 绘制一个点</span></pre></td></tr><tr><td data-num=\"40\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">drawArrays</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token constant\">POINTS</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"41\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><details class=\"info\"><summary>效果</summary><div>\n<p><img data-src=\"https://cdn.unistart.top/blog/webgl-study/s7spqnB.png\" alt=\"HelloPoint1\" /></p>\n</div></details>\n<blockquote>\n<p><span class=\"red\">顶点着色器 (Vertex shader):</span> 用来描述顶点特性(如位置,颜色等)的程序。顶点是值二维或三维空间中的一个点,比如二维或三维图像的端点或交点。<br />\n<span class=\"red\">片元着色器 (Fragment shader):</span> 进行逐片元处理过程如光照的程序。片元是 WebGL 术语,可以将其理解为像素(图像的单元)。</p>\n</blockquote>\n<details class=\"primary\"><summary>WebGL渲染过程</summary><div>\n<p><img data-src=\"https://cdn.unistart.top/blog/webgl-study/webgl-render-process.png\" alt=\"WebGL渲染过程\" /></p>\n</div></details>\n<div class=\"note primary\">\n<p>1.WebGL 程序包括两部分:运行在浏览器中的 JavaScript 和运行在 WebGL 系统的着色器程序<br />\n 2.WebGL 一般用于绘制三维图形,使用三维坐标。通常情况下 WebGL 中的坐标系是<span class=\"red\">右手坐标系</span>,<ins class=\"dot\">坐标原点是 canvas 绘图区域的正中心,x 轴正方向为向右,y 轴正方向为向上,z 轴正方向为垂直于屏幕向外</ins><br />\n 3.canvas 绘图区域的坐标原点在绘图区域的左上角,x 轴正方向为向右,y 轴正方向为向下</p>\n</div>\n<h2 id=\"动态向着色器传输数据\"><a class=\"anchor\" href=\"#动态向着色器传输数据\">#</a> 动态向着色器传输数据</h2>\n<blockquote>\n<p>之前的着色器程序中,点的位置、大小、颜色等信息都是硬编码的,缺乏可扩展性。我们需要动态从 JavaScript 程序中传递数据给着色器程序。<br />\n有两种方式可以做到这一点:<span class=\"red\">attribute 变量</span>和<span class=\"red\"> uniform 变量</span></p>\n</blockquote>\n<div class=\"note primary\">\n<ol>\n<li><span class=\"blue\">attribute 变量</span>:传输和顶点相关的数据</li>\n<li><span class=\"blue\">uniform 变量</span>:传输对于所有顶点都相同的数据或者和顶点无关的数据</li>\n</ol>\n</div>\n<figure class=\"highlight html\"><figcaption data-lang=\"HTML\"><span>HelloPoint2.html</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token doctype\"><span class=\"token punctuation\"><!</span><span class=\"token doctype-tag\">DOCTYPE</span> <span class=\"token name\">html</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>html</span> <span class=\"token attr-name\">lang</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>en<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>head</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>meta</span> <span class=\"token attr-name\">charset</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>UTF-8<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>meta</span> <span class=\"token attr-name\">http-equiv</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>X-UA-Compatible<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">content</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>IE=edge<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>meta</span> <span class=\"token attr-name\">name</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>viewport<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">content</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>width=device-width, initial-scale=1.0<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>title</span><span class=\"token punctuation\">></span></span>Draw a point (2)<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>title</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>head</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>body</span> <span class=\"token special-attr\"><span class=\"token attr-name\">onload</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token value javascript language-javascript\"><span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></span><span class=\"token punctuation\">\"</span></span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>canvas</span> <span class=\"token attr-name\">id</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>webgl<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">width</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>400<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">height</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>400<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> Please use the browser supporting \"canvas\"</pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>canvas</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>../lib/webgl-utils.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>../lib/webgl-debug.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>../lib/cuon-utils.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>HelloPoint2.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>body</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>html</span><span class=\"token punctuation\">></span></span></pre></td></tr></table></figure><figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"><span>HelloPoint2.js</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\">// 顶点着色器程序 (vertex shader program)</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token constant\">VSHADER_SOURCE</span> <span class=\"token operator\">=</span> </pre></td></tr><tr class=\"marked\"><td data-num=\"3\"></td><td><pre> <span class=\"token string\">'attribute vec4 a_Position;\\n'</span> <span class=\"token operator\">+</span></pre></td></tr><tr class=\"marked\"><td data-num=\"4\"></td><td><pre> <span class=\"token string\">'attribute float a_PointSize;\\n'</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token string\">'void main() {\\n'</span> <span class=\"token operator\">+</span> </pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token string\">' gl_Position = a_Position;\\n'</span> <span class=\"token operator\">+</span> <span class=\"token comment\">// 设置坐标</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token string\">' gl_PointSize = a_PointSize;\\n'</span> <span class=\"token operator\">+</span> <span class=\"token comment\">// 设置尺寸</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token string\">'}\\n'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre></pre></td></tr><tr><td data-num=\"10\"></td><td><pre></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token comment\">// 片元着色器程序 (Fragment shader program)</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token constant\">FSHADER_SOURCE</span> <span class=\"token operator\">=</span> </pre></td></tr><tr><td data-num=\"13\"></td><td><pre> <span class=\"token string\">'void main() {\\n'</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token string\">' gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\\n'</span> <span class=\"token operator\">+</span> <span class=\"token comment\">// 设置颜色</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token string\">'}\\n'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre></pre></td></tr><tr><td data-num=\"17\"></td><td><pre></pre></td></tr><tr><td data-num=\"18\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre> <span class=\"token comment\">// 获取 & lt;canvans > 元素</span></pre></td></tr><tr><td data-num=\"20\"></td><td><pre> <span class=\"token keyword\">var</span> canvans <span class=\"token operator\">=</span> <span class=\"token dom variable\">document</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getElementById</span><span class=\"token punctuation\">(</span><span class=\"token string\">'webgl'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre></pre></td></tr><tr><td data-num=\"22\"></td><td><pre> <span class=\"token comment\">// 获取 WebGL 上下文</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre> <span class=\"token keyword\">var</span> gl <span class=\"token operator\">=</span> <span class=\"token function\">getWebGLContext</span><span class=\"token punctuation\">(</span>canvans<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"24\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>gl<span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"25\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to get the rendering context for WebGL'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"26\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"27\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"28\"></td><td><pre></pre></td></tr><tr><td data-num=\"29\"></td><td><pre> <span class=\"token comment\">// 初始化着色器</span></pre></td></tr><tr><td data-num=\"30\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span><span class=\"token operator\">!</span><span class=\"token function\">initShaders</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">,</span> <span class=\"token constant\">VSHADER_SOURCE</span><span class=\"token punctuation\">,</span> <span class=\"token constant\">FSHADER_SOURCE</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"31\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to initialize shaders.'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"32\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"33\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"34\"></td><td><pre></pre></td></tr><tr><td data-num=\"35\"></td><td><pre> <span class=\"token comment\">// 获取 attribute 变量 a_Position 的存储地址</span></pre></td></tr><tr class=\"marked\"><td data-num=\"36\"></td><td><pre> <span class=\"token keyword\">var</span> a_Position <span class=\"token operator\">=</span> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getAttribLocation</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token property-access\">program</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'a_Position'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"37\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span>a_Position <span class=\"token operator\"><</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span></pre></td></tr><tr class=\"marked\"><td data-num=\"38\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to get the storage location of a_Position'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"39\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"40\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"41\"></td><td><pre></pre></td></tr><tr><td data-num=\"42\"></td><td><pre> <span class=\"token comment\">// 传输顶点大小(方式一)</span></pre></td></tr><tr class=\"marked\"><td data-num=\"43\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">vertexAttrib3f</span><span class=\"token punctuation\">(</span>a_Position<span class=\"token punctuation\">,</span> <span class=\"token number\">0.5</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.5</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"44\"></td><td><pre> <span class=\"token comment\">// 传输顶点大小(方式二)</span></pre></td></tr><tr><td data-num=\"45\"></td><td><pre> <span class=\"token comment\">// var positions = new Float32Array([0.5, 0.5, 0.0, 1.0])</span></pre></td></tr><tr><td data-num=\"46\"></td><td><pre> <span class=\"token comment\">// gl.vertexAttrib4fv(a_Position, positions);</span></pre></td></tr><tr><td data-num=\"47\"></td><td><pre></pre></td></tr><tr><td data-num=\"48\"></td><td><pre></pre></td></tr><tr><td data-num=\"49\"></td><td><pre> <span class=\"token comment\">// 获取 attribute 变量 a_PointSize 的存储地址</span></pre></td></tr><tr class=\"marked\"><td data-num=\"50\"></td><td><pre> <span class=\"token keyword\">var</span> a_PointSize <span class=\"token operator\">=</span> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getAttribLocation</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token property-access\">program</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'a_PointSize'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"51\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span>a_PointSize <span class=\"token operator\"><</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span></pre></td></tr><tr class=\"marked\"><td data-num=\"52\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to get the storage location of a_PointSize'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"53\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"54\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"55\"></td><td><pre> </pre></td></tr><tr><td data-num=\"56\"></td><td><pre> <span class=\"token comment\">// 传输顶点大小</span></pre></td></tr><tr class=\"marked\"><td data-num=\"57\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">vertexAttrib1f</span><span class=\"token punctuation\">(</span>a_PointSize<span class=\"token punctuation\">,</span> <span class=\"token number\">200.0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"58\"></td><td><pre></pre></td></tr><tr><td data-num=\"59\"></td><td><pre></pre></td></tr><tr><td data-num=\"60\"></td><td><pre> <span class=\"token comment\">// 设置 canvas 颜色</span></pre></td></tr><tr><td data-num=\"61\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">clearColor</span><span class=\"token punctuation\">(</span><span class=\"token number\">0.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1.0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"62\"></td><td><pre></pre></td></tr><tr><td data-num=\"63\"></td><td><pre> <span class=\"token comment\">// 清空 canvas</span></pre></td></tr><tr><td data-num=\"64\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">clear</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token constant\">COLOR_BUFFER_BIT</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"65\"></td><td><pre></pre></td></tr><tr><td data-num=\"66\"></td><td><pre> <span class=\"token comment\">// 绘制一个点</span></pre></td></tr><tr><td data-num=\"67\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">drawArrays</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token constant\">POINTS</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"68\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><details class=\"info\"><summary>效果</summary><div>\n<p><img data-src=\"https://cdn.unistart.top/blog/webgl-study/msedge_qugtfgX54h.png\" alt=\"HelloPoint2\" /></p>\n</div></details>\n<details class=\"primary\"><summary>相关函数说明</summary><div>\n<ul>\n<li><span class=\"red\">getAttribLocation(program, name)</span>:获取 name 参数指定的 attribute 变量的存储地址。program 参数指定包含顶点着色器和片元着色器的着色器程序对象。name 参数指定要获取的 attribute 变量的名称。成功返回变量地址,失败返回 - 1。</li>\n<li><span class=\"red\">vertexAttrib3f(location, v0, v1, v2)</span>:将 (v0, v1, v2) 传递给 location 参数指定的 attribute 变量。location 参数指定要修改的 attribute 变量的地址,v0 指定填充 attribute 变量第一个分量的值,v1、v2 依次类推。它是一系列同族方法中的一个,类型的有 vertexAttrib1f、vertexAttrib2f、vertexAttrib4f。</li>\n<li><span class=\"red\">vertexAttrib4fv(location, position)</span>:vertexAttrib4f 方法的矢量版本(名称以 v 结尾),它接受类型化数组为参数。location 指定要修改的 attribute 变量的地址,position 是代表矢量的类型化数组</li>\n</ul>\n</div></details>\n<div class=\"note warning\">\n<p><ins class=\"dot\">attribute 变量只能在顶点着色器中使用,uniform 变量在顶点着色器和片元着色器中均可使用</ins>。</p>\n</div>\n<blockquote>\n<p>还可以通过 js 事件来动态传输数据到着色器中</p>\n</blockquote>\n<figure class=\"highlight html\"><figcaption data-lang=\"HTML\"><span>ColoredPoint.html</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token doctype\"><span class=\"token punctuation\"><!</span><span class=\"token doctype-tag\">DOCTYPE</span> <span class=\"token name\">html</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>html</span> <span class=\"token attr-name\">lang</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>en<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>head</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>meta</span> <span class=\"token attr-name\">charset</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>UTF-8<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>meta</span> <span class=\"token attr-name\">http-equiv</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>X-UA-Compatible<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">content</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>IE=edge<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>meta</span> <span class=\"token attr-name\">name</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>viewport<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">content</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>width=device-width, initial-scale=1.0<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>title</span><span class=\"token punctuation\">></span></span>Colored Points<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>title</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>head</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>body</span> <span class=\"token special-attr\"><span class=\"token attr-name\">onload</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token value javascript language-javascript\"><span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></span><span class=\"token punctuation\">\"</span></span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>canvas</span> <span class=\"token attr-name\">id</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>webgl<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">width</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>400<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">height</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>400<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> Please use the browser supporting \"canvas\"</pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>canvas</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>../lib/webgl-utils.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>../lib/webgl-debug.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>../lib/cuon-utils.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"><</span>script</span> <span class=\"token attr-name\">src</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>ColoredPoints.js<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span><span class=\"token script\"></span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>script</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>body</span><span class=\"token punctuation\">></span></span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\"></</span>html</span><span class=\"token punctuation\">></span></span></pre></td></tr></table></figure><figure class=\"highlight js\"><figcaption data-lang=\"JavaScript\"><span>ColoredPoint.html</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\">// 顶点着色器程序 (vertex shader program)</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token constant\">VSHADER_SOURCE</span> <span class=\"token operator\">=</span> </pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token string\">'attribute vec4 a_Position;\\n'</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token string\">'attribute float a_PointSize;\\n'</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token string\">'void main() {\\n'</span> <span class=\"token operator\">+</span> </pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token string\">' gl_Position = a_Position;\\n'</span> <span class=\"token operator\">+</span> <span class=\"token comment\">// 设置坐标</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token string\">' gl_PointSize = a_PointSize;\\n'</span> <span class=\"token operator\">+</span> <span class=\"token comment\">// 设置尺寸</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token string\">'}\\n'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre></pre></td></tr><tr><td data-num=\"10\"></td><td><pre></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token comment\">// 片元着色器程序 (Fragment shader program)</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre><span class=\"token keyword\">var</span> <span class=\"token constant\">FSHADER_SOURCE</span> <span class=\"token operator\">=</span> </pre></td></tr><tr class=\"marked\"><td data-num=\"13\"></td><td><pre> <span class=\"token string\">'precision mediump float;\\n'</span> <span class=\"token operator\">+</span></pre></td></tr><tr class=\"marked\"><td data-num=\"14\"></td><td><pre> <span class=\"token string\">'uniform vec4 u_FragColor;\\n'</span> <span class=\"token operator\">+</span> <span class=\"token comment\">//uniform 变量</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token string\">'void main() {\\n'</span> <span class=\"token operator\">+</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token string\">' gl_FragColor = u_FragColor;\\n'</span> <span class=\"token operator\">+</span> <span class=\"token comment\">// 设置颜色</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token string\">'}\\n'</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre></pre></td></tr><tr><td data-num=\"19\"></td><td><pre></pre></td></tr><tr><td data-num=\"20\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre> <span class=\"token comment\">// 获取 & lt;canvans > 元素</span></pre></td></tr><tr><td data-num=\"22\"></td><td><pre> <span class=\"token keyword\">var</span> canvans <span class=\"token operator\">=</span> <span class=\"token dom variable\">document</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getElementById</span><span class=\"token punctuation\">(</span><span class=\"token string\">'webgl'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre></pre></td></tr><tr><td data-num=\"24\"></td><td><pre> <span class=\"token comment\">// 获取 WebGL 上下文</span></pre></td></tr><tr><td data-num=\"25\"></td><td><pre> <span class=\"token keyword\">var</span> gl <span class=\"token operator\">=</span> <span class=\"token function\">getWebGLContext</span><span class=\"token punctuation\">(</span>canvans<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"26\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>gl<span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"27\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to get the rendering context for WebGL'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"28\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"29\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"30\"></td><td><pre></pre></td></tr><tr><td data-num=\"31\"></td><td><pre> <span class=\"token comment\">// 初始化着色器</span></pre></td></tr><tr><td data-num=\"32\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span><span class=\"token operator\">!</span><span class=\"token function\">initShaders</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">,</span> <span class=\"token constant\">VSHADER_SOURCE</span><span class=\"token punctuation\">,</span> <span class=\"token constant\">FSHADER_SOURCE</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"33\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to initialize shaders.'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"34\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"35\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"36\"></td><td><pre></pre></td></tr><tr><td data-num=\"37\"></td><td><pre> <span class=\"token comment\">// 获取 a_Position 变量的存储位置</span></pre></td></tr><tr><td data-num=\"38\"></td><td><pre> <span class=\"token keyword\">var</span> a_Position <span class=\"token operator\">=</span> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getAttribLocation</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token property-access\">program</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'a_Position'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"39\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span>a_Position <span class=\"token operator\"><</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"40\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to get storage location of a_Position'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"41\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"42\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"43\"></td><td><pre> <span class=\"token comment\">// 获取 a_PointSize 变量的存储位置</span></pre></td></tr><tr><td data-num=\"44\"></td><td><pre> <span class=\"token keyword\">var</span> a_PointSize <span class=\"token operator\">=</span> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getAttribLocation</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token property-access\">program</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'a_PointSize'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"45\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span>a_PointSize <span class=\"token operator\"><</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"46\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to get storage location of a_PointSize'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"47\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"48\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"49\"></td><td><pre></pre></td></tr><tr><td data-num=\"50\"></td><td><pre> <span class=\"token comment\">// 获取 u_FragColor 变量的存储位置</span></pre></td></tr><tr class=\"marked\"><td data-num=\"51\"></td><td><pre> <span class=\"token keyword\">var</span> u_FragColor <span class=\"token operator\">=</span> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getUniformLocation</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token property-access\">program</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'u_FragColor'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"52\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span><span class=\"token operator\">!</span>u_FragColor<span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span></pre></td></tr><tr class=\"marked\"><td data-num=\"53\"></td><td><pre> <span class=\"token console class-name\">console</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'Failed to get storage location of u_FragColor'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"54\"></td><td><pre> <span class=\"token keyword control-flow\">return</span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"55\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"56\"></td><td><pre></pre></td></tr><tr><td data-num=\"57\"></td><td><pre> <span class=\"token comment\">// 传入 a_PointSize</span></pre></td></tr><tr><td data-num=\"58\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">vertexAttrib1f</span><span class=\"token punctuation\">(</span>a_PointSize<span class=\"token punctuation\">,</span> <span class=\"token number\">10.0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"59\"></td><td><pre></pre></td></tr><tr><td data-num=\"60\"></td><td><pre> <span class=\"token comment\">// 设置 canvas 颜色</span></pre></td></tr><tr><td data-num=\"61\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">clearColor</span><span class=\"token punctuation\">(</span><span class=\"token number\">0.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1.0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"62\"></td><td><pre></pre></td></tr><tr><td data-num=\"63\"></td><td><pre> <span class=\"token comment\">// 清空 canvas</span></pre></td></tr><tr><td data-num=\"64\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">clear</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token constant\">COLOR_BUFFER_BIT</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"65\"></td><td><pre></pre></td></tr><tr><td data-num=\"66\"></td><td><pre> <span class=\"token comment\">// 绘制一个点</span></pre></td></tr><tr><td data-num=\"67\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">drawArrays</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token constant\">POINTS</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"68\"></td><td><pre></pre></td></tr><tr><td data-num=\"69\"></td><td><pre> <span class=\"token comment\">// 注册鼠标点击事件响应函数</span></pre></td></tr><tr class=\"marked\"><td data-num=\"70\"></td><td><pre> canvans<span class=\"token punctuation\">.</span><span class=\"token method-variable function-variable method function property-access\">onmousedown</span> <span class=\"token operator\">=</span> <span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">ev</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr class=\"marked\"><td data-num=\"71\"></td><td><pre> <span class=\"token function\">click</span><span class=\"token punctuation\">(</span>ev<span class=\"token punctuation\">,</span> gl<span class=\"token punctuation\">,</span> canvans<span class=\"token punctuation\">,</span> a_Position<span class=\"token punctuation\">,</span> u_FragColor<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr class=\"marked\"><td data-num=\"72\"></td><td><pre></pre></td></tr><tr class=\"marked\"><td data-num=\"73\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"74\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"75\"></td><td><pre></pre></td></tr><tr><td data-num=\"76\"></td><td><pre><span class=\"token keyword\">var</span> g_points <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 鼠标点击位置数组</span></pre></td></tr><tr><td data-num=\"77\"></td><td><pre><span class=\"token keyword\">var</span> g_colors <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 存储颜色的数组</span></pre></td></tr><tr><td data-num=\"78\"></td><td><pre><span class=\"token doc-comment comment\">/**</pre></td></tr><tr><td data-num=\"79\"></td><td><pre> * 点击事件响应函数</pre></td></tr><tr><td data-num=\"80\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span><span class=\"token operator\">*</span><span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">ev</span> </pre></td></tr><tr><td data-num=\"81\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span><span class=\"token operator\">*</span><span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">gl</span> </pre></td></tr><tr><td data-num=\"82\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span><span class=\"token operator\">*</span><span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">canvans</span> </pre></td></tr><tr><td data-num=\"83\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span><span class=\"token operator\">*</span><span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">a_Position</span> </pre></td></tr><tr><td data-num=\"84\"></td><td><pre> * <span class=\"token keyword\">@param</span> <span class=\"token class-name\"><span class=\"token punctuation\">{</span><span class=\"token operator\">*</span><span class=\"token punctuation\">}</span></span> <span class=\"token parameter\">u_FragColor</span></pre></td></tr><tr><td data-num=\"85\"></td><td><pre> */</span></pre></td></tr><tr><td data-num=\"86\"></td><td><pre><span class=\"token keyword\">function</span> <span class=\"token function\">click</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">ev<span class=\"token punctuation\">,</span> gl<span class=\"token punctuation\">,</span> canvas<span class=\"token punctuation\">,</span> a_Position<span class=\"token punctuation\">,</span> u_FragColor</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"87\"></td><td><pre> <span class=\"token keyword\">var</span> x <span class=\"token operator\">=</span> ev<span class=\"token punctuation\">.</span><span class=\"token property-access\">clientX</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 鼠标点击处 x 坐标</span></pre></td></tr><tr><td data-num=\"88\"></td><td><pre> <span class=\"token keyword\">var</span> y <span class=\"token operator\">=</span> ev<span class=\"token punctuation\">.</span><span class=\"token property-access\">clientY</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 鼠标点击处 y 坐标</span></pre></td></tr><tr><td data-num=\"89\"></td><td><pre> <span class=\"token keyword\">var</span> rect <span class=\"token operator\">=</span> ev<span class=\"token punctuation\">.</span><span class=\"token property-access\">target</span><span class=\"token punctuation\">.</span><span class=\"token method function property-access\">getBoundingClientRect</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"90\"></td><td><pre></pre></td></tr><tr><td data-num=\"91\"></td><td><pre> x <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span>x <span class=\"token operator\">-</span> rect<span class=\"token punctuation\">.</span><span class=\"token property-access\">left</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">-</span> canvas<span class=\"token punctuation\">.</span><span class=\"token property-access\">width</span><span class=\"token operator\">/</span><span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token operator\">/</span><span class=\"token punctuation\">(</span>canvas<span class=\"token punctuation\">.</span><span class=\"token property-access\">width</span><span class=\"token operator\">/</span><span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"92\"></td><td><pre> y <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span>canvas<span class=\"token punctuation\">.</span><span class=\"token property-access\">height</span><span class=\"token operator\">/</span><span class=\"token number\">2</span> <span class=\"token operator\">-</span> <span class=\"token punctuation\">(</span>y <span class=\"token operator\">-</span> rect<span class=\"token punctuation\">.</span><span class=\"token property-access\">top</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token operator\">/</span><span class=\"token punctuation\">(</span>canvas<span class=\"token punctuation\">.</span><span class=\"token property-access\">height</span><span class=\"token operator\">/</span><span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"93\"></td><td><pre> <span class=\"token comment\">// 将坐标存储到 g_points 数组中</span></pre></td></tr><tr><td data-num=\"94\"></td><td><pre> g_points<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">push</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span>x<span class=\"token punctuation\">,</span> y<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> </pre></td></tr><tr><td data-num=\"95\"></td><td><pre></pre></td></tr><tr><td data-num=\"96\"></td><td><pre> <span class=\"token comment\">// 将点的颜色存储到 g_colors 数组中</span></pre></td></tr><tr><td data-num=\"97\"></td><td><pre> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span>x <span class=\"token operator\">>=</span> <span class=\"token number\">0.0</span> <span class=\"token operator\">&&</span> y <span class=\"token operator\">>=</span> <span class=\"token number\">0.0</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span> <span class=\"token comment\">// 第一象限</span></pre></td></tr><tr><td data-num=\"98\"></td><td><pre> g_colors<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">push</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">1.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1.0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// 红色</span></pre></td></tr><tr><td data-num=\"99\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token keyword control-flow\">else</span> <span class=\"token keyword control-flow\">if</span><span class=\"token punctuation\">(</span>x <span class=\"token operator\"><</span> <span class=\"token number\">0.0</span> <span class=\"token operator\">&&</span> y <span class=\"token operator\"><</span> <span class=\"token number\">0.0</span> <span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span> <span class=\"token comment\">// 第三象限</span></pre></td></tr><tr><td data-num=\"100\"></td><td><pre> g_colors<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">push</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">0.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1.0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// 绿色</span></pre></td></tr><tr><td data-num=\"101\"></td><td><pre> <span class=\"token punctuation\">}</span><span class=\"token keyword control-flow\">else</span> <span class=\"token punctuation\">{</span> <span class=\"token comment\">// 其他象限</span></pre></td></tr><tr><td data-num=\"102\"></td><td><pre> g_colors<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">push</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">1.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1.0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1.0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// 白色</span></pre></td></tr><tr><td data-num=\"103\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"104\"></td><td><pre></pre></td></tr><tr><td data-num=\"105\"></td><td><pre> <span class=\"token comment\">// 清除 canvas</span></pre></td></tr><tr><td data-num=\"106\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">clear</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token constant\">COLOR_BUFFER_BIT</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"107\"></td><td><pre></pre></td></tr><tr><td data-num=\"108\"></td><td><pre> <span class=\"token keyword\">var</span> len <span class=\"token operator\">=</span> g_points<span class=\"token punctuation\">.</span><span class=\"token property-access\">length</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"109\"></td><td><pre> <span class=\"token keyword control-flow\">for</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">var</span> i<span class=\"token operator\">=</span><span class=\"token number\">0</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\"><</span> len<span class=\"token punctuation\">;</span> i<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span></pre></td></tr><tr><td data-num=\"110\"></td><td><pre> <span class=\"token keyword\">var</span> xy <span class=\"token operator\">=</span> g_points<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"111\"></td><td><pre> <span class=\"token keyword\">var</span> rgba <span class=\"token operator\">=</span> g_colors<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"112\"></td><td><pre></pre></td></tr><tr><td data-num=\"113\"></td><td><pre> <span class=\"token comment\">// 将点的位置传递到变量 a_Position 中</span></pre></td></tr><tr><td data-num=\"114\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">vertexAttrib3f</span><span class=\"token punctuation\">(</span>a_Position<span class=\"token punctuation\">,</span> xy<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> xy<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0.0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"115\"></td><td><pre> <span class=\"token comment\">// 将点的颜色传递到变量 u_FragColor 中</span></pre></td></tr><tr><td data-num=\"116\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">uniform4f</span><span class=\"token punctuation\">(</span>u_FragColor<span class=\"token punctuation\">,</span> rgba<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> rgba<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span>rgba<span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> rgba<span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"117\"></td><td><pre></pre></td></tr><tr><td data-num=\"118\"></td><td><pre> <span class=\"token comment\">// 绘制点</span></pre></td></tr><tr><td data-num=\"119\"></td><td><pre> gl<span class=\"token punctuation\">.</span><span class=\"token method function property-access\">drawArrays</span><span class=\"token punctuation\">(</span>gl<span class=\"token punctuation\">.</span><span class=\"token constant\">POINTS</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></pre></td></tr><tr><td data-num=\"120\"></td><td><pre> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"121\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><details class=\"info\"><summary>效果</summary><div>\n<p><img data-src=\"https://cdn.unistart.top/blog/webgl-study/msedge_t5BmKl5bhp.png\" alt=\"ColoredPoint\" /></p>\n</div></details>\n<details class=\"primary\"><summary>相关函数说明</summary><div>\n<ul>\n<li><span class=\"blue\">getUniformLocation(program, name)</span>: 获取 name 指定的 uniform 变量的地址。program 参数指定着色器程序对象,name 参数指定要获取的 uniform 变量名称。成功返回 uniform 变量地址,失败返回 null。</li>\n<li><span class=\"blue\">uniform4f(location, v0, v1, v2, v3)</span>:将数据 (v0, v1, v2, v3) 传递给 location 参数指定的 uniform 变量。它也是一系列同组方法中的一个,类似的有 uniform1f、uniform2f、uniform3f。</li>\n</ul>\n</div></details>\n",
"tags": [
"WebGL",
"WebGL"
]
},
{
"id": "https://unistart2.github.io/posts/fc4990cc/",
"url": "https://unistart2.github.io/posts/fc4990cc/",
"title": "MySQL8 安装",
"date_published": "2023-03-08T09:36:03.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><div class=\"note info\">\n<p>OS:OpenCloudOS 8.6(兼容 CentOS 8)<br />\n安装方式:使用 yum 安装 MySQL8</p>\n</div>\n<h2 id=\"安装mysql-yum仓库\"><a class=\"anchor\" href=\"#安装mysql-yum仓库\">#</a> 安装 MySQL Yum 仓库</h2>\n<p>去<span class=\"exturl\" data-url=\"aHR0cHM6Ly9kZXYubXlzcWwuY29tL2Rvd25sb2Fkcy9yZXBvL3l1bS8=\"> MySQL Yum Repository</span> 下载需要的 yum 仓库</p>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\">#wget https://repo.mysql.com//your-mysql-yum-repository.rpm</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token function\">wget</span> https://repo.mysql.com//mysql80-community-release-el8-4.noarch.rpm</pre></td></tr></table></figure><p>然后安装下载好的 yum 仓库</p>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token function\">rpm</span> <span class=\"token parameter variable\">-ivh</span> mysql80-community-release-el8-4.noarch.rpm</pre></td></tr></table></figure><h2 id=\"安装mysql-server\"><a class=\"anchor\" href=\"#安装mysql-server\">#</a> 安装 MySQL Server</h2>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre>yum <span class=\"token function\">install</span> <span class=\"token parameter variable\">-y</span> mysql-server</pre></td></tr></table></figure><h2 id=\"启动mysql\"><a class=\"anchor\" href=\"#启动mysql\">#</a> 启动 MySQL</h2>\n<ol>\n<li>在 Linux 机器启动 MySQL 服务端,并以后台的形式运行</li>\n</ol>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># 启动 MySQL Server 服务</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>systemctl start mysqld.service</pre></td></tr></table></figure><ol start=\"2\">\n<li>查看 MySQL Server 服务的状态</li>\n</ol>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># 查看 MySQL Server 服务的状态</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>systemctl status mysqld.service</pre></td></tr></table></figure><p>如果看到 Active: <span class=\"green\">active (running)</span> 说明 MySQL 服务端后台启动成功<br />\n 3. 查看 MySQL 启动时生成默认密码</p>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># 不同 MySQL 版本,可能路径有一点不同</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token function\">grep</span> <span class=\"token string\">\"password\"</span> /var/log/mysql/mysqld.log</pre></td></tr></table></figure><ol start=\"4\">\n<li>连接到 MySQL 服务端</li>\n</ol>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># mysql 默认创建的用户名是 root</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\"># 如果上一步看到的默认密码是空,直接不输入密码回车就好</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>mysql <span class=\"token parameter variable\">-uroot</span> <span class=\"token parameter variable\">-p</span></pre></td></tr></table></figure><ol start=\"5\">\n<li>修改默认密码</li>\n</ol>\n<figure class=\"highlight shell\"><figcaption data-lang=\"Bash\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre>ALTER <span class=\"token environment constant\">USER</span> <span class=\"token string\">'root'</span>@<span class=\"token string\">'localhost'</span> IDENTIFIED BY <span class=\"token string\">'new password'</span><span class=\"token punctuation\">;</span></pre></td></tr></table></figure>",
"tags": [
"MySQL",
"MySQL"
]
},
{
"id": "https://unistart2.github.io/posts/41a47f87/",
"url": "https://unistart2.github.io/posts/41a47f87/",
"title": "Ruby元编程(三)",
"date_published": "2023-02-26T01:49:20.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><div class=\"note info\">\n<p>参考书籍《Ruby 元编程(第二版)》<br />\nRuby 版本:书上使用的是 2.x,自己使用的 3.1.2</p>\n</div>\n<h2 id=\"动态调用方法\"><a class=\"anchor\" href=\"#动态调用方法\">#</a> 动态调用方法</h2>\n<blockquote>\n<p>使用 [Object#send] 可以进行动态调用方法。<span class=\"red\">send 方法</span>的第一个参数是要调用的方法名字(可以是字符串或符号),剩下的参数和代码块会直接传递给调用的方法,这样可以在代码运行的最后一刻决定调用那个方法,这个技巧被称为<span class=\"red\">动态派发</span>(Dynamic Dispatch)</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>示例代码</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">MyClass</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">my_method</span></span><span class=\"token punctuation\">(</span>my_arg<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> my_arg <span class=\"token operator\">*</span> <span class=\"token number\">2</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr><td data-num=\"7\"></td><td><pre>obj <span class=\"token operator\">=</span> <span class=\"token class-name\">MyClass</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token comment\"># 常规调用方式</span></pre></td></tr><tr class=\"marked\"><td data-num=\"9\"></td><td><pre>p obj<span class=\"token punctuation\">.</span>my_method<span class=\"token punctuation\">(</span><span class=\"token number\">3</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> 6</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token comment\"># 使用 send 方法进行动态调用</span></pre></td></tr><tr class=\"marked\"><td data-num=\"12\"></td><td><pre>p obj<span class=\"token punctuation\">.</span>send<span class=\"token punctuation\">(</span><span class=\"token symbol\">:my_method</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> 6</span></pre></td></tr></table></figure><h2 id=\"动态定义方法\"><a class=\"anchor\" href=\"#动态定义方法\">#</a> 动态定义方法</h2>\n<blockquote>\n<p>使用<span class=\"red\"> Module#define_method</span> 方法可以随时定义一个方法,<ins class=\"dot\">只需要提供一个方法名和充当方法主体的块</ins>。</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>示例代码</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">MyClass2</span> </pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token comment\"># 动态定义了 my_method 方法</span></pre></td></tr><tr class=\"marked\"><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">define_method</span> <span class=\"token symbol\">:my_method</span> <span class=\"token keyword\">do</span> <span class=\"token operator\">|</span>my_arg<span class=\"token operator\">|</span></pre></td></tr><tr class=\"marked\"><td data-num=\"4\"></td><td><pre> my_arg <span class=\"token operator\">*</span> <span class=\"token number\">3</span></pre></td></tr><tr class=\"marked\"><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre></pre></td></tr><tr><td data-num=\"8\"></td><td><pre>obj2 <span class=\"token operator\">=</span> <span class=\"token class-name\">MyClass2</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr class=\"marked\"><td data-num=\"9\"></td><td><pre>p obj2<span class=\"token punctuation\">.</span>my_method<span class=\"token punctuation\">(</span><span class=\"token number\">2</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> 6</span></pre></td></tr></table></figure><h2 id=\"幽灵方法\"><a class=\"anchor\" href=\"#幽灵方法\">#</a> 幽灵方法</h2>\n<blockquote>\n<p><ins class=\"wavy\">在 Ruby 中,编译器并不检查方法的调用行为</ins>,这意味着可以带调用一个并不存在的方法。<br />\n当在对象上调用方法时,如果该方法在<span class=\"red\">祖先链</span>上找不到,那么就会调用<span class=\"red\"> method_missing</span> 方法,它是<span class=\"red\"> BasicObject</span> 的一个私有实例方法,<ins class=\"dot\">默认行为是抛出<span class=\"red\"> NoMethodError</span> 错误</ins>。<br />\n我们可以通过重写<span class=\"red\"> method_missing</span> 方法,<ins class=\"dot\">来拦截那些实际不存在的方法</ins>,达到实现<span class=\"red\">幽灵方法</span>(Ghost Method)的效果。</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>示例代码</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">Lawyer</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">something</span></span><span class=\"token punctuation\">;</span> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre>nick <span class=\"token operator\">=</span> <span class=\"token class-name\">Lawyer</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr class=\"marked\"><td data-num=\"6\"></td><td><pre>nick<span class=\"token punctuation\">.</span>talk_simple <span class=\"token comment\"># 运行时抛出异常: NoMethodError</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre></pre></td></tr><tr><td data-num=\"8\"></td><td><pre></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token comment\"># 重写 method_missing 方法</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">Lawyer2</span> </pre></td></tr><tr class=\"marked\"><td data-num=\"11\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">method_missing</span></span><span class=\"token punctuation\">(</span>method<span class=\"token punctuation\">,</span> <span class=\"token operator\">*</span>args<span class=\"token punctuation\">)</span></pre></td></tr><tr class=\"marked\"><td data-num=\"12\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">\"Your call: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">method</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\"> (</span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">args<span class=\"token punctuation\">.</span>join<span class=\"token punctuation\">(</span><span class=\"token string-literal\"><span class=\"token string\">', '</span></span><span class=\"token punctuation\">)</span></span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">)\"</span></span></pre></td></tr><tr class=\"marked\"><td data-num=\"13\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">\"(You also passed it a block)\"</span></span> <span class=\"token keyword\">if</span> block_given<span class=\"token operator\">?</span></pre></td></tr><tr class=\"marked\"><td data-num=\"14\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre></pre></td></tr><tr><td data-num=\"17\"></td><td><pre>bob <span class=\"token operator\">=</span> <span class=\"token class-name\">Lawyer2</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr class=\"marked\"><td data-num=\"18\"></td><td><pre>bob<span class=\"token punctuation\">.</span>talk_simple<span class=\"token punctuation\">(</span><span class=\"token string-literal\"><span class=\"token string\">'a'</span></span><span class=\"token punctuation\">,</span> <span class=\"token string-literal\"><span class=\"token string\">'b'</span></span><span class=\"token punctuation\">)</span> <span class=\"token keyword\">do</span> </pre></td></tr><tr class=\"marked\"><td data-num=\"19\"></td><td><pre> <span class=\"token comment\"># a block</span></pre></td></tr><tr class=\"marked\"><td data-num=\"20\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre><span class=\"token comment\">#=> 输出结果如下:</span></pre></td></tr><tr><td data-num=\"22\"></td><td><pre><span class=\"token comment\">#=> \"Your call: talk_simple (a, b)\"</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre><span class=\"token comment\">#=> \"(You also passed it a block)\"</span></pre></td></tr></table></figure><h2 id=\"respond_to方法\"><a class=\"anchor\" href=\"#respond_to方法\">#</a> respond_to? 方法</h2>\n<blockquote>\n<p>使用<span class=\"red\"> respond_to?</span> 方法可以询问一个对象是否有对应的方法。<br />\n当询问对象是否存在一个<span class=\"red\">幽灵方法</span>时,会再次调用<span class=\"red\"> respond_to_missing?</span> 方法,将其结果作为返回值返回,这个方法是一个<span class=\"red\">钩子方法</span>(Hook Method),默认实现是返回 false,当使用 [method_missing] 处理幽灵方法时,<ins class=\"dot\">一般就需要对其进行重写</ins>。</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>示例代码</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">Demo</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">method_missing</span></span><span class=\"token punctuation\">(</span>name<span class=\"token punctuation\">,</span> <span class=\"token operator\">*</span>args<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token comment\"># 只处理名称为 hello 的幽灵方法</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">super</span> <span class=\"token keyword\">if</span> name<span class=\"token punctuation\">.</span>to_s <span class=\"token operator\">!=</span> <span class=\"token string-literal\"><span class=\"token string\">'hello'</span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> p <span class=\"token string-literal\"><span class=\"token string\">\"method name: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">name</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">, args: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">args</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">\"</span></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token comment\"># 实际存在的一个方法</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">test</span></span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre></pre></td></tr><tr><td data-num=\"13\"></td><td><pre>a <span class=\"token operator\">=</span> <span class=\"token class-name\">Demo</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre></pre></td></tr><tr class=\"marked\"><td data-num=\"15\"></td><td><pre>p <span class=\"token string-literal\"><span class=\"token string\">\"respond_to: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">a<span class=\"token punctuation\">.</span>respond_to<span class=\"token operator\">?</span><span class=\"token punctuation\">(</span><span class=\"token string-literal\"><span class=\"token string\">'test'</span></span><span class=\"token punctuation\">)</span></span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">\"</span></span> <span class=\"token comment\">#=> true</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre><span class=\"token comment\"># 在调用 respond_to? 方法时,如果方法是一个幽灵方法,它会调用 respond_to_missing? 方法确定返回值</span></pre></td></tr><tr class=\"marked\"><td data-num=\"17\"></td><td><pre>p <span class=\"token string-literal\"><span class=\"token string\">\"respond_to: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">a<span class=\"token punctuation\">.</span>respond_to<span class=\"token operator\">?</span><span class=\"token punctuation\">(</span><span class=\"token string-literal\"><span class=\"token string\">'hello'</span></span><span class=\"token punctuation\">)</span></span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">\"</span></span> <span class=\"token comment\">#=> false</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre></pre></td></tr><tr><td data-num=\"19\"></td><td><pre></pre></td></tr><tr><td data-num=\"20\"></td><td><pre><span class=\"token comment\"># 重写 respond_to_missing? 方法之后</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">Demo</span></pre></td></tr><tr><td data-num=\"22\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">method_missing</span></span><span class=\"token punctuation\">(</span>name<span class=\"token punctuation\">,</span> <span class=\"token operator\">*</span>args<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre> <span class=\"token comment\"># 只处理名称为 hello 的幽灵方法</span></pre></td></tr><tr><td data-num=\"24\"></td><td><pre> <span class=\"token keyword\">super</span> <span class=\"token keyword\">if</span> name<span class=\"token punctuation\">.</span>to_s <span class=\"token operator\">!=</span> <span class=\"token string-literal\"><span class=\"token string\">'hello'</span></span></pre></td></tr><tr><td data-num=\"25\"></td><td><pre> p <span class=\"token string-literal\"><span class=\"token string\">\"method name: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">name</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">, args: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">args</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">\"</span></span></pre></td></tr><tr><td data-num=\"26\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"27\"></td><td><pre></pre></td></tr><tr><td data-num=\"28\"></td><td><pre> <span class=\"token comment\"># 实际存在的一个方法</span></pre></td></tr><tr><td data-num=\"29\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">test</span></span></pre></td></tr><tr><td data-num=\"30\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"31\"></td><td><pre> </pre></td></tr><tr><td data-num=\"32\"></td><td><pre> <span class=\"token comment\"># 重写 respond_to_missing? 方法</span></pre></td></tr><tr class=\"marked\"><td data-num=\"33\"></td><td><pre> <span class=\"token comment\"># 只对名为 hello 的幽灵方法返回 true</span></pre></td></tr><tr class=\"marked\"><td data-num=\"34\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">respond_to_missing</span></span><span class=\"token operator\">?</span><span class=\"token punctuation\">(</span>method<span class=\"token punctuation\">,</span> include_private<span class=\"token operator\">=</span><span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span></pre></td></tr><tr class=\"marked\"><td data-num=\"35\"></td><td><pre> method <span class=\"token operator\">==</span> <span class=\"token symbol\">:hello</span> <span class=\"token operator\">||</span> <span class=\"token keyword\">super</span></pre></td></tr><tr><td data-num=\"36\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"37\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"38\"></td><td><pre></pre></td></tr><tr><td data-num=\"39\"></td><td><pre>a <span class=\"token operator\">=</span> <span class=\"token class-name\">Demo</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr class=\"marked\"><td data-num=\"40\"></td><td><pre></pre></td></tr><tr><td data-num=\"41\"></td><td><pre>p <span class=\"token string-literal\"><span class=\"token string\">\"respond_to: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">a<span class=\"token punctuation\">.</span>respond_to<span class=\"token operator\">?</span><span class=\"token punctuation\">(</span><span class=\"token string-literal\"><span class=\"token string\">'test'</span></span><span class=\"token punctuation\">)</span></span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">\"</span></span> <span class=\"token comment\">#=> true</span></pre></td></tr><tr class=\"marked\"><td data-num=\"42\"></td><td><pre><span class=\"token comment\"># 当询问的幽灵方法名为 hello 时,返回 true</span></pre></td></tr><tr><td data-num=\"43\"></td><td><pre>p <span class=\"token string-literal\"><span class=\"token string\">\"respond_to: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">a<span class=\"token punctuation\">.</span>respond_to<span class=\"token operator\">?</span><span class=\"token punctuation\">(</span><span class=\"token string-literal\"><span class=\"token string\">'hello'</span></span><span class=\"token punctuation\">)</span></span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">\"</span></span> <span class=\"token comment\">#=> true</span></pre></td></tr><tr><td data-num=\"44\"></td><td><pre><span class=\"token comment\"># 否则,返回 false</span></pre></td></tr><tr><td data-num=\"45\"></td><td><pre>p <span class=\"token string-literal\"><span class=\"token string\">\"respond_to: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">a<span class=\"token punctuation\">.</span>respond_to<span class=\"token operator\">?</span><span class=\"token punctuation\">(</span><span class=\"token string-literal\"><span class=\"token string\">'hello2'</span></span><span class=\"token punctuation\">)</span></span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">\"</span></span> <span class=\"token comment\">#=> false</span></pre></td></tr></table></figure><h2 id=\"白板类\"><a class=\"anchor\" href=\"#白板类\">#</a> 白板类</h2>\n<blockquote>\n<p><span class=\"red\">白板类</span>是对幽灵方法的完善,如果<span class=\"red\">幽灵方法</span>和一个<span class=\"red\">真实方法</span>同名,那么<span class=\"red\">幽灵方法</span>会被忽略。<br />\n如果不需要继承来的方法(真实方法),可以通过删除它来解决问题,<ins class=\"dot\">这种拥有极少数方法的类称为<span class=\"red\">白板类</span></ins>(Blank Slate)。</p>\n</blockquote>\n<ul>\n<li>\n<p>实现方式一:</p>\n<ol>\n<li>通过继承<span class=\"red\"> BasicObject 类</span>,因为其只有少数几个实例方法。</li>\n<li>通过自定义<span class=\"red\">白班类</span>实现,这样可以确保实例方法的名称不会和自己要使用的幽灵方法重名。</li>\n</ol>\n</li>\n<li>\n<p>实现方式二:</p>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>示例代码</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">Test</span></pre></td></tr><tr class=\"marked\"><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">method_missing</span></span><span class=\"token punctuation\">(</span>method<span class=\"token punctuation\">)</span></pre></td></tr><tr class=\"marked\"><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">case</span> method</pre></td></tr><tr class=\"marked\"><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">when</span> <span class=\"token symbol\">:display</span></pre></td></tr><tr class=\"marked\"><td data-num=\"5\"></td><td><pre> p <span class=\"token string-literal\"><span class=\"token string\">'Display method (Ghost Method)'</span></span></pre></td></tr><tr class=\"marked\"><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr class=\"marked\"><td data-num=\"7\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre></pre></td></tr><tr><td data-num=\"10\"></td><td><pre>t <span class=\"token operator\">=</span> <span class=\"token class-name\">Test</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr class=\"marked\"><td data-num=\"11\"></td><td><pre>t<span class=\"token punctuation\">.</span>display <span class=\"token comment\">#=> nil</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre><span class=\"token comment\"># 上面返回 nil 是因为 Test 类自动继承自 Object 类,而 Object 中已经有一个 display 方法了,它回返回 nil</span></pre></td></tr><tr class=\"marked\"><td data-num=\"13\"></td><td><pre><span class=\"token builtin\">Object</span><span class=\"token punctuation\">.</span>methods<span class=\"token punctuation\">.</span>grep<span class=\"token punctuation\">(</span><span class=\"token regex-literal\"><span class=\"token regex\">/^display/</span></span><span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> [:display]</span></pre></td></tr></table></figure><p>1、使用<span class=\"red\"> Module#undef_method</span> 方法:<ins class=\"dot\">可以删除(包括继承而来的)所有方法</ins>。</p>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>使用undef_method删除方法</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">BlankSlate</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token keyword\">self</span><span class=\"token punctuation\">.</span><span class=\"token function\">hide</span></span><span class=\"token punctuation\">(</span>name<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token comment\"># 删除了继承自 Object 的 display 方法</span></pre></td></tr><tr class=\"marked\"><td data-num=\"4\"></td><td><pre> undef_method name <span class=\"token keyword\">if</span> name <span class=\"token operator\">==</span> <span class=\"token symbol\">:display</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr class=\"marked\"><td data-num=\"7\"></td><td><pre> instance_methods<span class=\"token punctuation\">.</span><span class=\"token keyword\">each</span> <span class=\"token punctuation\">{</span> <span class=\"token operator\">|</span>m<span class=\"token operator\">|</span> hide<span class=\"token punctuation\">(</span>m<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">}</span> </pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre></pre></td></tr><tr class=\"marked\"><td data-num=\"10\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">Test</span> <span class=\"token operator\"><</span> BlankSlate</pre></td></tr><tr><td data-num=\"11\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">method_missing</span></span><span class=\"token punctuation\">(</span>method<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token keyword\">case</span> method</pre></td></tr><tr><td data-num=\"13\"></td><td><pre> <span class=\"token keyword\">when</span> <span class=\"token symbol\">:display</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> p <span class=\"token string-literal\"><span class=\"token string\">'Display method (Ghost Method)'</span></span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre></pre></td></tr><tr><td data-num=\"19\"></td><td><pre> t <span class=\"token operator\">=</span> <span class=\"token class-name\">Test</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr class=\"marked\"><td data-num=\"20\"></td><td><pre> t<span class=\"token punctuation\">.</span>display <span class=\"token comment\">#=> \"Display method (Ghost Method)\"</span></pre></td></tr></table></figure><p>2、使用<span class=\"red\"> Module#remove_method</span> 方法:<ins class=\"dot\">只能删除当前类中定义的方法</ins>。</p>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>使用remove_method删除方法</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">Test</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token keyword\">self</span><span class=\"token punctuation\">.</span><span class=\"token function\">hide</span></span><span class=\"token punctuation\">(</span>name<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token comment\"># 删除了当前类 Test 中的实例方法 current</span></pre></td></tr><tr class=\"marked\"><td data-num=\"4\"></td><td><pre> remove_method<span class=\"token punctuation\">(</span>name<span class=\"token punctuation\">)</span> <span class=\"token keyword\">if</span> name <span class=\"token operator\">==</span> <span class=\"token symbol\">:current</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">method_missing</span></span><span class=\"token punctuation\">(</span>method<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token keyword\">case</span> method</pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token keyword\">when</span> <span class=\"token symbol\">:current</span> </pre></td></tr><tr><td data-num=\"10\"></td><td><pre> p <span class=\"token string-literal\"><span class=\"token string\">'Current method (Ghost Method)'</span></span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">current</span></span> </pre></td></tr><tr><td data-num=\"15\"></td><td><pre> p <span class=\"token string-literal\"><span class=\"token string\">\"current method...\"</span></span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre></pre></td></tr><tr class=\"marked\"><td data-num=\"18\"></td><td><pre> instance_methods<span class=\"token punctuation\">.</span><span class=\"token keyword\">each</span> <span class=\"token punctuation\">{</span> <span class=\"token operator\">|</span>m<span class=\"token operator\">|</span> hide<span class=\"token punctuation\">(</span>m<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"20\"></td><td><pre></pre></td></tr><tr><td data-num=\"21\"></td><td><pre>t <span class=\"token operator\">=</span> <span class=\"token class-name\">Test</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr class=\"marked\"><td data-num=\"22\"></td><td><pre>t<span class=\"token punctuation\">.</span>current <span class=\"token comment\">#=> \"Current method (Ghost Method)\"</span></pre></td></tr></table></figure></li>\n</ul>\n",
"tags": [
"Ruby",
"Ruby",
"Metaprogramming"
]
},
{
"id": "https://unistart2.github.io/posts/cbadcd5/",
"url": "https://unistart2.github.io/posts/cbadcd5/",
"title": "Ruby元编程(二)",
"date_published": "2023-02-24T03:48:39.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><div class=\"note info\">\n<p>参考书籍《Ruby 元编程(第二版)》<br />\nRuby 版本:书上使用的是 2.x,自己使用的 3.1.2</p>\n</div>\n<h2 id=\"关于self和私有方法调用之间的问题\"><a class=\"anchor\" href=\"#关于self和私有方法调用之间的问题\">#</a> 关于 self 和私有方法调用之间的问题</h2>\n<h3 id=\"ruby27之前版本\"><a class=\"anchor\" href=\"#ruby27之前版本\">#</a> Ruby2.7 之前版本</h3>\n<blockquote>\n<p>在<span class=\"red\"> Ruby 2.7 之前</span>,可以<ins class=\"dot\">使用 self 调用一个私有写入 / 赋值方法</ins>,<ins class=\"dot\">但是使用 self 调用任何其他私有方法将抛出<span class=\"blue\"> NoMethodError</span></ins>。</p>\n</blockquote>\n<p>示例如下:</p>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>ruby2.7以前</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">Counter</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">reset</span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">self</span><span class=\"token punctuation\">.</span>count <span class=\"token operator\">=</span> <span class=\"token number\">0</span> <span class=\"token comment\"># 允许调用</span></pre></td></tr><tr class=\"marked\"><td data-num=\"4\"></td><td><pre> puts <span class=\"token keyword\">self</span><span class=\"token punctuation\">.</span>count <span class=\"token comment\"># 抛出异常</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token keyword\">private</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> attr_accessor <span class=\"token symbol\">:count</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token class-name\">Counter</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span><span class=\"token punctuation\">.</span>reset</pre></td></tr></table></figure><p>上面的示例执行 <code>self.count = 0</code> 语句时,不会出现问题,但是执行到 <code>puts self.count</code> 语句时就会抛出<span class=\"blue\"> NoMethodError</span> 错误。<span class=\"hint--info hint--rounded hint--top\" data-hint=\"这和Java/Python等编程语言中的this/self的用法不一致\" ontouchstart>这种前后不一致的行为让人感到十分困惑</span>。</p>\n<h3 id=\"ruby27及以后版本\"><a class=\"anchor\" href=\"#ruby27及以后版本\">#</a> Ruby2.7 及以后版本</h3>\n<blockquote>\n<p>在<span class=\"red\"> Ruby 2.7</span> 版本,Ruby 修复这种让人感到困惑的不一致行为。换而言之,从<span class=\"red\"> Ruby 2.7</span> 开始,可以使用 self 调用任何私有方法,就像其他语言那样。</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>ruby2.7及以后</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">Counter</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">reset</span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">self</span><span class=\"token punctuation\">.</span>count <span class=\"token operator\">=</span> <span class=\"token number\">0</span> <span class=\"token comment\"># 允许调用</span></pre></td></tr><tr class=\"marked\"><td data-num=\"4\"></td><td><pre> puts <span class=\"token keyword\">self</span><span class=\"token punctuation\">.</span>count <span class=\"token comment\"># 允许调用</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">private</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> attr_accessor <span class=\"token symbol\">:count</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token class-name\">Counter</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span><span class=\"token punctuation\">.</span>reset</pre></td></tr></table></figure><h2 id=\"细化refine\"><a class=\"anchor\" href=\"#细化refine\">#</a> 细化(Refine)</h2>\n<blockquote>\n<p>使用<span class=\"red\">打开类</span>的方式扩展代码,有一个很明显的问题:修改时全局性的,很有可能无意中影响其他代码的正常使用。但通过<span class=\"red\">细化</span>的方式可以使修改只作用于局部,很好的解决了之前的问题。</p>\n</blockquote>\n<h3 id=\"细化的定义方式\"><a class=\"anchor\" href=\"#细化的定义方式\">#</a> 细化的定义方式</h3>\n<blockquote>\n<p>使用<span class=\"red\">细化</span>,首先需要先定一个模块,然后在这个模块的定义中调用<span class=\"red\"> refine</span> 方法。</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>示例代码</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">module</span> <span class=\"token class-name\">StringExtentions</span></pre></td></tr><tr class=\"marked\"><td data-num=\"2\"></td><td><pre> refine <span class=\"token builtin\">String</span> <span class=\"token keyword\">do</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">something</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token string-literal\"><span class=\"token string\">\"something...\"</span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><h3 id=\"通过using让细化生效\"><a class=\"anchor\" href=\"#通过using让细化生效\">#</a> 通过 using 让细化生效</h3>\n<blockquote>\n<p>在上述代码中,为<span class=\"red\"> String 类</span>添加了一个<span class=\"red\"> something 方法</span>,但<span class=\"red\">细化</span>在默认情况下并不生效,需要使用<span class=\"red\"> using</span> 让修改生效。</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>示例代码</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">module</span> <span class=\"token class-name\">StringExtentions</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> refine <span class=\"token builtin\">String</span> <span class=\"token keyword\">do</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">something</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token string-literal\"><span class=\"token string\">\"something...\"</span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre></pre></td></tr><tr class=\"marked\"><td data-num=\"9\"></td><td><pre><span class=\"token comment\"># 在当前文件内生效</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre>using StringExtentions</pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token string-literal\"><span class=\"token string\">\"My Something\"</span></span><span class=\"token punctuation\">.</span>something <span class=\"token comment\">#=> something...</span></pre></td></tr></table></figure><blockquote>\n<p>从<span class=\"red\"> Ruby 2.1</span> 开始,可以在一个模块内调用<span class=\"red\"> using</span> 方法,此时<span class=\"red\">细化</span>的作用范围仅限于当前模块。</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>示例代码</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">module</span> <span class=\"token class-name\">StringExtentions</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> refine <span class=\"token builtin\">String</span> <span class=\"token keyword\">do</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token comment\"># 为 String 类修改了 reverse 方法的定义</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">reverse</span></span> </pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token string-literal\"><span class=\"token string\">\"My reverse method...\"</span></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token keyword\">module</span> <span class=\"token class-name\">StringStuff</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> <span class=\"token comment\"># 在 StringStuff 模块内生效</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> using StringExtentions</pre></td></tr><tr class=\"marked\"><td data-num=\"13\"></td><td><pre> <span class=\"token string-literal\"><span class=\"token string\">\"test\"</span></span><span class=\"token punctuation\">.</span>reverse <span class=\"token comment\">#=> \"My reverse method...\"</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre></pre></td></tr><tr><td data-num=\"16\"></td><td><pre><span class=\"token comment\"># 出 StringStuff 模块失效</span></pre></td></tr><tr class=\"marked\"><td data-num=\"17\"></td><td><pre><span class=\"token string-literal\"><span class=\"token string\">\"test\"</span></span><span class=\"token punctuation\">.</span>reverse <span class=\"token comment\">#=> \"tset\"</span></pre></td></tr></table></figure>",
"tags": [
"Ruby",
"Ruby",
"Metaprogramming"
]
},
{
"id": "https://unistart2.github.io/posts/c0d30721/",
"url": "https://unistart2.github.io/posts/c0d30721/",
"title": "多个Github Pages设置",
"date_published": "2023-01-01T13:40:44.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><div class=\"note info\">\n<p>参考<span class=\"exturl\" data-url=\"aHR0cHM6Ly9zZWdtZW50ZmF1bHQuY29tL2EvMTE5MDAwMDAwMzk0Njk2OQ==\">单个 GitHub 帐号下添加多个 GitHub Pages 的相关问题</span><br />\n参考<span class=\"exturl\" data-url=\"aHR0cHM6Ly9kb2NzLmdpdGh1Yi5jb20vemgvcGFnZXMvZ2V0dGluZy1zdGFydGVkLXdpdGgtZ2l0aHViLXBhZ2VzL2Fib3V0LWdpdGh1Yi1wYWdlcw==\"> Github Pages Docs</span></p>\n</div>\n<h1 id=\"概述\"><a class=\"anchor\" href=\"#概述\">#</a> 概述</h1>\n<ul>\n<li>GitHub Pages 分为: <span class=\"red\">用户 / 组织主页</span>以及<span class=\"red\">项目主页</span></li>\n<li><span class=\"red\">用户主页或组织主页</span>分别都只能存在一个<span class=\"red\"> repo</span>,而<span class=\"red\">项目主页</span>则没有<span class=\"red\"> repo</span> 数量限制</li>\n<li>若要发布<span class=\"red\">用户主页</span>,必须创建名为<span class=\"red\"> <username>.github.io</span> 的 repo; 若要发布<span class=\"red\">组织主页</span>,必须创建名为<span class=\"red\"> <organization>.github.io</span> 的 repo; 而<span class=\"red\">项目主页</span>的仓库则<span class=\"red\">没有名称限制</span></li>\n<li><span class=\"red\">用户主页</span>默认域名形式为<span class=\"red\"> <username>.github.io</span></li>\n<li><span class=\"red\">组织主页</span>默认域名形式为<span class=\"red\"> <organization>.github.io </span></li>\n<li><span class=\"red\">项目主页</span>默认域名形式为<span class=\"red\"> <username>.github.io/<repository></span> 或者<span class=\"red\"> <organization>.github.io/<repository></span></li>\n<li><span class=\"red\">用户主页或组织主页</span>的展示内容以<span class=\"red\">主分支</span> (<span class=\"red\">master</span> 分支或<span class=\"red\"> main</span> 分支) 里的文件为准;而<span class=\"red\">项目主页</span>的展示内容以<span class=\"red\"> gh-pages</span> 分支内的文件为准</li>\n</ul>\n<h1 id=\"示例\"><a class=\"anchor\" href=\"#示例\">#</a> 示例</h1>\n<blockquote>\n<p>以部署一个<span class=\"red\"> VuePress</span> 项目主页为例</p>\n</blockquote>\n<ol>\n<li>参考<span class=\"exturl\" data-url=\"aHR0cHM6Ly92Mi52dWVwcmVzcy52dWVqcy5vcmcvemgvZ3VpZGUv\"> VuePress 指南</span>在本地建立好项目</li>\n<li>建立一个 Github 仓库,取名任意</li>\n<li>参考<span class=\"exturl\" data-url=\"aHR0cHM6Ly92Mi52dWVwcmVzcy52dWVqcy5vcmcvemgvZ3VpZGUvZGVwbG95bWVudC5odG1s\"> VuePress 部署</span>部署项目到 Github 上\n<ul>\n<li>注意在<span class=\"exturl\" data-url=\"aHR0cHM6Ly92Mi52dWVwcmVzcy52dWVqcy5vcmcvemgvZ3VpZGUvY29uZmlndXJhdGlvbi5odG1s\">配置文件</span>中设置正确的<span class=\"exturl\" data-url=\"aHR0cHM6Ly92Mi52dWVwcmVzcy52dWVqcy5vcmcvemgvcmVmZXJlbmNlL2NvbmZpZy5odG1sI2Jhc2U=\"> base</span> 选项,在该示例中 <code>base</code> 应为 <code>第2步建立的仓库名</code></li>\n<li>编写 <code>GitHub Actions</code> 工作流</li>\n<li>推送 <code>本地仓库</code> 到 <code>GitHub远程仓库</code></li>\n<li>进入 <code>GitHub项目仓库</code> 找到 <code>Settings</code> 下的 <code>Pages</code> ,修改 <code>Build and deployment</code> 下的 <code>branch</code> 选项为 <code>gh-pages</code> (此操作会触发 GitHub Actions 进行重新部署)</li>\n</ul>\n</li>\n<li>访问部署好的页面 ( <code>Github项目仓库</code> 的 <code>Settings</code> 下的 <code>Pages</code> 页面可以看到)</li>\n</ol>\n<h1 id=\"github-actions工作流参考\"><a class=\"anchor\" href=\"#github-actions工作流参考\">#</a> GitHub Actions 工作流参考</h1>\n<blockquote>\n<p>在本地项目根目录下新建 <code>.github/workflows/docs.yml</code> 文件来设置 <code>工作流</code> <br />\n下面的 <code>工作流示例</code> 和<span class=\"exturl\" data-url=\"aHR0cHM6Ly92Mi52dWVwcmVzcy52dWVqcy5vcmcvemgvZ3VpZGUvZGVwbG95bWVudC5odG1sI2dpdGh1Yi1wYWdlcw==\"> VuePress 官方写的工作流</span>写的略有不同,官方使用的 <code>pnpm</code> 作为包管理器,而 <code>工作流示例</code> 直接使用 <code>Node.js</code> 自带的 <code>npm</code> ,而且新增一步安装 <code>vuepress@next</code> 依赖(针对使用 <code>VuePrss 2.x</code> 版本),<br />\n <code>构建脚本步骤</code> 也进行了修改</p>\n</blockquote>\n<figure class=\"highlight yaml\"><figcaption data-lang=\"YAML\"><span>docs.yaml</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Build and Deploy</pre></td></tr><tr><td data-num=\"2\"></td><td><pre></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token key atrule\">on</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token comment\"># 每当 push 到 main 分支时触发部署</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token key atrule\">push</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token key atrule\">branches</span><span class=\"token punctuation\">:</span> <span class=\"token punctuation\">[</span>main<span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token comment\"># 手动触发部署</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token key atrule\">workflow_dispatch</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token key atrule\">jobs</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> <span class=\"token key atrule\">build-and-deploy</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token key atrule\">runs-on</span><span class=\"token punctuation\">:</span> ubuntu<span class=\"token punctuation\">-</span>latest</pre></td></tr><tr><td data-num=\"13\"></td><td><pre></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token key atrule\">steps</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Checkout </pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token key atrule\">uses</span><span class=\"token punctuation\">:</span> actions/checkout@v3</pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token key atrule\">with</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre> <span class=\"token comment\"># “最近更新时间” 等 git 日志相关信息,需要拉取全部提交记录</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre> <span class=\"token key atrule\">fetch-depth</span><span class=\"token punctuation\">:</span> <span class=\"token number\">0</span></pre></td></tr><tr><td data-num=\"20\"></td><td><pre></pre></td></tr><tr><td data-num=\"21\"></td><td><pre> <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Setup Node.js</pre></td></tr><tr><td data-num=\"22\"></td><td><pre> <span class=\"token key atrule\">uses</span><span class=\"token punctuation\">:</span> actions/setup<span class=\"token punctuation\">-</span>node@v3</pre></td></tr><tr><td data-num=\"23\"></td><td><pre> <span class=\"token key atrule\">with</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"24\"></td><td><pre> <span class=\"token comment\"># 选择要使用的 node 版本</span></pre></td></tr><tr><td data-num=\"25\"></td><td><pre> <span class=\"token key atrule\">node-version</span><span class=\"token punctuation\">:</span> <span class=\"token number\">16</span></pre></td></tr><tr><td data-num=\"26\"></td><td><pre> <span class=\"token comment\"># 缓存 npm 依赖</span></pre></td></tr><tr><td data-num=\"27\"></td><td><pre> <span class=\"token key atrule\">cache</span><span class=\"token punctuation\">:</span> npm</pre></td></tr><tr><td data-num=\"28\"></td><td><pre></pre></td></tr><tr><td data-num=\"29\"></td><td><pre> <span class=\"token comment\"># 安装 VuePress(基于 VuePress 2.x)</span></pre></td></tr><tr class=\"marked\"><td data-num=\"30\"></td><td><pre> <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Install VuePress</pre></td></tr><tr class=\"marked\"><td data-num=\"31\"></td><td><pre> <span class=\"token key atrule\">run</span><span class=\"token punctuation\">:</span> npm install <span class=\"token punctuation\">-</span>D vuepress@next</pre></td></tr><tr><td data-num=\"32\"></td><td><pre></pre></td></tr><tr><td data-num=\"33\"></td><td><pre> <span class=\"token comment\"># 运行构建脚本(使用 npm 命令)</span></pre></td></tr><tr class=\"marked\"><td data-num=\"34\"></td><td><pre> <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Build VuePress site</pre></td></tr><tr class=\"marked\"><td data-num=\"35\"></td><td><pre> <span class=\"token key atrule\">run</span><span class=\"token punctuation\">:</span> npm run docs<span class=\"token punctuation\">:</span>build</pre></td></tr><tr><td data-num=\"36\"></td><td><pre></pre></td></tr><tr><td data-num=\"37\"></td><td><pre> <span class=\"token comment\"># 查看 workflow 的文档来获取更多信息</span></pre></td></tr><tr><td data-num=\"38\"></td><td><pre> <span class=\"token comment\"># @see https://github.com/crazy-max/ghaction-github-pages</span></pre></td></tr><tr><td data-num=\"39\"></td><td><pre> <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Deploy to GitHub Pages</pre></td></tr><tr><td data-num=\"40\"></td><td><pre> <span class=\"token key atrule\">uses</span><span class=\"token punctuation\">:</span> crazy<span class=\"token punctuation\">-</span>max/ghaction<span class=\"token punctuation\">-</span>github<span class=\"token punctuation\">-</span>pages@v2</pre></td></tr><tr><td data-num=\"41\"></td><td><pre> <span class=\"token key atrule\">with</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"42\"></td><td><pre> <span class=\"token comment\"># 部署到 gh-pages 分支</span></pre></td></tr><tr><td data-num=\"43\"></td><td><pre> <span class=\"token key atrule\">target_branch</span><span class=\"token punctuation\">:</span> gh<span class=\"token punctuation\">-</span>pages</pre></td></tr><tr><td data-num=\"44\"></td><td><pre> <span class=\"token comment\"># 部署目录为 VuePress 的默认输出目录</span></pre></td></tr><tr><td data-num=\"45\"></td><td><pre> <span class=\"token key atrule\">build_dir</span><span class=\"token punctuation\">:</span> docs/.vuepress/dist</pre></td></tr><tr><td data-num=\"46\"></td><td><pre> <span class=\"token key atrule\">env</span><span class=\"token punctuation\">:</span></pre></td></tr><tr><td data-num=\"47\"></td><td><pre> <span class=\"token comment\"># @see https://docs.github.com/cn/actions/reference/authentication-in-a-workflow#about-the-github_token-secret</span></pre></td></tr><tr><td data-num=\"48\"></td><td><pre> <span class=\"token key atrule\">GITHUB_TOKEN</span><span class=\"token punctuation\">:</span> $<<span class=\"token tag\">!--swig</span>0<span class=\"token punctuation\">-</span><span class=\"token punctuation\">-</span><span class=\"token punctuation\">></span></pre></td></tr></table></figure>",
"tags": [
"Github",
"Github",
"Github Pages"
]
},
{
"id": "https://unistart2.github.io/posts/fc6fd6b9/",
"url": "https://unistart2.github.io/posts/fc6fd6b9/",
"title": "Ruby元编程(一)",
"date_published": "2022-11-28T08:38:48.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><div class=\"note info\">\n<p>参考书籍《Ruby 元编程(第二版)》<br />\nRuby 版本:书上使用的是 2.x,自己使用的 3.1.2</p>\n</div>\n<h2 id=\"打开类\"><a class=\"anchor\" href=\"#打开类\">#</a> 打开类</h2>\n<blockquote>\n<p>Ruby 中的<span class=\"red\"> class 关键字只有在第一次使用时是作为类型声明</span>语句使用,用于定义一个目前不存在的类。<br />\n<span class=\"red\">对于已经存在的类,class 关键字的作用更像是一个作用域操作符</span>,把你带到类的上下文中,让你可以在里面定义方法。<br />\n这种重新打开已有类并对之进行动态修改的方式就称为<ins class=\"dot red\">打开类</ins>。<br />\n在 Ruby 中<ins class=\"wavy\">标准库中的类也是可以重新打开的</ins>。</p>\n</blockquote>\n<ol>\n<li>打开类的实例</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>打开类</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># monetize 是 Ruby 中一个管理资金和现金的工具类</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\"># 没有安装过这个包的话可以使用 gem install 命名安装</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">require</span> <span class=\"token string-literal\"><span class=\"token string\">\"monetize\"</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token comment\"># 创建 Money 对象</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre>bargain_price <span class=\"token operator\">=</span> Monetize<span class=\"token punctuation\">.</span>from_numeric<span class=\"token punctuation\">(</span><span class=\"token number\">99</span><span class=\"token punctuation\">,</span> <span class=\"token string-literal\"><span class=\"token string\">\"USD\"</span></span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre>bargain_price<span class=\"token punctuation\">.</span>format</pre></td></tr><tr><td data-num=\"7\"></td><td><pre></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token comment\"># 这个类库还提供了一种便捷形式:</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token comment\"># 可以通过 Numeric#to_money 方法,把任意数值转为一个 Money 对象</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre>standard_price <span class=\"token operator\">=</span> <span class=\"token number\">100.</span>to_money<span class=\"token punctuation\">(</span><span class=\"token string-literal\"><span class=\"token string\">\"USD\"</span></span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre>standard_price<span class=\"token punctuation\">.</span>format</pre></td></tr></table></figure><blockquote>\n<p>monetize 包就是通过<span class=\"red\">打开类</span>的方式,对 Numeric 这个 Ruby 标准类进行扩展的。<br />\n在 <span class=\"red\">gems/monetize-1.12.0/lib/monetize/core_extensions/numberic.rb</span> 中可以找到如下代码</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">Numeric</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">to_money</span></span><span class=\"token punctuation\">(</span>currency <span class=\"token operator\">=</span> <span class=\"token keyword\">nil</span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> Monetize<span class=\"token punctuation\">.</span>from_numeric<span class=\"token punctuation\">(</span><span class=\"token keyword\">self</span><span class=\"token punctuation\">,</span> currency <span class=\"token operator\">||</span> Money<span class=\"token punctuation\">.</span>default_currency<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><ol start=\"2\">\n<li>打开类的问题</li>\n</ol>\n<blockquote>\n<p>对标准库类来说,虽然使用<span class=\"red\">打开类</span>的情况是常见的,但是要注意<span class=\"red\">在打开类时<ins class=\"dot\">不要定义和类库中同名的方法</ins></span>。</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># 假如你打开了 Array 类并定义 replace 方法,用于对数组中的元素进行替换</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">Array</span></span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">replace</span></span><span class=\"token punctuation\">(</span>original<span class=\"token punctuation\">,</span> replacement<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">self</span><span class=\"token punctuation\">.</span>map <span class=\"token punctuation\">{</span> <span class=\"token operator\">|</span>e<span class=\"token operator\">|</span> e <span class=\"token operator\">==</span> original <span class=\"token operator\">?</span> replcement <span class=\"token operator\">:</span> e <span class=\"token punctuation\">}</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><blockquote>\n<p>因为<span class=\"red\"> Array</span> 类中原来就有<span class=\"red\"> replace</span> 方法,在打开类中定义<span class=\"red\"> replace</span> 方法时,就会覆盖原有的<span class=\"red\"> replace</span> 方法。<br />\n<ins class=\"dot\">可能导致依赖原有<span class=\"red\"> replace</span> 方法的一些库、代码块出现异常或错误</ins>。</p>\n</blockquote>\n<h2 id=\"实例变量-方法\"><a class=\"anchor\" href=\"#实例变量-方法\">#</a> 实例变量、方法</h2>\n<blockquote>\n<p>和<span class=\"red\"> Java</span> 这样的静态语言不同,<span class=\"red\">Ruby</span> 中<ins class=\"dot\">对象的类和它的实例变量没有关系</ins>。<br />\n当对象调用方法并给实例变量赋值时,才会产生在对象中。因此,<ins class=\"blue wavy\">同一个类创建的不同对象可能会有不同的实例变量</ins>。</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>实例变量和类</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># Ruby 中实例变量以 \"@\" 符号开头</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\"># initialize 方法作用类似于 Java 类中的构造方法,只能显示定义一次,不能像 Java 中进行方法重载</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">MyClass</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token variable\">@filed1</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">initialize</span></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token variable\">@filed2</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre></pre></td></tr><tr><td data-num=\"10\"></td><td><pre>obj <span class=\"token operator\">=</span> <span class=\"token class-name\">MyClass</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre>p <span class=\"token punctuation\">(</span>obj<span class=\"token punctuation\">.</span>instance_variables<span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> []</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre></pre></td></tr><tr><td data-num=\"13\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">MyClass2</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre> <span class=\"token variable\">@filed1</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre> <span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">initialize</span></span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre> <span class=\"token variable\">@field2</span> <span class=\"token operator\">=</span> <span class=\"token number\">1</span> <span class=\"token comment\"># 产生实例变量 @filed2</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"18\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre></pre></td></tr><tr><td data-num=\"20\"></td><td><pre>obj2 <span class=\"token operator\">=</span> <span class=\"token class-name\">MyClass2</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr><tr><td data-num=\"21\"></td><td><pre>p <span class=\"token punctuation\">(</span>obj2<span class=\"token punctuation\">.</span>instance_variables<span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> [:@field2]</span></pre></td></tr></table></figure><blockquote>\n<p>除了实例变量外,对象还有方法。通过 Oject#methods 方法,可以获得一个对象的方法列表。<br />\n但实际上,<ins class=\"red wavy\">一个对象仅包含它的实例变量和其所属类的引用</ins>。<ins class=\"dot\">而<span class=\"blue\">方法是存在对象的类中</span>,这也是同一个类的对象共享相同方法的原因</ins>。</p>\n</blockquote>\n<figure class=\"highlight text\"><figcaption data-lang=\"text\"><span>对象、方法和类的关系图</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>+------------+ class +-------------+</pre></td></tr><tr><td data-num=\"2\"></td><td><pre>| obj +-------->| SomeClass |</pre></td></tr><tr><td data-num=\"3\"></td><td><pre>+------------+ +-------------+</pre></td></tr><tr><td data-num=\"4\"></td><td><pre>| | | |</pre></td></tr><tr><td data-num=\"5\"></td><td><pre>| @filed = 1 | | method1 |</pre></td></tr><tr><td data-num=\"6\"></td><td><pre>| | | |</pre></td></tr><tr><td data-num=\"7\"></td><td><pre>| .... | | ... |</pre></td></tr><tr><td data-num=\"8\"></td><td><pre>| | | |</pre></td></tr><tr><td data-num=\"9\"></td><td><pre>+------------+ | |</pre></td></tr><tr><td data-num=\"10\"></td><td><pre> +-------------+</pre></td></tr></table></figure><h2 id=\"类的本质\"><a class=\"anchor\" href=\"#类的本质\">#</a> 类的本质</h2>\n<h3 id=\"类是class的对象\"><a class=\"anchor\" href=\"#类是class的对象\">#</a> 类是 Class 的对象</h3>\n<blockquote>\n<p><ins class=\"dot\"><span class=\"red\">类本身也是对象</span></ins>,类像其他对象一样,也有自己的类,叫做<span class=\"red\"> Class</span>。</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre>p <span class=\"token string-literal\"><span class=\"token string\">\"hello\"</span></span><span class=\"token punctuation\">.</span><span class=\"token keyword\">class</span> <span class=\"token comment\">#=> String</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>p <span class=\"token builtin\">String</span><span class=\"token punctuation\">.</span><span class=\"token keyword\">class</span> <span class=\"token comment\">#=> Class</span></pre></td></tr></table></figure><blockquote>\n<p>把<span class=\"red\">类</span>看作<span class=\"red\"> Class</span> 的对象的话,那么<span class=\"red\">一个类中的方法就是 Class 的实例方法</span>。</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># false 表示忽略继承的方法</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>p <span class=\"token builtin\">Class</span><span class=\"token punctuation\">.</span>instance_methods<span class=\"token punctuation\">(</span><span class=\"token boolean\">false</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> [:allocate, :superclass, :subclasses, :new]</span></pre></td></tr></table></figure><blockquote>\n<p>Ruby 中除<span class=\"red\"> BasicObject</span> 这个最顶层类没有父类以外,其他类都有自己的父类,像<span class=\"red\"> Array 类</span>的父类是<span class=\"red\"> Object 类</span>,<span class=\"red\">Object 类</span>的父类是<span class=\"red\"> BasicObject 类</span>。<br />\n而<ins class=\"dot\"> Class 的父类是<span class=\"red\"> Module</span></ins>。<ins class=\"dot\">换而言之,每个类都是一个模块</ins>。<br />\n<ins class=\"wavy\">如果希望代码包含(include)到别的代码中,就使用模块(可以用来充当命名空间);如果希望代码被实例化或者被继承,就使用类</ins>。</p>\n</blockquote>\n<div class=\"note primary\">\n<p><ins class=\"dot\"><span class=\"red\">类</span>就是一个<span class=\"red\">对象</span>(Class 类的一个实例)外加一组<span class=\"red\">实例方法</span>和一个对其<span class=\"red\">父类的引用</span></ins></p>\n</div>\n<div class=\"note warning\">\n<p>注意:<ins class=\"red dot\">Class 类的类是它本身</ins></p>\n</div>\n<h3 id=\"类名和常量\"><a class=\"anchor\" href=\"#类名和常量\">#</a> 类名和常量</h3>\n<blockquote>\n<p>和普通对象一样,<ins class=\"wavy\">类也可以通过引用来访问。变量可以像引用普通对象一样引用类</ins>。</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">MyClass</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token comment\"># my_class 也是 Class 实例的一个引用</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre>my_class <span class=\"token operator\">=</span> MyClass</pre></td></tr><tr><td data-num=\"5\"></td><td><pre>obj <span class=\"token operator\">=</span> my_class<span class=\"token punctuation\">.</span><span class=\"token keyword\">new</span></pre></td></tr></table></figure><blockquote>\n<p>MyClass 和 my_class 都是对同一个 Class 类的实例的引用,<ins class=\"wavy\">唯一的区别在于,my_class 是变量,而 MyClass 是一个常量</ins>。<br />\n<ins class=\"red dot\">Ruby 中任何以大写字母开头的引用(包括模块名和类名)都是常量</ins>。</p>\n</blockquote>\n<div class=\"note info\">\n<p>常量可以通过<span class=\"red\">路径</span>来标识。类似于 C++ 中的域操作符,Ruby 中常量的路径使用双冒号进行分隔。</p>\n</div>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>常量路径</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">module</span> <span class=\"token class-name\">M</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">class</span> <span class=\"token class-name\">C</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token constant\">X</span> <span class=\"token operator\">=</span> <span class=\"token string-literal\"><span class=\"token string\">'a content'</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> <span class=\"token constant\">C</span><span class=\"token double-colon punctuation\">::</span><span class=\"token constant\">X</span> <span class=\"token comment\">#=> 'a content'</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token constant\">M</span><span class=\"token double-colon punctuation\">::</span><span class=\"token constant\">C</span><span class=\"token double-colon punctuation\">::</span><span class=\"token constant\">X</span> <span class=\"token comment\">#=> 'a content'</span></pre></td></tr></table></figure><div class=\"note info\">\n<p><ins class=\"dot\">双冒号开头可以表示路径的根位置</ins></p>\n</div>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>根路径</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>Outer <span class=\"token operator\">=</span> <span class=\"token string-literal\"><span class=\"token string\">'a root-level constant'</span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword\">module</span> <span class=\"token class-name\">Y</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> Inner <span class=\"token operator\">=</span> <span class=\"token string-literal\"><span class=\"token string\">'a constant in Y'</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> p Inner <span class=\"token comment\">#=> 'a constant in Y'</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> p <span class=\"token double-colon punctuation\">::</span>Outer <span class=\"token comment\">#=> 'a root-level constant'</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><figure class=\"highlight text\"><figcaption data-lang=\"text\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre>对象模型图</pre></td></tr><tr><td data-num=\"2\"></td><td><pre> +-------------+</pre></td></tr><tr><td data-num=\"3\"></td><td><pre> | |</pre></td></tr><tr><td data-num=\"4\"></td><td><pre> | BasicObject |</pre></td></tr><tr><td data-num=\"5\"></td><td><pre> +-------------+ class</pre></td></tr><tr><td data-num=\"6\"></td><td><pre> | +------------------------------------+</pre></td></tr><tr><td data-num=\"7\"></td><td><pre> | ... | |</pre></td></tr><tr><td data-num=\"8\"></td><td><pre> | | |</pre></td></tr><tr><td data-num=\"9\"></td><td><pre> +-------------+ |</pre></td></tr><tr><td data-num=\"10\"></td><td><pre> ^ |</pre></td></tr><tr><td data-num=\"11\"></td><td><pre> | |</pre></td></tr><tr><td data-num=\"12\"></td><td><pre> | superclass |</pre></td></tr><tr><td data-num=\"13\"></td><td><pre> | |</pre></td></tr><tr><td data-num=\"14\"></td><td><pre> +------+------+ +-------------+ |</pre></td></tr><tr><td data-num=\"15\"></td><td><pre> | | | | |</pre></td></tr><tr><td data-num=\"16\"></td><td><pre> | Object +------+ | Module | |</pre></td></tr><tr><td data-num=\"17\"></td><td><pre> +-------------+ | +-------------+ class |</pre></td></tr><tr><td data-num=\"18\"></td><td><pre> | | | | +-------+ |</pre></td></tr><tr><td data-num=\"19\"></td><td><pre> | ... | | | ... | | |</pre></td></tr><tr><td data-num=\"20\"></td><td><pre> | | | | | | |</pre></td></tr><tr><td data-num=\"21\"></td><td><pre> +-------------+ | +-------------+ | |</pre></td></tr><tr><td data-num=\"22\"></td><td><pre> ^ | ^ | |</pre></td></tr><tr><td data-num=\"23\"></td><td><pre> | | | | |</pre></td></tr><tr><td data-num=\"24\"></td><td><pre> | superclass | | superclass | |</pre></td></tr><tr><td data-num=\"25\"></td><td><pre>+--------+ | | | | |</pre></td></tr><tr><td data-num=\"26\"></td><td><pre>| obj1 | +------+------+ | +------+-------+ | |</pre></td></tr><tr><td data-num=\"27\"></td><td><pre>| +--+ | | +--->| | | |</pre></td></tr><tr><td data-num=\"28\"></td><td><pre>+--------+ | class | | | |<-----+ |</pre></td></tr><tr><td data-num=\"29\"></td><td><pre> +--------->| MyClass | class | Class | |</pre></td></tr><tr><td data-num=\"30\"></td><td><pre> +-------------+---------->+--------------+ |</pre></td></tr><tr><td data-num=\"31\"></td><td><pre>+--------+ +--------->| ... | | ... | |</pre></td></tr><tr><td data-num=\"32\"></td><td><pre>| obj2 | | class | | | |<--------+</pre></td></tr><tr><td data-num=\"33\"></td><td><pre>| +--+ +-------------+ +-----------+--+</pre></td></tr><tr><td data-num=\"34\"></td><td><pre>+--------+ ^ |</pre></td></tr><tr><td data-num=\"35\"></td><td><pre> | |</pre></td></tr><tr><td data-num=\"36\"></td><td><pre> +------+</pre></td></tr><tr><td data-num=\"37\"></td><td><pre> class</pre></td></tr></table></figure><h2 id=\"方法查找\"><a class=\"anchor\" href=\"#方法查找\">#</a> 方法查找</h2>\n<p>方法查找相关的两个概念:</p>\n<ol>\n<li>接收者(receiver)</li>\n</ol>\n<blockquote>\n<p><span class=\"red\">接收者</span>就是调用方法所在的对象。比如,my_string.reverse () 语句中,my_string 就是接收者。</p>\n</blockquote>\n<ol start=\"2\">\n<li>祖先链(ancestors chain)</li>\n</ol>\n<blockquote>\n<p>从<span class=\"red\">当前类</span>开始向上查找<span class=\"red\">超类</span>,直到找到<span class=\"red\"> BasicObject 类</span>为止,<ins class=\"dot\">所<span class=\"red\">经过的类路径</span>就称为该类的<span class=\"red\">祖先链</span></ins></p>\n</blockquote>\n<div class=\"note info\">\n<p><ins class=\"wavy\">方法查找的过程:Ruby 首先在<span class=\"red\">接收者的类中查找</span>,然后再顺着<span class=\"red\">祖先链</span>向上查找,直到找到这个方法为止,如果找到<span class=\"red\"> BasicObject</span> 还没有找到会报错(<span class=\"red\">NoMethodError</span>)</ins></p>\n</div>\n<div class=\"note info\">\n<p><ins class=\"red dot\">祖先链中也包括模块</ins>。当一个模块包含在一个类(或一个模块)中时,Ruby 会把这个模块加入该类的祖先链中。可以使用<span class=\"red\"> include</span> 和<span class=\"blue\"> prepend</span> 将模块插入到一个类的祖先链中,<span class=\"red\">include</span> 在祖先链的插入位置是<span class=\"red\">当前类的上方</span>,<span class=\"blue\">prepend</span> 则是<span class=\"blue\">当前类的下方</span>。可以通过<span class=\"red\"> ancestors</span> 方法获得类的祖先链</p>\n</div>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>include和prepend</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># 定义一个模块 M1</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword\">module</span> <span class=\"token class-name\">M1</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token comment\"># 定义一个类 C1,它包含 M1</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">C1</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token keyword\">include</span> <span class=\"token constant\">M1</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token comment\"># 定一个类 C2,它也包含 M1</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token keyword\">class</span> <span class=\"token class-name\">C2</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre> <span class=\"token keyword\">prepend</span> <span class=\"token constant\">M1</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre></pre></td></tr><tr><td data-num=\"15\"></td><td><pre><span class=\"token comment\"># 打印 C1、C2 的祖先链</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre>p <span class=\"token constant\">C1</span><span class=\"token punctuation\">.</span>ancestors <span class=\"token comment\">#=> [C1, M1, Object, PP::ObjectMixin, Kernel, BasicObject]</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre>p <span class=\"token constant\">C2</span><span class=\"token punctuation\">.</span>ancestors <span class=\"token comment\">#=> [M1, C2, Object, PP::ObjectMixin, Kernel, BasicObject]</span></pre></td></tr></table></figure><div class=\"note warning\">\n<p><ins class=\"dot\">在<span class=\"red\"> Ruby3</span> 中<span class=\"red\"> prepend</span> 一个<span class=\"red\">模块</span>时,它总会加入到祖先链中,即使它在祖先链中已经出现过;而<span class=\"red\"> include</span> 一个模块时,它只会加入到祖先链中一次,如果已有,就不会再加入了</ins>。</p>\n<p><ins class=\"dot\">在<span class=\"red\"> Ruby2</span> 中<span class=\"red\"> prepend</span> 和<span class=\"red\"> include</span> 都只会加入模块一次</ins></p>\n</div>\n",
"tags": [
"Ruby",
"Ruby",
"Metaprogramming"
]
},
{
"id": "https://unistart2.github.io/posts/f1ffccca/",
"url": "https://unistart2.github.io/posts/f1ffccca/",
"title": "Ruby学习-异常处理",
"date_published": "2022-11-22T11:03:31.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><h2 id=\"异常处理语句\"><a class=\"anchor\" href=\"#异常处理语句\">#</a> 异常处理语句</h2>\n<div class=\"note primary\">\n<p>Ruby 使用 begin~rescue~end 语句描述异常处理,类似于 Java 中的 try~catch 块</p>\n</div>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>异常处理</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">begin</span> </pre></td></tr><tr><td data-num=\"2\"></td><td><pre> 可能发生发生异常异常的代码</pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">rescue</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> 发生异常时的处理</pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><div class=\"note info\">\n<p>rescue 后面可以指定一个变量名,用于获取异常对象</p>\n</div>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>获取异常对象</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">begin</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token operator\">...</span><span class=\"token punctuation\">.</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">rescue</span> <span class=\"token operator\">=></span> 引用异常对象的变量</pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token operator\">...</span><span class=\"token punctuation\">.</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><div class=\"note warning\">\n<p>即使在 rescue 后不指定变量名,Ruby 也会把异常对象赋值给变量 $!</p>\n</div>\n<p><span class=\"label primary\">异常发生时被自动赋值的变量</span></p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align:center\">变量</th>\n<th style=\"text-align:center\">作用</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td style=\"text-align:center\">$!</td>\n<td style=\"text-align:center\">最后发生的异常(异常对象)</td>\n</tr>\n<tr>\n<td style=\"text-align:center\">$@</td>\n<td style=\"text-align:center\">最后发生的异常的位置信息</td>\n</tr>\n</tbody>\n</table>\n<p><span class=\"label info\">异常对象的方法</span></p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align:center\">方法名</th>\n<th style=\"text-align:center\">作用</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td style=\"text-align:center\">class</td>\n<td style=\"text-align:center\">获取异常的种类</td>\n</tr>\n<tr>\n<td style=\"text-align:center\">message</td>\n<td style=\"text-align:center\">获取异常信息</td>\n</tr>\n<tr>\n<td style=\"text-align:center\">backtrace</td>\n<td style=\"text-align:center\">获取异常发生的位置信息(<span class=\"katex\"><span class=\"katex-mathml\"><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><semantics><mrow><mi mathvariant=\"normal\">@</mi><mtext>和</mtext></mrow><annotation encoding=\"application/x-tex\">@和</annotation></semantics></math></span><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.69444em;vertical-align:0em;\"></span><span class=\"mord\">@</span><span class=\"mord cjk_fallback\">和</span></span></span></span>!.backtrace 等价)</td>\n</tr>\n</tbody>\n</table>\n<h2 id=\"后处理\"><a class=\"anchor\" href=\"#后处理\">#</a> 后处理</h2>\n<blockquote>\n<p>不管是否发生异常都希望进行的处理,可以放在 ensure 关键字之后,<br />\n类似 Java 中 try~catch~finally 中的 finally,一般由于资源的释放等</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>ensure关键字使用</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">begin</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> 可能发生异常的代码块</pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">rescue</span> <span class=\"token operator\">=></span> ex</pre></td></tr><tr><td data-num=\"4\"></td><td><pre> 发生异常后的处理</pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">ensure</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> 不管是否发生异常都会执行的处理</pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><h2 id=\"重试\"><a class=\"anchor\" href=\"#重试\">#</a> 重试</h2>\n<blockquote>\n<p><ins class=\"dot\">Ruby 中支持异常重试</ins>,这和 Java、C、Rust 中的异常处理方式是不同的<br />\n可以在 rescue 后的代码块中使用 retry 语句,<ins class=\"dot\">让 Ruby 从头开始执行 begin 块</ins></p>\n</blockquote>\n<div class=\"note danger\">\n<p><span class=\"red\">在 rescue 中使用 retry 进行异常重试时要注意避免陷入死循环的情况</span></p>\n</div>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>异常重试</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">begin</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> 可能发生异常的代码块</pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">rescue</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">retry</span> <span class=\"token comment\"># 重头执行 begin 代码块</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><h2 id=\"指定需要捕捉的异常\"><a class=\"anchor\" href=\"#指定需要捕捉的异常\">#</a> 指定需要捕捉的异常</h2>\n<blockquote>\n<p>Ruby 中所有异常都是 Exception 类的子类</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">begin</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token operator\">...</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">rescue</span> exception1<span class=\"token punctuation\">,</span> exception2 <span class=\"token operator\">=></span> ex1</pre></td></tr><tr><td data-num=\"4\"></td><td><pre> 对exception1或exception2的处理</pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">rescue</span> exception3 <span class=\"token operator\">=></span> ex2</pre></td></tr><tr><td data-num=\"6\"></td><td><pre> 对exception3的处理</pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token keyword\">rescue</span> </pre></td></tr><tr><td data-num=\"8\"></td><td><pre> 对上述异常以外的异常进行的处理</pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token keyword\">ensure</span> </pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token operator\">...</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><h2 id=\"手动抛出异常\"><a class=\"anchor\" href=\"#手动抛出异常\">#</a> 手动抛出异常</h2>\n<blockquote>\n<p>Ruby 中使用 raise 进行手动抛出异常,类似于 Java 中的 throw</p>\n</blockquote>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>手动抛出异常</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># raise 方法的调用方式</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\"># 1、抛出 RuntimError 异常,并把 message 设置给异常对象</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">raise</span> message</pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token comment\"># 2、抛出指定的异常</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token keyword\">raise</span> 异常类</pre></td></tr><tr><td data-num=\"7\"></td><td><pre></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token comment\"># 3、抛出指定的异常,并将 message 设置给异常对象</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token keyword\">raise</span> 异常类 message</pre></td></tr><tr><td data-num=\"10\"></td><td><pre></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token comment\"># 4、在 rescue 外抛出 RuntimeError,在 rescue 中调用时,会再次抛出最后一次发生的异常</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre><span class=\"token keyword\">raise</span></pre></td></tr></table></figure>",
"tags": [
"Ruby",
"Ruby"
]
},
{
"id": "https://unistart2.github.io/posts/f112a2c2/",
"url": "https://unistart2.github.io/posts/f112a2c2/",
"title": "Ruby学习-基础知识",
"date_published": "2022-11-10T07:53:43.000Z",
"content_html": "<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.jsdelivr.net/npm/[email protected]/dist/hexo-tag-hint.min.css\"><h2 id=\"变量\"><a class=\"anchor\" href=\"#变量\">#</a> 变量</h2>\n<p>Ruby 中有四种类型的变量,<ins class=\"wavy\">变量的命名方式决定了变量的类型</ins></p>\n<ol>\n<li>\n<p>局部变量<br />\n以英文字母或者_开头</p>\n</li>\n<li>\n<p>全局变量<br />\n以 $ 符号开头</p>\n</li>\n<li>\n<p>实例变量<br />\n以 @ 符号开头</p>\n</li>\n<li>\n<p>类变量<br />\n以 @@ 开头</p>\n</li>\n</ol>\n<blockquote>\n<p>除了上述四种类型以外,<ins class=\"wavy\">还有<span class=\"red\">伪变量</span>、<span class=\"blue\">预定义变量</span>这两种特殊变量</ins>。它们是 Ruby 预先定义好的<br />\n代表某特定值的特殊变量。<ins>伪变量不能改变其本身的值,对其赋值会产生错误</ins>,Ruby 中,self、<br />\ntrue、false、nil 等都是伪变量。<ins>预定义变量全部都是以 $ 开头的变量</ins>,$$ 就是其中一个,<br />\n它的值表示当前执行中的 Ruby 进程 ID</p>\n</blockquote>\n<div class=\"note info\">\n<p>常量:<ins>以大写英文字母开头,多个单词间使用下划线 (_) 隔开</ins>。比如 Ruby 的运行版本 (RUBY_VERSION)、命令行参数数组 (ARGV) 等,都是 Ruby 预先定义好的常量。对已经赋值的常量再进行赋值时,Ruby 会做出警告</p>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>对常量再次赋值</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">001</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> <span class=\"token constant\">IS_CONSTANT</span><span class=\"token operator\">=</span><span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">002</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> <span class=\"token constant\">IS_CONSTANT</span><span class=\"token operator\">=</span><span class=\"token number\">2</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token punctuation\">(</span>irb<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">2</span><span class=\"token operator\">:</span> warning<span class=\"token operator\">:</span> already initialized constant <span class=\"token constant\">IS_CONSTANT</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token punctuation\">(</span>irb<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">1</span><span class=\"token operator\">:</span> warning<span class=\"token operator\">:</span> previous definition of <span class=\"token constant\">IS_CONSTANT</span> was here</pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token number\">2</span></pre></td></tr></table></figure></div>\n<h2 id=\"多重赋值\"><a class=\"anchor\" href=\"#多重赋值\">#</a> 多重赋值</h2>\n<ol>\n<li>同时对 a,b,c 变量进行赋值</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>多重赋值</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">001</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> a<span class=\"token punctuation\">,</span> b<span class=\"token punctuation\">,</span> c <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token comment\"># 即使 = 号两边数量不相等也不会报错</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token comment\"># 左边个数较多时,Ruby 会自动将 nil 赋值给未分配的变量</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">002</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> x<span class=\"token punctuation\">,</span> y<span class=\"token punctuation\">,</span> z <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">003</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> p <span class=\"token punctuation\">[</span>x<span class=\"token punctuation\">,</span> y<span class=\"token punctuation\">,</span> z<span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">nil</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">nil</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre></pre></td></tr><tr><td data-num=\"12\"></td><td><pre><span class=\"token comment\"># 右边个数较多时,Ruby 会自动忽略多余的值</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">004</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> m<span class=\"token punctuation\">,</span> n <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">005</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> p <span class=\"token punctuation\">[</span>m<span class=\"token punctuation\">,</span> n<span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"16\"></td><td><pre><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span></pre></td></tr></table></figure><ol start=\"2\">\n<li>变量前加上 *,Ruby 会将未分配的值封装成数组赋值给该变量</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>多重赋值</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">006</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> q<span class=\"token punctuation\">,</span> w<span class=\"token punctuation\">,</span> <span class=\"token operator\">*</span>t <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">,</span> <span class=\"token number\">5</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">,</span> <span class=\"token number\">5</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">007</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> p <span class=\"token punctuation\">[</span>q<span class=\"token punctuation\">,</span> w<span class=\"token punctuation\">,</span> t<span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">,</span> <span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">,</span> <span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr><td data-num=\"7\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">008</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> d<span class=\"token punctuation\">,</span> <span class=\"token operator\">*</span>e<span class=\"token punctuation\">,</span> f <span class=\"token operator\">=</span> <span class=\"token number\">10</span><span class=\"token punctuation\">,</span> <span class=\"token number\">11</span><span class=\"token punctuation\">,</span> <span class=\"token number\">12</span><span class=\"token punctuation\">,</span> <span class=\"token number\">13</span><span class=\"token punctuation\">,</span> <span class=\"token number\">14</span><span class=\"token punctuation\">,</span> <span class=\"token number\">15</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">,</span> <span class=\"token number\">11</span><span class=\"token punctuation\">,</span> <span class=\"token number\">12</span><span class=\"token punctuation\">,</span> <span class=\"token number\">13</span><span class=\"token punctuation\">,</span> <span class=\"token number\">14</span><span class=\"token punctuation\">,</span> <span class=\"token number\">15</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">009</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> p <span class=\"token punctuation\">[</span>d<span class=\"token punctuation\">,</span> e<span class=\"token punctuation\">,</span> f<span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">,</span> <span class=\"token number\">12</span><span class=\"token punctuation\">,</span> <span class=\"token number\">13</span><span class=\"token punctuation\">,</span> <span class=\"token number\">14</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token number\">15</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">10</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token number\">11</span><span class=\"token punctuation\">,</span> <span class=\"token number\">12</span><span class=\"token punctuation\">,</span> <span class=\"token number\">13</span><span class=\"token punctuation\">,</span> <span class=\"token number\">14</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token number\">15</span><span class=\"token punctuation\">]</span></pre></td></tr></table></figure><ol start=\"3\">\n<li>用数组赋值时,左边有多个变量时,Ruby 会自动获取数组的元素进行多重赋值</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>多重赋值</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">001</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> arr <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">002</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> a<span class=\"token punctuation\">,</span> b <span class=\"token operator\">=</span> arr</pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">003</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> p <span class=\"token punctuation\">[</span>a<span class=\"token punctuation\">,</span> b<span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">]</span></pre></td></tr></table></figure><ol start=\"4\">\n<li>获取嵌套数组的元素</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>多重赋值</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">004</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> arr <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string-literal\"><span class=\"token string\">\"nickname\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token string-literal\"><span class=\"token string\">\"age\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token number\">20</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token string-literal\"><span class=\"token string\">\"nickname\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token string-literal\"><span class=\"token string\">\"age\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token number\">20</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">005</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> nickname<span class=\"token punctuation\">,</span> age <span class=\"token operator\">=</span> arr</pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token string-literal\"><span class=\"token string\">\"nickname\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token string-literal\"><span class=\"token string\">\"age\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token number\">20</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">006</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> p nickname</pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token string-literal\"><span class=\"token string\">\"nickname\"</span></span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token string-literal\"><span class=\"token string\">\"nickname\"</span></span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">007</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> p age</pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token punctuation\">[</span><span class=\"token string-literal\"><span class=\"token string\">\"age\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token number\">20</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token string-literal\"><span class=\"token string\">\"age\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token number\">20</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre></pre></td></tr><tr><td data-num=\"12\"></td><td><pre><span class=\"token comment\"># 使用 () 包裹变量,可以再进一步将内部数组的元素值取出来</span></pre></td></tr><tr><td data-num=\"13\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">008</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> arr <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">,</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"14\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"15\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">009</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> a<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span>b1<span class=\"token punctuation\">,</span> b2<span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span> c <span class=\"token operator\">=</span> arr</pre></td></tr><tr><td data-num=\"16\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"17\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">010</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> p b1</pre></td></tr><tr><td data-num=\"18\"></td><td><pre><span class=\"token number\">2</span></pre></td></tr><tr><td data-num=\"19\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token number\">2</span></pre></td></tr><tr><td data-num=\"20\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">011</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span> p b2</pre></td></tr><tr><td data-num=\"21\"></td><td><pre><span class=\"token number\">3</span></pre></td></tr><tr><td data-num=\"22\"></td><td><pre><span class=\"token operator\">=></span> <span class=\"token number\">3</span></pre></td></tr><tr><td data-num=\"23\"></td><td><pre>irb<span class=\"token punctuation\">(</span>main<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span><span class=\"token number\">012</span><span class=\"token operator\">:</span><span class=\"token number\">0</span><span class=\"token operator\">></span></pre></td></tr></table></figure><h2 id=\"条件语句\"><a class=\"anchor\" href=\"#条件语句\">#</a> 条件语句</h2>\n<div class=\"note primary\">\n<p>在 Ruby 中,<ins class=\"wavy\">真值可以是除 false、nil 以外的所有对象,假值只有 false、nil 两种情况</ins>。</p>\n</div>\n<div class=\"note info\">\n<p>条件语句中的 then 可以省略</p>\n</div>\n<ol>\n<li>if 语句</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>if语句</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>a <span class=\"token operator\">=</span> <span class=\"token number\">10</span> </pre></td></tr><tr><td data-num=\"2\"></td><td><pre>b <span class=\"token operator\">=</span> <span class=\"token number\">20</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token keyword\">if</span> a <span class=\"token operator\">></span> b</pre></td></tr><tr><td data-num=\"5\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">'a比b大'</span></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token keyword\">elsif</span> a <span class=\"token operator\"><</span> b</pre></td></tr><tr><td data-num=\"7\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">'a比b小'</span></span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token keyword\">else</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">'a等于b'</span></span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><ol start=\"2\">\n<li>unless 语句</li>\n</ol>\n<div class=\"note warning\">\n<p>和 if 语句不同,unless 语句是条件为假时进行处理</p>\n</div>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>unless语句</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>a <span class=\"token operator\">=</span> <span class=\"token number\">10</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>b <span class=\"token operator\">=</span> <span class=\"token number\">20</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">unless</span> a <span class=\"token operator\">></span> b <span class=\"token keyword\">then</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">'a不比b大'</span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><ol start=\"3\">\n<li>case 语句</li>\n</ol>\n<div class=\"note warning\">\n<p>case 语句中 when 判断值相等时,实际使用的是 === 运算符。当左边是数值或字符串时 === 和 == 意义相同</p>\n</div>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>case语句</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>tags <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string-literal\"><span class=\"token string\">'Java'</span></span><span class=\"token punctuation\">,</span> <span class=\"token string-literal\"><span class=\"token string\">'Ruby'</span></span><span class=\"token punctuation\">,</span> <span class=\"token string-literal\"><span class=\"token string\">'C/C++'</span></span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>tags<span class=\"token punctuation\">.</span><span class=\"token keyword\">each</span> <span class=\"token keyword\">do</span> <span class=\"token operator\">|</span>tagename<span class=\"token operator\">|</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">case</span> tagename</pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">when</span> <span class=\"token string-literal\"><span class=\"token string\">'Java'</span></span> <span class=\"token keyword\">then</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">'learning Java'</span></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">when</span> <span class=\"token string-literal\"><span class=\"token string\">'Ruby'</span></span> <span class=\"token keyword\">then</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">'learning Ruby'</span></span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token keyword\">else</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">'learning other languages'</span></span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><h2 id=\"循环语句\"><a class=\"anchor\" href=\"#循环语句\">#</a> 循环语句</h2>\n<ol>\n<li>times 方法</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>times方法</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>puts <span class=\"token string-literal\"><span class=\"token string\">'| --times-- |'</span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\"># 1. times 循环</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token number\">3.</span>times <span class=\"token keyword\">do</span> <span class=\"token operator\">|</span>i<span class=\"token operator\">|</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">\"| 第</span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">i <span class=\"token operator\">+</span> <span class=\"token number\">1</span></span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">次循环 |\"</span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><ol start=\"2\">\n<li>for 语句</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>for语句</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>puts <span class=\"token string-literal\"><span class=\"token string\">'| --loop--- |'</span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\"># 2. for 循环</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">for</span> i <span class=\"token keyword\">in</span> <span class=\"token number\">1.</span><span class=\"token number\">.3</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">\"| 第</span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">i</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">次循环 |\"</span></span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><ol start=\"3\">\n<li>while 语句</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>while语句</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>puts <span class=\"token string-literal\"><span class=\"token string\">'| --while-- |'</span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\"># 3. while 循环</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>i <span class=\"token operator\">=</span> <span class=\"token number\">5</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token keyword\">while</span> i <span class=\"token operator\"><</span> <span class=\"token number\">8</span> <span class=\"token keyword\">do</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">\"| 当前值: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">i</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\"> |\"</span></span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> i <span class=\"token operator\">+=</span> <span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><ol start=\"4\">\n<li>until 语句</li>\n</ol>\n<div class=\"note warning\">\n<p>until 语句当条件不成立时才进行循环</p>\n</div>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>until语句</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>puts <span class=\"token string-literal\"><span class=\"token string\">'| -until- |'</span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token comment\"># 4. until 语句</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>sum <span class=\"token operator\">=</span> <span class=\"token number\">0</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre>i <span class=\"token operator\">=</span> <span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">until</span> sum <span class=\"token operator\">>=</span> <span class=\"token number\">5</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">\"| 当前sum: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">sum</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\"> |\"</span></span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> sum <span class=\"token operator\">+=</span> i</pre></td></tr><tr><td data-num=\"8\"></td><td><pre> i <span class=\"token operator\">+=</span> <span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><ol start=\"5\">\n<li>each 方法</li>\n</ol>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>each方法</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>names <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string-literal\"><span class=\"token string\">\"alice\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token string-literal\"><span class=\"token string\">\"bob\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token string-literal\"><span class=\"token string\">\"cyndi\"</span></span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>names<span class=\"token punctuation\">.</span><span class=\"token keyword\">each</span> <span class=\"token keyword\">do</span> <span class=\"token operator\">|</span>name<span class=\"token operator\">|</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">\"'name': '</span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">name</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">'\"</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token comment\"># 等价写法</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre>names<span class=\"token punctuation\">.</span><span class=\"token keyword\">each</span> <span class=\"token punctuation\">{</span> <span class=\"token operator\">|</span>name<span class=\"token operator\">|</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">\"'name': '</span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">name</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">'\"</span></span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token punctuation\">}</span></pre></td></tr></table></figure><ol start=\"6\">\n<li>break、next、redo</li>\n</ol>\n<div class=\"note info\">\n<p>循环语句,一般配合 break、next、redo 使用</p>\n</div>\n<ul>\n<li>break: 终止程序,退出循环</li>\n</ul>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>break例子</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>puts <span class=\"token string-literal\"><span class=\"token string\">\"break的例子\"</span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>i <span class=\"token operator\">=</span> <span class=\"token number\">0</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>langs <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string-literal\"><span class=\"token string\">\"Ruby\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token string-literal\"><span class=\"token string\">\"Rust\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token string-literal\"><span class=\"token string\">\"Java\"</span></span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre>langs<span class=\"token punctuation\">.</span><span class=\"token keyword\">each</span> <span class=\"token keyword\">do</span> <span class=\"token operator\">|</span>lang<span class=\"token operator\">|</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> i <span class=\"token operator\">+=</span> <span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">if</span> i <span class=\"token operator\">==</span> langs<span class=\"token punctuation\">.</span>size</pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token keyword\">break</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> p <span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">,</span> lang<span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre></pre></td></tr><tr><td data-num=\"12\"></td><td><pre><span class=\"token comment\">=begin 输出</pre></td></tr><tr><td data-num=\"13\"></td><td><pre> break 的例子</pre></td></tr><tr><td data-num=\"14\"></td><td><pre> [1, \"Ruby\"]</pre></td></tr><tr><td data-num=\"15\"></td><td><pre> [2, \"Rust\"]</pre></td></tr><tr><td data-num=\"16\"></td><td><pre>=end</span></pre></td></tr></table></figure><ul>\n<li>next:跳过当前循环,直接进入下一次循环</li>\n</ul>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>next例子</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>puts <span class=\"token string-literal\"><span class=\"token string\">\"next例子\"</span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>i <span class=\"token operator\">=</span> <span class=\"token number\">0</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>langs <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string-literal\"><span class=\"token string\">\"Ruby\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token string-literal\"><span class=\"token string\">\"Rust\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token string-literal\"><span class=\"token string\">\"Java\"</span></span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre>langs<span class=\"token punctuation\">.</span><span class=\"token keyword\">each</span> <span class=\"token keyword\">do</span> <span class=\"token operator\">|</span>lang<span class=\"token operator\">|</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> i <span class=\"token operator\">+=</span> <span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">if</span> i <span class=\"token operator\">==</span> langs<span class=\"token punctuation\">.</span>size <span class=\"token operator\">-</span> <span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token keyword\">next</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> p <span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">,</span> lang<span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre></pre></td></tr><tr><td data-num=\"12\"></td><td><pre><span class=\"token comment\">=begin 输出</pre></td></tr><tr><td data-num=\"13\"></td><td><pre> next 的例子</pre></td></tr><tr><td data-num=\"14\"></td><td><pre> [1, \"Ruby\"]</pre></td></tr><tr><td data-num=\"15\"></td><td><pre> [3, \"Java\"]</pre></td></tr><tr><td data-num=\"16\"></td><td><pre>=end</span></pre></td></tr></table></figure><ul>\n<li>redo:再次执行一次相同的循环,不会再次进行条件判断</li>\n</ul>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>redo例子</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre>puts <span class=\"token string-literal\"><span class=\"token string\">\"redo例子\"</span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre>i <span class=\"token operator\">=</span> <span class=\"token number\">0</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre>langs <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string-literal\"><span class=\"token string\">\"Ruby\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token string-literal\"><span class=\"token string\">\"Rust\"</span></span><span class=\"token punctuation\">,</span> <span class=\"token string-literal\"><span class=\"token string\">\"Java\"</span></span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre>langs<span class=\"token punctuation\">.</span><span class=\"token keyword\">each</span> <span class=\"token keyword\">do</span> <span class=\"token operator\">|</span>lang<span class=\"token operator\">|</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre> i <span class=\"token operator\">+=</span> <span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre> <span class=\"token keyword\">if</span> i <span class=\"token operator\">==</span> langs<span class=\"token punctuation\">.</span>size <span class=\"token operator\">-</span> <span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre> <span class=\"token keyword\">redo</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> p <span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">,</span> lang<span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre></pre></td></tr><tr><td data-num=\"12\"></td><td><pre><span class=\"token comment\">=begin 输出</pre></td></tr><tr><td data-num=\"13\"></td><td><pre> redo 的例子</pre></td></tr><tr><td data-num=\"14\"></td><td><pre> [1, \"Ruby\"]</pre></td></tr><tr><td data-num=\"15\"></td><td><pre> [3, \"Rust\"]</pre></td></tr><tr><td data-num=\"16\"></td><td><pre> [4, \"Java\"]</pre></td></tr><tr><td data-num=\"17\"></td><td><pre>=end</span></pre></td></tr></table></figure><h2 id=\"方法\"><a class=\"anchor\" href=\"#方法\">#</a> 方法</h2>\n<div class=\"note info\">\n<p>如果想深入了解 Ruby 的方法可以参考《Ruby 元编程》一书</p>\n</div>\n<h3 id=\"定义带块的方法\"><a class=\"anchor\" href=\"#定义带块的方法\">#</a> 定义带块的方法</h3>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>带块的方法</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">myloop</span></span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> <span class=\"token keyword\">while</span> <span class=\"token boolean\">true</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> <span class=\"token keyword\">yield</span> <span class=\"token comment\"># 执行块</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre> <span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr><td data-num=\"7\"></td><td><pre>num <span class=\"token operator\">=</span> <span class=\"token number\">1</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre>myloop <span class=\"token keyword\">do</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre> <span class=\"token keyword\">break</span> <span class=\"token keyword\">if</span> num <span class=\"token operator\">></span> <span class=\"token number\">64</span> <span class=\"token comment\"># ruby 特有的写法,可以将条件判断后置</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">\"num is </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">num</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">\"</span></span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre> num <span class=\"token operator\">*=</span> <span class=\"token number\">2</span></pre></td></tr><tr><td data-num=\"12\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr></table></figure><blockquote>\n<p><ins class=\"dot\"><span class=\"red\">yield</span> 是定义带块方法的关键字</ins>。调用方法时通过块传进来的处理会在 <span class=\"read\">yield</span> 定义的地方执行。<br />\n如果 <span class=\"red\">yield</span> 部分有参数,会被当作块变量传入块里。<ins class=\"wavy\">块里最后的表达式的值既是块的执行结果,<br />\n同时也可以作为 <span class=\"red\">yield</span> 的返回值在块的外部使用</ins>。</p>\n</blockquote>\n<h3 id=\"可变参数方法\"><a class=\"anchor\" href=\"#可变参数方法\">#</a> 可变参数方法</h3>\n<blockquote>\n<p>通过使用 ”<span class=\"red\">* 变量名</span> “ 的形式来定义可变形参<br />\n<ins class=\"dot\">可变形参会被 R 封装成数组,供方法内部使用</ins></p>\n</blockquote>\n<div class=\"note warning\">\n<p>可变参数在方法的参数列表中只能出现一次</p>\n</div>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>可变形参</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">foo</span></span><span class=\"token punctuation\">(</span><span class=\"token operator\">*</span>args<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> args</pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre>p foo<span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">)</span> <span class=\"token comment\"># 输出 => [1, 2, 3]</span></pre></td></tr></table></figure><h3 id=\"关键字参数\"><a class=\"anchor\" href=\"#关键字参数\">#</a> 关键字参数</h3>\n<div class=\"note info\">\n<p>Ruby 中可以使用<span class=\"red\">关键字参数</span>定义方法的参数列表,<ins class=\"dot\">以 "参数名:参数值" 的形式传入方法</ins>。<br />\n这种形式为每个参数都指定了一个默认值,因此可以参略任何一个参数进行调用,由于参数名也传给了方法,<br />\n所以参数顺序可以自由更改</p>\n</div>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>方法中使用关键字参数</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># 使用关键字参数</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">person</span></span><span class=\"token punctuation\">(</span><span class=\"token symbol\">name</span><span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">'alice'</span></span><span class=\"token punctuation\">,</span> <span class=\"token symbol\">age</span><span class=\"token operator\">:</span> <span class=\"token number\">10</span><span class=\"token punctuation\">,</span> <span class=\"token symbol\">gender</span><span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">'m'</span></span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">\"[Person] {name: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">name</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">, age: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">age</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">, gender: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">gender</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">}\"</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token comment\"># 1. 省略所有参数</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre>person<span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> [Person] {name: alice, age: 10, gender: m}</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre><span class=\"token comment\"># 2. 改变参数传递顺序</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre>person<span class=\"token punctuation\">(</span><span class=\"token symbol\">gender</span><span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">'f'</span></span><span class=\"token punctuation\">,</span> <span class=\"token symbol\">name</span><span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">'cyndi'</span></span><span class=\"token punctuation\">,</span> <span class=\"token symbol\">age</span><span class=\"token operator\">:</span> <span class=\"token number\">32</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> [Person] {name: cyndi, age: 32, gender: f }</span></pre></td></tr></table></figure><div class=\"note warning\">\n<p>关键字参数只能出现参数列表的最后,如果有普通参数的话,普通参数要先定义</p>\n</div>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>混用参数形式</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token comment\"># 下面的方法会报错:Exception::,期望出现:号也就关键字参数的形式</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre><span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">person2</span></span><span class=\"token punctuation\">(</span><span class=\"token symbol\">name</span><span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">'alice'</span></span><span class=\"token punctuation\">,</span> <span class=\"token symbol\">age</span><span class=\"token operator\">:</span> <span class=\"token number\">10</span><span class=\"token punctuation\">,</span> <span class=\"token symbol\">gender</span><span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">'m'</span></span><span class=\"token punctuation\">,</span> other<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"3\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">\"[Person] {name: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">name</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">, age: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">age</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">, gender: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">gender</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">, other: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">other</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">}\"</span></span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"5\"></td><td><pre></pre></td></tr><tr><td data-num=\"6\"></td><td><pre><span class=\"token comment\"># 下面是正确的使用形式</span></pre></td></tr><tr><td data-num=\"7\"></td><td><pre><span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">person3</span></span><span class=\"token punctuation\">(</span>other<span class=\"token punctuation\">,</span> <span class=\"token symbol\">name</span><span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">'alice'</span></span><span class=\"token punctuation\">,</span> <span class=\"token symbol\">age</span><span class=\"token operator\">:</span> <span class=\"token number\">10</span><span class=\"token punctuation\">,</span> <span class=\"token symbol\">gender</span><span class=\"token operator\">:</span> <span class=\"token string-literal\"><span class=\"token string\">'m'</span></span><span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre> puts <span class=\"token string-literal\"><span class=\"token string\">\"[Person] {name: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">name</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">, age: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">age</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">, gender: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">gender</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">, other: </span><span class=\"token interpolation\"><span class=\"token delimiter punctuation\">#{</span><span class=\"token content\">other</span><span class=\"token delimiter punctuation\">}</span></span><span class=\"token string\">}\"</span></span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"10\"></td><td><pre></pre></td></tr><tr><td data-num=\"11\"></td><td><pre>person3<span class=\"token punctuation\">(</span><span class=\"token string-literal\"><span class=\"token string\">'null'</span></span><span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> [Person] {name: alice, age: 10, gender: m, other: null}</span></pre></td></tr></table></figure><div class=\"note primary\">\n<p>将参数传递给方法时,可以将数组分解后的元素作为参数传递给方法。<br />\n调用方法时使用 "<span class=\"red\">* 数组</span> " 的形式,这时传递给方法的是数组中各个元素。<br />\n<ins class=\"wavy\">需要注意的是:<span class=\"red\">数组元素传入的参数个数</span>以及<span class=\"red\">其他参数个数</span>总和要和<span class=\"red\">方法定义的参数个数</span>一致</ins>。</p>\n</div>\n<figure class=\"highlight ruby\"><figcaption data-lang=\"ruby\"><span>把数组分解为参数进行传递</span></figcaption><table><tr><td data-num=\"1\"></td><td><pre><span class=\"token keyword\">def</span> <span class=\"token method-definition\"><span class=\"token function\">foo</span></span><span class=\"token punctuation\">(</span>a<span class=\"token punctuation\">,</span> b<span class=\"token punctuation\">,</span> c<span class=\"token punctuation\">)</span></pre></td></tr><tr><td data-num=\"2\"></td><td><pre> a <span class=\"token operator\">+</span> b <span class=\"token operator\">+</span>c</pre></td></tr><tr><td data-num=\"3\"></td><td><pre><span class=\"token keyword\">end</span></pre></td></tr><tr><td data-num=\"4\"></td><td><pre></pre></td></tr><tr><td data-num=\"5\"></td><td><pre>p foo<span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> 6</span></pre></td></tr><tr><td data-num=\"6\"></td><td><pre></pre></td></tr><tr><td data-num=\"7\"></td><td><pre>args1 <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"8\"></td><td><pre>p foo<span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token operator\">*</span>args1<span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> 6</span></pre></td></tr><tr><td data-num=\"9\"></td><td><pre></pre></td></tr><tr><td data-num=\"10\"></td><td><pre>args2 <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span></pre></td></tr><tr><td data-num=\"11\"></td><td><pre>p foo<span class=\"token punctuation\">(</span><span class=\"token operator\">*</span>args2<span class=\"token punctuation\">)</span> <span class=\"token comment\">#=> 6</span></pre></td></tr></table></figure>",
"tags": [
"Ruby",
"Ruby"
]
}
]
}