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

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.

主站蜘蛛池模板: 晋中市| 重庆市| 浦北县| 凤山县| 大方县| 黑水县| 成都市| 诏安县| 嘉善县| 隆林| 清涧县| 长垣县| 铜山县| 黄浦区| 石景山区| 彰化市| 丹凤县| 渑池县| 灵寿县| 蓬莱市| 五原县| 龙门县| 岚皋县| 行唐县| 山阴县| 宁都县| 紫阳县| 朝阳市| 建宁县| 马关县| 班玛县| 读书| 怀柔区| 保德县| 侯马市| 本溪| 张掖市| 东明县| 宜昌市| 谷城县| 平潭县|