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

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.

主站蜘蛛池模板: 南投县| 宽城| 河南省| 姚安县| 绥棱县| 工布江达县| 锦州市| 孟津县| 永登县| 兴安盟| 抚州市| 绍兴市| 安远县| 平利县| 南京市| 桂平市| 昭苏县| 东台市| 突泉县| 固阳县| 繁昌县| 鄢陵县| 东丽区| 遵化市| 林西县| 商河县| 隆尧县| 淳化县| 泽库县| 曲阜市| 建德市| 景东| 普格县| 威海市| 龙岩市| 两当县| 常山县| 璧山县| 华容县| 海城市| 绥江县|