回答編集履歴

1

もうちょっと丁寧に

2017/12/21 16:14

投稿

turbgraphics200
turbgraphics200

スコア4267

test CHANGED
@@ -73,3 +73,69 @@
73
73
  render関数内で実行しているrequestAnimationFrameのidでreqId変数の内容を更新させます。
74
74
 
75
75
  これで、buttonクリックで停止させることができます。
76
+
77
+
78
+
79
+ で、ボタンクリックで、別の描画処理を行わせたい場合は、
80
+
81
+ ```js
82
+
83
+ var reqId = null;
84
+
85
+
86
+
87
+ monochromeButton.onclick = function() {
88
+
89
+ cancelRender();
90
+
91
+ monochromeRender();
92
+
93
+ };
94
+
95
+
96
+
97
+ sepiaButton.onclick = function() {
98
+
99
+ cancelRender();
100
+
101
+ sepiaRender();
102
+
103
+ };
104
+
105
+
106
+
107
+ function cancelRender() {
108
+
109
+ if(reqId) {
110
+
111
+ cancelAnimationFrame(reqId);
112
+
113
+ }
114
+
115
+ }
116
+
117
+
118
+
119
+ function monochromeRender() {
120
+
121
+ reqId = requestAnimationFrame(render);
122
+
123
+ // ...
124
+
125
+ }
126
+
127
+
128
+
129
+ function sepiaRender() {
130
+
131
+ reqId = requestAnimationFrame(render);
132
+
133
+ // ...
134
+
135
+ }
136
+
137
+ ```
138
+
139
+
140
+
141
+ といった感じに組みます。