官术网_书友最值得收藏!

More colors

One of the points this example sets out to illustrate is how thinking in terms of very simple abstractions and then building more complex ones on top of them make for code that is simpler to maintain and easier to modify.

As such, we will now update our animation to draw the sine wave in different colors. In this case, we would like to draw the wave in red if the sine of x is negative, and blue otherwise.

We already have the sine value coming through the sine-wave stream, so all we need to do is transform this stream into one that will give us the colors according to the preceding criteria:

(def colour (.pipe sine-wave 
(rx-map (fn [{:keys [sin]}] (if (< sin 0) "red" "blue")))))

The next step is to add the new stream into the main drawing loop—remember to comment the previous one so that we don't end up with multiple waves being drawn at the same time:

(-> (js/rxjs.zip sine-wave colour) 
    (.pipe (rx-take 700)) 
    (.subscribe (fn [[{:keys [x y]} colour]] 
                  (fill-rect x y colour)))) 

Once we save the file, we should see a new sine wave alternating between red and blue as the sine of x oscillates from -1 to 1.

主站蜘蛛池模板: 河北省| 丹寨县| 界首市| 乳山市| 大埔区| 英德市| 太仆寺旗| 宣汉县| 荔浦县| 浦东新区| 阿克陶县| 鹤峰县| 清徐县| 小金县| 交口县| 五华县| 白山市| 田林县| 武胜县| 弥渡县| 新疆| 张家川| 鄂托克前旗| 拜城县| 阿鲁科尔沁旗| 曲靖市| 聂荣县| 望谟县| 乐至县| 永春县| 崇义县| 东宁县| 博野县| 四子王旗| 吉林市| 社会| 嘉义市| 临海市| 集安市| 昌都县| 济宁市|