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

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.

主站蜘蛛池模板: 平阴县| 平陆县| 府谷县| 咸阳市| 昆明市| 万盛区| 灵山县| 宁陵县| 河北省| 武冈市| 旬邑县| 镇宁| 孟津县| 巴楚县| 南郑县| 嘉定区| 和林格尔县| 襄汾县| 贺州市| 合山市| 灵台县| 北安市| 水富县| 博乐市| 涿鹿县| 永善县| 莱芜市| 乌海市| 杭锦旗| 美姑县| 静海县| 玉龙| 宁夏| 黄冈市| 玛多县| 阜南县| 遂昌县| 友谊县| 阿鲁科尔沁旗| 九龙城区| 丰顺县|