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

  • Canvas Cookbook
  • Bhushan Purushottam Joshi
  • 115字
  • 2021-07-16 11:03:14

Drawing a rainbow

This is an implementation of a quadratic curve.

The output of our new recipe looks like this:

Drawing a rainbow

How to do it...

The recipe is quite simple, comprising seven calls to the function quadraticTo() to draw seven different curves.

Here is the recipe:

<html>
<head>
  <title>Rainbow</title>
  <script type="text/javascript">
    var can;
    var ctx;
    function init() {
      can = document.getElementById("MyCanvasArea");
      ctx = can.getContext("2d");
      y=can.height/2;
      x=can.width-20;
      mid=can.width/2;
      //rainbow - vibgyor
      drawQuadraticCurve(20,y,mid,0,x,y,"violet",7);
      drawQuadraticCurve(20,y-10,mid,-10,x,y-10,"indigo",7);
      drawQuadraticCurve(20,y-20,mid,-20,x,y-20,"blue",7);
      drawQuadraticCurve(20,y-30,mid,-30,x,y-30,"green",7);
      drawQuadraticCurve(20,y-40,mid,-40,x,y-40,"yellow",7);
      drawQuadraticCurve(20,y-50,mid,-50,x,y-50,"orange",7);
      drawQuadraticCurve(20,y-60,mid,-60,x,y-60,"red",7);    
      
    }
    function drawQuadraticCurve(xStart,yStart,xControl, yControl, xEnd, yEnd,color,width)
    {
      //refer the previous recipe for code
      //....
    }
  </script>
</head>
<body onload="init()">
  <canvas id="MyCanvasArea" width="800" height="400" style="border:2px solid black;" >
    browser doesn't support canvas
  </canvas>
</body>
</html>

How it works...

The drawQuadraticCurveTo() is the same function as used in the previous recipe. This function is called multiple times from the init() method.

主站蜘蛛池模板: 铜山县| 盐源县| 兴安盟| 夏河县| 石城县| 图木舒克市| 乌鲁木齐市| 南靖县| 乌苏市| 米脂县| 台北县| 阳山县| 中卫市| 丽江市| 共和县| 景谷| 盈江县| 临潭县| 西安市| 邹城市| 徐水县| 乌拉特中旗| 维西| 沐川县| 日照市| 册亨县| 榆中县| 新河县| 渝北区| 洛宁县| 上虞市| 闻喜县| 平顺县| 和龙市| 偃师市| 永丰县| 松滋市| 大英县| 茂名市| 嘉义县| 临澧县|